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

news2024/10/5 20:10:10

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

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/1829048.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

课时154:项目发布_手工发布_手工发布

1.2.3 手工发布 学习目标 这一节&#xff0c;我们从 基础知识、简单实践、小结 三个方面来学习 基础知识 简介 为了合理的演示生产环境的项目代码发布&#xff0c;同时又兼顾实际实验环境的资源&#xff0c;我们这里将 B主机和C主机 用一台VM主机来实现&#xff0c;A主机单…

牛客网刷题 | BC118 N个数之和

目前主要分为三个专栏&#xff0c;后续还会添加&#xff1a; 专栏如下&#xff1a; C语言刷题解析 C语言系列文章 我的成长经历 感谢阅读&#xff01; 初来乍到&#xff0c;如有错误请指出&#xff0c;感谢&#xff01; 描述 输入数字N&#xf…

【LeetCode 动态规划】买卖股票的最佳时机问题合集

文章目录 1. 买卖股票的最佳时机含冷冻期 1. 买卖股票的最佳时机含冷冻期 题目链接&#x1f517; &#x1f34e;题目思路&#xff1a; &#x1f34e;题目代码&#xff1a; class Solution { public:int maxProfit(vector<int>& prices) {int n prices.size();ve…

生产者消费者模型的同步与互斥:C++代码实现

文章目录 一、引言二、生产者消费者模型概述1、基本概念和核心思想2、生产者消费者模型的优点 三、消费者和生产者之间的同步与互斥四、代码实现1、事前准备2、环形队列的实现3、阻塞队列的实现4、两种实现方式的区别 一、引言 在现代计算机系统中&#xff0c;很多任务需要同时…

Spring运维之boo项目表现层测试加载测试的专用配置属性以及在JUnit中启动web服务器发送虚拟请求

测试表现层的代码如何测试 加载测试的专用属性 首先写一个测试 假定我们进行测试的时候要加一些属性 要去修改一些属性 我们可以写一个只在本测试有效的测试 写在配置里 测试 打印输出 我们把配置文件里面的配置注释掉后 我们同样可以启动 package com.example.demo;impo…

专业是软件工程,现在好迷茫,感觉什么都没有学到,该怎么办?

学习软件工程可能会遇到迷茫和困惑的时期&#xff0c;这很正常&#xff0c;尤其是在学习初期。这里有一些建议&#xff0c;或许可以帮助你找到方向&#xff1a; 明确目标&#xff1a;思考你学习软件工程的目的是什么&#xff0c;是为了将来从事软件开发工作&#xff0c;还是对编…

LabVIEW与C#的区别及重新开发自动测试程序的可行性分析

LabVIEW和C#是两种广泛使用的编程语言&#xff0c;各自有不同的应用领域和特点。本文将详细比较LabVIEW与C#在自动测试程序开发中的区别&#xff0c;并分析将已完成的LabVIEW自动测试程序重新用C#开发的合理性。本文帮助评估这种转换的必要性和潜在影响。 LabVIEW与C#的区别 开…

Windows环境利用 OpenCV 中 CascadeClassifier 分类器识别人眼 c++

Windows环境中配置OpenCV 关于在Windows环境中配置opencv的说明&#xff0c;具体可以参考&#xff1a;VS2022 配置OpenCV开发环境详细教程。 CascadeClassifier 分类器 CascadeClassifier 是 OpenCV 库中的一个类&#xff0c;它用于实现一种快速的物体检测算法&#xff0c;称…

LSTM模型预测时间序列

长短期记忆模型(Long Short-Term Memory, LSTM)&#xff0c;是一种特殊的循环神经网络&#xff0c;能够学习长期依赖性。长短期记忆模型在各种各样的问题上表现非常出色&#xff0c;现在被广泛使用&#xff0c;例如&#xff0c;文本生成、机器翻译、语音识别、时序数据预测、生…

Matlab电话按键拨号器设计

前言 这篇文章是目前最详细的 Matlab 电话按键拨号器设计开源教程。如果您在做课程设计或实验时需要参考本文章&#xff0c;请注意避免与他人重复&#xff0c;小心撞车。博主做这个也是因为实验所需&#xff0c;我在这方面只是初学者&#xff0c;但实际上&#xff0c;从完全不…

Python | 中心极限定理介绍及实现

统计学是数据科学项目的重要组成部分。每当我们想从数据集的样本中对数据集的总体进行任何推断&#xff0c;从数据集中收集信息&#xff0c;或者对数据集的参数进行任何假设时&#xff0c;我们都会使用统计工具。 中心极限定理 定义&#xff1a;中心极限定理&#xff0c;通俗…

【Liunx】基础开发工具的使用介绍-- yum / vim / gcc / gdb / make

前言 本章将介绍Linux环境基础开发工具的安装及使用&#xff0c;在Linux下安装软件&#xff0c;编写代码&#xff0c;调试代码等操作。 目录 1. yum 工具的使用1.1 什么是软件包&#xff1a;1.2 如何下载软件&#xff1a;1.3 配置国内yum源&#xff1a; 2. vim编辑器2.1 vim的安…

NetSuite Saved Search 之 Filter By Summary

在某些业务场景中&#xff0c;用户需要一个TOP X的报表。例如&#xff0c;过去一段时间内&#xff0c;最多数量的事务处理类型。这就需要利用Saved Search中的Filter By Summary功能。 这在Criteria下的Summary页签里可以定义。其作用是对Result中Summary类型的结果进行过滤。也…

【论文速读,找找启发点】2024/6/16

ICME 2023 End-To-End Part-Level Action Parsing With Transformer 类似 DETR&#xff0c;通过 加 query的方式实现 端到端 ELAN: Enhancing Temporal Action Detection with Location Awareness 如何实现位置感知&#xff1f; > 重叠的卷积核&#xff1f; Do we really …

解决MacOS docker 拉取镜像慢的问题

docker官网&#xff1a;https://docker.p2hp.com/get-started/index.html 下载完成之后&#xff0c;拉取镜像速度慢&#xff0c;问题如下&#xff1a; 解决方法 配置阿里源&#xff1a;https://cr.console.aliyun.com/cn-hangzhou/instances/mirrors在docker desktop里面设置…

代码随想录二刷DAY1~3

Day1 704 二分查找&#xff0c;简单 我也有自己写题解的能力了&#xff0c;而且思维很清晰&#xff1a; 找什么就在if里写什么。 class Solution {public: int search(vector<int>& nums, int target) { int l0,rnums.size()-1; while(l<r){ …

基于C++、MFC和Windows套接字实现的简单聊天室程序开发

一、一个简单的聊天室程序 该程序由服务器端和客户端两个项目组成&#xff0c;这两个项目均基于对话框的程序。服务器端项目负责管理客户端的上线、离线状态&#xff0c;以及转发客户端发送的信息。客户端项目则负责向服务器发送信息&#xff0c;并接收来自服务器的信息&#…

不一样的SYSTEM APP(SYSTEM flag和system_prop区别)

1.问题引入 在Android开发中, 1)Framework中PackageManager扫包后,会把app归类为SYSTEM, SYSTEM_EXT, PRIVILEGED 类别. 2)同样的, SeAndroid也会把APP归类程platform_app, system_app, untrusted_app(甚至还有其他,mediaprovider,gmscore_app). flag SYSTEM和system_app我们…

IDEA配置JavaFX

一、下载SDK &#x1f4ce;javafx-sdk-18.zip 二、配置依赖包 三、复制一个javafx代码 import javafx.application.Application; import javafx.scene.Scene; import javafx.scene.layout.VBox; import javafx.scene.shape.Line; import javafx.stage.Stage;public class Java…

基于Java和SSM框架的多人命题系统

你好呀&#xff0c;我是计算机学长猫哥&#xff01;如果你对多人命题系统感兴趣或者有相关开发需求&#xff0c;文末可以找到我的联系方式。 开发语言&#xff1a;Java 数据库&#xff1a;MySQL 技术&#xff1a;Java SSM框架 工具&#xff1a;Eclipse、MySQL Workbench、…