排序绑定结构元素
Sorting Tied Structure Elements
我正在尝试使用 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
其中Player 2
和Player 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
对绑定的 scores
进行二次排序,并生成第二个所需的输出?
如有任何帮助,我们将不胜感激。
您排序的方式不正确。比较函数接收指向结构的指针,而不是指向结构成员的指针。
按照分数排序的正确方法是在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方法比较浮点数,函数为:
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?
我正在尝试使用 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
其中Player 2
和Player 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
对绑定的 scores
进行二次排序,并生成第二个所需的输出?
如有任何帮助,我们将不胜感激。
您排序的方式不正确。比较函数接收指向结构的指针,而不是指向结构成员的指针。
按照分数排序的正确方法是在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方法比较浮点数,函数为:
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?