排序
//对信息按要求排序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.id, players.name, players.identity, players.rank, players.score);
}
}
修改这段代码,实现排序的功能,要能正常运行出来 根据您提供的代码,我发现其中存在一些问题。首先,在排序函数`SortPlayer`中,参数`Player players`应该是一个指针类型,而不是直接传递一个数组。另外,在对数组进行排序时,可以使用`qsort`函数,但在比较函数中,需要将`a`和`b`强制转换为`Player*`类型。
另外,还有一点值得注意的是,您在打印结果时使用了`players`数组的第一个元素,并且没有递增指针来访问其他元素。实际上,应该使用循环来遍历整个数组,以便打印出所有的排序结果。
下面是修改后的代码实现:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// 玩家结构体
typedef struct {
char id; // 编号
char name; // 姓名
char identity;// 身份
char rank; // 段位
int score; // 等级分
} Player;
// 按等级分排序比较函数
int SortByScore(const void* a, const void* b)
{
Player* playerA = (Player*)a;
Player* playerB = (Player*)b;
return playerB->score - playerA->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):");
getchar(); // 清空缓冲区中的回车符
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;
}
printf("排序结果:\n");
printf("%20s\t%20s\t%10s\t%12s\t%20s\n", "编号", "姓名", "身份", "段位", "等级分");
for (i = 0; i < num_players; i++) {
printf("%20s\t%21s\t%10s\t%12s\t%18d\n", players.id, players.name, players.identity, players.rank, players.score);
}
}
int main()
{
Player players;
strcpy(players.id, "001");
strcpy(players.name, "张三");
strcpy(players.identity, "职业");
strcpy(players.rank, "高段位");
players.score = 3000;
strcpy(players.id, "002");
strcpy(players.name, "李四");
strcpy(players.identity, "业余");
strcpy(players.rank, "低段位");
players.score = 1500;
strcpy(players.id, "003");
strcpy(players.name, "王五");
strcpy(players.identity, "职业");
strcpy(players.rank, "中段位");
players.score = 2500;
SortPlayer(players, 3);
return 0;
}
在以上代码中,我们首先定义了一个包含3个玩家信息的`players`数组,然后将其作为参数传递给`SortPlayer`函数进行排序。排序的结果会按照您选择的方式打印出来。
希望这个修改后的代码能够满足您的需求!如果还有其他问题,请随时提问。
以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。
页:
[1]