#include\x3ciostream\x3e\n#include\x3cfstream\x3e\n#include\x3cstring\x3e\n#include\x3ciomanip\x3e\n#include\x3cvector\x3e\n#include\x3calgorithm\x3e\n\nusing namespace std;\n\n// 学生基本信息结构体\nstruct StudentInfo {\n string id; // 学号\n string name; // 姓名\n string gender; // 性别\n string dorm; // 宿舍号码\n string phone; // 电话号码\n};\n\n// 学生成绩结构体\nstruct StudentScore {\n string id; // 学号\n string course_id; // 课程编号\n string course_name; // 课程名称\n int credit; // 学分\n double usual_score; // 平时成绩\n double lab_score; // 实验成绩\n double exam_score; // 卷面成绩\n double total_score; // 综合成绩\n int earned_credit; // 实得学分\n};\n\n// 学籍管理系统类\nclass StudentManagementSystem {\npublic:\n // 构造函数\n StudentManagementSystem() {\n // 读取学生基本信息\n ifstream file("A.TXT");\n string line;\n while (getline(file, line)) {\n StudentInfo info;\n info.id = line.substr(0, 2);\n info.name = line.substr(3, 9);\n info.gender = line.substr(13, 3);\n info.dorm = line.substr(17, 3);\n info.phone = line.substr(21, 8);\n m_studentInfos.push_back(info);\n }\n file.close();\n // 读取学生成绩信息\n file.open("B.TXT");\n while (getline(file, line)) {\n StudentScore score;\n score.id = line.substr(0, 2);\n score.course_id = line.substr(3, 3);\n score.course_name = line.substr(7, 6);\n score.credit = stoi(line.substr(14, 1));\n score.usual_score = stod(line.substr(16, 2));\n score.lab_score = stod(line.substr(19, 2));\n score.exam_score = stod(line.substr(22, 2));\n if (score.lab_score == -1) { // 无实验\n score.total_score = score.usual_score * 0.3 + score.exam_score * 0.7;\n } else { // 有实验\n score.total_score = score.usual_score * 0.15 + score.lab_score * 0.15 + score.exam_score * 0.7;\n }\n // 计算实得学分\n if (score.total_score >= 90) {\n score.earned_credit = score.credit;\n } else if (score.total_score >= 80) {\n score.earned_credit = score.credit * 0.8;\n } else if (score.total_score >= 70) {\n score.earned_credit = score.credit * 0.75;\n } else if (score.total_score >= 60) {\n score.earned_credit = score.credit * 0.6;\n } else {\n score.earned_credit = score.credit * 0;\n }\n m_studentScores.push_back(score);\n }\n file.close();\n }\n \n // 数据录入功能\n void inputScore() {\n StudentScore score;\n cout << "请输入学号:";\n cin >> score.id;\n cout << "请输入课程编号:";\n cin >> score.course_id;\n cout << "请输入课程名称:";\n cin >> score.course_name;\n cout << "请输入学分:";\n cin >> score.credit;\n cout << "请输入平时成绩:";\n cin >> score.usual_score;\n cout << "请输入实验成绩(无实验请输-1):";\n cin >> score.lab_score;\n cout << "请输入卷面成绩:";\n cin >> score.exam_score;\n if (score.lab_score == -1) { // 无实验\n score.total_score = score.usual_score * 0.3 + score.exam_score * 0.7;\n } else { // 有实验\n score.total_score = score.usual_score * 0.15 + score.lab_score * 0.15 + score.exam_score * 0.7;\n }\n // 计算实得学分\n if (score.total_score >= 90) {\n score.earned_credit = score.credit;\n } else if (score.total_score >= 80) {\n score.earned_credit = score.credit * 0.8;\n } else if (score.total_score >= 70) {\n score.earned_credit = score.credit * 0.75;\n } else if (score.total_score >= 60) {\n score.earned_credit = score.credit * 0.6;\n } else {\n score.earned_credit = score.credit * 0;\n }\n m_studentScores.push_back(score);\n // 写入文件\n ofstream fout("B.TXT", ios::app);\n fout << score.id << " " << score.course_id << " " << score.course_name << " " << score.credit << " "\n << score.usual_score << " " << score.lab_score << " " << score.exam_score << " " <<score.earned_credit<<endl;\n fout.close();\n }\n \n // 学生基本情况查询\n void searchStudentInfo() {\n int choice;\n cout << "请选择查询方式(1-学号,2-姓名):";\n cin >> choice;\n if (choice == 1) {\n string id;\n cout << "请输入学号:";\n cin >> id;\n for (auto info : m_studentInfos) {\n if (info.id == id) {\n cout << "学号:" << info.id << " 姓名:" << info.name << " 性别:" << info.gender\n << " 宿舍号码:" << info.dorm << " 电话号码:" << info.phone << endl;\n return;\n }\n }\n cout << "未找到该学生" << endl;\n } else if (choice == 2) {\n string name;\n cout << "请输入姓名:";\n cin >> name;\n for (auto info : m_studentInfos) {\n if (info.name == name) {\n cout << "学号:" << info.id << " 姓名:" << info.name << " 性别:" << info.gender\n << " 宿舍号码:" << info.dorm << " 电话号码:" << info.phone << endl;\n }\n }\n } else {\n cout << "输入有误" << endl;\n }\n }\n \n // 成绩查询\n void searchScore() {\n string id;\n cout << "请输入学号:"; \n cin >> id;\n cout << "学号:" << id << endl;\n double total_score_sum = 0; // 综合成绩之和\n int earned_credit_sum = 0; // 实得学分之和\n for (auto score : m_studentScores) {\n if (score.id == id) {\n cout << "课程编号:" << score.course_id << " 课程名称:" << score.course_name\n << " 综合成绩:" << setprecision(2) << fixed << score.total_score\n << " 实得学分:" << score.earned_credit << endl;\n total_score_sum += score.total_score;\n earned_credit_sum += score.earned_credit;\n }\n }\n cout << "共修:" << m_studentScores.size() << " 科,实得总学分为:" << earned_credit_sum << endl;\n }\n \n // 删除功能\n void deleteStudent(string id) {\n // 删除学生基本信息\n for (auto it = m_studentInfos.begin(); it != m_studentInfos.end(); ++it) {\n if (it->id == id) {\n m_studentInfos.erase(it);\n break;\n }\n }\n // 删除学生成绩信息\n vector\x3cStudentScore\x3e newScores;\n for (auto score : m_studentScores) {\n if (score.id != id) {\n newScores.push_back(score);\n }\n }\n m_studentScores = newScores;\n // 更新文件\n ofstream fout("A.TXT");\n for (auto info : m_studentInfos) {\n fout << info.id << " " << info.name << " " << info.gender << " " << info.dorm << " " << info.phone << endl;\n }\n fout.close();\n fout.open("B.TXT");\n for (auto score : m_studentScores) {\n fout << score.id << " " << score.course_id << " " << score.course_name << " " << score.credit << " "\n << score.usual_score << " " << score.lab_score << " " << score.exam_score << endl;\n }\n fout.close();\n }\n \n // 排序功能\n void sortScores(bool by_total_score, bool ascending) {\n if (by_total_score) { // 按综合成绩排序\n if (ascending) { // 升序\n sort(m_studentScores.begin(), m_studentScores.end(), [](const StudentScore& a, const StudentScore& b) {\n return a.total_score < b.total_score;\n });\n } else { // 降序\n sort(m_studentScores.begin(), m_studentScores.end(), [](const StudentScore& a, const StudentScore& b) {\n return a.total_score > b.total_score;\n });\n }\n } else { // 按实得学分排序\n if (ascending) { // 升序\n sort(m_studentScores.begin(), m_studentScores.end(), [](const StudentScore& a, const StudentScore& b) {\n return a.earned_credit < b.earned_credit;\n });\n } else { // 降序\n sort(m_studentScores.begin(), m_studentScores.end(), [](const StudentScore& a, const StudentScore& b) {\n return a.earned_credit > b.earned_credit;\n });\n }\n }\n // 输出排序结果\n cout << "学号\t课程编号\t课程名称\t学分\t平时成绩\t实验成绩\t卷面成绩\t综合成绩\t实得学分" << endl;\n for (auto score : m_studentScores) {\n cout << score.id << "\t" << score.course_id << "\t\t" << score.course_name << "\t\t" << score.credit << "\t"\n << score.usual_score << "\t\t" << score.lab_score << "\t\t" << score.exam_score << "\t\t"\n << setprecision(2) << fixed << score.total_score << "\t\t" << score.earned_credit << endl;\n }\n }\n \nprivate:\n vector\x3cStudentInfo\x3e m_studentInfos; // 学生基本信息\n vector\x3cStudentScore\x3e m_studentScores; // 学生成绩信息\n};\n\nint main() {\n StudentManagementSystem sms;\n int choice;\n while (true) {\n cout << "请选择功能(1-数据录入,2-学生基本情况查询,3-成绩查询,4-删除,5-排序,0-退出):";\n cin >> choice;\n switch (choice) {\n case 1: // 数据录入功能\n sms.inputScore();\n break;\n case 2: // 学生基本情况查询\n sms.searchStudentInfo();\n break;\n case 3: // 成绩查询\n sms.searchScore();\n break;\n case 4: { // 删除功能\n string id;\n cout << "请输入要删除的学号:";\n cin >> id;\n sms.deleteStudent(id);\n break;\n }\n case 5: { // 排序功能\n int sortChoice, orderChoice;\n cout << "请选择排序方式(1-按综合成绩,2-按实得学分):";\n cin >> sortChoice;\n cout << "请选择排序顺序(1-升序,2-降序):";\n cin >> orderChoice;\n sms.sortScores(sortChoice == 1, orderChoice == 1);\n break;\n }\n case 0: // 退出程序\n return 0;\n default:\n cout << "输入有误,请重新输入" << endl;\n }\n }\n return 0;\n

C++ 学生学籍管理系统 - 数据录入、查询、删除、排序

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

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