定义学生类,包括学号、姓名、性别、多门课程的成绩;假设有n个学生,由键盘输入 学生信息; 定义类成员函数,计算每个学生的平均成绩; 按课程统计每门课程的平均成绩、最高分和最低分; 同时输出所有学生信息; 通过输入学生学号或姓名查询某学生的信息。
时间: 2023-11-16 15:51:57 浏览: 113
学生成绩和信息管理系统
class Student {
private:
int stu_id; //学号
string name; //姓名
char gender; //性别
vector<double> grades; //多门课程的成绩
public:
Student(int id, string n, char g, vector<double> gds) {
stu_id = id;
name = n;
gender = g;
grades = gds;
}
double getAverage() {
double sum = 0;
for (int i = 0; i < grades.size(); i++) {
sum += grades[i];
}
return sum / grades.size();
}
int getId() {
return stu_id;
}
string getName() {
return name;
}
char getGender() {
return gender;
}
vector<double> getGrades() {
return grades;
}
};
class School {
private:
vector<Student> students;
public:
void addStudent(Student s) {
students.push_back(s);
}
void printAllStudents() {
for (int i = 0; i < students.size(); i++) {
cout << "学号:" << students[i].getId() << " 姓名:" << students[i].getName() << " 性别:" << students[i].getGender() << endl;
vector<double> grades = students[i].getGrades();
cout << "成绩:";
for (int j = 0; j < grades.size(); j++) {
cout << grades[j] << " ";
}
cout << "平均成绩:" << students[i].getAverage() << endl;
}
}
double getCourseAverage(int courseIndex) {
double sum = 0;
for (int i = 0; i < students.size(); i++) {
vector<double> grades = students[i].getGrades();
sum += grades[courseIndex];
}
return sum / students.size();
}
double getCourseHighest(int courseIndex) {
double highest = 0;
for (int i = 0; i < students.size(); i++) {
vector<double> grades = students[i].getGrades();
if (grades[courseIndex] > highest) {
highest = grades[courseIndex];
}
}
return highest;
}
double getCourseLowest(int courseIndex) {
double lowest = 100;
for (int i = 0; i < students.size(); i++) {
vector<double> grades = students[i].getGrades();
if (grades[courseIndex] < lowest) {
lowest = grades[courseIndex];
}
}
return lowest;
}
void printCourseInfo(int courseIndex) {
cout << "课程" << courseIndex << "的平均成绩:" << getCourseAverage(courseIndex) << endl;
cout << "课程" << courseIndex << "的最高分:" << getCourseHighest(courseIndex) << endl;
cout << "课程" << courseIndex << "的最低分:" << getCourseLowest(courseIndex) << endl;
}
void searchStudent(string keyword) {
for (int i = 0; i < students.size(); i++) {
if (students[i].getId() == stoi(keyword) || students[i].getName() == keyword) {
cout << "学号:" << students[i].getId() << " 姓名:" << students[i].getName() << " 性别:" << students[i].getGender() << endl;
vector<double> grades = students[i].getGrades();
cout << "成绩:";
for (int j = 0; j < grades.size(); j++) {
cout << grades[j] << " ";
}
cout << "平均成绩:" << students[i].getAverage() << endl;
}
}
}
};
阅读全文