往期源码回顾: 【C++实现图书管理系统(Qt C++ GUI界面版) 【Python】实现爬虫,爬取天气情况并进行分析(完整版) 【C++】图书管理系统(完整板)
系统概述 这是一个基于 C++ 的学生信息管理系统,采用面向对象设计,支持小学生、中学生和大学生三类学生信息的增删改查与统计。系统通过文件 IO 实现数据持久化,使用哈希表(unordered_map)存储学生数据,确保高效检索。 学生信息管理系统功能概览,具体操作大家可以自己运行试试:

代码概览:
SIMS.h 将学生等信息等从文件中读取出来以及其他操作,放入容器中,便于操作,不用一直对文件进行I/O.
Student.h 对学生进行的抽象,同样实现了>>的重载,便于文件读入,读出
Student(抽象基类)
├─ PrimaryStudent(小学生)
├─ MiddleStudent(中学生)
└─ Undergraduate(大学生)全部代码与讲解: 1.SIMS.cpp
#include <iostream>
#include "MiddleStudent.h"
#include "Undergraduate.h"
#include "Student.h"
#include <unordered_map>
#include<fstream>
#include<cstring>
#include<string>
#include <sstream>
using namespace std;
//unordered_map<string, Student>students;
unordered_map<string, Student*>students;
int main();
void showMenu();
void addSelect();
void init();
void search();
void showAll();
void updata();
void del();
void statistics();
int main()
{
init();
while (true)
{
showMenu();
cout << "请输入您要使用的功能序号:" << endl;
int choice;
cin >> choice;
switch (choice)
{
case 0: //添加功能
addSelect();
break;
case 1: //搜索功能
search();
break;
case 2: //显示功能
showAll();
break;
case 3: //修改功能
updata();
break;
case 4: //删除功能
del();
break;
case 5: //统计功能
statistics();
break;
default:
system("cls"); //cls的作用是清空屏幕的功能,使用户可以再次输入选项数字
break;
}
}
system("pause"); //使程序有一个暂停阻碍的功能,防止界面突然退出
return 0;
}
void 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;
}
void addSelect()
{
cout << "请问用户需要添加什么类型的学生信息:" << endl;
cout << "0. 小学生" << endl;
cout << "1. 中学生" << endl;
cout << "2. 大学生" << endl;
int type;
cin >> type;
if (type > 2 || type < 0)
{
cout << "无效的选项!" << endl;
}
else
{
string id, name, gender, className;
cout << "请输入您要增加的学生的学号:" << endl;
cin >> id;
if (students.count(id) > 0)
{
cout << "数据添加重复并取消本次添加" << endl;
return;
}
int age;
cout << "请输入学生姓名:" << endl;
cin >> name;
cout << "请输入学生性别:" << endl;
cin >> gender;
cout << "请输入学生年龄:" << endl;
cin >> age;
cout << "请输入学生班级:" << endl;
cin >> className;
ofstream writeFile("students.txt", ios::app);
if (type == 0)
{
int englishScore, mathScore, chineseScore;
cout << "请输入学生英语成绩:" << endl;
cin >> englishScore;
cout << "请输入学生数学成绩:" << endl;
cin >> mathScore;
cout << "请输入学生语文成绩:" << endl;
cin >> chineseScore;
PrimaryStudent* student = new PrimaryStudent(id, name, gender,age, className, englishScore, mathScore, chineseScore);
students[id] = student;
writeFile <<endl<< student;
writeFile.close();
cout << "恭喜成功添加学生学号为" << id << "的小学生信息" << endl;
}
else if (type == 1)
{
int englishScore, mathScore, chineseScore;
int geographyScore; // 地理成绩
int historyScore; // 历史成绩
string address; // 家庭住址
cout << "请输入学生英语成绩:" << endl;
cin >> englishScore;
cout << "请输入学生数学成绩:" << endl;
cin >> mathScore;
cout << "请输入学生语文成绩:" << endl;
cin >> chineseScore;
cout << "请输入学生地理成绩:" << endl;
cin >> geographyScore;
cout << "请输入学生历史成绩:" << endl;
cin >> historyScore;
cout << "请输入学生家庭住址:" << endl;
cin >> address;
MiddleStudent* student = new MiddleStudent(id, name, gender, age, className, englishScore, mathScore, chineseScore,
geographyScore, historyScore, address);
students[id] = student;
writeFile << endl << student;
writeFile.close();
cout << "恭喜成功添加学生学号为" << id << "的中学生信息" << endl;
}
else
{
string major; // 专业
string address; // 家庭住址
string phoneNumber;// 联系方式
cout << "请输入学生专业:"<<endl;
cin >>major;
cout << "请输入学生住址:" << endl;
cin >> address;
cout << "请输入学生联系方式:" << endl;
cin >> phoneNumber;
Undergraduate* student = new Undergraduate(id, name, gender, age, className, major, address, phoneNumber);
students[id] = student;
writeFile << endl << student;
writeFile.close();
cout << "恭喜成功添加学生学号为" << id << "的大学生信息" << endl;
}
}
}
void init()
{
ifstream readFile;
readFile.open("students.txt");
if (!readFile.is_open())
{
cout << "学生数据读取错误" << endl;
readFile.close();
return;
}
string line;
while (getline(readFile, line)) {
if (line.empty()) continue; // 跳过空行
istringstream iss(line);
if (line.find("Undergraduate-2") != string::npos) {
Undergraduate* u = new Undergraduate();
iss >> u;
string id = u->getId();
students[id] = u;
}
else if (line.find("PrimaryStudent-0") != string::npos) {
PrimaryStudent* p = new PrimaryStudent();
iss >> p;
string id = p->getId();
students[id] = p;
}
else if (line.find("MiddleStudent-1") != string::npos) {
MiddleStudent* m = new MiddleStudent();
iss >> m;
string id = m->getId();
students[id] = m;
}
}
readFile.close();
}
void search()
{
cout << "请问用户需要根据什么信息查询:" << endl;
cout << "1. 根据学号查询" << endl;
cout << "2. 根据姓名查询" << endl;
int searchOption;
cin >> searchOption;
if (searchOption == 1) {
string id;
cout << "请输入您要查询的学号:";
cin >> id;
if(students.count(id) == 0)
{
cout << "未找到相关记录!" << endl;
}
else
{
students[id]->display();
cout << endl;
}
}
else if (searchOption == 2) {
string name;
cout << "请输入您要查询的姓名:";
cin >> name;
bool found = false;
for (auto map : students)
{
Student* student = map.second;
if (student->getName() == name)
{
student->display();
cout << endl;
found = true;
}
}
if (!found) {
cout << "未找到相关记录!" << endl;
}
}
else {
cout << "无效的选项!" << endl;
}
}
void showAll()
{
if (students.size() == 0)
{
cout << "暂无学生信息记录" << endl;
}
else
{
for (auto map : students)
{
Student* student = map.second;
student->display();
cout << endl;
}
}
}
void updata()
{
cout << "请输入您要修改的学生的学号:" << endl;
string id;
cin >> id;
// 检查学生是否已存在
if (students.count(id) == 0) {
cout << "该学号不存在,修改失败" << endl;
}
else {
auto it = students.find(id);
if (it != students.end()) {
Student* student = it->second;
while (1)
{
string cmd;
string name; // 姓名
string gender; // 性别
int age; // 年龄
string className; // 班级
cout << "请输入您要修改的相应信息,输入对应序号即可:" << endl;
cout << "1.修改学生姓名" << endl;
cout << "2.修改学生性别" << endl;
cout << "3.修改学生年龄" << endl;
cout << "4.修改学生班级" << endl;
cout << "若您确认修改,请输入:y" << endl;
cin >> cmd;
if (cmd == "1")
{
cout << "请输入您要修改为的学生姓名:" << endl;
cin >> name;
student->setName(name);
}
else if (cmd == "2")
{
cout << "请输入您要修改为的性别:" << endl;
cin >> gender;
student->setGender(gender);
}
else if (cmd == "3")
{
cout << "请输入您要修改为的年龄:" << endl;
cin >> age;
student->setAge(age);
}
else if (cmd == "4")
{
cout << "请输入您要修改为的班级:" << endl;
cin >> className;
student->setClassName(className);
}
else if (cmd == "y")
{
ofstream writeFile("students.txt", ios::out);
for (auto student : students)
{
if (student.second->getType() == "PrimaryStudent-0")
writeFile << endl << (PrimaryStudent*)student.second;
else if (student.second->getType() == "MiddleStudent-1")
writeFile << endl << (MiddleStudent*)student.second;
else
writeFile << endl << (Undergraduate*)student.second;
}
writeFile.close();
cout << "修改成功" << endl;
break;
}
}
}
}
}
void del()
{
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)
{
if (student.second->getType() == "PrimaryStudent-0")
writeFile << endl << (PrimaryStudent*)student.second;
else if (student.second->getType() == "MiddleStudent-1")
writeFile << endl << (MiddleStudent*)student.second;
else
writeFile << endl << (Undergraduate*)student.second;
}
cout << "删除成功" << endl;
}
else {
cout << "删除失败,未知错误" << endl;
}
}
}
void statistics()
{
cout << "当前系统总人数: " << students.size() << endl;
cout << "请问用户需根据什么来统计学生信息:" << endl;
cout << "0. 性别" << endl;
cout << "1. 学生类型" << endl;
int type;
cin >> type;
if (type < 0 || type > 1)
{
cout << "无效的选项!" << endl;
return;
}
if (type == 0)
{
int man = 0, fman = 0;
for (auto map : students)
{
Student* student = map.second;
if (student->getGender() == "男")
{
man++;
}
else if (student->getGender() == "女")
{
fman++;
}
}
cout << "系统当前男生总数为: " << man << endl;
cout << "系统当前女生总数为: " << fman << endl;
}
else if (type == 1)
{
int m = 0, p = 0, u = 0;
for (auto map : students)
{
Student* student = map.second;
if (student->getType() == "PrimaryStudent - 0")
{
p++;
}
else if (student->getType() == "MiddleStudent-1")
{
m++;
}
else {
u++;
}
}
cout << "系统当前小学生总数为: " << p << endl;
cout << "系统当前中学生总数为: " << m << endl;
cout << "系统当前大学生总数为: " << u << endl;
}
}MiddleStudent.cpp
#include "MiddleStudent.h"
MiddleStudent::MiddleStudent() { s_type = "MiddleStudent-1"; }
MiddleStudent::MiddleStudent(string id, string name, string gender, int age, string className,
int englishScore, int mathScore, int chineseScore, int geographyScore, int historyScore,
string address) : PrimaryStudent(id, name, gender, age, className, englishScore, mathScore, chineseScore) {
s_type = "MiddleStudent-1";
this->geographyScore = geographyScore;
this->historyScore = historyScore;
this->address = address;
}
void MiddleStudent::setGeographyScore(int score) {
geographyScore = score;
}
int MiddleStudent::getGeographyScore() {
return geographyScore;
}
void MiddleStudent::setHistoryScore(int score) {
historyScore = score;
}
int MiddleStudent::getHistoryScore() {
return historyScore;
}
void MiddleStudent::setAddress( string newAddress) {
address = newAddress;
}
string MiddleStudent::getAddress() {
return address;
}
void MiddleStudent::display() {
PrimaryStudent::display();
cout << "地理成绩: " << geographyScore << " 历史成绩: " << historyScore
<< " 家庭住址: " << address;
}
istream& operator>>(istream& input, MiddleStudent* m) {
input >> static_cast<PrimaryStudent*>(m) >> m->geographyScore >> m->historyScore >> m->address;
return input;
}
ostream& operator<<(ostream& output, const MiddleStudent* m) {
output << static_cast<const PrimaryStudent*>(m)<<" " << m->geographyScore << " " << m->historyScore << " " << m->address;
return output;
}PrimaryStudent.cpp
#include "PrimaryStudent.h"
#include <iostream>
PrimaryStudent::PrimaryStudent()
{
s_type = "PrimaryStudent-0";
}
PrimaryStudent::PrimaryStudent(string id, string name, string gender, int age, string className,
int english, int math, int chinese): Student(id, name, gender, age, className) {
this->englishScore = english;
this->mathScore = math;
this->chineseScore = chinese;
s_type = "PrimaryStudent-0";
}
void PrimaryStudent::display() {
Student::display();
cout << "英语成绩: " << englishScore << " 数学成绩: " << mathScore
<< " 语文成绩: " << chineseScore;
}
istream& operator>>(istream& input, PrimaryStudent* p) {
input >> static_cast<Student*>(p) >> p->englishScore >> p->mathScore >> p->chineseScore;
return input;
}
ostream& operator<<(ostream& output, const PrimaryStudent* p) {
output << static_cast<const Student*>(p) << " " << p->englishScore << " " << p->mathScore << " " << p->chineseScore;
return output;
}student.cpp
#include "student.h"
Student::Student()
{
}
Student::Student(string id, string name, string gender, int age, string className)
{
s_id = id;
s_name = name;
s_gender = gender;
s_age = age;
s_className = className;
}
Student::~Student() {}
string Student::getId() {
return s_id;
}
string Student::getName() {
return s_name;
}
string Student::getGender() {
return s_gender;
}
int Student::getAge() {
return s_age;
}
string Student::getType() {
return s_type;
}
string Student::getClassName() {
return s_className;
}
void Student::setId(string newId) {
s_id = newId;
}
void Student::setName(string newName) {
s_name = newName;
}
void Student::setGender(string newGender) {
s_gender = newGender;
}
void Student::setAge(int newAge) {
s_age = newAge;
}
void Student::setClassName(string newClassName) {
s_className = newClassName;
}
void Student::display() {
cout << "学号: " << s_id << " 姓名: " << s_name << " 性别: " << s_gender
<< " 年龄: " << s_age << " 班级: " << s_className;
}
istream& operator>>(istream& input, Student* s) {
input >> s->s_id>> s->s_name>> s->s_gender >> s->s_age>> s->s_className>>s->s_type;
return input;
}
ostream& operator<<(ostream& output, const Student* s) {
output << s->s_id << " " << s->s_name << " " << s->s_gender << " " << s->s_age << " " << s->s_className <<" "<< s->s_type;
return output;
}
//istream& operator>>(istream& input, Student& s) {
// input >> s.s_id >> s.s_name >> s.s_gender >> s.s_age >> s.s_className >> s.s_type;
// return input;
//}
//
//ostream& operator<<(ostream& output, const Student& s) {
// output << s.s_id << " " << s.s_name << " " << s.s_gender << " " << s.s_age << " " << s.s_className << s.s_type;
// return output;
//}Undergraduate.cpp
#include "Undergraduate.h"
Undergraduate::Undergraduate() { s_type = "Undergraduate-2"; }
Undergraduate::Undergraduate(string id, string name, string gender, int age, string className,
string major, string address, string phoneNumber) : Student(id, name, gender, age, className) {
this->major = major;
this->address = address;
this->phoneNumber = phoneNumber;
this->s_type = "Undergraduate-2";
}
void Undergraduate::setMajor(string newMajor) { major = newMajor; }
string Undergraduate::getMajor() { return major; }
void Undergraduate::setAddress(string newAddress) { address = newAddress; }
string Undergraduate::getAddress() { return address; }
void Undergraduate::setPhoneNumber( string newPhoneNumber) { phoneNumber = newPhoneNumber; }
string Undergraduate::getPhoneNumber() { return phoneNumber; }
void Undergraduate::display() {
Student::display();
cout << "专业: " << major << " 家庭住址: " << address
<< " 联系方式: " << phoneNumber;
}
istream& operator>>(istream& input, Undergraduate* u) {
input >> static_cast<Student*>(u)>> u->major>> u->address >> u->phoneNumber;
return input;
}
ostream& operator<<(ostream& output, const Undergraduate* u) {
output << static_cast<const Student*>(u) << " " << u->major << " " << u->address << " " << u->phoneNumber;
return output;
}
//istream& operator>>(istream& input, Undergraduate& u) {
// input >> static_cast<Student&>(u) >> u.major >> u.address >> u.phoneNumber;
// return input;
//}
//
//ostream& operator<<(ostream& output, const Undergraduate& u) {
// output << static_cast<const Student&>(u) << " " << u.major << " " << u.address << " " << u.phoneNumber;
// return output;
//}