引言
学习c++后一直想写个学生管理系统,趁假期终于肝出了整个系统,让我对c++的理解更深了一层;写代码时真正体会到了内存操作的困难(想学java),但是通过几个巧妙的方法解决了内存问题,并在n次debug后终于调试出了完整代码,接下来我来介绍一下整个系统; ps:今天太累了,明天再写🤯; 代码如下:
#include <iostream>
#include<fstream>
#include<cstring>
#include<vector>
using namespace std;
#define FILENAME "stdFILE.txt"
class Student {
public:
virtual void showInfo() = 0;
string StId;
string m_name;
string m_Dept;
};
class Nomalstuden : public Student {
public:
Nomalstuden(string Id, string name, string dep);
void showInfo();
};
class ClassPresident : public Student {
public:
ClassPresident(string Id, string name, string dep);
void showInfo ();
};
class Classleader : public Student {
public:
Classleader(string Id, string name, string dep);
void showInfo ();
};
class StudentManager {
public:
StudentManager();
void Show_Menu();
void Exit_System();
void Addinfo();
void save();
void init_Std();
void show_Std();
void del_Std();
void mod_Std();
void find_Std();
void clean_File();
int IsExist(string id);
~StudentManager();
vector<Student*> *m_StdArray;
bool m_fileIsEmpty;
};
StudentManager :: StudentManager() {
ifstream ifs;
ifs.open(FILENAME, ios :: in);
if (!ifs.is_open()) {
cout << "该文件不存在!" << endl;
this -> m_fileIsEmpty = true;
this -> m_StdArray = NULL;
ifs.close();
return ;
}
char ch;
ifs >> ch;
if (ifs.eof()) {
cout << "该文件为空!" <<endl;
this -> m_fileIsEmpty = true;
this -> m_StdArray = NULL;
ifs.close();
return ;
}
this -> m_StdArray = new vector<Student*>;
this -> init_Std();
}
void StudentManager :: Show_Menu() {
cout << "-------------------------------------------" << endl;
cout << "------------ 欢迎使用学生管理系统! ----------" << endl;
cout << "------------- 0.退出管理程序 -------------" << endl;
cout << "------------- 1.增加学生信息 -------------" << endl;
cout << "------------- 2.显示学生信息 -------------" << endl;
cout << "------------- 3.删除学生信息 -------------" << endl;
cout << "------------- 4.修改学生信息 -------------" << endl;
cout << "------------- 5.查找学生信息 -------------" << endl;
cout << "------------- 6.清空所有数据 -------------" << endl;
cout << "-------------------------------------------" << endl;
cout << endl;
}
void StudentManager :: Exit_System() {
cout << "感谢您的使用!" << endl;
exit(-1);
}
void StudentManager :: Addinfo() {
if (this -> m_fileIsEmpty)
this -> m_StdArray = new vector<Student*>;
cout << "学生信息开始录入" << endl;
int i = 1;
while(true) {
char flag;
string id;
string name;
string dep;
cout << "请输入第" << i << "个学生学号:" << endl;
cin >> id;
cout << "请输入第" << i << "个学生姓名:" << endl;
cin >> name;
cout << "请输入第" << i << "个学生职位:(班长or班干部or普通学生)" << endl;
cin >> dep;
Student *std = NULL;
if (dep == "班长") {
std = new ClassPresident(id, name, dep);
}
else if (dep == "班干部") {
std = new Classleader(id, name, dep);
}
else if (dep == "普通学生") {
std = new Nomalstuden(id, name, dep);
}
else {
cout << "该学生职位不存在!信息录入结束!" <<endl;
break;
}
this -> m_StdArray -> push_back(std);
i++;
this -> m_fileIsEmpty = false;
cout << "是否继续录入信息?(y继续录入,n结束录入)" <<endl;
cin >> flag;
if (flag == 'y') continue;
else break;
}
cout << "成功添加了" << i - 1 << "名学生信息!" <<endl;
this -> save();
system("pause");
system("cls");
}
void StudentManager :: save() {
ofstream ofs;
ofs.open(FILENAME, ios :: out);
for (int i = 0; i < this -> m_StdArray -> size(); ++i) {
ofs << this -> m_StdArray -> at(i) -> StId << " "
<< this -> m_StdArray -> at(i) -> m_name << " "
<< this -> m_StdArray -> at(i) -> m_Dept << endl;
}
ofs.close();
}
void StudentManager :: init_Std() {
ifstream ifs;
ifs.open(FILENAME, ios :: in);
string id;
string name;
string dep;
while (ifs >> id && ifs >> name && ifs >> dep) {
Student * std = NULL;
if (dep == "班长") {
std = new ClassPresident(id, name, dep);
}
else if (dep == "班干部") {
std = new Classleader(id, name, dep);
}
else if (dep == "普通学生") {
std = new Nomalstuden(id, name, dep);
}
this -> m_StdArray -> push_back(std);
}
}
void StudentManager :: show_Std() {
if (this -> m_fileIsEmpty) {
cout << "文件不存在或者文件为空!" <<endl;
}
else {
for (int i = 0; i < this -> m_StdArray -> size(); ++i) {
this -> m_StdArray -> at(i) -> showInfo();
}
}
system("pause");
system("cls");
}
void StudentManager :: del_Std() {
if (this -> m_fileIsEmpty) {
cout << "文件不存在或者文件为空!" << endl;
}
else {
cout << "请输入需要删除的学生学号:" << endl;
string id;
cin >>id;
int index = this -> IsExist(id);
if (index != -1) {
this -> m_StdArray -> erase(this -> m_StdArray -> begin() + index);
this -> save();
cout << "删除成功!" <<endl;
}
else {
cout << "删除失败,不存在该学号的学生!" <<endl;
}
}
system("pause");
}
int StudentManager :: IsExist(string id) {
int len = this -> m_StdArray -> size();
int index = -1;
for (int i = 0; i < len; ++i) {
if (this -> m_StdArray -> at(i) -> StId == id) {
index = i;
break;
}
}
return index;
}
void StudentManager :: mod_Std() {
if (this -> m_fileIsEmpty) {
cout << "文件不存在或者文件为空" <<endl;
}
else {
cout << "请输入需要修改的学生学号:" << endl;
string id;
cin >> id;
int index = this -> IsExist(id);
if (index != -1) {
string newid;
string newname;
string newdep;
Student *std = NULL;
cout<< "搜索到学号为" << id << "的学生,请输入新学号:" << endl;
cin >> newid;
cout << "请输入新姓名:" <<endl;
cin >> newname;
cout << "请输入新职责:" <<endl;
cin >> newdep;
if (newdep == "班长") {
std = new ClassPresident(newid, newname, newdep);
}
else if (newdep == "班干部") {
std = new Classleader(newid, newname, newdep);
}
else if (newdep == "普通学生") {
std = new Nomalstuden(newid, newname, newdep);
}
this -> m_StdArray -> at(index) = std;
cout <<"修改成功!" << endl;
this -> save();
}
else {
cout << "修改失败,不存在该学号的学生!" << endl;
}
}
system("pause");
}
void StudentManager :: find_Std() {
if (this -> m_fileIsEmpty) {
cout << "文件不存在或者文件为空" <<endl;
}
else {
cout << "请输入需要查找的学生学号:" << endl;
string id;
cin >> id;
int index = this -> IsExist(id);
if (index != -1) {
cout<< "查找成功!该学生信息如下:" << endl;
this -> m_StdArray -> at(index) ->showInfo();
}
else {
cout << "查找失败!该学生不存在!" <<endl;
}
}
}
void StudentManager :: clean_File() {
cout << "确定清空所有数据?" << endl;
cout << "1,确定" <<endl;
cout << "2,返回" <<endl;
int selet = 0;
cin >> selet;
if (selet == 1) {
ofstream ofs(FILENAME, ios :: trunc);
ofs.close();
if (this -> m_StdArray) {
this -> m_StdArray -> clear();
vector<Student*>(*this -> m_StdArray).swap(*this -> m_StdArray);
this -> m_fileIsEmpty = true;
this -> m_StdArray = NULL;
}
cout << "清空成功!" << endl;
}
system("pause");
system("cls");
}
StudentManager :: ~StudentManager() {
if (this -> m_StdArray) {
this -> m_StdArray -> clear();
delete[] this -> m_StdArray;
this -> m_StdArray = NULL;
}
}
Nomalstuden :: Nomalstuden(string Id, string name, string dep) {
this -> StId = Id;
this -> m_name = name;
this -> m_Dept = dep;
}
void Nomalstuden :: showInfo() {
cout << "学生学号:" << this -> StId
<< "\t学生姓名" << this -> m_name
<< "\t学生职位" << this -> m_Dept
<< "\t学生任务:遵守班级纪律" << endl;
}
ClassPresident :: ClassPresident(string Id, string name, string dep) {
this -> StId = Id;
this -> m_name = name;
this -> m_Dept = dep;
}
void ClassPresident :: showInfo () {
cout << "学生学号:" << this -> StId
<< "\t学生姓名" << this -> m_name
<< "\t学生职位" << this -> m_Dept
<< "\t学生任务:管理班干部,与辅导员对接,带领班级" << endl;
}
Classleader :: Classleader(string Id, string name, string dep) {
this -> StId = Id;
this -> m_name = name;
this -> m_Dept = dep;
}
void Classleader :: showInfo () {
cout << "学生学号:" << this -> StId
<< "\t学生姓名" << this -> m_name
<< "\t学生职位" << this -> m_Dept
<< "\t学生任务:履行自己的职责,和各科老师对接,管理班级学生" << endl;
}
int main() {
StudentManager stm;
int choice;
while(true) {
stm.Show_Menu();
cout << "请输入您的选择:" << endl;
cin >> choice;
switch (choice)
{
case 0:
stm.Exit_System();
break;
case 1:
stm.Addinfo();
break;
case 2:
stm.show_Std();
break;
case 3:
stm.del_Std();
break;
case 4:
stm.mod_Std();
break;
case 5:
stm.find_Std();
break;
case 6:
stm.clean_File();
break;
default:
system("cls");
break;
}
}
system("pause");
return 0;
}
|