我正在尝试使用qsort
对结构数组进行排序。我有一个如下所示的结构:
typedef struct {
double score;
int player_num;
} player_t;
我为六名玩家创建了一系列结构,如下所示:
player_t *players = malloc(6 * sizeof(player_t));
我从这两个数组插入的数据:
int player_numbers[] = {1, 2, 3, 4, 5, 6};
double scores[] = {0.765, 0.454, 0.454, 0.345, 0.643, 0.532};
到目前为止,我正在尝试按分数对这个结构体数组进行排序,如果分数相同,则必须对玩家编号进行排序。到目前为止,我通过对分数进行排序得到了这个输出:
Player 1: Score: 0.765
Player 5: Score: 0.643
Player 6: Score: 0.532
Player 3: Score: 0.454
Player 2: Score: 0.454
Player 4: Score: 0.345
当我真正想要的是:
Player 1: Score: 0.765
Player 5: Score: 0.643
Player 6: Score: 0.532
Player 2: Score: 0.454
Player 3: Score: 0.454
Player 4: Score: 0.345
其中玩家2
和玩家3
交换了位置,因为他们的分数相同,所以他们各自的玩家编号被排序。阵列的其余部分保持不变。
到目前为止,我仅根据分数本身对这个结构数组进行了排序,这产生了第一个输出。我的代码如下所示:
#include <stdio.h>
#include <stdlib.h>
#define SIZE 6
int scorecmp(const void *a, const void *b);
typedef struct {
double score;
int player_num;
} player_t;
int
main(int argc, char *argv[]) {
int i;
int player_numbers[] = {1, 2, 3, 4, 5, 6};
double scores[] = {0.765, 0.454, 0.454, 0.345, 0.643, 0.532};
player_t *players = malloc(SIZE * sizeof(player_t));
for (i = 0; i < SIZE; i++) {
players[i].score = scores[i];
players[i].player_num = player_numbers[i];
}
qsort(players, SIZE, sizeof(*players), scorecmp);
for (i = 0; i < SIZE; i++) {
printf("Player %d: Score: %.3f\n", players[i].player_num, players[i].score);
}
free(players);
return 0;
}
int
scorecmp(const void *x, const void *y) {
if ((*(double*)x > *(double*)y)) {
return -1;
}
if ((*(double*)x < *(double*)y)) {
return +1;
}
return 0;
}
有什么方法可以通过使用player_num
来对并列的分数
进行二次排序,并产生第二个所需的输出?
如有任何帮助,我们将不胜感激。
最佳答案
您的排序方式不正确。比较函数接收指向结构的指针,而不是指向结构成员的指针。
按照分数排序的正确方法是在qsort函数中使用这个比较函数:
int ComparePlayerScore( const void* ap , const void* bp )
{
const player_t* const a = ap;
const player_t* const b = bp;
if( a->score < b->score )
{
return -1;
}
else if( a->score > b->score )
{
return 1;
}
return 0;
}
如果您想确保得分相同的玩家按字母顺序排序,则需要在排序功能中再次进行检查。首先检查玩家得分是否相同,然后按照玩家编号排序。
使用简单的1方法来比较 float ,函数将是:
if( a->score == b->score )
{
return CompareInt( a->player_num , b->player_num )
}
else if( a->score < b->score )
{
return -1;
}
else
{
return 1;
}
其中 CompareInt 是另一个函数:
int CompareInt( const int a , const int b )
{
if( a < b )
{
return -1;
}
else if( a > b )
{
return 1;
}
return 0;
}
<小时/>
1 使用简单的比较运算符来比较浮点可能会出现问题,请参阅:How should I do floating point comparison?
关于c - 对绑定(bind)结构元素进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40017745/