case 3: { int other; printf("1.查平均打分:\n"); printf("2.查成绩打分:\n"); while (scanf("%d", &other) != 1 || (other != 1 && other != 2)) { printf("无效的输入,请重新输入!\n"); while (getchar() != '\n'); } void (*func[])(struct Score[], int) = {statistics, showScores}; (*func[other - 1])(scores, numScores); break; } void showScores(struct Score scores[], int numScores) { printf("\n--------查询评教分数--------\n"); for (int i = 0; i < numScores; i++) { printf("%s:%d\n", scores[i].courseName, scores[i].score); } } void statistics(struct Score scores[], int numScores) { int i; int total = 0; printf("\n--------统计评教分数--------\n"); for ( i = 0; i < numScores; i++) { total += scores[i].score; } if (numScores != 0) { printf("平均分数:%d\n", total / numScores); } else { printf("还没有评教记录!\n"); } } 给每一行代码写注释
时间: 2024-01-28 15:05:07 浏览: 23
/*
* case 3: 是一个 switch 语句的一个分支,用于查询评教分数或统计评教分数
*/
case 3: {
int other;
printf("1.查平均打分:\n");
printf("2.查成绩打分:\n");
/*
* while 循环用于获取用户输入,只有输入 1 或 2 才会跳出循环
*/
while (scanf("%d", &other) != 1 || (other != 1 && other != 2)) {
printf("无效的输入,请重新输入!\n");
while (getchar() != '\n');
}
/*
* 定义一个指向函数的指针数组,数组元素分别指向 statistics 函数和 showScores 函数
* 根据用户输入的不同调用不同的函数
*/
void (*func[])(struct Score[], int) = {statistics, showScores};
(*func[other - 1])(scores, numScores);
break;
}
/*
* showScores 函数用于查询评教分数,遍历并输出所有的评教分数
*/
void showScores(struct Score scores[], int numScores) {
printf("\n--------查询评教分数--------\n");
for (int i = 0; i < numScores; i++) {
printf("%s:%d\n", scores[i].courseName, scores[i].score);
}
}
/*
* statistics 函数用于统计评教分数,计算所有评教分数的平均值并输出
*/
void statistics(struct Score scores[], int numScores) {
int i;
int total = 0;
printf("\n--------统计评教分数--------\n");
for ( i = 0; i < numScores; i++) {
total += scores[i].score;
}
if (numScores != 0) {
printf("平均分数:%d\n", total / numScores);
}
else {
printf("还没有评教记录!\n");
}
}