C++大作业——学生选课系统
创始人
2025-01-16 14:34:38
0

C++大作业——学生选课系统

  • 前言
  • 1.系统概述
  • 2.系统设计
    • 2.1概要设计
    • 2.2详细设计
      • 2.2.1学生类和课程类模块设计
      • 2.2.2文件信息读取和录入模块设计
      • 2.2.3增删改查模块设计
      • 2.2.4登入模块设计
      • 2.2.5选课模块设计
      • 2.2.6退选模块设计
  • 3.完整代码
  • 4.总结与展望
    • 4.1设计总结
    • 4.2设计展望

前言


本次课程设计是我第一次独自完成这样一个相对完整的项目,收获很多。但是我更想说一下本次设计中存在的不足。

首先就是在最初设计课程类和学生类时,没有考虑的那么全面,导致出现了很多冗余的接口,和公有私有混着用的情况。其次就是,虽然是用C++写的,却没有把封装体现出来,使用了很多friend友元函数也破坏了类的封装效果。

由于我最开始是在Linux系统上编写的代码,没有装中文包,所以很多注释和输出语句都是用英文写的。后来又转到Windows下的VS环境下编程,也写了一些中文的语句。导致会有中英文混用的情况,可能会影响观感。

在学习这个系统之前,建议先把学生成绩管理系统熟练掌握,掌握最基本的增删改查操作,我在本文中就不再赘述了,把重点放在选课和退选的功能讲解上。

在最后面,我也会把整个项目代码都放出来,大家可以拿去在自己的电脑上运行,应付下作业铁定是没问题的(手动狗头)。


1.系统概述


本系统是学生选课系统,主要是实现大学生的选课需求。进入系统,需要先选择用户(学生或管理员),选择管理员,需要输入对应的密码。选择完身份后,进入主菜单,会出现两个子菜单,学生菜单和课程菜单,选择相应的选项,就会进入相应的菜单,其中0选项是返回上级目录。

进入课程菜单,会看到增删改查相应的操作,还有显示全部课程信息。其中,学生用户没有对课程进行增删改的权利。管理员是超级用户,有所有操作的权利。里面的细节我们在模块的具体实现时再说。

进入学生菜单,也会看到增删改查相关的操作,还有显示全部学生的信息。不同的是,学生菜单中可以进行选课和退选的相关操作。学生用户只有查看信息,选课退选的权利,没有增删改的权利。具体细节也是放在模块中详谈。


2.系统设计


2.1概要设计


在这里插入图片描述


2.2详细设计


2.2.1学生类和课程类模块设计


int flag = 0;                   // default is student  class Course  { public:     friend void Input_course(vector& cour);     friend void Delete_course(vector& cour);     friend void Modify_course(vector& cour);     friend void Lookup_course(vector& cour);     friend void Show_course(vector& cour);     friend void Add_course(vector& cour);                                                                            friend int Read_course(vector& cour);     friend void Write_course(vector& 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 compulsory     int hour;               // class time                                                                                  int credir;             // credit public:     int count;              // count of student 0-60 };  class Student { public:     friend void Input_stu(vector& stu);     friend void Delete_stu(vector& stu);     friend void Modify_stu(vector& stu);     friend void Lookup_stu(vector& stu);     friend void Add_stu(vector& stu);     friend void Show_stu(vector& stu);     friend int Read_stu(vector& stu);     friend void Write_stu(vector& stu, int n);      friend void Select(vector& cour, vector& stu);     friend void Deselect(vector& cour, vector& 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 cour; vector stu; 

在这个模块中,我设计了两个类来分别存储学生和课程的信息。课程的相关信息包括,课程编号、课程名、课程类型(选修/必修)、学时、学分、以及选课人数。其中规定,每门课选课人数最多不超过60人。还设计了一个构造函数,将课程的编号和课程名都定义为None

学生类中,相关信息包括,学号、班级、姓名、所选择的必修课信息、所选的选修课信息、所选必修课数量、所选选修课数量。其中规定,每名学生所选必修课不超过两门,选修课不超过一门。

在接下来的增删查改操作中,我使用了全局变量cour,和stu这两个动态数组来分别存储课程和学生。其中全局变量flag是一个确定身份状态的变量,默认是0,学生用户。1是超级用户。


2.2.2文件信息读取和录入模块设计


void Write_course(vector& 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& cour) {     cour.clear();                           // clear     fstream myfile;     myfile.open("course.txt", ios::in | ios::binary);     if (!myfile)     {         cout << "ERROR!course.txt can't open." << endl;         abort();     }      int n;                                  // count of course     myfile.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 Write_stu(vector& 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& stu) {     stu.clear();                            // clear     fstream 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; } 

这里,我利用了两个文件,一个course.txt来存放课程信息,一个student.txt来存放学生信息。写入文件的第一个数据一定是课程,学生的数量。在调用Write函数要传一个数量进入。

需要注意,在向文件中写入学生的信息时,我并没有将学生成员变量中,_cour_compulsory_cour_elective数组中的信息全部写入,只是写入的学生所选课程的编号和课程名。


2.2.3增删改查模块设计


void Input_course(vector& 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 0          cour.push_back(temp);           // push_back          cout << "whether continue(y or n): ";         cin >> sign;         i++;     }      Write_course(cour, i);                  // save }  void Delete_course(vector& 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]);      // save     char 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& cour) {     system("cls");     int n = Read_course(cour);                      // count of course     char 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;      // save     char 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& 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& 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& 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;     }      // save     Write_course(cour, n); } 

Input函数只适用于第一次数据的录入,如果文件中有数据,使用Input函数会将原内容覆盖。追加课程使用函数Add

关于课程的增删改查,没有太多要说的点,需要注意一个细节,就是如果该课程的选课人数不为零,那么是不允许对该课程进行修改或删除操作的,需要先联系选该课的学生全部退选,才可以进行修改或删除,不然就太坑了。

删除课程时,我使用了算法库中的函数swap,这个函数的参数是一个模版参数,可以交换任意类型的数据。

void Input_stu(vector& 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_back         cout << "whether continue(y or n): ";         cin >> sign;         i++;     }     Write_stu(stu, i); }  void Delete_stu(vector& stu) {     system("cls");     int n = Read_stu(stu);     int m = Read_course(cour);  // number of course     int 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]);      // save     char 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& 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;      // save     char 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& 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& 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_back          cout << "whether continue(y or n): ";         cin >> sign;     }      // save     Write_stu(stu, n); }  void Show_stu(vector& 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"); } 

以上是学生信息的增删查改,细节较多,在这里一一说明。首先,删除学生时,如果该学生选的有课,那么将学生删除后,对应课程的选课人数也应该减减;其次,打印学生所选课程时,需要先判断课程的id是否为None,如果是就打印None,表示没有选课,id不为None就说明选的有课,就打印该课程的课程名。


2.2.4登入模块设计


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;         }     }     else         flag = 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");         }         else             break;     }      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; } 

可以看到,在进入系统后,需要先选择用户,如果选择管理员身份登入需要输入密码,选择学生身份则不用。随后会进入主菜单,有三个选项,1.课程菜单,2.学生菜单,0.返回上级目录。选择1或2就会跳转到相应的子菜单,选择0则会回到选择用户身份的界面。进入子菜单后,可以看到,课程菜单和学生菜单的前几个选项是一样的,唯一不同的是学生菜单中多了两个选项,选课和退选。

选择身份界面:

在这里插入图片描述

主菜单:

在这里插入图片描述

子菜单:

在这里插入图片描述

在这里插入图片描述


2.2.5选课模块设计


void Select(vector& cour, vector& stu)  {     system("cls");     int n_cour = Read_course(cour);     int n_stu = Read_stu(stu);        int i = 0;              // student     int j = 0;              // course      char 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++;       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];                   stu[i]._num_compulsory++;                 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");             }         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");             }         }     } } 

需要先从文件中读取到数据,然后需要输入学生id,确定身份,再输入你想选择的课程编号。如果找不到该学生或课程,就会打印no found.。找到之后,先判断的就是该课程的选课人数是否达到上限60人,如果达到,就会提示人数上限,选课失败,没达到,就进入下一环。接着判断所选择的课程类型,是必修,就判断该学生选择的必修课程数量是否达到上限,没有达到上限才会提示选课成功;是选修,就判断该学生选择的选修课数量是否达到上限,没有达到才会提示选课成功。


2.2.6退选模块设计


void Deselect(vector& cour, vector& stu) {     system("cls");     int n_cour = Read_course(cour);     int n_stu = Read_stu(stu);     int i = 0;              // student     int j = 0;              // course      char 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;   	} } 

退选功能实现的前半部分和选课功能类似,先从文件中读取信息,输入学生id,确定身份,再输入想退选的课程id,确定要退选的课程是否存在。如果存在,接着判断该学生是否选择了该课程,只有选择了该课程,才会提示退选成功,否则提示退选失败。

退选功能中有一个细节需要尤其注意,就是每回退选课程,都需要对学生信息中的课程数组重新排序,相当于在学生信息的课程数组中,删除了这个课程。为什么要排序呢?因为我在实现选课和退选功能的时候,都是用学生成员变量中的_num_compulsory_num_elective来做课程数组的下标的,如果不进行重新排序,会导致逻辑混乱。举个例子,现在有一个学生选了两门必修课,_cour_compulsory[0]存的是第一门课,_cour_compulsory[1]存的是第二门课,那_num_compulsory毫无疑问就是2。现在我们退选第一门课,也就是将_cour_compulsory[0]的数据改成默认值,idNonename也是None,再对_num_compulsory减减。现在没有进行排序,此时_num_compulsory为1,再对该学生进行选课操作时,执行代码stu[i]._cour_compulsory[stu[i]._num_compulsory] = cour[j]; 会将_cour_compulsory[1]中的有效数据覆盖,而_cour_compulsory[0]中仍然是无效数据,出现逻辑错误。所以我们需要对数组重新排序,让_cour_compulsory[1]的先值给到_cour_compulsory[0]


3.完整代码


#define _CRT_SECURE_NO_WARNINGS  #include #include #include #include   #include using namespace std;  int flag = 0;                   // default is student  class Course  { public:     friend void Input_course(vector& cour);     friend void Delete_course(vector& cour);     friend void Modify_course(vector& cour);     friend void Lookup_course(vector& cour);     friend void Show_course(vector& cour);     friend void Add_course(vector& cour);                                                                            friend int Read_course(vector& cour);     friend void Write_course(vector& 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 compulsory     int hour;               // class time                                                                                  int credir;             // credit public:     int count;              // count of student 0-60 };  class Student { public:     friend void Input_stu(vector& stu);     friend void Delete_stu(vector& stu);     friend void Modify_stu(vector& stu);     friend void Lookup_stu(vector& stu);     friend void Add_stu(vector& stu);     friend void Show_stu(vector& stu);     friend int Read_stu(vector& stu);     friend void Write_stu(vector& stu, int n);      friend void Select(vector& cour, vector& stu);     friend void Deselect(vector& cour, vector& 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 cour; vector stu;  // ========================================== Course ==================================================== //  void Write_course(vector& 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& cour) {     cour.clear();                           // clear     fstream myfile;     myfile.open("course.txt", ios::in | ios::binary);     if (!myfile)     {         cout << "ERROR!course.txt can't open." << endl;         abort();     }      int n;                                  // count of course     myfile.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& 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 0          cour.push_back(temp);           // push_back          cout << "whether continue(y or n): ";         cin >> sign;         i++;     }      Write_course(cour, i);                  // save }  void Delete_course(vector& 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]);      // save     char 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& cour) {     system("cls");     int n = Read_course(cour);                      // count of course     char 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;      // save     char 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& 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& 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& 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;     }      // save     Write_course(cour, n); }  // ============================================ student ============================================== //  void Write_stu(vector& 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& stu) {     stu.clear();                            // clear     fstream 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& 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_back         cout << "whether continue(y or n): ";         cin >> sign;         i++;     }     Write_stu(stu, i); }  void Delete_stu(vector& stu) {     system("cls");     int n = Read_stu(stu);     int m = Read_course(cour);  // number of course     int 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]);      // save     char 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& 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;      // save     char 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& 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& 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_back          cout << "whether continue(y or n): ";         cin >> sign;     }      // save     Write_stu(stu, n); }  void Show_stu(vector& 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& cour, vector& stu)  {     system("cls");     int n_cour = Read_course(cour);     int n_stu = Read_stu(stu);        int i = 0;              // student     int j = 0;              // course      char 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& cour, vector& stu) {     system("cls");     int n_cour = Read_course(cour);     int n_stu = Read_stu(stu);     int i = 0;              // student     int j = 0;              // course      char 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;         }     }     else         flag = 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");         }         else             break;     }      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; } 

大家可以自行复制运行。


4.总结与展望


4.1设计总结


本次课程设计是我第一次独自完成这样一个相对完整的项目,收获很多。但是我更想说一下本次设计中存在的不足。首先就是在最初设计课程类和学生类时,没有考虑的那么全面,导致出现了很多冗余的接口,和公有私有混着用的情况。其次就是,虽然是用C++写的,却没有把封装体现出来,使用了很多friend友元函数也破坏了类的封装效果。


4.2设计展望


接下来,我打算再优化一下这个系统,首先就是从友元函数入手,减少友元。我打算将增删改查都设计成成员函数,这样函数的命名也会方便很多。

然后就是将用到字符串的地方,全部使用string代替,可以避免使用很多C中的字符串操作函数。我还准备设计一下学生类和课程类的输入输出流重载,这样也能让代码量减少很多。

以上就是我对本次程序设计实习的设计展望。谢谢大家的支持!


相关内容

热门资讯

二次私人局wepoke线上技巧... 【福星临门,好运相随】;二次私人局wepoke线上技巧(开挂)wpk辅助器安装(2025已更新)(哔...
玩家攻略(德州智星菠萝)软件透... 玩家攻略(德州智星菠萝)软件透明挂(辅助挂)透明辅助挂脚本(2022已更新)(哔哩哔哩);德州智星菠...
五分钟小程序微扑克辅助钻石(透... 五分钟小程序微扑克辅助钻石(透视挂)wepoke苹果版外挂(2022已更新)(哔哩哔哩);相信小伙伴...
七次靠谱微扑克线上代打工具(脚... 七次靠谱微扑克线上代打工具(脚本)wpk机器人代打(2021已更新)(哔哩哔哩);1.微扑克 ai辅...
5分钟外挂wepoke系统是免... 5分钟外挂wepoke系统是免费的的(开挂)wpk微扑克智能辅助(2024已更新)(哔哩哔哩);亲真...
七分钟德州版微扑克wpk辅助存... 七分钟德州版微扑克wpk辅助存在的(插件)wpk 辅助工具(2023已更新)(哔哩哔哩);详细微扑克...
6分钟漏洞wepoke人有挂的... 6分钟漏洞wepoke人有挂的(工具)微扑克ai辅助(2024已更新)(哔哩哔哩);是一款可以让一直...
八分钟德州局wpk外挂存在的(... 八分钟德州局wpk外挂存在的(开挂)wepoke免费软件透明挂(2022已更新)(哔哩哔哩);最新版...
力荐辅助(德扑之星)软件透明挂... 力荐辅助(德扑之星)软件透明挂(辅助挂)外挂透明挂辅助器(2022已更新)(哔哩哔哩);1、点击下载...
四分钟最新款wpk德州插件(脚... 【福星临门,好运相随】;四分钟最新款wpk德州插件(脚本)微扑克发牌机制(2025已更新)(哔哩哔哩...