马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
这段代码哪里有问题,为什么不能实现排序
//对信息按要求排序
int SortByScore(const void* a, const void* b)
{
Player* playerA = (Player*)a;
Player* playerB = (Player*)b;
return playerA -> score - playerB -> score;
}
int SortByRank(const void* a, const void* b)
{
const Player* playerA = (const Player*)a;
const Player* playerB = (const Player*)b;
if(strcmp(playerA -> identity, "职业") == 0 && strcmp(playerB -> identity, "职业") == 0)
{
int Comp = strcmp(playerB -> rank, playerA -> rank);
if(Comp == 0)
{
return strcmp(playerA -> id, playerB -> id);
}
return Comp;
}
return strcmp(playerA -> identity, "职业") == 0 ? -1 : 1;
}
void SortPlayer(Player players[], int num_players)
{
int i = 0;
char sort_choice;
printf("A. 所有棋手按等级分进行降序排序\n");
printf("B. 职业棋手按段位进行降序排序\n");
printf("C. 退出当前功能\n");
printf("请选择(A-C):");
scanf("%c", &sort_choice);
switch(sort_choice)
{
case 'A':
qsort(players, num_players, sizeof(Player), SortByScore);
break;
case 'B':
qsort(players, num_players, sizeof(Player), SortByRank);
break;
case 'C':
return;
}
getchar();
printf("排序结果:\n");
printf("%20s\t%20s\t%10s\t%12s\t%20s\n", "编号","姓名","身份","段位","等级分\n");
for(i = 0; i < num_players; i++)
{
printf("%20s\t%21s\t%10s\t%12s\t%18s\n", players[i].id, players[i].name, players[i].identity, players[i].rank, players[i].score);
}
}
|