结构体练习-1创建结构体student,包括姓名,数学成绩和英语成绩,输入3名同学的姓名和成绩,输出总分最高的同学的数学成绩和英语成绩 c++
时间: 2024-10-28 20:09:09 浏览: 15
在C++中,你可以创建一个名为`Student`的结构体来表示学生的信息,并通过数组或动态容器存储多名学生的数据。下面是一个简单的示例程序:
```cpp
#include <iostream>
#include <string>
using namespace std;
// 定义结构体 Student
struct Student {
string name;
int mathScore;
int englishScore;
};
int main() {
// 定义数组,用于存储3名同学的数据
const int numStudents = 3;
Student students[numStudents];
for (int i = 0; i < numStudents; ++i) {
cout << "请输入第" << (i + 1) << "位同学的姓名、数学成绩和英语成绩:" << endl;
cin >> students[i].name >> students[i].mathScore >> students[i].englishScore;
}
// 初始化最高分变量
int highestMath = students[0].mathScore;
int highestEnglish = students[0].englishScore;
string studentWithHighestScore;
// 比较每个学生的分数,找出总分最高的
for (int i = 1; i < numStudents; ++i) {
if (students[i].mathScore + students[i].englishScore > highestMath + highestEnglish) {
highestMath = students[i].mathScore;
highestEnglish = students[i].englishScore;
studentWithHighestScore = students[i].name;
}
}
cout << "总分最高的同学(" << studentWithHighestScore << ")的数学成绩是: " << highestMath << ", 英语成绩是: " << highestEnglish << endl;
return 0;
}
```
阅读全文