#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
class Student {
public:
std::string name;
std::string student_id;
std::string class_name;
std::string major;
struct CourseScore {
std::string course_name;
std::string course_code;
float score;
};
std::vector<CourseScore> scores;
float GetAverageScore() const {
float sum = 0;
for (const auto& score : scores) {
sum += score.score;
}
return scores.empty() ? 0 : sum / scores.size();
}
float GetMaxScore() const {
if (scores.empty()) {
return 0.0f; // 或者返回一个您认为合适的默认值
}
auto max_it = std::max_element(scores.begin(), scores.end(), [](const CourseScore& a, const CourseScore& b) {
return a.score < b.score;
});
return max_it->score; // 直接通过迭代器访问score成员
}
float GetMinScore() const {
if (scores.empty()) {
return 0.0f; // 或者返回一个您认为合适的默认值
}
auto min_it = std::min_element(scores.begin(), scores.end(), [](const CourseScore& a, const CourseScore& b) {
return a.score < b.score;
});
return min_it->score; // 直接通过迭代器访问score成员
}
};
class StudentManager {
std::vector<Student> students;
public:
std::string name;
std::string student_id;
std::string class_name;
std::string major;
struct CourseScore {
std::string course_name;
std::string course_code;
float score;
};
std::vector<CourseScore> scores;
void AddStudent() {
Student newStudent;
// 获取学生姓名
std::cout << "请输入学生姓名: ";
std::getline(std::cin.ignore(), newStudent.name);
// 获取学生学号
std::cout << "请输入学生学号: ";
std::getline(std::cin, newStudent.student_id);
// 获取班级名称
std::cout << "请输入班级名称: ";
std::getline(std::cin, newStudent.class_name);
// 获取学生专业
std::cout << "请输入学生专业: ";
std::getline(std::cin, newStudent.major);
// 假设我们只输入一个课程成绩作为示例
Student::CourseScore courseScore;
std::cout << "请输入课程名称: ";
std::getline(std::cin, courseScore.course_name);
std::cout << "请输入课程代码: ";
std::getline(std::cin, courseScore.course_code);
std::cout << "请输入课程成绩: ";
std::cin >> courseScore.score;
std::cin.ignore(); // 忽略换行符,以免影响后续的getline调用
newStudent.scores.push_back(courseScore);
// 将课程成绩添加到学生成绩中
students.push_back(newStudent);
std::cout << "已经保存\n";
// 此处可以将 newStudent 添加到学生管理系统中,例如:
// students.push_back(newStudent);
}
void RemoveStudent(const std::string& student_id) {
auto it = std::remove_if(students.begin(), students.end(), [&student_id](const Student& s) {
return s.student_id == student_id;
});
students.erase(it, students.end());
}
bool changeScore() {
std::string course_code;
float new_score;
std::cout << "请输入要修改的课程代码: ";
std::cin >> course_code;
std::cout << "请输入新的成绩: ";
std::cin >> new_score;
for (auto& score : scores) {
if (score.course_code == course_code) {
score.score = new_score;
std::cout << "成绩已成功更新!" << std::endl;
return true; // 找到了对应的课程并更新了成绩
}
}
std::cout << "未找到课程代码为 " << course_code << " 的课程,无法更新成绩。" << std::endl;
return false; // 没有找到对应的课程,无法更新成绩
}
void QueryStudent(const std::string& student_id) {
for (const auto& student : students) {
if (student.student_id == student_id) {
// 找到匹配的学生,打印相关信息
std::cout << "学生姓名: " << student.name << std::endl;
std::cout << "学生学号: " << student.student_id << std::endl;
std::cout << "班级名称: " << student.class_name << std::endl;
std::cout << "学生专业: " << student.major << std::endl;
for (const auto& score : student.scores) {
std::cout << "课程名称: " << score.course_name << ",课程编码" << score.course_code << ",课程成绩: " << score.score << std::endl;
}
return; // 找到后直接返回,不再继续查找
}
}
std::cout << "未找到学号为 " << student_id << " 的学生信息。" << std::endl;
}
void PrintMenu() const {
std::cout << "******* 学生成绩管理系统 *******" << std::endl;
std::cout << "******* 1. 添加学生成绩 *******" << std::endl;
std::cout << "******* 2. 删除学生成绩 *******" << std::endl;
std::cout << "******* 3. 修改学生成绩 *******" << std::endl;
std::cout << "******* 4. 查询学生成绩 *******" << std::endl;
std::cout << "******* 5. 显示所有成绩 *******" << std::endl;
std::cout << "******* 6. 退出系统 *******" << std::endl;
std::cout << "请输入您的选择(1-6): ";
}
void DisplayAllStudentsGrades() {
for (const auto& student : students) {
std::cout << "学生姓名: " << student.name << std::endl;
std::cout << "学生学号: " << student.student_id << std::endl;
std::cout << "班级名称: " << student.class_name << std::endl;
std::cout << "学生专业: " << student.major << std::endl;
for (const auto& score : student.scores) {
std::cout << "课程名称: " << score.course_name
<< ", 课程代码: " << score.course_code
<< ", 课程成绩: " << score.score << std::endl;
}
std::cout << "---------------------------" << std::endl; // 分隔线,方便查看
}
}
};
int main() {
StudentManager manager;
int choice;
std::vector<Student> students;
while (true) {
manager.PrintMenu();
std::cin >> choice;
switch (choice) {
case 1: {
// 增加学生的代码
Student newStudent;
// ... 填充 newStudent 的信息,可以从用户输入获取
manager.AddStudent();
break;
}
case 2: {
// 删除学生的代码
std::string student_id;
std::cout << "请输入要删除学生的学号: ";
std::cin >> student_id;
manager.RemoveStudent(student_id);
break;
}
case 3: {
manager.changeScore();
break;
}
case 4: {
// 查询学生的代码
std::string student_id;
std::cout << "请输入要查询学生的学号: ";
std::cin >> student_id;
manager.QueryStudent(student_id);
break;
}
case 5: {
//显示所有成绩
manager.DisplayAllStudentsGrades();
break;
}
case 6: {
// 退出系统的代码
return 0;
}
default: {
std::cout << "无效的选择,请重新输入。" << std::endl;
break;
}
}
}
}
总是查询不到该课程号