优化此程序段:void gradelist(struct student *list,int amount) { int i=0; printf("等级 人数 百分比 \n"); int j=0; for(i=0;i<amount;i++) { if(list[i].grade=='A') j++; } printf("\t\t A %d %6.3lf \n",j,(double)j/amount); j=0; for(i=0;i<amount;i++) { if(list[i].grade=='B') j++; } printf("\t\t B %d %6.3lf \n",j,(double)j/amount); j=0; for(i=0;i<amount;i++) { if(list[i].grade=='C') j++; } printf("\t\t C %d %6.3lf \n",j,(double)j/amount); j=0; for(i=0;i<amount;i++) { if(list[i].grade=='D') j++; } printf("\t\t D %d %6.3lf \n",j,(double)j/amount); j=0; for(i=0;i<amount;i++) { if(list[i].grade=='E') j++; } printf("\t\t E %d %6.3lf \n",j,(double)j/amount); printf("\n\n\n ****** 请摁任意键继续! ******"); getch(); } void find(struct student *list,int amount) { int i=0; int flag=1; char temp[10]; dispinfo(); int flag2; do { flag2=1; printf(" 请输入学号:"); scanf("%s",temp); for(i=0;i<amount;i++) { flag=strcmp(temp,list[i].code); if(flag==0) { flag2=0; break; } } if(flag2==1) printf(" 您所输入的学号不存在,请重新输入!\n"); }while(flag2); printf("\t\t 学号 总评 等级 \n"); printf("\t\t%10s %6.1f%6c \n",list[i].code,list[i].score,list[i].grade); printf("\n\n\n ****** 按任意键继续! ****** \n"); getch(); } void save(struct student *list,int amount) { int i=0; FILE *fp; fp=fopen("C:\Users\dell\Desktop\软件设计-源文件","w"); fprintf(fp,"\t\t 学号 总评 等级 \n"); for(i=0;i<amount;i++) { fprintf(fp,"\t\t %10s %6.1f %6c \n",list[i].code,list[i].score,list[i].grade); } printf("\n\n ****** 输出成功!请查看!摁任意键继续 ****** \n"); fclose(fp); getch(); } int main() { system("color F4"); struct student list[100]; int choice; int amount; while(1) { dispinfo(); set(&choice); switch(choice) { case '1':amount=duqu(list);break; case '2':calculate(list,amount);break; case '3':findgrade(list,amount);break; case '4':paixu(list,amount);break; case '5':find(list,amount);break; case '6':gradelist(list,amount);break; case '7':save(list,amount);break; case '0': system("cls"); printf("\n\n\n\n\n\n\n\n\n\n\n ******* 谢谢使用!欢迎再次光临! ******\n"); getch(); break; } } return 0; }
时间: 2024-04-06 07:32:42 浏览: 89
void gradelist(struct student *list,int amount) {
int i=0;
printf("等级 人数 百分比 \n");
int grades[5] = {0}; // 用数组记录每个等级的人数
for(i=0;i<amount;i++) {
switch(list[i].grade) {
case 'A': grades[0]++; break;
case 'B': grades[1]++; break;
case 'C': grades[2]++; break;
case 'D': grades[3]++; break;
case 'E': grades[4]++; break;
}
}
char grade_labels[5] = {'A', 'B', 'C', 'D', 'E'}; // 存放等级标签的数组
for(i=0;i<5;i++) {
printf("\t\t %c %d %6.3lf \n", grade_labels[i], grades[i], (double)grades[i]/amount);
}
printf("\n\n\n ****** 请摁任意键继续! ******");
getch();
}
void find(struct student *list,int amount) {
int i=0;
int flag=1;
char temp[10];
dispinfo();
int flag2;
do {
flag2=1;
printf(" 请输入学号:");
scanf("%s",temp);
for(i=0;i<amount;i++) {
flag=strcmp(temp,list[i].code);
if(flag==0) {
flag2=0;
break;
}
}
if(flag2==1) printf(" 您所输入的学号不存在,请重新输入!\n");
}while(flag2);
printf("\t\t 学号 总评 等级 \n");
printf("\t\t%10s %6.1f%6c \n",list[i].code,list[i].score,list[i].grade);
printf("\n\n\n ****** 按任意键继续! ****** \n");
getch();
}
void save(struct student *list,int amount) {
int i=0;
FILE *fp;
fp=fopen("C:\Users\dell\Desktop\软件设计-源文件","w");
fprintf(fp,"\t\t 学号 总评 等级 \n");
for(i=0;i<amount;i++) {
fprintf(fp,"\t\t %10s %6.1f %6c \n",list[i].code,list[i].score,list[i].grade);
}
printf("\n\n ****** 输出成功!请查看!摁任意键继续 ****** \n");
fclose(fp);
getch();
}
int main() {
system("color F4");
struct student list[100];
int choice;
int amount;
while(1) {
dispinfo();
set(&choice);
switch(choice) {
case '1':amount=duqu(list);break;
case '2':calculate(list,amount);break;
case '3':findgrade(list,amount);break; // 函数名与代码段中不一致,这里修改为正确的函数名
case '4':paixu(list,amount);break;
case '5':find(list,amount);break;
case '6':gradelist(list,amount);break;
case '7':save(list,amount);break;
case '0':
system("cls");
printf("\n\n\n\n\n\n\n\n\n\n\n ******* 谢谢使用!欢迎再次光临! ******\n");
getch();
return 0; // 程序结束,直接返回
default:
printf(" 无效的选项,请重新输入!\n");
break;
}
}
}
阅读全文