// 获取学生总分 void get_total_score(Student *student) { int total = 0; for (int i = 0; i < MAX_COURSE_NUM; i++) { student->total_score = total; } } // 显示学生信息 int show_student_info( Student *student) { printf("学号:%d\t姓名:%s\t成绩:%d %d %d\t总分:%d\n",student->id, student->name,student->scores[0], student->scores[1], student->scores[2],student->total_score); } // 显示所有学生信息及总排名 void show_all_students(Student *student) { // 计算总分 for (int i = 0; i < student_count; i++) { students[i].total_score = get_total_score(&students[i]); } // 排序 for (int i = 0; i < student_count - 1; i++) { for (int j = i + 1; j < student_count; j++) { if (students[i].total_score < students[j].total_score) { Student temp = students[i]; students[i] = students[j]; students[j] = temp; } } } // 显示信息 printf("\n所有学生信息及总排名:\n"); for (int i = 0; i < student_count; i++) { show_student_info(&students[i]); } }哪错了
时间: 2024-01-11 09:02:09 浏览: 114
在函数 get_total_score 中,你每次循环都将总分赋值为0,然后又用循环内的最后一个分数覆盖了总分,所以每个学生的总分都是最后一个分数。应该在循环结束后再将总分赋值给学生。
修改后的代码如下:
```
void get_total_score(Student *student) {
int total = 0;
for (int i = 0; i < MAX_COURSE_NUM; i++) {
total += student->scores[i];
}
student->total_score = total;
}
int show_student_info(Student *student) {
printf("学号:%d\t姓名:%s\t成绩:%d %d %d\t总分:%d\n", student->id, student->name,
student->scores[0], student->scores[1], student->scores[2], student->total_score);
}
void show_all_students(Student *students) {
// 计算总分
for (int i = 0; i < student_count; i++) {
get_total_score(&students[i]);
}
// 排序
for (int i = 0; i < student_count - 1; i++) {
for (int j = i + 1; j < student_count; j++) {
if (students[i].total_score < students[j].total_score) {
Student temp = students[i];
students[i] = students[j];
students[j] = temp;
}
}
}
// 显示信息
printf("\n所有学生信息及总排名:\n");
for (int i = 0; i < student_count; i++) {
show_student_info(&students[i]);
}
}
```
阅读全文