1. 定义学生类,包括学号、姓名、性别、多门课程的成绩;假设有n个学生,由键盘输入学生信息; 2.定义类成员函数,计算每个学生的平均成绩; 3.按课程统计每门课程的平均成绩、最高分和最低分; 4.同时输出所有学生信息; 5. 通过输入学生学号或姓名查询某学生的信息。
时间: 2023-12-15 10:12:19 浏览: 123
1. 定义学生类:
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 getAvgScore() {
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. 计算每个学生的平均成绩:
vector<Student> students;
// 从键盘输入n个学生信息
for (int i = 0; i < n; i++) {
string id, name, gender;
vector<double> scores;
// 从键盘输入学生信息
// ...
Student student(id, name, gender, scores);
students.push_back(student);
}
for (Student& student : students) {
double avgScore = student.getAvgScore();
// 输出学生平均成绩
// ...
}
3. 按课程统计每门课程的平均成绩、最高分和最低分:
vector<string> courseNames = {"数学", "语文", "英语", "物理", "化学", "生物"}; // 假设共有6门课程
vector<vector<double>> courseScores(courseNames.size());
for (Student& student : students) {
vector<double> scores = student.getScores();
for (int i = 0; i < scores.size(); i++) {
courseScores[i].push_back(scores[i]);
}
}
for (int i = 0; i < courseNames.size(); i++) {
double sum = 0;
double maxScore = courseScores[i][0];
double minScore = courseScores[i][0];
for (double score : courseScores[i]) {
sum += score;
maxScore = max(maxScore, score);
minScore = min(minScore, score);
}
double avgScore = sum / courseScores[i].size();
// 输出课程平均成绩、最高分和最低分
// ...
}
4. 同时输出所有学生信息:
for (Student& student : students) {
// 输出学生信息
// ...
}
5. 通过输入学生学号或姓名查询某学生的信息:
string search;
// 从键盘输入学号或姓名
// ...
for (Student& student : students) {
if (student.getId() == search || student.getName() == search) {
// 输出学生信息
// ...
break;
}
}
阅读全文