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

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

  • 前言
  • 1.学生类和课程类的实现
  • 2.输入输出流重载的实现
  • 3.增删改查的实现
  • 4.多级菜单的实现
  • 5.选课和退选的实现
  • 5.完整代码

前言


本文是对本人之前写过的一个学生选课系统的优化,整体上的逻辑是和我上一篇博客一样的(链接在此:https://blog.csdn.net/weixin_73870552/article/details/135298985?spm=1001.2014.3001.5501)。本文将具体介绍优化的部分,而不对基本的实现做过多阐述。


1.学生类和课程类的实现


课程类:

class Course
{
	// 输入输出流重载
    friend ostream& operator<<(ostream& out, const Course& c);
    friend fstream& operator<<(fstream& fout, const Course& c);
    // friend istream& operator>>(istream& in, Course& c);        这个重载不好实现,因为要查重
    friend fstream& operator>>(fstream& fin, Course& c);

    friend ostream& operator<<(ostream& out, const Student& s);

public:
	// 构造函数初始化
    Course()
        : _id("None")
        , _name("None")
        , _type(0)
        , _hour(0)
        , _credir(0)
        , _count(0)
    {}

	// 相关接口
    const string c_id() { return _id; }                   // 编号的接口
    const string c_name() { return _name; }               // 名字的接口
    int& c_count() { return _count; }                     // 选课人数的接口
    const bool c_type() { return _type; }                 // 类型接口

	// 文件操作
    void Write(vector<Course>& cour, int n) {}		// 向文件写入数据
    int Read(vector<Course>& cour) {}				// 从文件读取数据

    void Add(vector<Course>& cour) {}				// 增
    void Delete(vector<Course>& cour) {}			// 删
    void Modify(vector<Course>& cour) {}			// 改
    void Lookup(vector<Course>& cour) {}			// 查
    void Show(vector<Course>& cour) {}				// 展示所有信息

private:
    string _id;         // 编号
    string _name;       // 课程名
    bool _type;         // 课程类型,1必修,0选修
    int _hour;          // 学时
    int _credir;        // 学分
    int _count;         // 选课人数
};

1.这个课程类的成员变量中记录了课程的基本信息,其中编号和课程名,这种有字符串属性的数据,我使用了string来存储。方便后续直接使用==!=进行字符串的比较。

2.写了构造函数用来初始化数据。

3.将对课程的操作都写成成员函数,封装起来。后续的操作函数使用涉及到匿名对象,不了解的同学可以看下我之前的博客,这里就不再过多赘述了。

4.设计了一些接口,以便可以在类外访问私有成员变量。其中只有选课人数的接口是没有const修饰的,因为要在类外对其进行修改。

5.声明了输入输出流的重载,用来简化代码。学生类的输出流重载也要在课程类中友元声明一下,因为后续要在这个重载函数中访问课程的一些私有变量,且在这个重载函数中接口会失效。

学生类:

class Student
{
	// 输入输出流重载
    friend ostream& operator<<(ostream& out, const Student& s);
    friend fstream& operator<<(fstream& fout, const Student& s);
    // friend istream& operator>>(istream& in, Student& s);        这个重载不好实现,因为要查重
    friend fstream& operator>>(fstream& fin, Student& s);
public:
    Student()
        : _id("None")                       // 学号
        , _class("None")                    // 班级
        , _name("None")                     // 姓名
        , _num_compulsory(0)                
        , _num_elective(0)
    {}

    void Write(vector<Student>& stu, int n) {}
    int Read(vector<Student>& stu) {}

    void Add(vector<Student>& stu) {}
    void Delete(vector<Student>& stu) {}
    void Modify(vector<Student>& stu) {}
    void Lookup(vector<Student>& stu) {}
    void Show(vector<Student>& stu) {}
    void Select(vector<Course>& cour, vector<Student>& stu) {}		// 选课
    void Deselect(vector<Course>& cour, vector<Student>& stu) {}	// 退选

private:
    string _id;                                 // 学号
    string _class;                              // 班级
    string _name;                               // 名字
    Course _cour_compulsory[SIZE_COMP];         // 必修课信息
    Course _cour_elective[SIZE_ELECT];          // 选修课信息
    int _num_compulsory;                        // 已选必修课数量
    int _num_elective;                          // 已选选修课数量
};

1.学生类的成员变量记录了学生的基本信息。其中,课程的信息我用两个数组存了起来,SIZE_COMPSIZE_ELECT是两个宏,分别对应学生所能选择的必修课和选修课最大数量。

2.学生相关的操作函数作为成员函数,写在Studnet类中,相比课程类多了选课和退选的模块。

3.写了构造函数来初始化数据。

4.声明了输入输出流重载,简化代码。


2.输入输出流重载的实现


课程类的输入输出重载:

ostream& operator<<(ostream& out, const Course& c)
{
    out << c._id   << "\t" << c._name   << "\t" << (c._type ? "必修" : "选修") << "\t"
        << c._hour << "\t" << c._credir << "\t" << c._count;
    return out;
}

fstream& operator<<(fstream& fout, const Course& c)
{
    fout << c._id   << "\t" << c._name   << "\t" << c._type << "\t"
         << c._hour << "\t" << c._credir << "\t" << c._count;
    return fout;
}

fstream& operator>>(fstream& fin, Course& c)
{
    fin >> c._id   >> c._name   >> c._type
        >> c._hour >> c._credir >> c._count;
    return fin;
}

要写一个io流的输出重载,还要写一个文件流的输入输出重载。由于课程类在输入信息时还需要判断课程的编号是否重复,输入流重载略显复杂,还不如直接输入,所以这里就不再实现了。

学生类的输入输出重载:

ostream& operator<<(ostream& out, const Student& s)
{
    out << s._id << "\t" << s._class << "\t" << s._name << "\t";
   for (int i = 0; i < SIZE_COMP; i++)
       out << s._cour_compulsory[i]._name << " ";
   out << "\t";
   for (int i = 0; i < SIZE_ELECT; i++)
       out << s._cour_elective[i]._name << " ";
    return out;
}

fstream& operator<<(fstream& fout, const Student& s)
{
    fout << s._id << "\t" << s._class << "\t" << s._name << "\t";
    for (int i = 0; i < SIZE_COMP; i++)
        fout << s._cour_compulsory[i] << "\t";
    for (int i = 0; i < SIZE_ELECT; i++)
        fout << s._cour_elective[i] << "\t";
    fout << s._num_compulsory << "\t" << s._num_elective;
    return fout;
}

fstream& operator>>(fstream& fin, Student& s)
{
    fin >> s._id >> s._class >> s._name;
    for (int i = 0; i < SIZE_COMP; i++)
        fin >> s._cour_compulsory[i];
    for (int i = 0; i < SIZE_ELECT; i++)
        fin >> s._cour_elective[i];
    fin >> s._num_compulsory >> s._num_elective;
    return fin;
}

学生类的io流输入和课程类同理,要判断学号是否重复,所以这里不再实现重载。应注意,学生类的io流输出重载中,课程信息只输出课程名;学生类的文件流输入输出重载中,复用了课程类的文件输入输出重载。


3.增删改查的实现


实现增删查改,需要借助两个全局的动态数组。vector<Course> courvector<Student> stu

课程类:

class Course
{
    friend ostream& operator<<(ostream& out, const Course& c);
    friend fstream& operator<<(fstream& fout, const Course& c);
    // friend istream& operator>>(istream& in, Course& c);        这个重载不好实现,因为要查重
    friend fstream& operator>>(fstream& fin, Course& c);

    friend ostream& operator<<(ostream& out, const Student& s);

public:
    Course()
        : _id("None")
        , _name("None")
        , _type(0)
        , _hour(0)
        , _credir(0)
        , _count(0)
    {}

    const string c_id() { return _id; }                   // 编号的接口
    const string c_name() { return _name; }               // 名字的接口
    int& c_count() { return _count; }                     // 选课人数的接口
    const bool c_type() { return _type; }                 // 类型接口


    void Write(vector<Course>& cour, int n)
    {
        fstream myfile;
        myfile.open("course.txt", ios::out | ios::binary);
        if (!myfile)
        {
            cout << "course.txt can't open." << endl;
            abort();
        }

        myfile << n << endl << endl;
        for (int i = 0; i < n; i++)
            myfile << cour[i] << endl;

        myfile.close();
    }

    int Read(vector<Course>& cour)
    {
        cour.clear();
        fstream myfile;
        myfile.open("course.txt", ios::in | ios::binary);
        if (!myfile)
        {
            cout << "course.txt can't open." << endl;
            abort();
        }

        int n;
        myfile.seekg(0);
        myfile >> n;

        Course temp;
        for (int i = 0; i < n; i++)
        {
            myfile >> temp;
            cour.push_back(temp);
        }

        myfile.close();
        return n;
    }

    void Add(vector<Course>& cour)
    {
        system("cls");
        int n = Read(cour);
        char sign = '0';
        Course temp;

        cout << "============ 增加课程信息 ============" << endl;
        while (sign != 'n' && sign != 'N')
        {
        loop:
            cout << "ID: ";
            cin >> temp._id;

            // 查重
            int c = 0;
            while (c < n)
            {
                c++;
                if (temp._id == cour[n - c]._id)
                {
                    cout << "编号重复,请重新输入。" << endl;
                    goto loop;
                }
            }

            cout << "课程名: ";
            cin >> temp._name;
            cout << "课程类型(1必修,0选修): ";
            cin >> temp._type;
            cout << "学时: ";
            cin >> temp._hour;
            cout << "学分: ";
            cin >> temp._credir;
            
            n++;

            cour.push_back(temp);

            cout << "是否继续添加(y 或 n)";
            cin >> sign;
        }
        // 保存进文件
        Write(cour, n);
    }

    void Delete(vector<Course>& cour)
    {
        system("cls");
        int n = Read(cour);
        int i = 0;
        string id;

        cout << "============ 删除课程 ============" << endl;
        cout << "请输入需要删除课程的编号: ";
        cin >> id;

        // 检查有没有这个课程
        while (i < n && id != cour[i]._id)
            i++;

        if (i == n)
        {
            cout << "没找到该课程。" << endl;
            system("pause");
            return;
        }
        else
        {
            if (cour[i]._count)
            {
                cout << "所选人数不为零,请联系所有学生退选后,再删除该课程。" << endl;
                system("pause");
                return;
            }
        }

        for (int j = i; j < n - 1; j++)
            cour[j] = cour[j + 1];              // 编译器自动生成的赋值运算符重载

        // 保存
        char sign;
        cout << "是否保存(y 或 n): ";
        cin >> sign;
        if (sign != 'n' && sign != 'N')
        {
            cour.erase(cour.end() - 1);
            Write(cour, n - 1);
            cout << "============ 删除成功 ============" << endl;
            system("pause");
        }
    }

    void Modify(vector<Course>& cour)
    {
        system("cls");
        int n = Read(cour);
        string id;
        int i = 0;

        cout << "============ 修改课程信息 ============" << endl;
        cout << "请输入你想修改的课程id: ";
        cin >> id;

        // 看有没有这个课程
        cin >> id;
        while (i < n && id != cour[i]._id)
            i++;

        if (i == n)
        {
            cout << "没有找到该课程。" << endl;
            system("pause");
            return;
        }
        else
        {
            if (cour[i]._count)
            {
                cout << "所选人数不为零,请联系所有学生退选后,再修改课程信息。" << endl;
                system("pause");
                return;
            }
        }

        cout << "-------------------------------------------------" << endl;
        cout << "编号" << "\t" << "课程名" << "\t" << "类型"     << "\t"
             << "学时" << "\t" << "学分"   << "\t" << "选课人数" << endl;
        cout << cour[i] << endl;
        cout << "-------------------------------------------------" << endl;

        cout << "请重新输入课程信息。" << endl;
        cout << "课程名: ";
        cin >> cour[i]._name;
        cout << "课程类型(1必修,0选修): ";
        cin >> cour[i]._type;
        cout << "学时: ";
        cin >> cour[i]._hour;
        cout << "学分: ";
        cin >> cour[i]._credir;

        // 保存
        char sign;
        cout << "是否保存修改(y 或 n): ";
        cin >> sign;
        if (sign != 'n' && sign != 'N')
        {
            Write(cour, n);
            cout << "============ 修改成功 ============" << endl;
            system("pause");
        }
    }

    void Lookup(vector<Course>& cour)
    {
        system("cls");
        int n = Read(cour);
        int i = 0;
        string id;
        
        cout << "============ 查找课程 ============" << endl;
        cout << "请输入要查找课程的编号: ";
        cin >> id;

        // 检查是否存在
        while (i < n && id != cour[i]._id)
            i++;

        if (i == n)
        {
            cout << "没有找到该课程。" << endl;
        }
        else
        {
            cout << "-------------------------------------------------" << endl;
            cout << "编号" << "\t" << "课程名" << "\t" << "类型"     << "\t"
                 << "学时" << "\t" << "学分"   << "\t" << "选课人数" << endl;
            cout << "-------------------------------------------------" << endl;
            cout << cour[i] << endl;
        }

        system("pause");
    }

    void Show(vector<Course>& cour)
    {
        system("cls");
        int n = Read(cour);

        cout << "============ 展示所有课程信息 ============" << endl;
        cout << "-------------------------------------------------" << endl;
        cout << "编号" << "\t" << "课程名" << "\t" << "类型"     << "\t"
             << "学时" << "\t" << "学分"   << "\t" << "选课人数" << endl;
        cout << "-------------------------------------------------" << endl;
        for (int i = 0; i < n; i++)
            cout << cour[i] << endl;

        system("pause");
    }

private:
    string _id;         // 编号
    string _name;       // 课程名
    bool _type;         // 课程类型,1必修,0选修
    int _hour;          // 学时
    int _credir;        // 学分
    int _count;         // 选课人数
};

学生类:

class Student
{
    friend ostream& operator<<(ostream& out, const Student& s);
    friend fstream& operator<<(fstream& fout, const Student& s);
    // friend istream& operator>>(istream& in, Student& s);        这个重载不好实现,因为要查重
    friend fstream& operator>>(fstream& fin, Student& s);
public:
    Student()
        : _id("None")                       // 学号
        , _class("None")                    // 班级
        , _name("None")                     // 姓名
        , _num_compulsory(0)                
        , _num_elective(0)
    {}

    void Write(vector<Student>& stu, int n)
    {
        fstream myfile;
        myfile.open("student.txt", ios::out | ios::binary);
        if (!myfile)
        {
            cout << "student.txt can't open." << endl;
            abort();
        }

        myfile << n << endl << endl;
        for (int i = 0; i < n; i++)
            myfile << stu[i] << endl;

        myfile.close();
    }

    int Read(vector<Student>& stu)
    {
        stu.clear();
        fstream myfile;
        myfile.open("student.txt", ios::in | ios::binary);
        if (!myfile)
        {
            cout << "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;
            stu.push_back(temp);
        }

        myfile.close();
        return n;
    }

    void Add(vector<Student>& stu)
    {
        system("cls");
        int n = Read(stu);
        char sign = '0';
        Student temp;

        cout << "============ 添加学生 ============" << endl;
        while (sign != 'n' && sign != 'N')
        {
        loop:
            cout << "学号: ";
            cin >> temp._id;

            // 检查重复
            int c = 0;
            while (c < n)
            {
                c++;
                if (temp._id == stu[n - c]._id)
                {
                    cout << "学号重复,请重新输入。" << endl;
                    goto loop;
                }
            }

            cout << "班级: ";
            cin >> temp._class;
            cout << "姓名: ";
            cin >> temp._name;
            n++;

            stu.push_back(temp);

            cout << "是否继续添加(y 或 n): ";
            cin >> sign;
        }

        Write(stu, n);
    }

    void Delete(vector<Student>& stu)
    {
        system("cls");
        int n = Read(stu);
        int m = Course().Read(cour);                    // 课程数量
        int i = 0;
        string id;

        cout << "============ 删除学生 =============" << endl;
        cout << "请输入要删除学生的学号: ";
        cin >> id;

        while (i < n && id != stu[i]._id)
            i++;

        if (i == n)
        {
            cout << "没有找到该学生。" << endl;
            system("pause");
            return;
        }

        // 找到该学生所选的全部课程,并让这些课程的选课人数-1
        if (stu[i]._num_compulsory)
        {
            while (stu[i]._num_compulsory > 0)
            {
                stu[i]._num_compulsory--;

                int c = 0;
                while (c < m)
                {
                    if (stu[i]._cour_compulsory[stu[i]._num_compulsory].c_id() == cour[c].c_id())
                    {
                        cour[c].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 (stu[i]._cour_elective[stu[i]._num_elective].c_id() == cour[c].c_id())
                    {
                        cour[c].c_count()--;
                        break;
                    }
                    c++;
                }
            }
        }

        for (int j = i; j < n - 1; j++)
            stu[j] = stu[j + 1];

        // 保存
        char sign;
        cout << "请确定是否删除(y 或 n): ";
        cin >> sign;
        if (sign != 'n' && sign != 'N')
        {
            stu.erase(stu.end() - 1);
            Write(stu, n - 1);
            Course().Write(cour, m);
            cout << "============ 删除成功 ============" << endl;
            system("pause");
        }
    }

    void Modify(vector<Student>& stu)
    {
        system("cls");
        int n = Read(stu);
        string id;
        int i = 0;

        cout << "============ 修改学生信息 ===========" << endl;
        cout << "请输入你想修改学生的学号: ";
        cin >> id;

        while (i < n && id != stu[i]._id)
            i++;

        if (i == n)
        {
            cout << "找不到该学生。" << endl;
            system("pause");
            return;
        }

        cout << "-------------------------------------------------------" << endl;
        cout << "学号: " << stu[i]._id    << endl;
        cout << "班级: " << stu[i]._class << endl;
        cout << "姓名: " << stu[i]._name  << endl;
        cout << "-------------------------------------------------------" << endl;

        cout << "请重新输入学生信息。" << endl;
        cout << "班级: ";
        cin >> stu[i]._class;
        cout << "姓名: ";
        cin >> stu[i]._name;

        // 保存
        char sign;
        cout << "是否保存修改(y 或 n): ";
        cin >> sign;
        if (sign != 'n' && sign != 'N')
        {
            cout << "修改成功。" << endl;
            Write(stu, n);
            system("pause");
        }
    }

    void Lookup(vector<Student>& stu)
    {
        system("cls");
        int n = Read(stu);
        Course().Read(cour);
        int i = 0;
        string id;

        cout << "============== 查找学生 ==============" << endl;
        cout << "请输入要查找学生的学号: ";
        cin >> id;

        while (i < n && id != stu[i]._id)
            i++;

        if (i == n)
        {
            cout << "找不到该学生。" << endl;
        }
        else
        {
            cout << stu[i] << endl;
        }

        system("pause");
    }

    void Show(vector<Student>& stu)
    {
        system("cls");
        int n = Read(stu);

        cout << "=============================== 展示所有学生信息 ==============================" << endl;
        cout << "学号" << "\t" << "班级" << "\t" << "姓名" << "\t" << "必修课" << "\t" << "选修课" << endl;
        cout << "-------------------------------------------------------------------------------" << endl;
        for (int i = 0; i < n; i++)
            cout << stu[i] << endl;

        system("pause");
    }

private:
    string _id;                                 // 学号
    string _class;                              // 班级
    string _name;                               // 名字
    Course _cour_compulsory[SIZE_COMP];         // 必修课信息
    Course _cour_elective[SIZE_ELECT];          // 选修课信息
    int _num_compulsory;                        // 已选必修课数量
    int _num_elective;                          // 已选选修课数量
};

可以发现,在查找和展示所有的函数中,我使用了输出流重载,让代码的可读性更高,也减少了代码量。

需要注意,和老版本不同,我没有再定义初始化文件的函数Input。所以这需要我们提前创建好两个文本文件,并进行相应的数据录入,具体格式如下:

  • course.txt:
    在这里插入图片描述

  • student.txt:
    在这里插入图片描述


4.多级菜单的实现


int choose_user()
{
loop:
    system("cls");
    int c = 0;
    cout << "=================================================================" << endl;
    cout << "-------------- 欢迎来到学生选课系统 ---------------" << endl;
    cout << "请选择你的身份(1管理员 或 2学生)(输入0退出): ";
    cin >> c;

    string passwd;
    if (c == 1)
    {
        cout << "请输入密码: ";
        cin >> passwd;
        if (passwd == string(PASSWORD))		// PASSWORD是一个宏,#define PASSWORD 密码
        {
            flag = 1;
            cout << "登入成功." << endl;
            system("pause");
        }
        else
        {
            cout << "密码错误." << endl;
            system("pause");
            goto loop;
        }
    }
    else
        flag = 0;

    if (c < 0 || c > 2)
    {
        cout << "请输入 0-2." << endl;
        system("pause");
        goto loop;
    }

    return c;
}

void menu_course()
{
    int c = 0;
    while (1)
    {
        system("cls");
        cout << "============ 课程菜单 ============" << endl;
        cout << "           1. 增加课程            " << endl;
        cout << "           2. 删除课程            " << endl;
        cout << "           3. 修改课程            " << endl;
        cout << "           4. 查找课程            " << endl;
        cout << "           5. 展示所有课程        " << endl;
        cout << "           0. 返回上级目录        " << endl;
        cout << "----------------------------------" << endl;
        cout << "请选择你的操作(0-6): ";
        cin >> c;
        switch (c)
        {
        case 1:
            if (flag)
                Course().Add(cour);
            else
            {
                cout << "request denied." << endl;
                system("pause");
            }
            break;
        case 2:
            if (flag)
                Course().Delete(cour);
            else
            {
                cout << "request denied." << endl;
                system("pause");
            }
            break;
        case 3:
            if (flag)
                Course().Modify(cour);
            else
            {
                cout << "request denied." << endl;
                system("pause");
            }
            break;
        case 4:
            Course().Lookup(cour);
            break;
        case 5:
            Course().Show(cour);
            break;
        case 0:
            cout << "------------ 返回成功 ------------" << endl;
            system("pause");
            return;
        default:
            cout << "请输入 0-6." << endl;
            system("pause");
            break;
        }
    }
}

void menu_student()
{
    int c = 0;
    while (1)
    {
        system("cls");
        cout << "============ 学生菜单 ============" << endl;
        cout << "           1. 添加学生            " << endl;
        cout << "           2. 删除学生            " << endl;
        cout << "           3. 修改学生            " << endl;
        cout << "           4. 查找学生            " << endl;
        cout << "           5. 展示所有学生信息    " << endl;
        cout << "           6. 选课                " << endl;
        cout << "           7. 退选                " << endl;
        cout << "           0. 返回                " << endl;
        cout << "----------------------------------" << endl;
        cout << "请选择你的操作(0-7): ";
        cin >> c;
        switch (c)
        {
        case 1:
            if (flag)
                Student().Add(stu);
            else
            {
                cout << "request denied." << endl;
                system("pause");
            }
            break;
        case 2:
            if (flag)
                Student().Delete(stu);
            else
            {
                cout << "request denied." << endl;
                system("pause");
            }
            break;
        case 3:
            if (flag)
                Student().Modify(stu);
            else
            {
                cout << "request denied." << endl;
                system("pause");
            }
            break;
        case 4:
            Student().Lookup(stu);
            break;
        case 5:
            Student().Show(stu);
            break;
        case 6:
            Student().Select(cour, stu);
            break;
        case 7:
            Student().Deselect(cour, stu);
            break;
        case 0:
            cout << "------------ 返回成功 ------------" << endl;
            system("pause");
            return;
        default:
            cout << "请输入 0-7." << endl;
            system("pause");
            break;
        }
    }
}

int menu_main()
{
    int c = 0;
    while (1)
    {
        system("cls");
        cout << "==========================================" << endl;
        cout << "               1. 课程菜单                " << endl;
        cout << "               2. 学生菜单                " << endl;
        cout << "               0. 回到上级目录            " << endl;
        cout << "请选择你的操作(0-2): ";
        cin >> c;

        if (c < 0 || c > 2)
        {
            cout << "请输入 0-2." << endl;
            system("pause");
        }
        else
            break;
    }

    return c;
}


int main()
{
    cour.reserve(100);
    stu.reserve(100);
    while (1)
    {
    loop:
        if (!choose_user())
        {
            cout << "=============== 谢谢使用!再见! ===============" << endl;
            return 0;
        }
        while (1)
        {
            switch (menu_main())
            {
            case 1:
                menu_course();
                break;
            case 2:
                menu_student();
                break;
            case 0:
                cout << "=============== 返回成功 ===============" << endl;
                system("pause");
                goto loop;
            default:
                cout << "请输入 0-2." << endl;
                system("pause");
                break;
            }
        }
    }
    return 0;
}

这个菜单的实现基本和上一个版本相同,只不过密码处做了些许改动。定义了一个宏PASSWORD,方便后续的维护。


5.选课和退选的实现


class Student
{
public:
    
    // ...

	// 选课
    void Select(vector<Course>& cour, vector<Student>& stu)
    {
        system("cls");
        int n_cour = Course().Read(cour);
        int n_stu = Read(stu);
        int i = 0;              // 学生
        int j = 0;              // 课程

        string id_c;            // 课程  
        string id_s;            // 学生                                                                           
        cout << "============ 开始选课 ============" << endl;
        cout << "请输入学生学号: ";
        cin >> id_s;
        while (i < n_stu && stu[i]._id != id_s)
            i++;

        if (i == n_stu)
        {
            cout << "找不到该学生。" << endl;
            system("pause");
            return;
        }

        cout << ("请输入想选择的课程编号: ");
        cin >> id_c;
        while (j < n_cour && cour[j].c_id() != id_c)
            j++;

        if (j == n_cour)
        {
            cout << "找不到该课程" << endl;
            system("pause");
            return;
        }

        if (cour[j].c_count() >= 60)
        {
            cout << "课程人数已达上限,选课失败。" << endl;
            system("pause");
        }
        else
        {
            if (cour[j].c_type())
                if (stu[i]._num_compulsory < 2)
                {
                    int c = 0;
                    while (c < stu[i]._num_compulsory && cour[j].c_id() != stu[i]._cour_compulsory[c].c_id())
                        c++;

                    if (c < stu[i]._num_compulsory)
                    {
                        cout << "请勿重复选择课程。" << endl;
                        system("pause");
                        return;
                    }
                    stu[i]._cour_compulsory[stu[i]._num_compulsory] = cour[j];
                    stu[i]._num_compulsory++;
                    cour[j].c_count()++;
                    Course().Write(cour, n_cour);
                    Write(stu, n_stu);
                    cout << "选课成功。" << endl;
                    system("pause");
                }
                else
                {
                    cout << "必修课数量已达上限,选课失败。" << endl;
                    system("pause");
                }
            else
            {
                if (stu[i]._num_elective < 1)
                {
                    int c = 0;
                    while (c < stu[i]._num_elective && cour[j].c_id() != stu[i]._cour_elective[c].c_id())
                        c++;

                    if (c < stu[i]._num_elective)
                    {
                        cout << "请勿重复选择课程。" << endl;
                        system("pause");
                        return;
                    }

                    stu[i]._cour_elective[stu[i]._num_elective] = cour[j];
                    stu[i]._num_elective++;
                    cour[j].c_count()++;
                    Course().Write(cour, n_cour);
                    Write(stu, n_stu);
                    cout << "选课成功。" << endl;
                    system("pause");
                }
                else
                {
                    cout << "选修课数量已达上限,选课失败。" << endl;
                    system("pause");
                }
            }
        }
    }

	// 退选
    void Deselect(vector<Course>& cour, vector<Student>& stu)
    {
        system("cls");
        int n_cour = Course().Read(cour);
        int n_stu = Read(stu);
        int i = 0;              // 学生
        int j = 0;              // 课程

        string id_c;          // 课程  
        string id_s;          // 学生                                                                               
        cout << "============ 退选 ============" << endl;
        cout << "请输入学生学号: ";
        cin >> id_s;
        while (i < n_stu && stu[i]._id != id_s)
            i++;

        if (i == n_stu)
        {
            cout << "找不到该学生。" << endl;
            system("pause");
            return;
        }

        cout << ("请输入想要退选的课程编号: ");
        cin >> id_c;
        while (j < n_cour && cour[j].c_id() != id_c)
            j++;

        if (j == n_cour)
        {
            cout << "找不到该课程" << endl;
            system("pause");
            return;
        }

        if (stu[i]._num_compulsory)
        {
            int c = stu[i]._num_compulsory - 1;
            while (c >= 0)
            {
                if (stu[i]._cour_compulsory[c].c_id() == id_c)
                {
                    cour[j].c_count()--;
                    stu[i]._num_compulsory--;

                    stu[i]._cour_compulsory[c] = Course();

                    // 重新排序
                    for (; c < stu[i]._num_compulsory; c++)
                        swap(stu[i]._cour_compulsory[c], stu[i]._cour_compulsory[c + 1]);

                    Write(stu, n_stu);
                    Course().Write(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 (stu[i]._cour_elective[c].c_id() == id_c)
                    {
                        cour[j].c_count()--;
                        stu[i]._num_elective--;

                        stu[i]._cour_elective[c] = Course();

                        for (; c < stu[i]._num_elective; c++)
                            swap(stu[i]._cour_elective[c], stu[i]._cour_elective[c + 1]);

                        Write(stu, n_stu);
                        Course().Write(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 (stu[i]._cour_elective[c].c_id() == id_c)
                {
                    cour[j].c_count()--;
                    stu[i]._num_elective--;

                    stu[i]._cour_elective[c] = Course();

                    for (; c < stu[i]._num_elective; c++)
                        swap(stu[i]._cour_elective[c], stu[i]._cour_elective[c + 1]);

                    Write(stu, n_stu);
                    Course().Write(cour, n_cour);
                    cout << "退选成功。" << endl;
                    system("pause");
                    return;
                }
                c--;
            }
            cout << "你并未选择该课程。" << endl;
            system("pause");
            return;
        }
    }

private:
    string _id;                                 // 学号
    string _class;                              // 班级
    string _name;                               // 名字
    Course _cour_compulsory[SIZE_COMP];         // 必修课信息
    Course _cour_elective[SIZE_ELECT];          // 选修课信息
    int _num_compulsory;                        // 已选必修课数量
    int _num_elective;                          // 已选选修课数量
};

这一块的实现也和老版本没有多大区别,这里不再过多赘述,细节在我的上一篇博客中都有很详细的讲解。


5.完整代码


#define _CRT_SECURE_NO_WARNINGS

#include<iostream>
#include<fstream>
#include<string>
#include<vector>

using namespace std;

#define SIZE_COMP 2              // 可以选择的必修课数量
#define SIZE_ELECT 1             // 可选择的选修课数量
#define PASSWORD "123456"        // 管理员密码

int flag = 0;                    // 确定身份状态的状态变量,0是学生,1是管理员,默认是0

class Student;                   // 向前声明学生类

class Course
{
    friend ostream& operator<<(ostream& out, const Course& c);
    friend fstream& operator<<(fstream& fout, const Course& c);
    // friend istream& operator>>(istream& in, Course& c);        这个重载不好实现,因为要查重
    friend fstream& operator>>(fstream& fin, Course& c);

    friend ostream& operator<<(ostream& out, const Student& s);

public:
    Course()
        : _id("None")
        , _name("None")
        , _type(0)
        , _hour(0)
        , _credir(0)
        , _count(0)
    {}

    const string c_id() { return _id; }                   // 编号的接口
    const string c_name() { return _name; }               // 名字的接口
    int& c_count() { return _count; }                     // 选课人数的接口
    const bool c_type() { return _type; }                 // 类型接口


    void Write(vector<Course>& cour, int n)
    {
        fstream myfile;
        myfile.open("course.txt", ios::out | ios::binary);
        if (!myfile)
        {
            cout << "course.txt can't open." << endl;
            abort();
        }

        myfile << n << endl << endl;
        for (int i = 0; i < n; i++)
            myfile << cour[i] << endl;

        myfile.close();
    }

    int Read(vector<Course>& cour)
    {
        cour.clear();
        fstream myfile;
        myfile.open("course.txt", ios::in | ios::binary);
        if (!myfile)
        {
            cout << "course.txt can't open." << endl;
            abort();
        }

        int n;
        myfile.seekg(0);
        myfile >> n;

        Course temp;
        for (int i = 0; i < n; i++)
        {
            myfile >> temp;
            cour.push_back(temp);
        }

        myfile.close();
        return n;
    }

    void Add(vector<Course>& cour)
    {
        system("cls");
        int n = Read(cour);
        char sign = '0';
        Course temp;

        cout << "============ 增加课程信息 ============" << endl;
        while (sign != 'n' && sign != 'N')
        {
        loop:
            cout << "ID: ";
            cin >> temp._id;

            // 查重
            int c = 0;
            while (c < n)
            {
                c++;
                if (temp._id == cour[n - c]._id)
                {
                    cout << "编号重复,请重新输入。" << endl;
                    goto loop;
                }
            }

            cout << "课程名: ";
            cin >> temp._name;
            cout << "课程类型(1必修,0选修): ";
            cin >> temp._type;
            cout << "学时: ";
            cin >> temp._hour;
            cout << "学分: ";
            cin >> temp._credir;
            
            n++;

            cour.push_back(temp);

            cout << "是否继续添加(y 或 n)";
            cin >> sign;
        }
        // 保存进文件
        Write(cour, n);
    }

    void Delete(vector<Course>& cour)
    {
        system("cls");
        int n = Read(cour);
        int i = 0;
        string id;

        cout << "============ 删除课程 ============" << endl;
        cout << "请输入需要删除课程的编号: ";
        cin >> id;

        // 检查有没有这个课程
        while (i < n && id != cour[i]._id)
            i++;

        if (i == n)
        {
            cout << "没找到该课程。" << endl;
            system("pause");
            return;
        }
        else
        {
            if (cour[i]._count)
            {
                cout << "所选人数不为零,请联系所有学生退选后,再删除该课程。" << endl;
                system("pause");
                return;
            }
        }

        for (int j = i; j < n - 1; j++)
            cour[j] = cour[j + 1];              // 编译器自动生成的赋值运算符重载

        // 保存
        char sign;
        cout << "是否保存(y 或 n): ";
        cin >> sign;
        if (sign != 'n' && sign != 'N')
        {
            cour.erase(cour.end() - 1);
            Write(cour, n - 1);
            cout << "============ 删除成功 ============" << endl;
            system("pause");
        }
    }

    void Modify(vector<Course>& cour)
    {
        system("cls");
        int n = Read(cour);
        string id;
        int i = 0;

        cout << "============ 修改课程信息 ============" << endl;
        cout << "请输入你想修改的课程id: ";
        cin >> id;

        // 看有没有这个课程
        cin >> id;
        while (i < n && id != cour[i]._id)
            i++;

        if (i == n)
        {
            cout << "没有找到该课程。" << endl;
            system("pause");
            return;
        }
        else
        {
            if (cour[i]._count)
            {
                cout << "所选人数不为零,请联系所有学生退选后,再修改课程信息。" << endl;
                system("pause");
                return;
            }
        }

        cout << "-------------------------------------------------" << endl;
        cout << "编号" << "\t" << "课程名" << "\t" << "类型"     << "\t"
             << "学时" << "\t" << "学分"   << "\t" << "选课人数" << endl;
        cout << cour[i] << endl;
        cout << "-------------------------------------------------" << endl;

        cout << "请重新输入课程信息。" << endl;
        cout << "课程名: ";
        cin >> cour[i]._name;
        cout << "课程类型(1必修,0选修): ";
        cin >> cour[i]._type;
        cout << "学时: ";
        cin >> cour[i]._hour;
        cout << "学分: ";
        cin >> cour[i]._credir;

        // 保存
        char sign;
        cout << "是否保存修改(y 或 n): ";
        cin >> sign;
        if (sign != 'n' && sign != 'N')
        {
            Write(cour, n);
            cout << "============ 修改成功 ============" << endl;
            system("pause");
        }
    }

    void Lookup(vector<Course>& cour)
    {
        system("cls");
        int n = Read(cour);
        int i = 0;
        string id;
        
        cout << "============ 查找课程 ============" << endl;
        cout << "请输入要查找课程的编号: ";
        cin >> id;

        // 检查是否存在
        while (i < n && id != cour[i]._id)
            i++;

        if (i == n)
        {
            cout << "没有找到该课程。" << endl;
        }
        else
        {
            cout << "-------------------------------------------------" << endl;
            cout << "编号" << "\t" << "课程名" << "\t" << "类型"     << "\t"
                 << "学时" << "\t" << "学分"   << "\t" << "选课人数" << endl;
            cout << "-------------------------------------------------" << endl;
            cout << cour[i] << endl;
        }

        system("pause");
    }

    void Show(vector<Course>& cour)
    {
        system("cls");
        int n = Read(cour);

        cout << "============ 展示所有课程信息 ============" << endl;
        cout << "-------------------------------------------------" << endl;
        cout << "编号" << "\t" << "课程名" << "\t" << "类型"     << "\t"
             << "学时" << "\t" << "学分"   << "\t" << "选课人数" << endl;
        cout << "-------------------------------------------------" << endl;
        for (int i = 0; i < n; i++)
            cout << cour[i] << endl;

        system("pause");
    }

private:
    string _id;         // 编号
    string _name;       // 课程名
    bool _type;         // 课程类型,1必修,0选修
    int _hour;          // 学时
    int _credir;        // 学分
    int _count;         // 选课人数
};

vector<Course> cour;

ostream& operator<<(ostream& out, const Course& c)
{
    out << c._id   << "\t" << c._name   << "\t" << (c._type ? "必修" : "选修") << "\t"
        << c._hour << "\t" << c._credir << "\t" << c._count;
    return out;
}

fstream& operator<<(fstream& fout, const Course& c)
{
    fout << c._id   << "\t" << c._name   << "\t" << c._type << "\t"
         << c._hour << "\t" << c._credir << "\t" << c._count;
    return fout;
}

fstream& operator>>(fstream& fin, Course& c)
{
    fin >> c._id   >> c._name   >> c._type
        >> c._hour >> c._credir >> c._count;
    return fin;
}

class Student
{
    friend ostream& operator<<(ostream& out, const Student& s);
    friend fstream& operator<<(fstream& fout, const Student& s);
    // friend istream& operator>>(istream& in, Student& s);        这个重载不好实现,因为要查重
    friend fstream& operator>>(fstream& fin, Student& s);
public:
    Student()
        : _id("None")                       // 学号
        , _class("None")                    // 班级
        , _name("None")                     // 姓名
        , _num_compulsory(0)                
        , _num_elective(0)
    {}

    void Write(vector<Student>& stu, int n)
    {
        fstream myfile;
        myfile.open("student.txt", ios::out | ios::binary);
        if (!myfile)
        {
            cout << "student.txt can't open." << endl;
            abort();
        }

        myfile << n << endl << endl;
        for (int i = 0; i < n; i++)
            myfile << stu[i] << endl;

        myfile.close();
    }

    int Read(vector<Student>& stu)
    {
        stu.clear();
        fstream myfile;
        myfile.open("student.txt", ios::in | ios::binary);
        if (!myfile)
        {
            cout << "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;
            stu.push_back(temp);
        }

        myfile.close();
        return n;
    }

    void Add(vector<Student>& stu)
    {
        system("cls");
        int n = Read(stu);
        char sign = '0';
        Student temp;

        cout << "============ 添加学生 ============" << endl;
        while (sign != 'n' && sign != 'N')
        {
        loop:
            cout << "学号: ";
            cin >> temp._id;

            // 检查重复
            int c = 0;
            while (c < n)
            {
                c++;
                if (temp._id == stu[n - c]._id)
                {
                    cout << "学号重复,请重新输入。" << endl;
                    goto loop;
                }
            }

            cout << "班级: ";
            cin >> temp._class;
            cout << "姓名: ";
            cin >> temp._name;
            n++;

            stu.push_back(temp);

            cout << "是否继续添加(y 或 n): ";
            cin >> sign;
        }

        Write(stu, n);
    }

    void Delete(vector<Student>& stu)
    {
        system("cls");
        int n = Read(stu);
        int m = Course().Read(cour);                    // 课程数量
        int i = 0;
        string id;

        cout << "============ 删除学生 =============" << endl;
        cout << "请输入要删除学生的学号: ";
        cin >> id;

        while (i < n && id != stu[i]._id)
            i++;

        if (i == n)
        {
            cout << "没有找到该学生。" << endl;
            system("pause");
            return;
        }

        // 找到该学生所选的全部课程,并让这些课程的选课人数-1
        if (stu[i]._num_compulsory)
        {
            while (stu[i]._num_compulsory > 0)
            {
                stu[i]._num_compulsory--;

                int c = 0;
                while (c < m)
                {
                    if (stu[i]._cour_compulsory[stu[i]._num_compulsory].c_id() == cour[c].c_id())
                    {
                        cour[c].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 (stu[i]._cour_elective[stu[i]._num_elective].c_id() == cour[c].c_id())
                    {
                        cour[c].c_count()--;
                        break;
                    }
                    c++;
                }
            }
        }

        for (int j = i; j < n - 1; j++)
            stu[j] = stu[j + 1];

        // 保存
        char sign;
        cout << "请确定是否删除(y 或 n): ";
        cin >> sign;
        if (sign != 'n' && sign != 'N')
        {
            stu.erase(stu.end() - 1);
            Write(stu, n - 1);
            Course().Write(cour, m);
            cout << "============ 删除成功 ============" << endl;
            system("pause");
        }
    }

    void Modify(vector<Student>& stu)
    {
        system("cls");
        int n = Read(stu);
        string id;
        int i = 0;

        cout << "============ 修改学生信息 ===========" << endl;
        cout << "请输入你想修改学生的学号: ";
        cin >> id;

        while (i < n && id != stu[i]._id)
            i++;

        if (i == n)
        {
            cout << "找不到该学生。" << endl;
            system("pause");
            return;
        }

        cout << "-------------------------------------------------------" << endl;
        cout << "学号: " << stu[i]._id    << endl;
        cout << "班级: " << stu[i]._class << endl;
        cout << "姓名: " << stu[i]._name  << endl;
        cout << "-------------------------------------------------------" << endl;

        cout << "请重新输入学生信息。" << endl;
        cout << "班级: ";
        cin >> stu[i]._class;
        cout << "姓名: ";
        cin >> stu[i]._name;

        // 保存
        char sign;
        cout << "是否保存修改(y 或 n): ";
        cin >> sign;
        if (sign != 'n' && sign != 'N')
        {
            cout << "修改成功。" << endl;
            Write(stu, n);
            system("pause");
        }
    }

    void Lookup(vector<Student>& stu)
    {
        system("cls");
        int n = Read(stu);
        Course().Read(cour);
        int i = 0;
        string id;

        cout << "============== 查找学生 ==============" << endl;
        cout << "请输入要查找学生的学号: ";
        cin >> id;

        while (i < n && id != stu[i]._id)
            i++;

        if (i == n)
        {
            cout << "找不到该学生。" << endl;
        }
        else
        {
            cout << stu[i] << endl;
        }

        system("pause");
    }

    void Show(vector<Student>& stu)
    {
        system("cls");
        int n = Read(stu);

        cout << "=============================== 展示所有学生信息 ==============================" << endl;
        cout << "学号" << "\t" << "班级" << "\t" << "姓名" << "\t" << "必修课" << "\t" << "选修课" << endl;
        cout << "-------------------------------------------------------------------------------" << endl;
        for (int i = 0; i < n; i++)
            cout << stu[i] << endl;

        system("pause");
    }

    void Select(vector<Course>& cour, vector<Student>& stu)
    {
        system("cls");
        int n_cour = Course().Read(cour);
        int n_stu = Read(stu);
        int i = 0;              // 学生
        int j = 0;              // 课程

        string id_c;            // 课程  
        string id_s;            // 学生                                                                           
        cout << "============ 开始选课 ============" << endl;
        cout << "请输入学生学号: ";
        cin >> id_s;
        while (i < n_stu && stu[i]._id != id_s)
            i++;

        if (i == n_stu)
        {
            cout << "找不到该学生。" << endl;
            system("pause");
            return;
        }

        cout << ("请输入想选择的课程编号: ");
        cin >> id_c;
        while (j < n_cour && cour[j].c_id() != id_c)
            j++;

        if (j == n_cour)
        {
            cout << "找不到该课程" << endl;
            system("pause");
            return;
        }

        if (cour[j].c_count() >= 60)
        {
            cout << "课程人数已达上限,选课失败。" << endl;
            system("pause");
        }
        else
        {
            if (cour[j].c_type())
                if (stu[i]._num_compulsory < 2)
                {
                    int c = 0;
                    while (c < stu[i]._num_compulsory && cour[j].c_id() != stu[i]._cour_compulsory[c].c_id())
                        c++;

                    if (c < stu[i]._num_compulsory)
                    {
                        cout << "请勿重复选择课程。" << endl;
                        system("pause");
                        return;
                    }
                    stu[i]._cour_compulsory[stu[i]._num_compulsory] = cour[j];
                    stu[i]._num_compulsory++;
                    cour[j].c_count()++;
                    Course().Write(cour, n_cour);
                    Write(stu, n_stu);
                    cout << "选课成功。" << endl;
                    system("pause");
                }
                else
                {
                    cout << "必修课数量已达上限,选课失败。" << endl;
                    system("pause");
                }
            else
            {
                if (stu[i]._num_elective < 1)
                {
                    int c = 0;
                    while (c < stu[i]._num_elective && cour[j].c_id() != stu[i]._cour_elective[c].c_id())
                        c++;

                    if (c < stu[i]._num_elective)
                    {
                        cout << "请勿重复选择课程。" << endl;
                        system("pause");
                        return;
                    }

                    stu[i]._cour_elective[stu[i]._num_elective] = cour[j];
                    stu[i]._num_elective++;
                    cour[j].c_count()++;
                    Course().Write(cour, n_cour);
                    Write(stu, n_stu);
                    cout << "选课成功。" << endl;
                    system("pause");
                }
                else
                {
                    cout << "选修课数量已达上限,选课失败。" << endl;
                    system("pause");
                }
            }
        }
    }

    void Deselect(vector<Course>& cour, vector<Student>& stu)
    {
        system("cls");
        int n_cour = Course().Read(cour);
        int n_stu = Read(stu);
        int i = 0;              // 学生
        int j = 0;              // 课程

        string id_c;          // 课程  
        string id_s;          // 学生                                                                               
        cout << "============ 退选 ============" << endl;
        cout << "请输入学生学号: ";
        cin >> id_s;
        while (i < n_stu && stu[i]._id != id_s)
            i++;

        if (i == n_stu)
        {
            cout << "找不到该学生。" << endl;
            system("pause");
            return;
        }

        cout << ("请输入想要退选的课程编号: ");
        cin >> id_c;
        while (j < n_cour && cour[j].c_id() != id_c)
            j++;

        if (j == n_cour)
        {
            cout << "找不到该课程" << endl;
            system("pause");
            return;
        }

        if (stu[i]._num_compulsory)
        {
            int c = stu[i]._num_compulsory - 1;
            while (c >= 0)
            {
                if (stu[i]._cour_compulsory[c].c_id() == id_c)
                {
                    cour[j].c_count()--;
                    stu[i]._num_compulsory--;

                    stu[i]._cour_compulsory[c] = Course();

                    // 重新排序
                    for (; c < stu[i]._num_compulsory; c++)
                        swap(stu[i]._cour_compulsory[c], stu[i]._cour_compulsory[c + 1]);

                    Write(stu, n_stu);
                    Course().Write(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 (stu[i]._cour_elective[c].c_id() == id_c)
                    {
                        cour[j].c_count()--;
                        stu[i]._num_elective--;

                        stu[i]._cour_elective[c] = Course();

                        for (; c < stu[i]._num_elective; c++)
                            swap(stu[i]._cour_elective[c], stu[i]._cour_elective[c + 1]);

                        Write(stu, n_stu);
                        Course().Write(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 (stu[i]._cour_elective[c].c_id() == id_c)
                {
                    cour[j].c_count()--;
                    stu[i]._num_elective--;

                    stu[i]._cour_elective[c] = Course();

                    for (; c < stu[i]._num_elective; c++)
                        swap(stu[i]._cour_elective[c], stu[i]._cour_elective[c + 1]);

                    Write(stu, n_stu);
                    Course().Write(cour, n_cour);
                    cout << "退选成功。" << endl;
                    system("pause");
                    return;
                }
                c--;
            }
            cout << "你并未选择该课程。" << endl;
            system("pause");
            return;
        }
    }

private:
    string _id;                                 // 学号
    string _class;                              // 班级
    string _name;                               // 名字
    Course _cour_compulsory[SIZE_COMP];         // 必修课信息
    Course _cour_elective[SIZE_ELECT];          // 选修课信息
    int _num_compulsory;                        // 已选必修课数量
    int _num_elective;                          // 已选选修课数量
};

vector<Student> stu;

ostream& operator<<(ostream& out, const Student& s)
{
    out << s._id << "\t" << s._class << "\t" << s._name << "\t";
   for (int i = 0; i < SIZE_COMP; i++)
       out << s._cour_compulsory[i]._name << " ";
   out << "\t";
   for (int i = 0; i < SIZE_ELECT; i++)
       out << s._cour_elective[i]._name << " ";
    return out;
}

fstream& operator<<(fstream& fout, const Student& s)
{
    fout << s._id << "\t" << s._class << "\t" << s._name << "\t";
    for (int i = 0; i < SIZE_COMP; i++)
        fout << s._cour_compulsory[i] << "\t";
    for (int i = 0; i < SIZE_ELECT; i++)
        fout << s._cour_elective[i] << "\t";
    fout << s._num_compulsory << "\t" << s._num_elective;
    return fout;
}

fstream& operator>>(fstream& fin, Student& s)
{
    fin >> s._id >> s._class >> s._name;
    for (int i = 0; i < SIZE_COMP; i++)
        fin >> s._cour_compulsory[i];
    for (int i = 0; i < SIZE_ELECT; i++)
        fin >> s._cour_elective[i];
    fin >> s._num_compulsory >> s._num_elective;
    return fin;
}

// --------------------------------------------------------------------------------------------------//

int choose_user()
{
loop:
    system("cls");
    int c = 0;
    cout << "=================================================================" << endl;
    cout << "-------------- 欢迎来到学生选课系统 ---------------" << endl;
    cout << "请选择你的身份(1管理员 或 2学生)(输入0退出): ";
    cin >> c;

    string passwd;
    if (c == 1)
    {
        cout << "请输入密码: ";
        cin >> passwd;
        if (passwd == string(PASSWORD))
        {
            flag = 1;
            cout << "登入成功." << endl;
            system("pause");
        }
        else
        {
            cout << "密码错误." << endl;
            system("pause");
            goto loop;
        }
    }
    else
        flag = 0;

    if (c < 0 || c > 2)
    {
        cout << "请输入 0-2." << endl;
        system("pause");
        goto loop;
    }

    return c;
}

void menu_course()
{
    int c = 0;
    while (1)
    {
        system("cls");
        cout << "============ 课程菜单 ============" << endl;
        cout << "           1. 增加课程            " << endl;
        cout << "           2. 删除课程            " << endl;
        cout << "           3. 修改课程            " << endl;
        cout << "           4. 查找课程            " << endl;
        cout << "           5. 展示所有课程        " << endl;
        cout << "           0. 返回上级目录        " << endl;
        cout << "----------------------------------" << endl;
        cout << "请选择你的操作(0-6): ";
        cin >> c;
        switch (c)
        {
        case 1:
            if (flag)
                Course().Add(cour);
            else
            {
                cout << "request denied." << endl;
                system("pause");
            }
            break;
        case 2:
            if (flag)
                Course().Delete(cour);
            else
            {
                cout << "request denied." << endl;
                system("pause");
            }
            break;
        case 3:
            if (flag)
                Course().Modify(cour);
            else
            {
                cout << "request denied." << endl;
                system("pause");
            }
            break;
        case 4:
            Course().Lookup(cour);
            break;
        case 5:
            Course().Show(cour);
            break;
        case 0:
            cout << "------------ 返回成功 ------------" << endl;
            system("pause");
            return;
        default:
            cout << "请输入 0-6." << endl;
            system("pause");
            break;
        }
    }
}

void menu_student()
{
    int c = 0;
    while (1)
    {
        system("cls");
        cout << "============ 学生菜单 ============" << endl;
        cout << "           1. 添加学生            " << endl;
        cout << "           2. 删除学生            " << endl;
        cout << "           3. 修改学生            " << endl;
        cout << "           4. 查找学生            " << endl;
        cout << "           5. 展示所有学生信息    " << endl;
        cout << "           6. 选课                " << endl;
        cout << "           7. 退选                " << endl;
        cout << "           0. 返回                " << endl;
        cout << "----------------------------------" << endl;
        cout << "请选择你的操作(0-7): ";
        cin >> c;
        switch (c)
        {
        case 1:
            if (flag)
                Student().Add(stu);
            else
            {
                cout << "request denied." << endl;
                system("pause");
            }
            break;
        case 2:
            if (flag)
                Student().Delete(stu);
            else
            {
                cout << "request denied." << endl;
                system("pause");
            }
            break;
        case 3:
            if (flag)
                Student().Modify(stu);
            else
            {
                cout << "request denied." << endl;
                system("pause");
            }
            break;
        case 4:
            Student().Lookup(stu);
            break;
        case 5:
            Student().Show(stu);
            break;
        case 6:
            Student().Select(cour, stu);
            break;
        case 7:
            Student().Deselect(cour, stu);
            break;
        case 0:
            cout << "------------ 返回成功 ------------" << endl;
            system("pause");
            return;
        default:
            cout << "请输入 0-7." << endl;
            system("pause");
            break;
        }
    }
}

int menu_main()
{
    int c = 0;
    while (1)
    {
        system("cls");
        cout << "==========================================" << endl;
        cout << "               1. 课程菜单                " << endl;
        cout << "               2. 学生菜单                " << endl;
        cout << "               0. 回到上级目录            " << endl;
        cout << "请选择你的操作(0-2): ";
        cin >> c;

        if (c < 0 || c > 2)
        {
            cout << "请输入 0-2." << endl;
            system("pause");
        }
        else
            break;
    }

    return c;
}


int main()
{
    cour.reserve(100);
    stu.reserve(100);
    while (1)
    {
    loop:
        if (!choose_user())
        {
            cout << "=============== 谢谢使用!再见! ===============" << endl;
            return 0;
        }
        while (1)
        {
            switch (menu_main())
            {
            case 1:
                menu_course();
                break;
            case 2:
                menu_student();
                break;
            case 0:
                cout << "=============== 返回成功 ===============" << endl;
                system("pause");
                goto loop;
            default:
                cout << "请输入 0-2." << endl;
                system("pause");
                break;
            }
        }
    }
    return 0;
}

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

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

这次,数据泄露的目标受害者指向了---救护车服务公司

已停业的救护车服务遭到勒索软件攻击导致近百万人受到威胁&#xff01; 此次数据泄露的目标受害者是法伦救护车服务公司&#xff0c;该公司是Transformative Healthcare的子公司。ALPHV勒索软件团伙声称对2023年4月下旬对Transformative Healthcare的攻击负责&#xff0c;并导…

3dmax全景图用什么渲染 全景图云渲染使用教程

在给客户展示设计概念时&#xff0c;应用3ds Max创建的全景图是一个高效直观的方法。这种方式不仅可以全方位地呈现整个空间&#xff0c;让客户沉浸式地感受设计师的创意&#xff0c;而且在展现大型空间设计&#xff0c;如展览馆或者会议室等&#xff0c;效果尤其显著&#xff…

如何在anaconda里安装basemap和pyproj库

当直接使用conda命令进行安装basemap和pyproj库时&#xff0c;会出现版本不对应的报错问题(如下图)&#xff0c;所以此篇博客用以展示如何安装basemap和pyproj库 题主默认使用的anaconda源已经切换成了清华大学源&#xff0c;但是仍然会出现报错&#xff0c;所以不是源的问题&a…

什么是向量数据库

什么是向量数据库 向量数据库是一种应用在高效存储和查询高维向量的数据库。在传统的OLTP和OLAP数据库中&#xff08;如上图所示&#xff09;&#xff0c;数据按行和列组织&#xff08;这些称为表&#xff09;&#xff0c;并根据这些列中的值执行查询。然而&#xff0c;在某些应…

ASP.NET Core AOT

Native AOT 最初在 .NET 7 中引入&#xff0c;在即将发布的 .NET 8 版本中可以与 ASP.NET Core 一起使用。在这篇文章中&#xff0c;我们从总体角度审视其优点和缺点&#xff0c;并进行测量以量化不同平台上的改进。 源代码&#xff1a;https://download.csdn.net/download/he…

华清远见作业第二十天——IO(第三天)

思维导图&#xff1a; 使用标准IO完成两个文件的拷贝 代码&#xff1a; #include<stdio.h> #include<string.h> #include<stdlib.h> #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> int main(int argc, const char *…

SQL Server注入之攻防技战法

那天下着很大的雨&#xff0c;母亲从城里走回来的时候&#xff0c;浑身就是一个泥人&#xff0c;那一刻我就知道我没有别的选择了 1.Mssql报错注入 0.判断数据库类型 1.爆当前用户名 2.爆版本 3.爆服务器名 4.判断数据库个数 5.获取全部数据库 语句只适合>2005 爆当前数据…

桶式移位器

前言 本篇文章介绍CPU的核心部件之一&#xff1a;桶式移位器&#xff0c;简称BS&#xff0c;英文全称为Barrel Shifter 桶式移位器最大的特点就是能在单周期内完成多种方式&#xff0c;各种位数的移位操作 常见的移位操作 常见的移位操作种类如下&#xff1a; 算术右移 是指…

google-java-format 配置及应用

以google 的java 风格为基准&#xff0c;以后面的开发中&#xff0c;都需要满足这个&#xff0c;主要用到的是google的两个插件&#xff1a;google-java-format, google_checks.xml代码格式检测&#xff0c;这两个堪称双剑合并&#xff0c;代码成诗。google-java-format替换ide…

RS485数据采集模块,如何一次采集多个modbus设备数据?

在工业数据采集中&#xff0c;RS485是一种常见的数据通信协议&#xff0c;而Modbus则是其上的常用设备协议。那么&#xff0c;如何用一个模块高效采集多个Modbus设备的数据呢&#xff1f;这就是我们今天要探讨的话题&#xff01; 什么是RS485数据采集模块&#xff1f; 首先&a…

算法28:力扣64题,最小路径和------------样本模型

题目&#xff1a; 给定一个二维数组matrix&#xff0c;一个人必须从左上角出发&#xff0c;最后到达右下角 。沿途只可以向下或者向右走&#xff0c;沿途的数字都累加就是距离累加和 * 返回累加和最小值 思路&#xff1a; 1. 既然是给定二维数组matrix&#xff0c;那么二维数…

FreeRTOS——队列及其实战

1.队列概念 1&#xff09;队列是任务到任务、任务到中断、中断到任务数据交流的一种机制&#xff08;消息传递&#xff09; 2&#xff09;队列类似数组&#xff0c;只能存储有限数量、相同类型的数据&#xff0c;在创建时需指定队列长度与队列项大小 3&#xff09;出队入队阻塞…

xshell登录不上虚拟机了

电脑重启后连不上本地虚机了 1、关闭防火墙 2 虚拟机ping得到主机&#xff0c;而主机ping不到虚拟机的解决办法 原因&#xff1a;可能是主机的网络适配器没有调好 首先&#xff0c;找到虚拟机的网络配置器 根据虚拟机的IP信息修改主机虚拟适配器VMnet8 修改ip使得和虚拟机连…

Element-ui自定义input框非空校验

1、vue自定义非空指令&#xff1a; main.js中自定义非空指令 当input框或下拉框中数据更新时&#xff0c;触发校验 Vue.directive(isEmpty,{update:function(el,binding,vnode){if(vnode.componentInstance.value""){el.classList.add("is-required");}e…

[Unity]实时阴影技术方案总结

一&#xff0c;Planar Shadow 原理就是将模型压扁之后绘制在需要接受阴影的物体上&#xff0c;这种方式十分高效&#xff0c;消耗很低。具体实现过程参考Unity Shader - Planar Shadow - 平面阴影。具按照自己的理解&#xff0c;其实就是根据光照方向计算片元在接受阴影的平面…

详解卡尔曼滤波(Kalman Filter)

1. 从维纳滤波到卡尔曼滤波 黑盒&#xff08;Black Box&#xff09;思想最早由维纳&#xff08;Wiener&#xff09;在1939年提出&#xff0c;即假定我们对从数据到估计中间的映射过程一无所知&#xff0c;仅仅用线性估计&#xff08;我们知道在高斯背景下&#xff0c;线性估计…

计算机创新协会冬令营——暴力枚举题目01

首先是欢迎大家参加此次的冬令营&#xff0c;我们协会欢迎所有志同道合的同学们。话不多说&#xff0c;先来看看今天的题目吧。 题目 力扣题号&#xff1a;2351. 第一个出现两次的字母 注&#xff1a;下述题目和示例均来自力扣 题目 给你一个由小写英文字母组成的字符串 s &…

RocketMQ5.0Pop消费模式

前言 RocketMQ 5.0 消费者引入了一种新的消费模式&#xff1a;Pop 消费模式&#xff0c;目的是解决 Push 消费模式的一些痛点。 RocketMQ 4.x 之前&#xff0c;消费模式分为两种&#xff1a; Pull&#xff1a;拉模式&#xff0c;消费者自行拉取消息、上报消费结果Push&#x…

探索Allure Report:提升自动化测试效率的秘密武器

亲爱的小伙伴们&#xff0c;由于微信公众号改版&#xff0c;打乱了发布时间&#xff0c;为了保证大家可以及时收到文章的推送&#xff0c;可以点击上方蓝字关注测试工程师成长之路&#xff0c;并设为星标就可以第一时间收到推送哦&#xff01; 一.使用 Allure2 运行方式-Python…

【操作系统xv6】学习记录4 -CPU上下文:进程上下文、线程上下文、中断上下文

什么是cpu上下文 CPU 寄存器和程序计数器就是 CPU 上下文&#xff0c;因为它们都是 CPU 在运行任何任务前&#xff0c;必须的依赖环境。 什么是 CPU 上下文切换 先把前一个任务的 CPU 上下文&#xff08;也就是 CPU 寄存器和程序计数器&#xff09;保存起来&#xff0c;然后…
最新文章