💕💕💕大家好,这是作业侠系列之C++实现学生管理系统,还是那句话,大家不想cv或者cv了跑不起来,三连后都可以来找我要源码,私信或评论留下你的邮箱即可。有任何问题有可以私聊我,大家觉得还行的话,期待你们的三连,这也是我创作的最大动力💕💕💕
往期源码回顾:
【Java】实现绘图板(完整版)
【C++】图书管理系统(完整板)
【Java】实现计算器(完整版)
【Python】实现爬虫,爬取天气情况并进行分析(完整版)
【Java】实现记事本(完整版)
【Java】实现多线程计算阶乘和(完整版)
插个小广告
本人在线接单,无中介,价格实惠!!!有兴趣的小伙伴可以私聊我,质量保证
学生管理系统功能概览,具体操作大家可以自己运行试试:
代码概览:
OperationManagement.h 将学生,成绩等信息等从文件中读取出来以及其他操作,放入容器中,便于操作,不用一直对文件进行I/O.
Score.h 用于对成绩抽象,并实现了>>的重载,便于文件读入,读出
Student.h 对学生进行的抽象,同样实现了>>的重载,便于文件读入,读出
插个小广告
本人在线接单,无中介,价格实惠!!!有兴趣的小伙伴可以私聊我,质量保证
全部代码与讲解:
1.sms.cpp
#include <iostream>
#include "OperationManagement.h"
int main()
{OperationManagement om;om.init();while (true){om.showMenu();cout << "请输入您要使用的功能序号:" << endl;int choice;cin >> choice;switch (choice){case 0: //添加功能om.addStudent();break;case 1: //删除功能 om.deleteStudent();break;case 2: //更新功能om.updateStudent();break;case 3: om.addScore();break;case 4: om.deleteScore();break;case 5: om.updateScore();break;case 6: om.displayStudentInfo();break;default:system("cls"); //cls的作用是清空屏幕的功能,使用户可以再次输入选项数字break;}}system("pause"); //使程序有一个暂停阻碍的功能,防止界面突然退出return 0;}
score.cpp
#include "Score.h"Score::Score()
{
}Score::Score(string id, string course, int score): studentId(id), courseName(course), scoreValue(score) {}string Score::getStudentId() { return studentId; }string Score::getCourseName() { return courseName; }int Score::getScoreValue() { return scoreValue; }void Score::setStudentId(string id) {studentId = id;
}void Score::setCourseName(string course) {courseName = course;
}void Score::setScoreValue(int score) {scoreValue = score;
}istream& operator>>(istream& input, Score* s) {input >> s->studentId >> s->courseName >> s->scoreValue ;return input;
}ostream& operator<<(ostream& output, const Score* s) {output << s->studentId << " " << s->courseName << " " << s->scoreValue << endl;return output;
}void Score::display() {cout << "学生ID: " << studentId << " "<<"课程名称: " << courseName << " "<<"分值: " << scoreValue << endl;
}
student.cpp
#include "Student.h"Student::Student()
{
}Student::Student(string id,string n, string dorm,string phone): studentId(id), name(n), dormitory(dorm), phoneNumber(phone) {}string Student::getStudentId() { return studentId; }string Student::getName() { return name; }string Student::getDormitory() { return dormitory; }string Student::getPhoneNumber() { return phoneNumber; }void Student::setStudentId(string id) {studentId = id;
}void Student::setName(string n) {name = n;
}void Student::setDormitory(string dorm) {dormitory = dorm;
}void Student::setPhoneNumber(string phone) {phoneNumber = phone;
}istream& operator>>(istream& input, Student* s) {input >> s->studentId >> s->name >> s->dormitory >> s->phoneNumber ;return input;
}ostream& operator<<(ostream& output, const Student* s) {output << s->studentId << " " << s->name << " " << s->dormitory << " " << s->phoneNumber ;return output;
}
OperationManagement.cpp
#include "OperationManagement.h"
#include<fstream>
#include<iostream>using namespace std;
void OperationManagement::init()
{ifstream readFile;readFile.open("students.txt");if (!readFile.is_open()){cout << "学生数据读取错误" << endl;readFile.close();return;}while (!readFile.eof()){Student* student = new Student();readFile >>student;string id = student->getStudentId();students[id] = student;}readFile.close();readFile.open("scores.txt");if (!readFile.is_open()){cout << "成绩数据读取错误" << endl;readFile.close();return;}while (!readFile.eof()){Score* score = new Score();readFile >> score;string id = score->getStudentId();scores.insert(make_pair(id, score));}readFile.close();
}void OperationManagement::displayStudentInfo() {string studentId;cout << "请输入您要查询的学生学号" << endl;cin >> studentId;for (auto map : students) {if (map.first == studentId) {Student* student = map.second;cout << "学号: "<< studentId <<"学生信息如下:" << endl;cout << "姓名: " << student->getName() << " " << "宿舍: " << student->getDormitory() << " " << "电话: " << student->getPhoneNumber() << endl;// 查询学生成绩cout << "该生成绩情况如下:" << endl;if (scores.size() == 0) {cout << "暂无成绩" << endl;}else {for (auto smap : scores) {if (smap.first == studentId) {Score* score = new Score();score = smap.second;cout << "课程名: " << score->getCourseName() << " 分值: " << score->getScoreValue() << endl;}}}return;}}cout << "未找到该学生信息!" << endl;
}void OperationManagement::showMenu()
{cout << "------------O.o 欢迎您使用学生管理系统 o.O------------" << endl;cout << "------------O.o 可用的功能编号如下 : o.O------------" << endl;cout << "------------O.o 0,添加学生信息功能 o.O------------" << endl;cout << "------------O.o 1,查找学生信息功能 o.O------------" << endl;cout << "------------O.o 2,修改学生信息功能 o.O------------" << endl;cout << "------------O.o 3,添加学生成绩功能 o.O------------" << endl;cout << "------------O.o 4,删除学生成绩功能 o.O------------" << endl;cout << "------------O.o 5,修改学生成绩功能 o.O------------" << endl;cout << "------------O.o 6,查询学生信息 o.O------------" << endl;
}void OperationManagement::addStudent() {cout << "请输入您要添加的学生的学号:" << endl;string id;cin >> id;// 检查学生是否已存在if (students.count(id) > 0) {cout << "该学号已存在,添加失败" << endl;}else {string name;string dormitory;string phoneNumber;cout << "请输入您要添加的学生的姓名:" << endl;cin >> name;cout << "请输入您要添加的学生的所在宿舍:" << endl;cin >> dormitory;cout << "请输入您要添加的学生的电话:" << endl;cin >> phoneNumber;Student* student = new Student(id, name, dormitory, phoneNumber);students[id] = student;ofstream writeFile("students.txt", ios::app);writeFile << endl << student;cout << "学生信息添加成功!" << endl;}
}void OperationManagement::deleteStudent() {cout << "请输入您要删除的学生的学号:" << endl;string id;cin >> id;// 检查学生是否已存在if (students.count(id) == 0) {cout << "该学号不存在,删除失败" << endl;}else {auto it = students.find(id);// 如果找到了对应的学生,则从unordered_map中删除该学生if (it != students.end()) {delete it->second; // 删除指针指向的内存students.erase(it); // 从unordered_map中删除该学生ofstream writeFile("students.txt", ios::out);for (auto student : students){writeFile << endl << student.second;}cout << "删除成功" << endl;}else {cout << "删除失败,未知错误" << endl;}}
}void OperationManagement::updateStudent() {cout << "请输入您要修改的学生的学号:" << endl;string id;cin >> id;// 检查学生是否已存在if (students.count(id) == 0) {cout << "该学号不存在,修改失败" << endl;}else {auto it = students.find(id);string cmd, name;string dormitory;string phoneNumber;if (it != students.end()) {while (1){cout << "请输入您要修改的相应信息,输入对应序号即可:" << endl;cout << "1.修改学生姓名" << endl;cout << "2.修改学生宿舍" << endl;cout << "3.修改学生电话" << endl;cout << "若您确认修改,请输入:y" << endl;cin >> cmd;if (cmd == "1"){cout << "请输入您要修改为的学生姓名:" << endl;cin >> name;it->second->setName(name);}else if (cmd == "2"){cout << "请输入您要修改为的宿舍:" << endl;cin >> dormitory;it->second->setDormitory(dormitory);}else if (cmd == "3"){cout << "请输入您要修改为的电话:" << endl;cin >> phoneNumber;it->second->setPhoneNumber(phoneNumber);}else if (cmd == "y"){ofstream writeFile("students.txt", ios::out);for (auto student : students){writeFile << endl << student.second;}writeFile.close();cout << "修改成功" << endl;break;}}}}
}void OperationManagement::addScore() {cout << "请输入您要添加成绩的学生的学号:" << endl;string id;cin >> id;// 检查学生是否已存在if (students.count(id) == 0) {cout << "该学号不存在,添加失败" << endl;}else {string courseName;int scoreValue;cout << "请输入您要添加的课程名称:" << endl;cin >> courseName;bool flag = false;for (auto score : scores){if (score.second->getCourseName() == courseName){flag = true;break;}}if (flag){cout << "该学生本课程已有分数若要修改,请使用修改成绩功能" << endl;}cout << "请输入您要添加的课程分值:" << endl;cin >> scoreValue;Score* score = new Score(id, courseName,scoreValue);scores.insert(make_pair(id, score));ofstream writeFile("scores.txt", ios::app);writeFile << endl << score;cout << "学生成绩添加成功!" << endl;}
}void OperationManagement::deleteScore() {cout << "请输入您要删除成绩的学生的学号:" << endl;string id;cin >> id;// 检查学生是否已存在if (scores.count(id) == 0) {cout << "该学号学生暂无课程成绩,删除失败" << endl;}else {cout << "找到该学生课程成绩情况如下" << endl;for (auto score : scores){if (score.second->getStudentId() == id){score.second->display();}}cout << "请输入您要删除的该学生课程成绩名称" << endl;string delName;cin >> delName;bool flag = false;for (auto it = scores.begin(); it != scores.end(); ) {// 获取当前元素的键和值string studentId = it->first;Score* score = it->second;// 如果需要删除该元素,则进行删除操作if (studentId == id && score->getCourseName() == delName) {delete score; // 释放Score对象占用的内存it = scores.erase(it); // 从unordered_map中删除该元素flag = true;break;}else {++it; // 移动到下一个元素}}if (flag){ofstream writeFile("scores.txt", ios::out);for (auto score : scores){writeFile << endl << score.second;}cout << "删除成功" << endl;}else {cout << "删除失败,该学生没有此课程名称的成绩" << endl;}}
}void OperationManagement::updateScore() {cout << "请输入您要修改成绩的学生的学号:" << endl;string id;cin >> id;// 检查学生是否已存在if (scores.count(id) == 0) {cout << "该学号学生暂无课程成绩,修改失败" << endl;}else {cout << "找到该学生课程成绩情况如下" << endl;for (auto score : scores){if (score.second->getStudentId() == id){score.second->display();}}cout << "请输入您要修改的该学生课程成绩名称" << endl;string upName;cin >> upName;bool flag = false;for (auto it = scores.begin(); it != scores.end(); ) {// 获取当前元素的键和值string studentId = it->first;Score* score = it->second;// 如果需要删除该元素,则进行修改操作if (studentId == id && score->getCourseName() == upName) {cout << "请输入您要修改为的分值:" << endl;int newS;cin >> newS;score->setScoreValue(newS);flag = true;break;}else {++it; // 移动到下一个元素}}if (flag){ofstream writeFile("scores.txt", ios::out);for (auto score : scores){writeFile << endl << score.second;}cout << "修改成功" << endl;}else {cout << "修改失败,该学生没有此课程名称的成绩" << endl;}}
}