【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

相关文章

SpringBoot集成redisson实现延时队列教程

《SpringBoot集成redisson实现延时队列教程》文章介绍了使用Redisson实现延迟队列的完整步骤,包括依赖导入、Redis配置、工具类封装、业务枚举定义、执行器实现、Bean创建、消费... 目录1、先给项目导入Redisson依赖2、配置redis3、创建 RedissonConfig 配

Python的Darts库实现时间序列预测

《Python的Darts库实现时间序列预测》Darts一个集统计、机器学习与深度学习模型于一体的Python时间序列预测库,本文主要介绍了Python的Darts库实现时间序列预测,感兴趣的可以了解... 目录目录一、什么是 Darts?二、安装与基本配置安装 Darts导入基础模块三、时间序列数据结构与

C++右移运算符的一个小坑及解决

《C++右移运算符的一个小坑及解决》文章指出右移运算符处理负数时左侧补1导致死循环,与除法行为不同,强调需注意补码机制以正确统计二进制1的个数... 目录我遇到了这么一个www.chinasem.cn函数由此可以看到也很好理解总结我遇到了这么一个函数template<typename T>unsigned

Python使用FastAPI实现大文件分片上传与断点续传功能

《Python使用FastAPI实现大文件分片上传与断点续传功能》大文件直传常遇到超时、网络抖动失败、失败后只能重传的问题,分片上传+断点续传可以把大文件拆成若干小块逐个上传,并在中断后从已完成分片继... 目录一、接口设计二、服务端实现(FastAPI)2.1 运行环境2.2 目录结构建议2.3 serv

C#实现千万数据秒级导入的代码

《C#实现千万数据秒级导入的代码》在实际开发中excel导入很常见,现代社会中很容易遇到大数据处理业务,所以本文我就给大家分享一下千万数据秒级导入怎么实现,文中有详细的代码示例供大家参考,需要的朋友可... 目录前言一、数据存储二、处理逻辑优化前代码处理逻辑优化后的代码总结前言在实际开发中excel导入很

SpringBoot+RustFS 实现文件切片极速上传的实例代码

《SpringBoot+RustFS实现文件切片极速上传的实例代码》本文介绍利用SpringBoot和RustFS构建高性能文件切片上传系统,实现大文件秒传、断点续传和分片上传等功能,具有一定的参考... 目录一、为什么选择 RustFS + SpringBoot?二、环境准备与部署2.1 安装 RustF

Nginx部署HTTP/3的实现步骤

《Nginx部署HTTP/3的实现步骤》本文介绍了在Nginx中部署HTTP/3的详细步骤,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学... 目录前提条件第一步:安装必要的依赖库第二步:获取并构建 BoringSSL第三步:获取 Nginx

MyBatis Plus实现时间字段自动填充的完整方案

《MyBatisPlus实现时间字段自动填充的完整方案》在日常开发中,我们经常需要记录数据的创建时间和更新时间,传统的做法是在每次插入或更新操作时手动设置这些时间字段,这种方式不仅繁琐,还容易遗漏,... 目录前言解决目标技术栈实现步骤1. 实体类注解配置2. 创建元数据处理器3. 服务层代码优化填充机制详

Python实现Excel批量样式修改器(附完整代码)

《Python实现Excel批量样式修改器(附完整代码)》这篇文章主要为大家详细介绍了如何使用Python实现一个Excel批量样式修改器,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一... 目录前言功能特性核心功能界面特性系统要求安装说明使用指南基本操作流程高级功能技术实现核心技术栈关键函

Java实现字节字符转bcd编码

《Java实现字节字符转bcd编码》BCD是一种将十进制数字编码为二进制的表示方式,常用于数字显示和存储,本文将介绍如何在Java中实现字节字符转BCD码的过程,需要的小伙伴可以了解下... 目录前言BCD码是什么Java实现字节转bcd编码方法补充总结前言BCD码(Binary-Coded Decima