正好迎来暑假,在家中没啥事情可做,想着把C++的内容捞一捞,所以就自己先来没事搞一个学生管理系统,因为时间有限,所以并没有去实现界面,就是单纯的控制台应用,主要是为了锻炼逻辑和在重新熟悉以下C++的语法之类的,同时进行一些简单的类的设计,有多时间的看官可以看看图一乐。以下时运行时的一些效果。
因为代码量少(几百行而已)所以就没有进行头文件的拆分,主要就是练练手,下面附上代码
#include#include #include using namespace std; # define FILE_NAME "StuMsg.txt" // 学生管理系统 // 需求分析: // 字段: 姓名, 年龄, 班级, 学号 //学生类 class Student { private: string* m_name; int m_age; string* m_classID; string* m_StuID; string* m_gender; public: //提供构造函数 Student(string name, string classID, string stuID, int age, string gender) { string* stuName = new string(name); string* stuClassID = new string(classID); string* stuId = new string(stuID); string* stuGender = new string(gender); this->m_name = stuName; this->m_classID = stuClassID; this->m_StuID = stuId; this->m_gender = stuGender; this->m_age = age; } // 析构函数 ~Student() { if (this->m_name != nullptr) { delete this->m_name; } if (this->m_classID != nullptr) { delete this->m_classID; } if (this->m_gender != nullptr) { delete this->m_gender; } if (this->m_StuID != nullptr) { delete this->m_StuID; } } public: void setName(string name) { string* stuName = new string(name); this->m_name = stuName; } void setClassID(string classID) { string* stuClassID = new string(classID); this->m_classID = stuClassID; } void setAge(int age) { this->m_age = age; } void setStuID(string stuID) { string* StuId = new string(stuID); this->m_StuID = StuId; } void setStuGender(string gender) { string* stuGender = new string(gender); this->m_gender = stuGender; } public: string getName() { return *(this->m_name); } int getAge() { return this->m_age; } string getStuID() { return *(this->m_StuID); } string getClassID() { return *(this->m_classID); } string getGender() { return *(this->m_gender); } }; void printMsg(ofstream& ofs, Student* stu) { ofs << "姓名:" << stu->getName() << " " << "年龄:" << stu->getAge() << " " << "性别:" << stu->getGender() << " " << "班级:" << stu->getClassID() << " " << "学号:" << stu->getStuID() << " " << endl; } void printMsg(Student* stu) { cout << "姓名:" << stu->getName() << " " << "年龄:" << stu->getAge() << " " << "性别:" << stu->getGender() << " " << "班级:" << stu->getClassID() << " " << "学号:" << stu->getStuID() << " " << endl; } //功能管理类 //提供静态方法,给外界调用 class FuncManager { private: int static record_num; // 记录条数 static vector stu_vec; // 用来存放学生 public: // 初始化容器 void static init() { ifstream ifs; ifs.open(FILE_NAME, ios::in); string name, gender, classID, stuID, age; if (ifs.is_open()) { while (ifs >> name && ifs >> age && ifs >> gender && ifs >> classID && ifs >> stuID) { int pos1 = name.find(":"); int pos2 = age.find(":"); int pos3 = gender.find(":"); int pos4 = classID.find(":"); int pos5 = stuID.find(":"); string stuName, stuAge, stuGender, stuClassID, stuId; stuName = name.substr(pos1 + 1, name.length()); stuAge = age.substr(pos2 + 1, age.length()); stuGender = gender.substr(pos3 + 1, gender.length()); stuClassID = classID.substr(pos4 + 1, classID.length()); stuId = stuID.substr(pos5 + 1, stuID.length()); Student* stu = new Student(stuName, stuClassID, stuId, atoi(stuAge.c_str()), stuGender); stu_vec.push_back(stu); } record_num = stu_vec.size(); } else { cout << "文件打开失败!"; } } // 菜单 void static menu() { cout << "tttt" << "------------------欢迎使用学生管理系统------------------" << endl; cout << "tttt" << "| ******************** |" << endl; cout << "tttt" << "| * 1.添加学生 * |" << endl; cout << "tttt" << "| * 2.查找学生 * |" << endl; cout << "tttt" << "| * 3.删除学生 * |" << endl; cout << "tttt" << "| * 4.删除全部 * |" << endl; cout << "tttt" << "| * 5.修改信息 * |" << endl; cout << "tttt" << "| * 6.查看学生 * |" << endl; cout << "tttt" << "| * 7.退出系统 * |" << endl; cout << "tttt" << "| ******************** |" << endl; cout << "tttt" << "--------------------------------------------------------" << endl; cout << "tttt" << "请输入您的选择:"; } // 添加新学生 void static addNewStu() { string name; string age; string stuID; string classID; string gender; cout << "tttt" << "请输入学生的姓名:" << endl; cin >> name; cout << "tttt" << "请输入学生的年龄:" << endl; cin >> age; while (true) { cout << "tttt" << "请输入学生的学号:" << endl; cin >> stuID; if (checkOnlyClassID(stuID)) { break; } else { cout << "tttt" << "输入有误,请重新输入!" << endl; } } cout << "tttt" << "请输入学生的班级:" << endl; cin >> classID; while (true) { cout << "tttt" << "请输入学生的性别:" << endl; cin >> gender; if (checkGender(gender)) { break; } else { cout << "tttt" << "输入有误,请重新输入!" << endl; } } int stuAge = checkRange(age); Student* stu = new Student(name, classID, stuID, stuAge, gender); save(stu); // 保存记录到磁盘 stu_vec.push_back(stu); //更新学生数量 record_num = stu_vec.size(); cout << "tttt" << "添加成功!" << endl; cout << "tttt" << "当前有" << record_num << "条记录" << endl; system("pause"); system("cls"); } //查找学生 void static findStu() { string msg; cout << "tttt" << "请输入学生信息:" << endl; cin >> msg; vector vec; //可以通过学号或者姓名查找 for (auto stu_ptr : stu_vec) { if (stu_ptr->getStuID() == msg || stu_ptr->getName() == msg) { vec.push_back(stu_ptr); } } if (vec.size() == 0) { cout << "tttt" << "查无此人!" << endl; } else { int record_num = vec.size(); cout << "tttt" << "查询到以下" << record_num << "条记录" << endl; int current_record = 1; for (auto stu_ptr : vec) { cout << current_record << "、"; printMsg(stu_ptr); current_record++; } } system("pause"); system("cls"); } //删除学生 void static deleteStu() { if (record_num == 0) { cout << "tttt" << "当前记录为空" << endl; return; } // 根据学号来删除学生 string stuID; cout << "tttt" << "请输入要删除学生的学号:" << endl; cin >> stuID; for (vector ::const_iterator con_it = stu_vec.begin(); con_it != stu_vec.end(); con_it++) { Student* stu_ptr = *con_it; if (stu_ptr->getStuID() == stuID) { con_it = stu_vec.erase(con_it); } } ofstream ofs; ofs.open(FILE_NAME, ios::out | ios::trunc); // 重新写入文件 for (auto stu_ptr : stu_vec) { printMsg(ofs, stu_ptr); } // 更新记录条数 record_num = stu_vec.size(); cout << "tttt" << "操作成功!" << endl; system("pause"); system("cls"); } // 删除全部学生 void static deleteAllStu() { string selected; while (true) { cout << "tttt" << "确定要删除全部学生信息吗?" << endl; cout << "tttt" << "1.确定" << endl; cout << "tttt" << "2.取消" << endl; cin >> selected; int select_num = atoi(selected.c_str()); if (select_num == 1) { ofstream ofs; ofs.open(FILE_NAME, ios::trunc); cout << "tttt" << "操作成功!" << endl; stu_vec.clear(); record_num = 0; break; } if (select_num == 2) { break; } else { cout << "tttt" << "选择有误, 请重新选择!" << endl; } } system("pause"); system("cls"); } // 修改学生信息 void static modifyStu() { bool isFind = false; // 判断是否找到 cout << "tttt" << "请输入学生的学号:" << endl; string stuID; cin >> stuID; for (auto stu_ptr : stu_vec) { if (stu_ptr->getStuID() == stuID) { isFind = true; while (true) { string msg; string selected; cout << "tttt" << "请选择要修改的信息:" << endl; cout << "tttt" << "1.姓名" << endl; cout << "tttt" << "2.性别" << endl; cout << "tttt" << "3.年龄" << endl; cout << "tttt" << "4.学号" << endl; cout << "tttt" << "5.班级" << endl; cout << "tttt" << "0.结束操作" << endl; cin >> selected; int select_num = atoi(selected.c_str()); switch (select_num) { case 1: cout << "tttt" << "请输入新信息" << endl; cin >> msg; stu_ptr->setName(msg); break; case 2: cout << "tttt" << "请输入新信息" << endl; cin >> msg; stu_ptr->setStuGender(msg); break; case 3: cout << "tttt" << "请输入新信息" << endl; cin >> msg; stu_ptr->setAge(atoi(msg.c_str())); break; case 4: cout << "tttt" << "请输入新信息" << endl; cin >> msg; stu_ptr->setStuID(msg); break; case 5: cout << "tttt" << "请输入新信息" << endl; cin >> msg; stu_ptr->setClassID(msg); break; case 0: goto A; break; } } } } if (!isFind) { cout << "tttt" << "查无此人!" << endl; system("pause"); system("cls"); return; } A: ofstream ofs; ofs.open(FILE_NAME, ios::out | ios::trunc); for (auto stu_ptr : stu_vec) { printMsg(ofs, stu_ptr); } cout << "操作成功!" << endl; system("pause"); system("cls"); } // 查看所有学生记录 void static showAllStu() { int index = 1; cout << "tttt" << "当前共有" << record_num << "条记录" << endl; if (record_num == 0) { system("pause"); system("cls"); return; } for (auto stu_ptr : stu_vec) { cout << "tttt" << index << ":"; printMsg(stu_ptr); } system("pause"); system("cls"); } // 析构函数 ~FuncManager() { for (auto stu_ptr : stu_vec) { if (stu_ptr != nullptr) cout << "Func" << endl; delete stu_ptr; } } private: // 保存记录到文件 void static save(Student* stu) { //保存记录到磁盘 ofstream ofs; ofs.open(FILE_NAME, ios::app | ios::out); printMsg(ofs, stu); ofs.close(); } int static checkRange(string age) { int stuAge = atoi(age.c_str()); return (stuAge > 0 && stuAge < 120) ? stuAge : 0; } bool static checkGender(string gender) { if (gender == "男" || gender == "女") { return true; } return false; } // 需要保证ClassID唯一 bool static checkOnlyClassID(string classID) { for (auto stu_ptr : stu_vec) { if (stu_ptr->getClassID() == classID) { return false; } } return true; } }; // 声明静态成员 int FuncManager::record_num; vector FuncManager::stu_vec; int main() { string selected; //初始化 FuncManager::init(); while (true) { FuncManager::menu(); cin >> selected; int selected_num = atoi(selected.c_str()); switch (selected_num) { case 1: // 添加学生 FuncManager::addNewStu(); break; case 2: // 查找学生" FuncManager::findStu(); break; case 3: // 删除学生 FuncManager::deleteStu(); break; case 4: // 删除全部学生 FuncManager::deleteAllStu(); break; case 5: // 修改学生信息 FuncManager::modifyStu(); break; case 6: // 展示所有学生信息 FuncManager::showAllStu(); break; case 7: cout << "tttt" << "欢迎您的下次使用 !" << endl; system("pause"); exit(0); break; default: cout << "tttt" << "选择有误, 请从新选择!" << endl; system("pause"); system("cls"); break; } } return 0; }
因为技术有限,所以并没有涉及到数据库操作,不过用到了文件操作,还是值得一看的,还有一点就是我将学生类中的成员变量都利用了指针代替,第一个目的主要是为了再次熟悉以下指针相关的操作,然后就是在对象进行赋值的时候可以提升效率,这也是一种类设计的思想。



