定义一个结构体数组存放五位学生的学号,姓名以及3门课程的成绩,编写程序从屏幕输入这五位学生的信息,对次数组按总分进行排序再打印排序后的学生信息。
时间: 2023-06-13 11:06:09 浏览: 103
以下是一个解决该问题的C++代码示例:
```cpp
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
struct Student {
string id;
string name;
int scores[3];
int totalScore;
};
bool compare(Student a, Student b) {
return a.totalScore > b.totalScore;
}
int main() {
Student students[5];
for (int i = 0; i < 5; i++) {
cout << "请输入第" << i+1 << "位学生的信息:" << endl;
cout << "学号:";
cin >> students[i].id;
cout << "姓名:";
cin >> students[i].name;
cout << "3门课程的成绩:";
for (int j = 0; j < 3; j++) {
cin >> students[i].scores[j];
students[i].totalScore += students[i].scores[j];
}
}
sort(students, students+5, compare);
cout << "按总分排序后的学生信息:" << endl;
for (int i = 0; i < 5; i++) {
cout << "学号:" << students[i].id << endl;
cout << "姓名:" << students[i].name << endl;
cout << "3门课程的成绩:";
for (int j = 0; j < 3; j++) {
cout << students[i].scores[j] << " ";
}
cout << endl;
cout << "总分:" << students[i].totalScore << endl;
cout << endl;
}
return 0;
}
```
运行程序后,输入5位学生的信息,程序将按总分排序后输出学生信息。
阅读全文