|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
请分别简要解释这四段代码,并说明用哪些函数实现的,函数的输入和输出是什么
//查找棋手信息 void SearchPlayer(Player players[], int num_players)
{
char reply, keyword[20];
int i = 0, back = 0;
printf("A.按棋手身份查询\n");
printf("B.按棋手姓名查询\n");
printf("C.按棋手段位查询\n");
printf("D.退出当前功能\n");
printf("请选择(A-D):");
getchar();
scanf("%c", &reply);
if(reply == 'D')
{
return;
}
printf("请输入%s:", reply == 'A' ? "棋手身份" : reply == 'B' ? "棋手姓名" : "棋手段位");
scanf("%s", keyword);
for(i = 0; i < num_players; i++)
{
int sel = 0;
switch(reply)
{
case 'A':
sel = strcmp(players[i].identity, keyword) == 0;
break;
case'B':
sel = strcmp(players[i].name, keyword) == 0;
break;
case'C':
sel = strcmp(players[i].rank, keyword) == 0;
break;
}
if(sel)
{
if(!back)
{
printf("%20s\t%20s\t%10s\t%12s\t%20s\n", "编号","姓名","身份","段位","等级分\n");
}
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);
back++;
}
}
if(!back)
{
printf("未找到该棋手信息\n");
}
else
{
printf("共%d人\n", back);
}
}
//更改棋手信息 void FixPlayer(Player players[], int num_players)
{
char id[10], name[50];
printf("请输入棋手编号、姓名:");
scanf("%s%s", id, name);
int i = 0, fix_choice, fix_index = -1;
char fix_ch;
for(i = 0; i < num_players; i++)
{
if(strcmp(players[i].id, id) == 0 && strcmp(players[i].name, name) == 0)
{
fix_index = i;
break;
}
}
if(fix_index == -1)
{
printf("查无此人\n");
return;
}
printf("完整信息:%20s\t%21s\t%10s\t%12s\t%18s\n",
players[fix_index].id, players[fix_index].name, players[fix_index].identity, players[fix_index].rank, players[fix_index].score);
printf("选择要修改的数据项(编号不能修改,1-姓名 2-身份 3-段位 4-等级分):");
scanf("%d", &fix_choice);
printf("是否修改该数据y/n?:");
getchar();
scanf("%c", &fix_ch);
if(fix_ch == 'y')
{
switch(fix_choice)
{
case 1:
printf("请输入修改后的姓名:");
scanf("%s", players[fix_index].name);
break;
case 2:
printf("输入修改后的身份:");
scanf("%s", players[fix_index].identity);
break;
case 3:
printf("输入修改后的段位:");
scanf("%s", players[fix_index].rank);
break;
case 4:
printf("输入修改后的等级分:");
scanf("%s", players[fix_index].rank);
break;
default:
printf("输入错误,请按照规范重新输入!\n");
break;
}
printf("修改成功!\n");
SavePlayer(players, num_players);
printf("本地文件更新成功\n");
}
}
//统计棋手信息 void StatPlayer(Player players[], int num_players)
{
char stat_choice, identity[10], rank[10];
int i = 0, stat_count = 0;
printf("A. 统计某身份某段位棋手总数\n");
printf("B. 统计职业二段以上的棋手总数\n");
printf("C. 统计业余棋手总数\n");
printf("D. 退出当前功能\n");
printf("请选择(A-D):");
getchar();
scanf("%c", &stat_choice);
switch(stat_choice)
{
case 'A':
printf("请输入棋手身份和段位:");
scanf("%s%s", identity, rank);
for(i = 0; i < num_players; i++)
{
if(strcmp(players[i].identity, identity) == 0 && strcmp(players[i].rank, rank) == 0)
{
stat_count++;
}
}
break;
case 'B':
for(i = 0; i < num_players; i++)
{
if(strcmp(players[i].identity, "职业") == 0 && strcmp(players[i].rank, "二段") > 0)
{
stat_count++;
}
}
break;
case 'C':
for(i = 0; i < num_players; i++)
{
if(strcmp(players[i].identity, "业余") == 0)
{
stat_count++;
}
}
break;
case 'D':
break;
}
printf("当前棋手数量:%d\n", stat_count);
if(stat_count > 0)
{
printf("%20s\t%20s\t%10s\t%12s\t%20s\n", "编号","姓名","身份","段位","等级分\n");
for(i = 0; i < num_players; i++)
{
switch(stat_choice)
{
case 'A':
if(strcmp(players[i].identity, identity) == 0 && strcmp(players[i].rank, rank) == 0)
{
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);
}
break;
case 'B':
if(strcmp(players[i].identity, "职业") == 0 && strcmp(players[i].rank, "二段") > 0)
{
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);
}
break;
case 'C':
if(strcmp(players[i].identity, "业余") == 0)
{
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);
}
break;
}
}
}
}
//对信息按要求排序
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):");
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;
}
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);
}
}
第一个代码段是用于查找棋手信息的函数。函数的输入参数有一个Player类型的数组players和一个整数num_players表示棋手的数量。函数的功能是根据用户的选择按照不同的方式查找棋手信息,并打印出符合条件的棋手信息。函数内部使用了getchar和scanf函数获取用户的选择和关键词,然后通过for循环和switch语句遍历棋手数组,根据用户选择的方式进行比较,如果找到符合条件的棋手,则打印出其信息。该函数无返回值。
第二个代码段是用于更改棋手信息的函数。函数的输入参数与第一个代码段相同。函数的功能是根据用户输入的棋手编号和姓名,找到对应的棋手,然后允许用户选择要修改的数据项,并修改该数据项的值。最后将修改后的棋手信息保存到本地文件中。函数内部通过for循环和if语句找到要修改的棋手,然后根据用户的选择修改对应的数据项。修改成功后,通过调用SavePlayer函数保存修改后的棋手信息到本地文件。
第三个代码段是用于统计棋手信息的函数。函数的输入参数与第一个代码段相同。函数的功能是根据用户的选择,统计满足特定条件的棋手数量,并打印出符合条件的棋手信息。函数内部使用了getchar和scanf函数获取用户的选择和输入,然后通过for循环和switch语句统计符合条件的棋手数量,并打印出这些棋手的信息。
第四个代码段是用于对棋手信息按照要求进行排序的函数。函数的输入参数与第一个代码段相同。函数的功能是根据用户的选择,对棋手信息进行排序,并打印出排序结果。函数内部使用了qsort函数和自定义的比较函数SortByScore和SortByRank来实现对棋手信息的排序。根据用户的选择不同,调用不同的比较函数进行排序。排序完成后,通过for循环打印出排好序的棋手信息。
以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。
|
|