【C++】实现学生管理系统(完整版)

2024-06-16 22:12

本文主要是介绍【C++】实现学生管理系统(完整版),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

💕💕💕大家好,这是作业侠系列之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;}}
}

这篇关于【C++】实现学生管理系统(完整版)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1067691

相关文章

MySQL中查找重复值的实现

《MySQL中查找重复值的实现》查找重复值是一项常见需求,比如在数据清理、数据分析、数据质量检查等场景下,我们常常需要找出表中某列或多列的重复值,具有一定的参考价值,感兴趣的可以了解一下... 目录技术背景实现步骤方法一:使用GROUP BY和HAVING子句方法二:仅返回重复值方法三:返回完整记录方法四:

IDEA中新建/切换Git分支的实现步骤

《IDEA中新建/切换Git分支的实现步骤》本文主要介绍了IDEA中新建/切换Git分支的实现步骤,通过菜单创建新分支并选择是否切换,创建后在Git详情或右键Checkout中切换分支,感兴趣的可以了... 前提:项目已被Git托管1、点击上方栏Git->NewBrancjsh...2、输入新的分支的

Python实现对阿里云OSS对象存储的操作详解

《Python实现对阿里云OSS对象存储的操作详解》这篇文章主要为大家详细介绍了Python实现对阿里云OSS对象存储的操作相关知识,包括连接,上传,下载,列举等功能,感兴趣的小伙伴可以了解下... 目录一、直接使用代码二、详细使用1. 环境准备2. 初始化配置3. bucket配置创建4. 文件上传到os

关于集合与数组转换实现方法

《关于集合与数组转换实现方法》:本文主要介绍关于集合与数组转换实现方法,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录1、Arrays.asList()1.1、方法作用1.2、内部实现1.3、修改元素的影响1.4、注意事项2、list.toArray()2.1、方

使用Python实现可恢复式多线程下载器

《使用Python实现可恢复式多线程下载器》在数字时代,大文件下载已成为日常操作,本文将手把手教你用Python打造专业级下载器,实现断点续传,多线程加速,速度限制等功能,感兴趣的小伙伴可以了解下... 目录一、智能续传:从崩溃边缘抢救进度二、多线程加速:榨干网络带宽三、速度控制:做网络的好邻居四、终端交互

从入门到精通C++11 <chrono> 库特性

《从入门到精通C++11<chrono>库特性》chrono库是C++11中一个非常强大和实用的库,它为时间处理提供了丰富的功能和类型安全的接口,通过本文的介绍,我们了解了chrono库的基本概念... 目录一、引言1.1 为什么需要<chrono>库1.2<chrono>库的基本概念二、时间段(Durat

java实现docker镜像上传到harbor仓库的方式

《java实现docker镜像上传到harbor仓库的方式》:本文主要介绍java实现docker镜像上传到harbor仓库的方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地... 目录1. 前 言2. 编写工具类2.1 引入依赖包2.2 使用当前服务器的docker环境推送镜像2.2

C++20管道运算符的实现示例

《C++20管道运算符的实现示例》本文简要介绍C++20管道运算符的使用与实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧... 目录标准库的管道运算符使用自己实现类似的管道运算符我们不打算介绍太多,因为它实际属于c++20最为重要的

Java easyExcel实现导入多sheet的Excel

《JavaeasyExcel实现导入多sheet的Excel》这篇文章主要为大家详细介绍了如何使用JavaeasyExcel实现导入多sheet的Excel,文中的示例代码讲解详细,感兴趣的小伙伴可... 目录1.官网2.Excel样式3.代码1.官网easyExcel官网2.Excel样式3.代码

Visual Studio 2022 编译C++20代码的图文步骤

《VisualStudio2022编译C++20代码的图文步骤》在VisualStudio中启用C++20import功能,需设置语言标准为ISOC++20,开启扫描源查找模块依赖及实验性标... 默认创建Visual Studio桌面控制台项目代码包含C++20的import方法。右键项目的属性: