void statisticScores() //成绩统计 { for (int j = 0; j < courseCount; j++) { printf("%s:\n", courses[j].name); int max = 0; int pos = studentCount; double aver = 0.0; for (int i = 0; i < studentCount; i++) //比较学生成绩,找出成绩最高的学生 { aver = aver + students[i].scores[j]; if (students[i].scores[j] > max) { max = students[i].scores[j]; pos = i; } } aver /= studentCount; //计算平均成绩 int count = 0; for (int i = 0; i < studentCount; i++) //计算超过平均成绩的人数 { if (students[i].scores[j] > aver) { count ++; } } if (pos == studentCount) { printf("No student\n"); break; } else { printf("成绩最高学生:"); printf(" %s %s %d\n", students[pos].id, students[pos].name, max); printf("平均成绩:"); printf(" %.2lf\n", aver); printf("超过平均成绩的人数:"); printf(" %d\n", count); } } }
时间: 2024-02-14 22:21:41 浏览: 28
以上代码是一个 C 语言函数,函数名为 statisticScores,用于对每门课程进行成绩统计。在该函数中,首先遍历课程数组,然后对于每个课程,输出课程名字,并且统计该课程中成绩最高的学生的信息,包括学号、姓名和成绩,并且计算该课程的平均成绩和超过平均成绩的人数。在统计超过平均成绩的人数时,通过遍历学生数组并且判断成绩是否大于平均成绩来实现。如果某个课程中不存在学生,则输出相应的错误信息并且跳出循环。该函数有效地实现了对每门课程的成绩统计,并且输出详细的统计结果。
相关问题
void displayScores() //成绩显示 { if (studentCount == 0) { printf("No student\n"); return; } printf(" 学号 姓名 "); for (int i = 0; i < courseCount; i++) //输出课程名字 { printf(" %s", courses[i].name); } printf("\n"); for (int i = 0; i < studentCount; i++) //输出学生信息 { printf("%s %s", students[i].id, students[i].name); //输出学生学号,姓名 for (int j = 0; j < courseCount; j++) //输出学生成绩 { printf(" %d", students[i].scores[j]); } printf("\n"); } }
这段代码是用于输出成绩信息的函数,主要包括以下几个步骤:
1. 如果学生数量为0,则输出"No student",并直接返回。
2. 输出表头,包括"学号"和"姓名"以及所有课程的名字。
3. 循环输出每个学生的信息,包括学号、姓名以及每门课程的成绩。
4. 在内层循环中,输出学生每门课程的成绩。
需要注意的是,输出的格式比较规整,每个字段之间用空格隔开,方便查看。同时,也需要注意输入的成绩是否合理,比如是否超出了成绩的取值范围等,不合理的成绩可能会影响结果的正确性。
void displayScores() //成绩显示 { if (studentCount == 0) { printf("No student\n"); return; } printf(" 学号 姓名 "); for (int i = 0; i < courseCount; i++) //输出课程名字 { printf(" %s", courses[i].name); } printf("\n"); for (int i = 0; i < studentCount; i++) //输出学生信息 { printf("%s %s", students[i].id, students[i].name); //输出学生学号,姓名 for (int j = 0; j < courseCount; j++) //输出学生成绩 { printf(" %d", students[i].scores[j]); } printf("\n"); } }
这段代码是用来显示学生成绩的,它会按照一定的格式输出所有学生的学号、姓名以及每个学生在每个课程上的成绩。具体实现是通过两重循环依次输出每个学生的信息和成绩。在输出学生信息时,会先输出学生的学号和姓名,然后在循环输出该学生在每个课程上的成绩。在输出课程名字时,会先输出一个表头,再按照一定的格式输出每个课程的名字。如果没有学生的信息,会输出"No student"。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)