《软件开发与创新课程设计》第一次课后作业——对学生选课系统的改进

news/2025/2/22 20:02:30/文章来源:https://www.cnblogs.com/ldgdd/p/18731310

(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;
}
运行环境:操作系统:Win11 IDE:VS2019

该系统的大致功能如下:
管理员:课程增删改查、学生信息管理
学生:选课/退选、查看已选课程
数据存储: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;}
}
针对input功能冗余问题,需要将input_course和input_stu这两个函数删掉,并相应地修改menu栏,具体修改如下:
点击查看代码
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)实践心得
通过本次的小实验,我收获了很多。通过花费时间对整个系统进行剖析并发现其中存在的不足,我更为透彻地理解整个逆向工程的具体步骤。同时,我在实践过程中对代码进行大量的调试与修改,也增强了我对代码的理解,提升了我的耐心。另外,逆向工程实践不仅让我掌握了代码分析与重构的核心技能,更深刻理解了系统健壮性和可维护性的重要性。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.hqwc.cn/news/888121.html

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

pikachu unsafe Fileupload

在上传点上传非法文件,提示上传文件不符合要求,且BP没有新的数据包产生,判断为客户端检查禁用浏览器JavaScript后刷新网页,再次上传文件,提示上传成功,文件路径为uploads/test.phpedge: 设置->Cookie和网站权限->所有权限->Javascript->禁用 Chorme:设置-&g…

rust学习十九.1、模式匹配(match patterns)

本章节大概是书本上比较特殊一个,因为它没有什么复杂的内容,通篇主要讨论模式匹配的语法。 一、两个名词a.可反驳 - refutable 对某些可能的值进行匹配会失败的模式被称为是 可反驳的(refutable) let Some(x) = some_option_value;如果 some_option_value 的值是…

大对数电缆打线顺序

5种线缆主色:白色、红色、黑色、黄色、紫色 5种线缆配色:蓝色、橙色、绿色、棕色、灰色 25对电话电缆色谱线序表30对电话电缆色谱线序 这里要特别说明下:30对的电话电缆要注意了,30对通信电缆里有2种白色的主色,大于25对了就一定要看标识线了!!有一小把是用“白蓝"…

01-springsecurity数据库登录

01 - SpringSecurity实现数据库登录 环境: springboot 3.4.2, springsecurity 6.4.2, mybatis 3.0.4springsecurity中的UserDetails接口用于表示用户信息, 包含用户名、密码等信息。UserDetailsService接口用于加载用户信息, 里边就这一个方法 public interface UserDetailsSer…

【喜与悲】- 2025.2.22 晚

下图为《Balatro》中的一张小丑牌:【喜与悲】喜与悲可以重新触发所有打出的人头牌,是重新触发家族中的一员。但其特性也决定了其强度方差极大,有配合则强度很高,没有配合则纯浪费小丑位。但很少有小丑能与其配合,而能与其配合的小丑大多单独拎出来又不强。更多时候其几乎只…

莫队算法学习笔记

莫队算法的发明者是一个叫做 莫涛 的人,所以被称为莫队算法,简称莫队。 英语 Mos algorithm。 使用场景 莫队算法常常被用来处理多次区间询问的问题,离线处理询问(必须满足!!!)。 插叙:离线是一种得到所有询问再进行计算的方法,是很重要的思想。 对于这种“区间询问”…

参数-返回值-局部变量-数组

参数和局部变量没有本质区别,都是栈中的数据 参数时在函数调用前分配的值,局部变量是在函数调用时分配的值 参数 ebp+* 局部变量 ebp-* 赋值的本质是把运算结果放到某个内存里数组: 一堆连续存储的等宽数据

详细介绍java的线程池状态

一、详细介绍java的线程池状态 Java 中的线程池状态是 ThreadPoolExecutor 类内部管理的一个重要概念。线程池的状态决定了线程池的行为,例如是否接受新任务、是否处理队列中的任务、是否中断正在执行的任务等。 线程池的状态通过一个 AtomicInteger 变量(ctl)来表示,该变量…

[Java SE] Java静态代码块与静态属性的执行顺序

序 重要结论先说结论,再去观察实验现象,印证结论。静态变量初始化和静态代码块的执行顺序是:按照它们在类中出现的顺序进行的。代码实验 实验1import org.slf4j.Logger; import org.slf4j.LoggerFactory;public class JavaStaticTest {private final static String VAR = &qu…

PyTorch TensorBoard 使用

这篇文章介绍如何在 PyTorch 中使用 TensorBoard 记录训练数据。 记录数据 初始化 在程序启动时创建 SummaryWriter 对象用于写入日志数据。 from torch.utils.tensorboard import SummaryWriter import datetime# 获取当前时间戳,一般以时间戳作为记录文件夹名称的一部分 tim…

Svelte 最新中文文档教程(16)—— Context(上下文)

前言 Svelte,一个语法简洁、入门容易,面向未来的前端框架。从 Svelte 诞生之初,就备受开发者的喜爱,根据统计,从 2019 年到 2024 年,连续 6 年一直是开发者最感兴趣的前端框架 No.1:Svelte 以其独特的编译时优化机制著称,具有轻量级、高性能、易上手等特性,非常适合构…

微信小程序-授权获取手机号

前端 wxml <button name=phone class=phone value={{userInfo.phone}} wx:if="{{!userInfo.phone}}" bindgetphonenumber="getPhoneNumber" hover-class=none open-type=getPhoneNumber>点击获取 </button>js import { wxGetPhoneNumber } fr…