From d67d3cea4712b0fd2347bf4f1a0e281adedb46e4 Mon Sep 17 00:00:00 2001 From: prjuvme2c <3767838439@qq.com> Date: Sat, 31 May 2025 17:29:14 +0800 Subject: [PATCH] ADD file via upload --- C++ student-grade-manager.cpp | 243 ++++++++++++++++++++++++++++++++++ 1 file changed, 243 insertions(+) create mode 100644 C++ student-grade-manager.cpp diff --git a/C++ student-grade-manager.cpp b/C++ student-grade-manager.cpp new file mode 100644 index 0000000..d647c61 --- /dev/null +++ b/C++ student-grade-manager.cpp @@ -0,0 +1,243 @@ +#include +#include +#include +#include +#include + +using namespace std; + +struct Student { + string id; + string name; + vector scores; + float total; +}; + +// 输入学生信息 +// 添加总分计算函数 +void calculateTotal(Student& s) { + s.total = s.scores[0] + s.scores[1] + s.scores[2]; +} + +// 修改输入学生信息函数 +// 新增学号输入函数 +void inputStudentId(Student& s) { + while(true) { + cout << "输入学号: "; + cin >> s.id; + if(s.id.find_first_not_of("0123456789") == string::npos) break; + cout << "学号必须为纯数字,请重新输入!\n"; + cin.clear(); + cin.ignore(numeric_limits::max(), '\n'); + } +} + +// 修改后的学生信息输入 +void inputStudentInfo(Student& s) { + cout << "输入姓名: "; + cin >> s.name; + + s.scores.resize(3); + cout << "输入语文成绩: "; + cin >> s.scores[0]; + cout << "输入数学成绩: "; + cin >> s.scores[1]; + cout << "输入英语成绩: "; + cin >> s.scores[2]; + + calculateTotal(s); +} + +// 前向声明printStudent函数 +void printStudent(const Student& s); + +// 学生管理系统类 +class StudentManager { +private: + vector students; + +public: + // 添加学生(修复丢失的成员函数) + void addStudent() { + Student s; + + // 学号验证循环 + do { + inputStudentId(s); + auto exist = find_if(students.begin(), students.end(), + [&](const Student& stu){ return stu.id == s.id; }); + if (exist == students.end()) break; + cout << "学号已存在,请重新输入!\n"; + } while(true); + + // 输入其他信息 + inputStudentInfo(s); + students.push_back(s); + cout << "添加成功!\n"; + } + // 显示全部学生(始终按学号排序) + // 显示全部学生 + void displayAll() { + if (students.empty()) { + cout << "当前没有学生记录\n"; + return; + } + + // 创建临时副本进行排序 + vector temp = students; + sort(temp.begin(), temp.end(), [](const Student& a, const Student& b) { + // 修改为升序排列:先比长度,短的在前;长度相同按字典序升序 + if (a.id.length() != b.id.length()) + return a.id.length() < b.id.length(); + return a.id < b.id; + }); + + for_each(temp.begin(), temp.end(), printStudent); + } + + // 按总分排序(不修改原始数据) + void sortByTotal() { + if(students.empty()) { + cout << "当前没有学生记录\n"; + return; + } + + vector temp = students; + sort(temp.begin(), temp.end(), + [](const Student& a, const Student& b){ return a.total > b.total; }); + + cout << "=== 总分排序结果 ===\n"; + for_each(temp.begin(), temp.end(), printStudent); + } + + // 删除学生 + void deleteStudent(const string& id) { + auto it = find_if(students.begin(), students.end(), + [&](const Student& s){ return s.id == id; }); + if (it != students.end()) { + students.erase(it); + cout << "删除成功\n"; + } else { + cout << "未找到该学生\n"; + } + } + + // 修改学生信息 + // 修正修改学生信息逻辑 + // 修改学生信息 + void modifyStudent(const string& id) { + auto it = find_if(students.begin(), students.end(), + [&](const Student& s){ return s.id == id; }); + if (it != students.end()) { + Student original = *it; // 保存完整原始数据 + + // 输入学号并进行验证 + inputStudentId(*it); + + // 检查学号是否重复(排除自己) + if (it->id != original.id) { + auto conflict = find_if(students.begin(), students.end(), + [&](const Student& s){ return s.id == it->id && &s != &(*it); }); + if (conflict != students.end()) { + cout << "错误:学号已存在!\n"; + *it = original; // 恢复全部原始数据 + return; + } + } + + // 输入其他信息 + inputStudentInfo(*it); + cout << "修改成功\n"; + } else { + cout << "未找到该学生\n"; + } + } + + // 新增查询学生函数 + void queryStudent(const string& id) { + auto it = find_if(students.begin(), students.end(), + [&](const Student& s){ return s.id == id; }); + if (it != students.end()) { + printStudent(*it); + } else { + cout << "未找到学号为 " << id << " 的学生\n"; + } + } +}; + +// 修改菜单界面 +void showMenu() { + cout << "\n====== 学生成绩管理系统 ======\n" + << "1. 添加学生\n" + << "2. 显示所有学生\n" + << "3. 按总分排序\n" + << "4. 删除学生\n" + << "5. 修改学生信息\n" + << "6. 查询学生信息\n" // 新增菜单项 + << "0. 退出\n" + << "=============================\n" + << "请选择操作: "; +} + +int main() { + StudentManager manager; + int choice = -1; // 初始化为非0值 + string tmpId; + + do { + showMenu(); + if (!(cin >> choice)) { + cin.clear(); + cin.ignore(numeric_limits::max(), '\n'); + cout << "请输入数字选项!" << endl; + choice = -1; // 重置为无效值 + continue; + } + cin.ignore(numeric_limits::max(), '\n'); // 统一处理换行符 + + switch(choice) { + case 1: + manager.addStudent(); + break; + case 2: + manager.displayAll(); + break; + case 3: + manager.sortByTotal(); + break; + case 4: + cout << "输入要删除的学号: "; + cin >> tmpId; + manager.deleteStudent(tmpId); + break; + case 5: + cout << "输入要修改的学号: "; + cin >> tmpId; + manager.modifyStudent(tmpId); + break; + case 6: + cout << "输入要查询的学号: "; + cin >> tmpId; + manager.queryStudent(tmpId); + break; + case 0: + cout << "感谢使用,再见!\n"; + break; + default: + cout << "无效输入,请重新选择!\n"; + // 删除此处的ignore,已在主循环统一处理 + } + } while(choice != 0); // 只有当明确输入0时退出 + + return 0; +} + +// 后续补充printStudent的具体实现 +void printStudent(const Student& s) { + cout << "学号: " << s.id + << "\t姓名: " << s.name + << "\t总分: " << s.total + << " (语文:" << s.scores[0] + << " 数学:" << s.scores[1] + << " 英语:" << s.scores[2] << ")\n"; +} \ No newline at end of file