#include \n#include \n#include \n#include \n\nusing namespace std;\n\n// 学生基本信息结构体\nstruct Student {\n string studentId;\n string name;\n string gender;\n string dormitory;\n string phoneNumber;\n};\n\n// 学生成绩基本信息结构体\nstruct Score {\n string studentId;\n string courseCode;\n string courseName;\n int credit;\n int dailyScore;\n int experimentScore;\n int finalScore;\n int comprehensiveScore;\n int earnedCredit;\n};\n\n// 学生管理系统类\nclass StudentManagementSystem {\nprivate:\n vector students;\n vector scores;\npublic:\n void loadData() {\n string line;\n ifstream studentFile("A.txt");\n if (studentFile.is_open()) {\n while (getline(studentFile, line)) {\n Student student;\n student.studentId = line.substr(0, 2);\n student.name = line.substr(3, 6);\n student.gender = line.substr(10, 2);\n student.dormitory = line.substr(13, 3);\n student.phoneNumber = line.substr(20, 8);\n students.push_back(student);\n }\n studentFile.close();\n }\n\n ifstream scoreFile("B.txt");\n if (scoreFile.is_open()) {\n while (getline(scoreFile, line)) {\n Score score;\n score.studentId = line.substr(0, 2);\n score.courseCode = line.substr(3, 3);\n score.courseName = line.substr(7, 10);\n score.credit = stoi(line.substr(18, 1));\n score.dailyScore = stoi(line.substr(20, 2));\n score.experimentScore = stoi(line.substr(23, 2));\n score.finalScore = stoi(line.substr(26, 2));\n score.comprehensiveScore = calculateComprehensiveScore(score);\n score.earnedCredit = calculateEarnedCredit(score);\n scores.push_back(score);\n }\n scoreFile.close();\n }\n }\n\n void saveData() {\n ofstream studentFile("A.txt");\n if (studentFile.is_open()) {\n for (const auto& student : students) {\n studentFile << student.studentId << " " << student.name << " " << student.gender << " " << student.dormitory << " " << student.phoneNumber << endl;\n }\n studentFile.close();\n }\n\n ofstream scoreFile("B.txt");\n if (scoreFile.is_open()) {\n for (const auto& score : scores) {\n scoreFile << score.studentId << " " << score.courseCode << " " << score.courseName << " " << score.credit << " " << score.dailyScore << " " << score.experimentScore << " " << score.finalScore << endl;\n }\n scoreFile.close();\n }\n }\n\n void addScore() {\n Score score;\n cout << "请输入学号: ";\n cin >> score.studentId;\n cout << "请输入课程编号: ";\n cin >> score.courseCode;\n cout << "请输入课程名称: ";\n cin >> score.courseName;\n cout << "请输入学分: ";\n cin >> score.credit;\n cout << "请输入平时成绩: ";\n cin >> score.dailyScore;\n cout << "请输入实验成绩: ";\n cin >> score.experimentScore;\n cout << "请输入卷面成绩: ";\n cin >> score.finalScore;\n score.comprehensiveScore = calculateComprehensiveScore(score);\n score.earnedCredit = calculateEarnedCredit(score);\n scores.push_back(score);\n }\n\n void searchStudent() {\n int choice;\n cout << "请选择查询方式(1.学号 2.姓名): ";\n cin >> choice;\n if (choice == 1) {\n string studentId;\n cout << "请输入学号: ";\n cin >> studentId;\n for (const auto& student : students) {\n if (student.studentId == studentId) {\n cout << "学号: " << student.studentId << " 姓名: " << student.name << " 性别: " << student.gender << " 宿舍号码: " << student.dormitory << " 电话号码: " << student.phoneNumber << endl;\n break;\n }\n }\n } else if (choice == 2) {\n string name;\n cout << "请输入姓名: ";\n cin >> name;\n for (const auto& student : students) {\n if (student.name == name) {\n cout << "学号: " << student.studentId << " 姓名: " << student.name << " 性别: " << student.gender << " 宿舍号码: " << student.dormitory << " 电话号码: " << student.phoneNumber << endl;\n }\n }\n }\n }\n\n void searchScore() {\n string studentId;\n cout << "请输入学号: ";\n cin >> studentId;\n for (const auto& score : scores) {\n if (score.studentId == studentId) {\n cout << "学号: " << score.studentId << " 姓名: " << getStudentNameById(score.studentId) << endl;\n cout << "课程编号: " << score.courseCode << " 课程名称: " << score.courseName << " 综合成绩: " << score.comprehensiveScore << " 实得学分: " << score.earnedCredit << endl;\n }\n }\n }\n\n void deleteStudent() {\n string studentId;\n cout << "请输入学号: ";\n cin >> studentId;\n students.erase(remove_if(students.begin(), students.end(), [&studentId](const Student& student) { return student.studentId == studentId; }), students.end());\n scores.erase(remove_if(scores.begin(), scores.end(), [&studentId](const Score& score) { return score.studentId == studentId; }), scores.end());\n }\n\n void sortScores() {\n int choice;\n cout << "请选择排序方式(1.综合成绩升序 2.综合成绩降序 3.实得学分升序 4.实得学分降序): ";\n cin >> choice;\n if (choice == 1) {\n sort(scores.begin(), scores.end(), [](const Score& a, const Score& b) { return a.comprehensiveScore < b.comprehensiveScore; });\n } else if (choice == 2) {\n sort(scores.begin(), scores.end(), [](const Score& a, const Score& b) { return a.comprehensiveScore > b.comprehensiveScore; });\n } else if (choice == 3) {\n sort(scores.begin(), scores.end(), [](const Score& a, const Score& b) { return a.earnedCredit < b.earnedCredit; });\n } else if (choice == 4) {\n sort(scores.begin(), scores.end(), [](const Score& a, const Score& b) { return a.earnedCredit > b.earnedCredit; });\n }\n }\n\nprivate:\n int calculateComprehensiveScore(const Score& score) {\n if (score.experimentScore == -1) {\n return score.dailyScore * 0.3 + score.finalScore * 0.7;\n } else {\n return score.dailyScore * 0.15 + score.experimentScore * 0.15 + score.finalScore * 0.7;\n }\n }\n\n int calculateEarnedCredit(const Score& score) {\n if (score.comprehensiveScore >= 90 && score.comprehensiveScore <= 100) {\n return score.credit * 1;\n } else if (score.comprehensiveScore >= 80 && score.comprehensiveScore < 90) {\n return score.credit * 0.8;\n } else if (score.comprehensiveScore >= 70 && score.comprehensiveScore < 80) {\n return score.credit * 0.75;\n } else if (score.comprehensiveScore >= 60 && score.comprehensiveScore < 70) {\n return score.credit * 0.6;\n } else {\n return score.credit * 0;\n }\n }\n\n string getStudentNameById(const string& studentId) {\n for (const auto& student : students) {\n if (student.studentId == studentId) {\n return student.name;\n }\n }\n return "";\n }\n};\n\nint main() {\n StudentManagementSystem system;\n system.loadData();\n\n int choice;\n while (true) {\n cout << "请选择操作(1.数据录入 2.学生基本情况查询 3.成绩查询 4.删除 5.排序 6.保存数据 7.退出): ";\n cin >> choice;\n if (choice == 1) {\n system.addScore();\n } else if (choice == 2) {\n system.searchStudent();\n } else if (choice == 3) {\n system.searchScore();\n } else if (choice == 4) {\n system.deleteStudent();\n } else if (choice == 5) {\n system.sortScores();\n } else if (choice == 6) {\n system.saveData();\n } else if (choice == 7) {\n break;\n }\n }\n\n return 0;\n}


原文地址: https://www.cveoy.top/t/topic/pq0C 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录