#include #include #include typedef struct { char *uid; char *name; int math, eng, cs; } Score; void save_to_file(Score **scores, int n, char *file_name) { FILE *fp = fopen(file_name, "w"); fprintf(fp, "%d\n", n); for (int i = 0; i < n; i++) { fprintf(fp, "%s %s %d %d %d\n", scores[i]->uid, scores[i]->name, scores[i]->math, scores[i]->eng, scores[i]->cs); } fclose(fp); } int main() { Score *scores[105]; int n; // 如果 score.dat 不存在,就从键盘输入 FILE *fp = fopen("score.dat", "r"); if (fp == NULL) { printf("score.dat not found, please input:\n"); scanf("%d", &n); fp = fopen("score.dat", "w"); fprintf(fp, "%d\n", n); for (int i = 0; i < n; i++) { scores[i] = (Score *)malloc(sizeof(Score)); char *uid = (char *)malloc(sizeof(char) * 105); char *name = (char *)malloc(sizeof(char) * 105); scanf("%s %s %d %d %d", uid, name, &scores[i]->math, &scores[i]->eng, &scores[i]->cs); scores[i]->uid = uid; scores[i]->name = name; fprintf(fp, "%s %s %d %d %d\n", scores[i]->uid, scores[i]->name, scores[i]->math, scores[i]->eng, scores[i]->cs); } fclose(fp); } else { // 从 score.dat 读取 fscanf(fp, "%d", &n); for (int i = 0; i < n; i++) { scores[i] = (Score *)malloc(sizeof(Score)); char *uid = (char *)malloc(sizeof(char) * 105); char *name = (char *)malloc(sizeof(char) * 105); fscanf(fp, "%s %s %d %d %d", uid, name, &scores[i]->math, &scores[i]->eng, &scores[i]->cs); scores[i]->uid = uid; scores[i]->name = name; } fclose(fp); } printf("Total: %d\n", n); printf("当前数据:\n"); // 输出所有人的信息 for (int i = 0; i < n; i++) { printf("%s %s %d %d %d\n", scores[i]->uid, scores[i]->name, scores[i]->math, scores[i]->eng, scores[i]->cs); } printf("--------------------\n"); // 按照每个科目排序并输出对应的排名 printf("按照数学成绩排序:\n"); for (int i = 0; i < n; i++) { int max = i; for (int j = i + 1; j < n; j++) { if (scores[j]->math > scores[max]->math) { max = j; } } Score *tmp = scores[i]; scores[i] = scores[max]; scores[max] = tmp; printf("%s %s %d %d %d\n", scores[i]->uid, scores[i]->name, scores[i]->math, scores[i]->eng, scores[i]->cs); } save_to_file(scores, n, "math.dat"); printf("--------------------\n"); printf("按照英语成绩排序:\n"); for (int i = 0; i < n; i++) { int max = i; for (int j = i + 1; j < n; j++) { if (scores[j]->eng > scores[max]->eng) { max = j; } } Score *tmp = scores[i]; scores[i] = scores[max]; scores[max] = tmp; printf("%s %s %d %d %d\n", scores[i]->uid, scores[i]->name, scores[i]->math, scores[i]->eng, scores[i]->cs); } save_to_file(scores, n, "eng.dat"); printf("--------------------\n"); printf("按照计算机成绩排序:\n"); for (int i = 0; i < n; i++) { int max = i; for (int j = i + 1; j < n; j++) { if (scores[j]->cs > scores[max]->cs) { max = j; } } Score *tmp = scores[i]; scores[i] = scores[max]; scores[max] = tmp; printf("%s %s %d %d %d\n", scores[i]->uid, scores[i]->name, scores[i]->math, scores[i]->eng, scores[i]->cs); } save_to_file(scores, n, "cs.dat"); printf("--------------------\n"); // 计算平均分,按平均成绩排序,写到 average.dat printf("平均分:\n"); FILE *fp_average = fopen("average.dat", "w"); fprintf(fp_average, "%d\n", n); double avg_scores[105]; for (int i = 0; i < n; i++) { avg_scores[i] = (scores[i]->math + scores[i]->eng + scores[i]->cs) / 3.0; } // sort for (int i = 0; i < n; i++) { int max = i; for (int j = i + 1; j < n; j++) { if (avg_scores[j] > avg_scores[max]) { max = j; } } int tmp = avg_scores[i]; avg_scores[i] = avg_scores[max]; avg_scores[max] = tmp; Score *tmp_score = scores[i]; scores[i] = scores[max]; scores[max] = tmp_score; } for (int i = 0; i < n; i++) { printf("%s %s %d %d %d %.2f\n", scores[i]->uid, scores[i]->name, scores[i]->math, scores[i]->eng, scores[i]->cs, avg_scores[i] * 1.0); fprintf(fp_average, "%s %s %d %d %d %.2f\n", scores[i]->uid, scores[i]->name, scores[i]->math, scores[i]->eng, scores[i]->cs, avg_scores[i] * 1.0); } fclose(fp_average); printf("--------------------\n"); // 求出各门课程的平均成绩、最高分、最低分、不及格人数、60-69分人数、70-79分人数、 // 80-89分人数、90分以上人数。 int math_sum = 0, eng_sum = 0, cs_sum = 0; int math_max = 0, eng_max = 0, cs_max = 0; int math_min = 100, eng_min = 100, cs_min = 100; int math_fail = 0, eng_fail = 0, cs_fail = 0; int math_60_69 = 0, eng_60_69 = 0, cs_60_69 = 0; int math_70_79 = 0, eng_70_79 = 0, cs_70_79 = 0; int math_80_89 = 0, eng_80_89 = 0, cs_80_89 = 0; int math_90 = 0, eng_90 = 0, cs_90 = 0; for (int i = 0; i < n; i++) { math_sum += scores[i]->math; eng_sum += scores[i]->eng; cs_sum += scores[i]->cs; if (scores[i]->math > math_max) { math_max = scores[i]->math; } if (scores[i]->eng > eng_max) { eng_max = scores[i]->eng; } if (scores[i]->cs > cs_max) { cs_max = scores[i]->cs; } if (scores[i]->math < math_min) { math_min = scores[i]->math; } if (scores[i]->eng < eng_min) { eng_min = scores[i]->eng; } if (scores[i]->cs < cs_min) { cs_min = scores[i]->cs; } if (scores[i]->math < 60) { math_fail++; } if (scores[i]->eng < 60) { eng_fail++; } if (scores[i]->cs < 60) { cs_fail++; } if (scores[i]->math >= 60 && scores[i]->math <= 69) { math_60_69++; } if (scores[i]->eng >= 60 && scores[i]->eng <= 69) { eng_60_69++; } if (scores[i]->cs >= 60 && scores[i]->cs <= 69) { cs_60_69++; } if (scores[i]->math >= 70 && scores[i]->math <= 79) { math_70_79++; } if (scores[i]->eng >= 70 && scores[i]->eng <= 79) { eng_70_79++; } if (scores[i]->cs >= 70 && scores[i]->cs <= 79) { cs_70_79++; } if (scores[i]->math >= 80 && scores[i]->math <= 89) { math_80_89++; } if (scores[i]->eng >= 80 && scores[i]->eng <= 89) { eng_80_89++; } if (scores[i]->cs >= 80 && scores[i]->cs <= 89) { cs_80_89++; } if (scores[i]->math >= 90) { math_90++; } if (scores[i]->eng >= 90) { eng_90++; } if (scores[i]->cs >= 90) { cs_90++; } } printf("数学:\n平均分:%.2f 最高分:%d 最低分:%d 不及格人数:%d " "60-69分人数:%d " "70-79分人数:%d 80-89分人数:%d 90分以上人数:%d\n", math_sum * 1.0 / n, math_max, math_min, math_fail, math_60_69, math_70_79, math_80_89, math_90); printf("英语:\n平均分:%.2f 最高分:%d 最低分:%d 不及格人数:%d " "60-69分人数:%d " "70-79分人数:%d 80-89分人数:%d 90分以上人数:%d\n", eng_sum * 1.0 / n, eng_max, eng_min, eng_fail, eng_60_69, eng_70_79, eng_80_89, eng_90); printf("计算机:\n平均分:%.2f 最高分:%d 最低分:%d 不及格人数:%d " "60-69分人数:%d " "70-79分人数:%d 80-89分人数:%d 90分以上人数:%d\n", cs_sum * 1.0 / n, cs_max, cs_min, cs_fail, cs_60_69, cs_70_79, cs_80_89, cs_90); // 根据姓名或学号查询某人的各门成绩,重名情况也能处理。 while (1) { printf("请输入学号或姓名:\n"); char *query = (char *)malloc(sizeof(char) * 105); scanf("%s", query); int found = 0; for (int i = 0; i < n; i++) { if (strcmp(scores[i]->uid, query) == 0 || strcmp(scores[i]->name, query) == 0) { printf("%s %s %d %d %d\n", scores[i]->uid, scores[i]->name, scores[i]->math, scores[i]->eng, scores[i]->cs); found = 1; } } if (!found) { printf("未找到\n"); } } return 0; }