#pragma once #ifndef UNDERGRADUATE_MANAGER_H #define UNDERGRADUATE_MANAGER_H #include #include #include #include "undergraduate.h" using namespace std; class UnderGraduateManager { private: int top = 0; UnderGraduate undergraduates[100]; public: UnderGraduateManager();//读取文件 int queryByNo(string sno); void clearStudent(); int addStudent(UnderGraduate s); int modifyStudent(string sno); int deleteStudent(string sno); int queryStudent(string sno); void displayAll(); void dataManage(); void dataSave(); void dataRead(); ~UnderGraduateManager();//写入文件 }; UnderGraduateManager::UnderGraduateManager() { dataRead(); } int UnderGraduateManager::queryByNo(string sno) { // 查找,有:i, 无: -1 for (int i = 0; i <= top; i++) if (undergraduates[i].getStudentNo() == sno) return i; return -1; } void UnderGraduateManager::clearStudent() {//停止检索最后一个元素,相当于删除元素 top = -1; } int UnderGraduateManager::addStudent(UnderGraduate s) {//若不存在该元素(return -1),则元素数+1,保存至文件 int p = queryByNo(s.getStudentNo()); if (p == -1) { top++; undergraduates[top] = s; dataSave(); return 1; } else { cout << "existed!" << endl; return 0; } } int UnderGraduateManager::modifyStudent(string sno) {//若存在该元素(return != -1),则修改属性,保存至文件 int p = queryByNo(sno); if (p == -1) { cout << "Not exist!" << endl; return 0; } else { cout << "please input new detials" << endl; undergraduates[p].inputData(); dataSave(); return 1; } } int UnderGraduateManager::deleteStudent(string sno) {//删除元素,其他元素前移 int p = queryByNo(sno); if (p == -1) { cout << "Not exist!" << endl; return 0; } else { for (int i = 0; i < top; i++) undergraduates[i] = undergraduates[i + 1]; top--; cout << "Deleted!" << endl; dataSave(); return 1; } } int UnderGraduateManager::queryStudent(string sno) { int p = queryByNo(sno); if (p == -1) { cout << "Not exist!" << endl; return 0; } else { cout << "exist!" << endl; undergraduates[p].displayDetails(); return 1; } } void UnderGraduateManager::displayAll() { for (int i = 0; i <= top; i++) { cout << "The NO." << i << "student's info:" << endl; undergraduates[i].displayDetails(); } } UnderGraduateManager::~UnderGraduateManager() { dataSave(); } void UnderGraduateManager::dataManage() { int choice = 1; string sstudentNo; UnderGraduate s; while (choice != 0) { cout << "**********************************" << endl; cout << "\t\t undergraduate maintainace\n"; cout << "**********************************" << endl; cout << "\n\t\t 1新增"; cout << "\n\t\t 2.修改"; cout << "\n\t\t 3.删除"; cout << "\n\t\t 4.查找"; cout << "\n\t\t 5.显示"; cout << "\n\t\t 6.全部删除"; cout << "\n\t\t 0.退出"; cout << endl; cout << "**********************************" << endl; cout << "Please take your choice: "; cin >> choice; switch (choice) { case 1: s.inputData(); addStudent(s); break; case 2: cout << " Please input student's number:"; cin >> sstudentNo; modifyStudent(sstudentNo); break; case 3: cout << " Please input student's number:"; cin >> sstudentNo; deleteStudent(sstudentNo); break; case 4: cout << " Please input student's number:"; cin >> sstudentNo; queryStudent(sstudentNo); break; case 5: displayAll(); break; case 6: clearStudent(); default: break; } } } void UnderGraduateManager::dataSave() { fstream file("D:\\ProgramData\\vs\\design\\UGM.docx", ios::out);// ! for (int i = 0; i <= top; i++) { file.write((char*)&undergraduates[i], sizeof(undergraduates[i])); } file.close(); } void UnderGraduateManager::dataRead() { UnderGraduate s; top--; fstream file("D:\\ProgramData\\vs\\design\\UGM.docx", ios::in); // ! while (1) { file.read((char*)&s, sizeof(s)); if (!file) break; top++; undergraduates[top] = s; } file.close(); } #endif //UNDERGRADUATE_MANAGER_H