(1)博客介绍
本文的学生选课系统的源码来自于csdn的一篇博客当中。该系统的实现语言以C++为主,本文的主要内容围绕该系统进行分析,并针对系统的主要问题进行一些修改或重构。
本篇如有问题存在,请各位读者多多指正!
(2)学生选课系统分析
源代码如下:
点击查看代码
#define _CRT_SECURE_NO_WARNINGS#include<iostream>
#include<fstream>
#include<vector>
#include<string.h>
#include<stdlib.h>
using namespace std;int flag = 0; // default is studentclass Course
{
public:friend void Input_course(vector<Course>& cour);friend void Delete_course(vector<Course>& cour);friend void Modify_course(vector<Course>& cour);friend void Lookup_course(vector<Course>& cour);friend void Show_course(vector<Course>& cour);friend void Add_course(vector<Course>& cour); friend int Read_course(vector<Course>& cour);friend void Write_course(vector<Course>& cour, int n);const char* c_id() { return id; }const char* c_name() { return name; }const bool c_nature() { return nature; }Course(): id("None"), name("None"), count(0){}public:char id[20];char name[20];
private:bool nature; // 0 indicates elective and 1 indicates compulsoryint hour; // class time int credir; // credit
public:int count; // count of student 0-60
};class Student
{
public:friend void Input_stu(vector<Student>& stu);friend void Delete_stu(vector<Student>& stu);friend void Modify_stu(vector<Student>& stu);friend void Lookup_stu(vector<Student>& stu);friend void Add_stu(vector<Student>& stu);friend void Show_stu(vector<Student>& stu);friend int Read_stu(vector<Student>& stu);friend void Write_stu(vector<Student>& stu, int n);friend void Select(vector<Course>& cour, vector<Student>& stu);friend void Deselect(vector<Course>& cour, vector<Student>& stu);
public:char _id[20];char _class[20];char _name[20];Course _cour_compulsory[2];Course _cour_elective[1];int _num_compulsory = 0;int _num_elective = 0;
};vector<Course> cour;
vector<Student> stu;// ========================================== Course ==================================================== //void Write_course(vector<Course>& cour, int n)
{fstream myfile;myfile.open("course.txt", ios::out | ios::binary);if (!myfile){cout << "ERROR!course.txt can't open." << endl;abort();}myfile << n << endl << endl;for (int i = 0; i < n; i++){myfile << cour[i].id << "\t" << cour[i].name << "\t"<< cour[i].nature << "\t" << cour[i].hour << "\t"<< cour[i].credir << "\t" << cour[i].count << endl;}myfile.close();
}int Read_course(vector<Course>& cour)
{cour.clear(); // clearfstream myfile;myfile.open("course.txt", ios::in | ios::binary);if (!myfile){cout << "ERROR!course.txt can't open." << endl;abort();}int n; // count of coursemyfile.seekg(0);myfile >> n;Course temp;for (int i = 0; i < n; i++){myfile >> temp.id >> temp.name >> temp.nature>> temp.hour >> temp.credir >> temp.count;cour.push_back(temp); // push_back}myfile.close();return n;
}void Input_course(vector<Course>& cour)
{system("cls");cour.clear();int i = 0;char sign = '0';Course temp;cout << "=============== Please input the massage ===============" << endl;while (sign != 'n' && sign != 'N'){loop:cout << "ID: ";cin >> temp.id;int c = 0;while (c < i){c++;if (!strcmp(temp.id, cour[i - c].id)){cout << "ID repeat, please input again." << endl;goto loop;}}cout << "name of course: ";cin >> temp.name;cout << "elective or compilsory(0 or 1): ";cin >> temp.nature;cout << "class hour: ";cin >> temp.hour;cout << "credir: ";cin >> temp.credir;temp.count = 0; // the default is 0cour.push_back(temp); // push_backcout << "whether continue(y or n): ";cin >> sign;i++;}Write_course(cour, i); // save
}void Delete_course(vector<Course>& cour)
{system("cls");int n = Read_course(cour);int i = 0;char id[20];cout << "=============== Delete the message of course ===============" << endl;cout << "please input the ID of course which you want to delete: ";cin >> id;while (i < n && strcmp(id, cour[i].id))i++;if (i == n){cout << "no fount." << endl;system("pause");return;}else{if (cour[i].count){cout << "所选人数不为零,请联系所有学生退课后,再修改课程信息。" << endl;system("pause");return;}}for (int j = i; j < n - 1; j++)swap(cour[j], cour[j + 1]);// savechar sign;cout << "whether delete(y or n): ";cin >> sign;if (sign != 'n' && sign != 'N'){cour.erase(cour.end() - 1);Write_course(cour, n - 1);cout << "=============== Delete succeed ===============" << endl;}
}void Modify_course(vector<Course>& cour)
{system("cls");int n = Read_course(cour); // count of coursechar id[20];int i = 0;cout << "=============== Modify course ===============" << endl;cout << "input the ID of course you want to modify: ";cin >> id;while ( i < n && strcmp(id, cour[i].id)) // i < n 要提前判断i++;if (i == n){cout << "no found." << endl;system("pause");return;}else{if (cour[i].count){cout << "所选人数不为零,请联系所有学生退课后,再修改课程信息。" << endl;system("pause");return;}}cout << "----------------------------------------------------" << endl;cout << "ID" << "\t" << "name" << "\t" << "nature" << "\t"<< "hour" << "\t" << "credir" << "\t" << "count" << endl;cout << "----------------------------------------------------" << endl;cout << cour[i].id << "\t" << cour[i].name << "\t" << cour[i].nature << "\t"<< cour[i].hour << "\t" << cour[i].credir << "\t" << cour[i].count << endl;cout << "please reenter the message of course." << endl;cout << "name of course: ";cin >> cour[i].name;cout << "elective or compilsory(0 or 1): ";cin >> cour[i].nature;cout << "class hour: ";cin >> cour[i].hour;cout << "credir: ";cin >> cour[i].credir;// savechar sign;cout << "whether save the data(y or n): ";cin >> sign;if (sign != 'n' && sign != 'N'){Write_course(cour, n);cout << "=============== Modify succeed ===============" << endl;system("pause");}
}void Lookup_course(vector<Course>& cour)
{system("cls");int n = Read_course(cour);int i = 0;char id[20];cout << "=============== Lookup the course ===============" << endl;cout << "please input the course ID you want to seek: ";cin >> id;while (i < n && strcmp(id, cour[i].id))i++;if (i == n){cout << "no found." << endl;}else{cout << "---------------------------------------------" << endl;cout << "ID: " << cour[i].id << endl;cout << "name: " << cour[i].name << endl;cout << "nature: " << (cour[i].nature ? "compulsory" : "elective") << endl;cout << "class hour: " << cour[i].hour << endl;cout << "credir: " << cour[i].credir << endl;cout << "student count: " << cour[i].count << endl;}system("pause");
}void Show_course(vector<Course>& cour)
{system("cls");int n = Read_course(cour);cout << "================ display all course ================" << endl;cout << "----------------------------------------------------" << endl;cout << "ID" << "\t" << "name" << "\t" << "nature" << "\t"<< "hour" << "\t" << "credir" << "\t" << "count" << endl;cout << "----------------------------------------------------" << endl;for (int i = 0; i < n; i++){cout << cour[i].id << "\t" << cour[i].name << "\t" << cour[i].nature << "\t" << cour[i].hour << "\t" << cour[i].credir << "\t" << cour[i].count << endl;}system("pause");
}void Add_course(vector<Course>& cour)
{system("cls");int n = Read_course(cour);char sign = '0';Course temp;cout << "=============== add course ===============" << endl;while (sign != 'n' && sign != 'N'){loop:cout << "ID: ";cin >> temp.id;int c = 0;while (c < n){c++;if (!strcmp(temp.id, cour[n - c].id)){cout << "ID repeat, please input again." << endl;goto loop;}}cout << "name of course: ";cin >> temp.name;cout << "elective or compulsory(0 or 1): ";cin >> temp.nature;cout << "class hour: ";cin >> temp.hour;cout << "credir: ";cin >> temp.credir;temp.count = 0;n++;cour.push_back(temp);cout << "whether continue(y or n): ";cin >> sign;}// saveWrite_course(cour, n);
}// ============================================ student ============================================== //void Write_stu(vector<Student>& stu, int n)
{fstream myfile;myfile.open("student.txt", ios::out | ios::binary);if (!myfile){cout << "ERROR! student.txt can't open!" << endl;abort();}myfile << n << endl << endl;for (int i = 0; i < n; i++){myfile << stu[i]._id << "\t" << stu[i]._class << "\t"<< stu[i]._name << "\t" << stu[i]._cour_compulsory[0].c_id() << "\t"<< stu[i]._cour_compulsory[0].c_name() << "\t"<< stu[i]._cour_compulsory[1].c_id() << "\t"<< stu[i]._cour_compulsory[1].c_name() << "\t"<< stu[i]._cour_elective[0].c_id() << "\t"<< stu[i]._cour_elective[0].c_name() << "\t"<< stu[i]._num_compulsory << "\t" << stu[i]._num_elective << endl;}myfile.close();
}int Read_stu(vector<Student>& stu)
{stu.clear(); // clearfstream myfile;myfile.open("student.txt", ios::in | ios::binary);if (!myfile){cout << "ERROR! student.txt can't open!" << endl;abort();}int n;myfile.seekg(0);myfile >> n;Student temp;for (int i = 0; i < n; i++){myfile >> temp._id >> temp._class>> temp._name>> temp._cour_compulsory[0].id>> temp._cour_compulsory[0].name>> temp._cour_compulsory[1].id>> temp._cour_compulsory[1].name>> temp._cour_elective[0].id>> temp._cour_elective[0].name>> temp._num_compulsory >> temp._num_elective;stu.push_back(temp); // push_back}myfile.close();return n;
}void Input_stu(vector<Student>& stu)
{system("cls");stu.clear();int i = 0;char sign = '\0';Student temp;cout << "============ please input the message ============" << endl;while (sign != 'n' && sign != 'N'){loop:cout << "student ID: ";cin >> temp._id;int c = 0;while (c < i){c++;if (!strcmp(temp._id, stu[i - c]._id)){cout << "ID repeat, please input again." << endl;goto loop;}}cout << "name: ";cin >> temp._name;cout << "class: ";cin >> temp._class;stu.push_back(temp); // push_backcout << "whether continue(y or n): ";cin >> sign;i++;}Write_stu(stu, i);
}void Delete_stu(vector<Student>& stu)
{system("cls");int n = Read_stu(stu);int m = Read_course(cour); // number of courseint i = 0;char id[20];cout << "============ delete student ============" << endl;cout << "please input the ID of student who you want to delete: ";cin >> id;while (i < n && strcmp(id, stu[i]._id))i++;if (i == n){cout << "no fount." << endl;system("pause");return;}if (stu[i]._num_compulsory){while (stu[i]._num_compulsory > 0){stu[i]._num_compulsory--;int c = 0;while (c < m){if (!strcmp(stu[i]._cour_compulsory[stu[i]._num_compulsory].id, cour[c].id)){cour[c].count--;break;}c++;}}}else if (stu[i]._num_elective){while (stu[i]._num_elective > 0){stu[i]._num_elective--;int c = 0;while (c < m){if (!strcmp(stu[i]._cour_elective[stu[i]._num_elective].id, cour[c].id)){cour[c].count--;break;}c++;}}}for (int j = i; j < n - 1; j++)swap(stu[j], stu[j + 1]);// savechar sign;cout << "whether delete(y or n): ";cin >> sign;if (sign != 'n' && sign != 'N'){stu.erase(stu.end() - 1);Write_stu(stu, n - 1);Write_course(cour, m);cout << "============ delete succeed ============" << endl;system("pause");}
}void Modify_stu(vector<Student>& stu)
{system("cls");int n = Read_stu(stu);char id[20];int i = 0;cout << "============ Modify the message of student ============" << endl;cout << "please input the ID of student who you want to modify: ";cin >> id;while (i < n && strcmp(id, stu[i]._id))i++;if (i == n){cout << "no found." << endl;system("pause");return;}cout << "-------------------------------------------------------" << endl;cout << "ID: " << stu[i]._id << endl;cout << "class: " << stu[i]._class << endl;cout << "name: " << stu[i]._name << endl;cout << "-------------------------------------------------------" << endl;cout << "please reenter the message." << endl;cout << "class: ";cin >> stu[i]._class;cout << "name: ";cin >> stu[i]._name;// savechar sign;cout << "whether save the data(y or n): ";cin >> sign;if (sign != 'n' && sign != 'N'){cout << "modify succeed." << endl; Write_stu(stu, n);system("pause");}
}void Lookup_stu(vector<Student>& stu)
{system("cls"); int n = Read_stu(stu);Read_course(cour);int i = 0;char id[20]; cout << "============ Lookup the score of student ============" << endl; cout << "please input the ID of student you want to seek: ";cin >> id;while (i < n && strcmp(id, stu[i]._id))i++;if (i == n){cout << "no found." << endl;}else{ cout << "ID: " << stu[i]._id << endl;cout << "class: " << stu[i]._class << endl;cout << "name: " << stu[i]._name << endl;cout << "compulsory: " << (!strcmp(stu[i]._cour_compulsory[0].id, "None") ? "None" : stu[i]._cour_compulsory[0].c_name())<< " " << (!strcmp(stu[i]._cour_compulsory[1].id, "None") ? "None" : stu[i]._cour_compulsory[1].c_name()) << endl;cout << "elective: " << (!strcmp(stu[i]._cour_elective[0].id, "None") ? "None" : stu[i]._cour_elective[0].c_name()) << endl;}system("pause");
}void Add_stu(vector<Student>& stu)
{system("cls");int n = Read_stu(stu);char sign = '0';Student temp;cout << "============ add student ============" << endl;while (sign != 'n' && sign != 'N'){loop:cout << "student ID: ";cin >> temp._id;int c = 0;while (c < n){c++;if (!strcmp(temp._id, stu[n - c]._id)){cout << "ID repeat, please input again." << endl;goto loop;}}cout << "class: ";cin >> temp._class;cout << "name: ";cin >> temp._name;n++;stu.push_back(temp); // push_backcout << "whether continue(y or n): ";cin >> sign;}// saveWrite_stu(stu, n);
}void Show_stu(vector<Student>& stu)
{ system("cls"); int n = Read_stu(stu);cout << "============================ diplay all student ==========================" << endl;cout << "ID" << "\t" << "class" << "\t" << "name" << "\t" << "compulsory" << "\t\t" << "elective" << endl; cout << "--------------------------------------------------------------------------" << endl;for (int i = 0; i < n; i++){cout << stu[i]._id << "\t" << stu[i]._class << "\t" << stu[i]._name << "\t" << (!strcmp(stu[i]._cour_compulsory[0].id, "None") ? "None" : stu[i]._cour_compulsory[0].name)<< " "<< (!strcmp(stu[i]._cour_compulsory[1].id, "None") ? "None" : stu[i]._cour_compulsory[1].c_name())<< "\t"<< (!strcmp(stu[i]._cour_elective[0].id, "None") ? "None" : stu[i]._cour_elective[0].c_name()) << endl;}system("pause");
}void Select(vector<Course>& cour, vector<Student>& stu)
{system("cls");int n_cour = Read_course(cour);int n_stu = Read_stu(stu); int i = 0; // studentint j = 0; // coursechar id_c[20]; // course char id_s[20]; // student cout << "============ select system ============" << endl;cout << "please input the ID of student: ";cin >> id_s;while ( i < n_stu && strcmp(stu[i]._id, id_s))i++;if (i == n_stu) {cout << "no found." << endl;system("pause");return;}cout << ("please input the ID of course you want to select: ");cin >> id_c;while ( j < n_cour && strcmp(cour[j].c_id(), id_c))j++;// cout << cour[j].id << endl;if (j == n_cour){cout << "no found." << endl;system("pause");return;}if (cour[j].count >= 60){cout << "student number full, select failed." << endl;system("pause");}else{if (cour[j].c_nature())if (stu[i]._num_compulsory < 2){int c = 0;while (c < stu[i]._num_compulsory && strcmp(cour[j].id, stu[i]._cour_compulsory[c].id))c++;if (c < stu[i]._num_compulsory){cout << "course repeat." << endl;system("pause");return;}stu[i]._cour_compulsory[stu[i]._num_compulsory] = cour[j]; // cout << stu[i]._cour_compulsory[stu[i]._num_compulsory].id << endl;// cout << stu[i]._cour_compulsory[stu[i]._num_compulsory].c_name() << endl;stu[i]._num_compulsory++;cour[j].count++;Write_course(cour, n_cour);Write_stu(stu, n_stu);// cout << stu[i]._cour_compulsory[stu[i]._num_compulsory].id << endl;cout << "select succeed." << endl;system("pause");}else{cout << "class number upper limit, select failed." << endl;system("pause");}else{if (stu[i]._num_elective < 1){int c = 0;while (c < stu[i]._num_elective && strcmp(cour[j].id, stu[i]._cour_elective[c].id))c++;if (c < stu[i]._num_elective){cout << "course repeat." << endl;system("pause");return;}stu[i]._cour_elective[stu[i]._num_elective] = cour[j];stu[i]._num_elective++;cour[j].count++;Write_course(cour, n_cour);Write_stu(stu, n_stu);cout << "select succeed." << endl;system("pause");}else{cout << "class number upper limit, select failed." << endl;system("pause");}}}
}void Deselect(vector<Course>& cour, vector<Student>& stu)
{system("cls");int n_cour = Read_course(cour);int n_stu = Read_stu(stu);int i = 0; // studentint j = 0; // coursechar id_c[20]; // course char id_s[20]; // student cout << "============ deselect system ============" << endl;cout << "please input the ID of student: ";cin >> id_s;while (i < n_stu && strcmp(stu[i]._id, id_s))i++;if (i == n_stu){cout << "no found." << endl;system("pause");return;}cout << ("please input the ID of course you want to deselect: ");cin >> id_c;while (j < n_cour && strcmp(cour[j].c_id(), id_c))j++;if (j == n_cour){cout << "no found." << endl;system("pause");return;}if (stu[i]._num_compulsory){int c = stu[i]._num_compulsory - 1;while (c >= 0){if (!strcmp(stu[i]._cour_compulsory[c].id, id_c)){cour[j].count--;stu[i]._num_compulsory--;strcpy(stu[i]._cour_compulsory[c].id, "None");strcpy(stu[i]._cour_compulsory[c].name, "None");// 重新排序for (; c < stu[i]._num_compulsory; c++)swap(stu[i]._cour_compulsory[c], stu[i]._cour_compulsory[c + 1]);Write_stu(stu, n_stu);Write_course(cour, n_cour);cout << "退选成功。" << endl;system("pause");return;}c--;}if (stu[i]._cour_elective){int c = stu[i]._num_elective - 1;while (c >= 0){if (!strcmp(stu[i]._cour_elective[c].id, id_c)){cour[j].count--;stu[i]._num_elective--;strcpy(stu[i]._cour_elective[c].id, "None");strcpy(stu[i]._cour_elective[c].name, "None");for (; c < stu[i]._num_elective; c++)swap(stu[i]._cour_elective[c], stu[i]._cour_elective[c + 1]);Write_stu(stu, n_stu);Write_course(cour, n_cour);cout << "退选成功。" << endl;system("pause");return;}c--;}cout << "你并未选择该课程。" << endl;system("pause");return;}}if (stu[i]._cour_elective){int c = stu[i]._num_elective - 1;while (c >= 0){if (!strcmp(stu[i]._cour_elective[c].id, id_c)){cour[j].count--;stu[i]._num_elective--;strcpy(stu[i]._cour_elective[c].id, "None");strcpy(stu[i]._cour_elective[c].name, "None");for (; c < stu[i]._num_elective; c++)swap(stu[i]._cour_elective[c], stu[i]._cour_elective[c + 1]);Write_stu(stu, n_stu);Write_course(cour, n_cour);cout << "退选成功。" << endl;system("pause");return;}c--;}cout << "你并未选择该课程。" << endl;system("pause");return;}
}// =========================================== menu ========================================== //int choose_user()
{
loop:system("cls");int c = 0;cout << "=================================================================" << endl;cout << "-------------- welcome to course selection system ---------------" << endl;cout << "please choose your identity, admin or student(1 or 2)(input 0 to exit): ";cin >> c;char passwd[20];if (c == 1){cout << "please input password: ";cin >> passwd;if (!strcmp(passwd, "123456")){flag = 1;cout << "log on succeed." << endl;system("pause");}else{cout << "password error." << endl;system("pause");goto loop;}}elseflag = 0;if (c < 0 || c > 2){cout << "please input 0-2." << endl;system("pause");goto loop;}return c;
}void menu_course()
{int c = 0;while (1){system("cls");cout << "============ menu of course ============" << endl;cout << " 1. input " << endl;cout << " 2. delete " << endl;cout << " 3. modify " << endl;cout << " 4. lookup " << endl;cout << " 5. add " << endl;cout << " 6. show all " << endl;cout << " 0. return " << endl;cout << "----------------------------------------" << endl;cout << "please choose your operation(0-6): ";cin >> c;switch (c){case 1:if (flag)Input_course(cour);else{cout << "request denied." << endl;system("pause");}break;case 2:if (flag)Delete_course(cour);else{cout << "request denied." << endl;system("pause");}break;case 3:if (flag)Modify_course(cour);else{cout << "request denied." << endl;system("pause");}break;case 4:Lookup_course(cour);break;case 5:if (flag)Add_course(cour);else{cout << "request denied." << endl;system("pause");}break;case 6:Show_course(cour);break;case 0:cout << "------------ return succeed ------------" << endl;system("pause");return;default:cout << "please input 0-6." << endl;system("pause");break;}}
}void menu_student()
{int c = 0;while (1){system("cls");cout << "============ menu of student ============" << endl;cout << " 1. input " << endl; cout << " 2. delete " << endl; cout << " 3. modify " << endl; cout << " 4. lookup " << endl; cout << " 5. add " << endl;cout << " 6. show all " << endl;cout << " 7. select course " << endl;cout << " 8. deselect " << endl;cout << " 0. return " << endl;cout << "----------------------------------------" << endl; cout << "please choose your operation(0-6): ";cin >> c;switch (c) {case 1: if (flag)Input_stu(stu);else{cout << "request denied." << endl;system("pause");}break; case 2:if (flag) Delete_stu(stu); else{cout << "request denied." << endl;system("pause");}break; case 3: if (flag) Modify_stu(stu); else{cout << "request denied." << endl;system("pause");}break; case 4: Lookup_stu(stu); break; case 5: if (flag) Add_stu(stu); else{cout << "request denied." << endl;system("pause");}break; case 6: Show_stu(stu); break;case 7:Select(cour, stu);break;case 8:Deselect(cour, stu);break;case 0:cout << "------------ return succeed ------------" << endl;system("pause");return;default:cout << "please input 0-6." << endl;system("pause");break;}}
}int menu_main()
{int c = 0;while (1){system("cls");cout << "====================================================" << endl;cout << " 1. course message " << endl;cout << " 2. student message " << endl;cout << " 0. go back to higher-leve menu " << endl;cout << "choose your operation: ";cin >> c;if (c < 0 || c > 2){cout << "please input 0-2." << endl;system("pause");}elsebreak;}return c;
}// ============================================================================================================= //int main()
{cour.reserve(100);stu.reserve(100);while (1){loop:if (!choose_user()){cout << "=============== thank you for using ===============" << endl;return 0;}while (1){switch (menu_main()){case 1:menu_course();break;case 2:menu_student();break;case 0:cout << "=============== return scceed ===============" << endl;system("pause");goto loop;default:cout << "please input 0-2." << endl;system("pause");break;}}}return 0;
}
该系统的大致功能如下:
管理员:课程增删改查、学生信息管理
学生:选课/退选、查看已选课程
数据存储:course.txt 和 student.txt 文件存储数据
系统详细介绍:本系统是学生选课系统,主要是实现大学生的选课需求。进入系统,需要先选择用户(学生或管理员),选择管理员,需要输入对应的密码。选择完身份后,进入主菜单,会出现两个子菜单,学生菜单和课程菜单,选择相应的选项,就会进入相应的菜单,其中0选项是返回上级目录。 进入课程菜单,会看到增删改查相应的操作,还有显示全部课程信息。其中,学生用户没有对课程进行增删改的权利。管理员是超级用户,有所有操作的权利。里面的细节我们在模块的具体实现时再说。 进入学生菜单,也会看到增删改查相关的操作,还有显示全部学生的信息。不同的是,学生菜单中可以进行选课和退选的相关操作。学生用户只有查看信息,选课退选的权利,没有增删改的权利。
(3)存在的主要问题:
首先,在使用该选课系统过后会发现菜单栏提供的两个功能有相似之处但又不相同,就是input和add这两个功能。input的功能大致就是清空原来存档的txt文件内部信息并重新添加新的信息,而add的功能则是在原有基础上进行信息的添加。我认为前者较为冗余,在现实中实用性不大,并且在菜单栏中容易混淆。
add的功能展示:
input的功能展示:
另外,该系统作为选课系统缺少“课余量”这一重要的数据,每门课程对应着相应的课余量,学生在选课时只能选课余量不为0的课,我认为“课余量”这一数据需要添加到该系统中。
(4)系统改进
针对课余量所修改的代码区块(course类中已添加max_students这一变量名作为课余量):
点击查看代码
void Show_course(vector<Course>& cour)
{system("cls");int n = Read_course(cour);cout << "================ display all course ================" << endl;cout << "----------------------------------------------------" << endl;cout << "ID\t名称\t性质\t课时\t学分\t已选/最大\t剩余" << endl;cout << "----------------------------------------------------" << endl;for (int i = 0; i < n; i++) {cout << cour[i].id << "\t" << cour[i].name << "\t"<< (cour[i].nature ? "必修" : "选修") << "\t"<< cour[i].hour << "\t" << cour[i].credir << "\t"<< cour[i].count << "/" << cour[i].max_students << "\t\t"<< cour[i].remaining() << endl; // 显示剩余量}system("pause");
}void Add_course(vector<Course>& cour) {system("cls");int n = Read_course(cour);char sign = '0';Course temp;cout << "=============== ADD COURSE ===============" << endl;while (sign != 'n' && sign != 'N') {loop:cout << "ID: ";cin >> temp.id;int c = 0;while (c < n) {if (!strcmp(temp.id, cour[c].id)) { // 检查与文件中的课程ID重复cout << "ID重复,请重新输入!" << endl;goto loop;}c++;}cout << "课程名称: ";cin >> temp.name;cout << "课程性质 (0选修/1必修): ";cin >> temp.nature;cout << "课时: ";cin >> temp.hour;cout << "学分: ";cin >> temp.credir;cout << "最大容量: "; // 新增:输入容量cin >> temp.max_students;temp.count = 0;cour.push_back(temp);n++;cout << "继续添加?(y/n): ";cin >> sign;}Write_course(cour, n);
}
//下面是选课和退选两个函数的修改
void Select(vector<Course>& cour, vector<Student>& stu) {system("cls");int n_cour = Read_course(cour);int n_stu = Read_stu(stu);int i = 0; // student indexint j = 0; // course indexchar id_c[20], id_s[20];cout << "============ SELECT COURSE ============" << endl;cout << "Student ID: ";cin >> id_s;// Find studentwhile (i < n_stu && strcmp(stu[i]._id, id_s)) i++;if (i == n_stu) {cout << "Student not found." << endl;system("pause");return;}// Find coursecout << "Course ID: ";cin >> id_c;while (j < n_cour && strcmp(cour[j].c_id(), id_c)) j++;if (j == n_cour) {cout << "Course not found." << endl;system("pause");return;}// Check remaining capacityif (cour[j].remaining() <= 0) {cout << "Course is full!" << endl;system("pause");return;}// Check course natureif (cour[j].c_nature()) { // Compulsoryif (stu[i]._num_compulsory >= 2) {cout << "Compulsory course limit reached (max 2)." << endl;system("pause");return;}// Check duplicatefor (int k = 0; k < stu[i]._num_compulsory; k++) {if (strcmp(stu[i]._cour_compulsory[k].id, cour[j].id) == 0) {cout << "Already enrolled in this compulsory course." << endl;system("pause");return;}}// Add coursestu[i]._cour_compulsory[stu[i]._num_compulsory] = cour[j];stu[i]._num_compulsory++;cour[j].count++; // 只增加一次}else { // Electiveif (stu[i]._num_elective >= 1) {cout << "Elective course limit reached (max 1)." << endl;system("pause");return;}// Check duplicatefor (int k = 0; k < stu[i]._num_elective; k++) {if (strcmp(stu[i]._cour_elective[k].id, cour[j].id) == 0) {cout << "Already enrolled in this elective course." << endl;system("pause");return;}}// Add coursestu[i]._cour_elective[stu[i]._num_elective] = cour[j];stu[i]._num_elective++;cour[j].count++; // 只增加一次}// 统一保存数据Write_course(cour, n_cour);Write_stu(stu, n_stu);cout << "Enrollment successful! Remaining slots: " << cour[j].remaining() << endl;system("pause");
}void Deselect(vector<Course>& cour, vector<Student>& stu)
{system("cls");int n_cour = Read_course(cour);int n_stu = Read_stu(stu);int i = 0; // studentint j = 0; // coursechar id_c[20]; // course char id_s[20]; // student cout << "============ deselect system ============" << endl;cout << "please input the ID of student: ";cin >> id_s;while (i < n_stu && strcmp(stu[i]._id, id_s))i++;if (i == n_stu){cout << "no found." << endl;system("pause");return;}cout << ("please input the ID of course you want to deselect: ");cin >> id_c;while (j < n_cour && strcmp(cour[j].c_id(), id_c))j++;if (j == n_cour){cout << "no found." << endl;system("pause");return;}if (stu[i]._num_compulsory){int c = stu[i]._num_compulsory - 1;while (c >= 0){if (!strcmp(stu[i]._cour_compulsory[c].id, id_c)){cour[j].count--;stu[i]._num_compulsory--;strcpy(stu[i]._cour_compulsory[c].id, "None");strcpy(stu[i]._cour_compulsory[c].name, "None");// 重新排序for (; c < stu[i]._num_compulsory; c++)swap(stu[i]._cour_compulsory[c], stu[i]._cour_compulsory[c + 1]);Write_stu(stu, n_stu);Write_course(cour, n_cour);cout << "退选成功。" << endl;system("pause");return;}c--;}if (stu[i]._cour_elective){int c = stu[i]._num_elective - 1;while (c >= 0){if (!strcmp(stu[i]._cour_elective[c].id, id_c)){cour[j].count--;stu[i]._num_elective--;strcpy(stu[i]._cour_elective[c].id, "None");strcpy(stu[i]._cour_elective[c].name, "None");for (; c < stu[i]._num_elective; c++)swap(stu[i]._cour_elective[c], stu[i]._cour_elective[c + 1]);Write_stu(stu, n_stu);Write_course(cour, n_cour);cout << "退选成功。" << endl;system("pause");return;}c--;}cout << "你并未选择该课程。" << endl;system("pause");return;}}if (stu[i]._cour_elective){int c = stu[i]._num_elective - 1;while (c >= 0){if (!strcmp(stu[i]._cour_elective[c].id, id_c)){cour[j].count--;stu[i]._num_elective--;strcpy(stu[i]._cour_elective[c].id, "None");strcpy(stu[i]._cour_elective[c].name, "None");for (; c < stu[i]._num_elective; c++)swap(stu[i]._cour_elective[c], stu[i]._cour_elective[c + 1]);Write_stu(stu, n_stu);Write_course(cour, n_cour);cout << "退选成功。" << endl;system("pause");return;}c--;}cout << "你并未选择该课程。" << endl;system("pause");return;}
}
点击查看代码
void menu_course() {int c = 0;while (1) {system("cls");cout << "============ COURSE MANAGEMENT ============" << endl;cout << " 1. Add Course " << endl; // 原Input改为Addcout << " 2. Delete Course " << endl;cout << " 3. Modify Course " << endl;cout << " 4. Lookup Course " << endl;cout << " 5. Show All Courses " << endl;cout << " 0. Return " << endl;cout << "-------------------------------------------" << endl;cout << "Choose operation (0-5): ";cin >> c;switch (c) {case 1:if (flag)Add_course(cour); // Input_course → Add_courseelse {cout << "Permission denied." << endl;system("pause");}break;case 2:if (flag)Delete_course(cour);else {cout << "Permission denied." << endl;system("pause");}break;case 3:if (flag)Modify_course(cour);else {cout << "Permission denied." << endl;system("pause");}break;case 4:Lookup_course(cour);break;case 5:Show_course(cour);break;case 0:cout << "Returning to main menu..." << endl;system("pause");return;default:cout << "Invalid input! Please enter 0-5." << endl;system("pause");break;}}
}void menu_student() {int c = 0;while (1) {system("cls");cout << "============ STUDENT MANAGEMENT ============" << endl;cout << " 1. Add Student " << endl; // 原Input改为Addcout << " 2. Delete Student " << endl;cout << " 3. Modify Student " << endl;cout << " 4. Lookup Student " << endl;cout << " 5. Show All Students " << endl;cout << " 6. Select Course " << endl;cout << " 7. Deselect Course " << endl;cout << " 0. Return " << endl;cout << "--------------------------------------------" << endl;cout << "Choose operation (0-7): ";cin >> c;switch (c) {case 1:if (flag)Add_stu(stu); // Input_stu → Add_stuelse {cout << "Permission denied." << endl;system("pause");}break;case 2:if (flag)Delete_stu(stu);else {cout << "Permission denied." << endl;system("pause");}break;case 3:if (flag)Modify_stu(stu);else {cout << "Permission denied." << endl;system("pause");}break;case 4:Lookup_stu(stu);break;case 5:Show_stu(stu);break;case 6:Select(cour, stu);break;case 7:Deselect(cour, stu);break;case 0:cout << "Returning to main menu..." << endl;system("pause");return;default:cout << "Invalid input! Please enter 0-7." << endl;system("pause");break;}}
}
下面是修改后程序运行的结果:
菜单栏修改:
按照上述操作对该系统进行修改,能够更好地避免函数功能混淆,并增加课余量这一有效变量来直观地反映课程所剩余量供学生参考。
(5)实践心得
通过本次的小实验,我收获了很多。通过花费时间对整个系统进行剖析并发现其中存在的不足,我更为透彻地理解整个逆向工程的具体步骤。同时,我在实践过程中对代码进行大量的调试与修改,也增强了我对代码的理解,提升了我的耐心。另外,逆向工程实践不仅让我掌握了代码分析与重构的核心技能,更深刻理解了系统健壮性和可维护性的重要性。