1. 定义学生类,包括学号、姓名、性别、多门课程的成绩;假设有n个学生,由键盘输入学生信息; 2.定义类成员函数,计算每个学生的平均成绩; 3.按课程统计每门课程的平均成绩、最高分和最低分; 4.同时输出所有学生信息; 5. 通过输入学生学号或姓名查询某学生的信息。
时间: 2023-12-15 07:28:07 浏览: 41
1. 定义学生类:
```c++
class Student {
private:
string id; // 学号
string name; // 姓名
string gender; // 性别
vector<double> scores; // 多门课程的成绩
public:
Student(string id, string name, string gender, vector<double> scores) {
this->id = id;
this->name = name;
this->gender = gender;
this->scores = scores;
}
double getAverageScore() { // 计算平均成绩
double sum = 0;
for (double score : scores) {
sum += score;
}
return sum / scores.size();
}
string getId() { // 获取学号
return id;
}
string getName() { // 获取姓名
return name;
}
string getGender() { // 获取性别
return gender;
}
vector<double> getScores() { // 获取成绩
return scores;
}
};
```
2. 输入学生信息:
```c++
vector<Student> students;
int n;
cout << "请输入学生人数:";
cin >> n;
for (int i = 0; i < n; i++) {
string id, name, gender;
vector<double> scores;
cout << "请输入第" << i + 1 << "个学生的学号、姓名、性别和多门课程的成绩:";
cin >> id >> name >> gender;
for (int j = 0; j < 3; j++) {
double score;
cin >> score;
scores.push_back(score);
}
Student student(id, name, gender, scores);
students.push_back(student);
}
```
3. 统计每门课程的平均成绩、最高分和最低分:
```c++
vector<double> chineseScores, mathScores, englishScores;
for (Student student : students) {
chineseScores.push_back(student.getScores()[0]);
mathScores.push_back(student.getScores()[1]);
englishScores.push_back(student.getScores()[2]);
}
double chineseAverage = accumulate(chineseScores.begin(), chineseScores.end(), 0.0) / chineseScores.size();
double mathAverage = accumulate(mathScores.begin(), mathScores.end(), 0.0) / mathScores.size();
double englishAverage = accumulate(englishScores.begin(), englishScores.end(), 0.0) / englishScores.size();
double chineseMax = *max_element(chineseScores.begin(), chineseScores.end());
double mathMax = *max_element(mathScores.begin(), mathScores.end());
double englishMax = *max_element(englishScores.begin(), englishScores.end());
double chineseMin = *min_element(chineseScores.begin(), chineseScores.end());
double mathMin = *min_element(mathScores.begin(), mathScores.end());
double englishMin = *min_element(englishScores.begin(), englishScores.end());
cout << "语文平均成绩:" << chineseAverage << ",最高分:" << chineseMax << ",最低分:" << chineseMin << endl;
cout << "数学平均成绩:" << mathAverage << ",最高分:" << mathMax << ",最低分:" << mathMin << endl;
cout << "英语平均成绩:" << englishAverage << ",最高分:" << englishMax << ",最低分:" << englishMin << endl;
```
4. 输出所有学生信息:
```c++
cout << "所有学生信息:" << endl;
for (Student student : students) {
cout << "学号:" << student.getId() << ",姓名:" << student.getName() << ",性别:" << student.getGender() << ",成绩:";
for (double score : student.getScores()) {
cout << score << " ";
}
cout << ",平均成绩:" << student.getAverageScore() << endl;
}
```
5. 查询某学生的信息:
```c++
string query;
cout << "请输入查询学生的学号或姓名:";
cin >> query;
for (Student student : students) {
if (query == student.getId() || query == student.getName()) {
cout << "学号:" << student.getId() << ",姓名:" << student.getName() << ",性别:" << student.getGender() << ",成绩:";
for (double score : student.getScores()) {
cout << score << " ";
}
cout << ",平均成绩:" << student.getAverageScore() << endl;
}
}
```