Visual Studio 2022版本 B站黑马程序员C++自学分享-第三阶段(2)--- 已完结!-----大家一定要结合黑马程序员的C++视频一起食用!!!

Visual Studio 2022版本 B站黑马程序员C++自学分享-第三阶段(2)(主要包括:自己敲的代码、通过注释来备注上自己对代码的理解)---已完结-----大家一定要结合黑马程序员的C++视频一起食用!!!

※※※前言

黑马的课程里面分为了三大阶段
一、第一阶段 C++基础语法入门 对C++有初步了解,能够有基础编程能力
二、第二阶段 C++核心编程 介绍C++面向对象编程,为大型项目做铺垫
三、第三阶段 C++提高编程 介绍C++泛型编程思想,以及STL的基本使用
※※※我会一个阶段发一个文章,一共发布三个文章来把学习的代码还有自己在代码旁边标注的对于这些代码的理解和心得体会一起分享给大家!!!

该文章的目的:
1.把本人跟着黑马敲的代码都发布在文章内,供大家复制粘贴和参考等等
2.就当作存储代码的一个云空间了,放在电脑里还占内存
3.在学习的时候,对于代码的理解我都在旁边进行了注释,望大家能够快速消化吸收

因为本人还有好多代码是没有深究的,到了后来我记得有一个地方要在类内成员函数后面要加const的这个(2022版本要敲,但是黑马那个2015的就不用),类似的等等,没有特别研究,是看弹幕大佬这么说的我就这么去敲的,所以如果大家能够对这篇文章所有的代码有疑问的话请积极在评论区留言,大家一块讨论。

6.演讲比赛流程管理系统

Speaker.h

#pragma once
#include <iostream>
#include <string>
using namespace std;

class Speaker
{
public:
	//选手姓名
	string m_Name;

	//分数,最多有两轮得分
	double m_Score[2];
};

SpeechManager.h

#pragma once
#include <iostream>
using namespace std;
#include <vector>   
#include <map> 
#include <algorithm>//随机数
#include "Speaker.h"

//设计演讲管理类
class SpeechManager
{
public:
//成员函数
    //构造函数
    SpeechManager();

    //菜单功能
    void show_Menu();

    //退出系统
    void exitSystem();

    //析构函数
    ~SpeechManager();

    //初始化容器和属性
    void initSpeech();

    //创建12名选手
    void createSpeaker();

    //开始比赛 比赛整个流程控制函数
    void startSpeech();

    //抽签
    void speechDraw();

    //比赛
    void speechContest();

    //显示得分
    void showScore();

    //保存记录
    void saveRecord();

    //读取记录
    void loadRecord();

    //判断文件是否为空
    bool fileIsEmpty;

    //存放往届记录的容器
    map<int, vector<string>>m_Record;

    //显示记录
    void showRecord();

    //清空记录
    void clearRecord();

//成员属性
    //保存第一轮选手编号
    vector<int>v1;

    //保存晋级选手编号
    vector<int>v2;

    //保存前三名选手编号
    vector<int>vVictory;

    //存放编号以及其对应具体选手的容器
    map<int, Speaker>m_Speaker;

    //存放比赛轮数
    int m_Index;
};

SpeechManager.cpp

#include "SpeechManager.h"
#include <deque>
#include <functional>//greater是内建函数对象
#include <numeric>//accumulate
#include <fstream>//文件操作头文件

//构造函数
//初始化容器和属性并且创建12名选手
SpeechManager::SpeechManager()
{
    //初始化容器和属性
    this->initSpeech();

    //创建12名选手
    this->createSpeaker();

    //加载往届记录
    this->loadRecord();

}

//菜单功能
void SpeechManager::show_Menu()
{
    cout << "*************************************************" << endl
        << "**************** 欢迎参加演讲比赛 ***************" << endl
        << "**************** 1、开始演讲比赛 ****************" << endl
        << "**************** 2、查看往届记录 ****************" << endl
        << "**************** 3、清空比赛记录 ****************" << endl
        << "**************** 0、退出比赛程序 ****************" << endl
        << "*************************************************" << endl;
}

//退出系统
void SpeechManager::exitSystem()
{
    cout << "欢迎下次使用" << endl;
    system("pause");
    exit(0);
}

//析构函数
SpeechManager::~SpeechManager()
{

}

//初始化容器和属性
void SpeechManager::initSpeech()
{
    //容器都置空
    this->v1.clear();
    this->v2.clear();
    this->vVictory.clear();
    this->m_Speaker.clear();

    //初始化比赛轮数
    this->m_Index = 1;

    //bug4:在初始化时,没有初始化记录容器
    //将记录的容器也清空
    this->m_Record.clear();
}

//创建12名选手
void SpeechManager::createSpeaker()
{
    string nameSeed = "ABCDEFGHIJKL";
    for (int i = 0; i < nameSeed.size(); i++)
    {
        string name = "选手";
        name += nameSeed[i];

        //创建具体选手
        Speaker sp;
        sp.m_Name = name;

        //初始化选手分数
        for (int j = 0; j < 2; j++)
        {
            sp.m_Score[j] = 0;
        }

        //创建选手编号 并且放入到v1容器中
        this->v1.push_back(i + 10001);

        //选手编号以及对应选手放入到map容器
        this->m_Speaker.insert(make_pair(i + 10001, sp));
    }
}

//开始比赛 比赛整个流程控制函数
void SpeechManager::startSpeech()
{
    //第一轮开始比赛

    //1、抽签
    this->speechDraw();
    //2、比赛
    this->speechContest();
    //3、显示晋级结果
    this->showScore();
    //第二轮开始比赛
    this->m_Index++;
    //1、抽签
    this->speechDraw();
    //2、比赛
    speechContest();
    //3、显示晋级结果
    this->showScore();
    //4、保存分数到文件中
    this->saveRecord();

    //bug3:比完赛后查不到本届比赛的记录,没有实时更新
    //重置比赛 ,获取记录
    //初始化容器和属性
    this->initSpeech();

    //创建12名选手
    this->createSpeaker();

    //加载往届记录
    this->loadRecord();

    cout << "--------------------------本届比赛完毕!--------------------------" << endl;
    system("pause");
    system("cls");
}

//抽签
void SpeechManager::speechDraw()
{
    cout << "第 << " << this->m_Index << " >> 轮比赛选手正在抽签" << endl
         << "---------------------------------------" << endl
         << "抽签后的演讲顺序如下: " << endl;

    if (this->m_Index == 1)
    {
        //第一轮比赛
        random_shuffle(v1.begin(), v1.end());//随机抽签
        for (vector<int>::iterator it = v1.begin(); it != v1.end(); it++)
        {
            cout << *it << " ";
        }
        cout << endl;
    }
    else
    {
        //第二轮比赛
        random_shuffle(v2.begin(), v2.end());
        for (vector<int>::iterator it = v2.begin(); it != v2.end(); it++)
        {
            cout << *it << " ";
        }
        cout << endl;
    }

    cout << "---------------------------------------" << endl;
    system("pause");
    cout << endl;
}

//比赛
void SpeechManager::speechContest()
{
    cout << "-----------------------第 " << this->m_Index << " 轮比赛正式开始-----------------------" << endl;
    
    //准备临时容器去存放小组成绩,mutimap可以允许插入重复的key
    multimap<double, int, greater<double>> groupScore;//分数,编号,倒叙,分数作为key是为了后面取前三名方便

    int num = 0;//统计记录人员的个数,6人一组

    vector<int>v_Src;//比赛选手容器
    if (this->m_Index == 1)
    {
        v_Src = v1;
    }
    else
    {
        v_Src = v2;
    }

    //遍历所有选手进行比赛
    for (vector<int>::iterator it = v_Src.begin(); it != v_Src.end(); it++)
    {
        //评委打分
        deque<double>d;
        for (int i = 0; i < 10; i++)
        {
            double score = (rand() % 401 + 600) / 10.0f;//600~1000,10.0f是告诉它是个小数
            cout << score << " " ;
            d.push_back(score);
        }
        cout << endl;

        num++;//人员个数+1

        //排序
        sort(d.begin(), d.end(), greater<double>());
        d.pop_front();//去除最高值
        d.pop_back();//去除最低值

        double sum = accumulate(d.begin(), d.end(), 0.0f);//0是起始累加值
        double avg = sum / (double)d.size();//平均分

     
        //将平均分放入到map容器里
        this->m_Speaker[*it].m_Score[this->m_Index - 1] = avg;

        //打印平均分
        cout << "编号: " << *it << " 姓名: " << this->m_Speaker[*it].m_Name
            << " 获取平均分: " << this->m_Speaker[*it].m_Score[this->m_Index - 1] << endl;
        cout << endl;

        //将打分数据放入临时小组容器中
        groupScore.insert(make_pair(avg, *it));//key是得分,value是其选手的编号
       
        //每六人取出前三名
        if (num % 6 == 0)
        {
            cout << "第" << num / 6 << "小组比赛名次: " << endl;
            for (multimap<double, int, greater<double>>::iterator it = groupScore.begin(); it != groupScore.end(); it++)
            {
                cout << "编号: " << it->second << " 姓名: " << this->m_Speaker[it->second].m_Name
                     << " 成绩: " << this->m_Speaker[it->second].m_Score[m_Index - 1] << endl;
            }//greater<double>降序排序的语法

            //取走前三名
            int count = 0;
            for (multimap<double, int, greater<double>>::iterator it = groupScore.begin(); it != groupScore.end()&&count<3; it++, count++)
            {
                if (this->m_Index == 1)
                {
                    v2.push_back((*it).second);
                }
                else
                {
                    vVictory.push_back((*it).second);
                }
            }

            groupScore.clear();//小组容器清空
            cout << endl;
        }
    }
    cout << endl;
    cout << "-----------------------第 " << this->m_Index << " 轮比赛完毕-----------------------" << endl;
    system("pause");

}

//显示得分
void SpeechManager::showScore()
{
    cout << "--------------------第 " << this->m_Index << " 轮晋级选手信息如下--------------------" << endl;

    vector<int>v;
    if (this->m_Index == 1)
    {
        v = v2;
    }
    else
    {
        v = vVictory;
    }

    for (vector<int>::iterator it = v.begin(); it != v.end(); it++)
    {
        cout << "选手编号: " << *it << " 姓名: " << this->m_Speaker[*it].m_Name 
             << " 得分: " << this->m_Speaker[*it].m_Score[this->m_Index - 1] << endl;
        
    }
    cout << endl;

    system("pause");
    system("cls");
    this->show_Menu();
}

//保存记录到文件中
void SpeechManager::saveRecord()
{
    ofstream ofs;//写数据
    ofs.open("speech.csv", ios::out | ios::app);//用追加的方式写文件

    //将每个选手的数据写入到文件中
    for (vector<int>::iterator it = vVictory.begin(); it != vVictory.end(); it++)
    {
        ofs << *it << "," << this->m_Speaker[*it].m_Score[1] << ",";
    }
    ofs << endl;

    //关闭文件
    ofs.close();
    cout << "记录已经保存" << endl;

    //bug2:若记录为空或不存在,比完赛后依然提示记录为空
    //更改文件不为空状态
    this->fileIsEmpty = false;
}

//读取文件中的往届记录
void SpeechManager::loadRecord()
{
    ifstream ifs("speech.csv",ios::in);//读文件
    if (!ifs.is_open())
    {
        this->fileIsEmpty = true;
        cout << "文件不存在" << endl;
        ifs.close();
        return;
    }

    //文件清空情况
    char ch;
    ifs >> ch;//取一个字符看看是不是直接读到尾了
    if (ifs.eof())
    {
        cout << "文件为空" << endl;
        this->fileIsEmpty = true;
        ifs.close();
        return;
    }

    //文件不为空
    this->fileIsEmpty = false;
    ifs.putback(ch);//将上面ch取的那一个字符给拿回来

    string data;
    int index = 0;//第0届
    while (ifs >> data)
    {
        //cout<<data<<endl;
       
        vector<string>v;//存放6个string的字符串

        int pos = -1;//查到“,”位置的变量,-1则是上来就默认为找不到","
        int start = 0;//起始位置为0

        while (true)
        {
            pos = data.find(",", start);//从start = 0开始索引,找不到","时,返给pos的值是-1
            if (pos == -1)
            {
                //没有找到的情况
                break;
            }
            string temp = data.substr(start, pos - start);//把不是逗号的数据给截走,截的是start~pos -start之间的数据,也就是不是逗号的数据
            //cout << temp << endl;

            v.push_back(temp);

            start = pos + 1;
        }

        this->m_Record.insert(make_pair(index, v));
        index++;
    }

    ifs.close();
    for (map<int, vector<string>>::iterator it = m_Record.begin(); it != m_Record.end(); it++)
    {
        cout << "第" << it->first + 1 << "届" << " 冠军编号: " << it->second[0] << " 分数: " << it->second[1] << endl;
    }
}

//显示保存到文件中的记录
void SpeechManager::showRecord()
{
    if (this->fileIsEmpty)//bug1:查看往届比赛记录,若文件不存在或为空,但并未提示
    {
        cout << "文件为空或者文件不存在!" << endl;
    }
    else
    {
        for (int i = 0; i < this->m_Record.size(); i++)
        {
            cout << "第" << i + 1 << "届 "
                << "冠军编号:" << this->m_Record[i][0] << " 得分: " << this->m_Record[i][1] << " "
                << "亚军编号:" << this->m_Record[i][2] << " 得分: " << this->m_Record[i][3] << " "
                << "季军编号:" << this->m_Record[i][4] << " 得分: " << this->m_Record[i][5] << endl;
        }
    }


    system("pause");
    system("cls");
}

//清空记录
void SpeechManager::clearRecord()
{
    cout << "是否确定清空文件?" << endl;
    cout << "1、是" << endl;
    cout << "2、否" << endl;

    int select = 0;
    cin >> select;

    if (select == 1)
    {
        //确认清空
        ofstream ofs("speech.csv", ios::trunc);//打开模式 ios::trunc 如果存在则删除文件并重新创建一个新文件
        ofs.close();

        //初始化容器和属性
        this->initSpeech();

        //创建12名选手
        this->createSpeaker();

        //加载往届记录
        this->loadRecord();

        cout << "清空成功!" << endl;
    }

    system("pause");
    system("cls");
}

演讲比赛流程管理系统.cpp

#include "SpeechManager.h"
#include <ctime>//随机数种子
//比赛规则
/*
学校举行一场演讲比赛,共有12个人参加。比赛共两轮,第一轮为淘汰赛,第二轮为决赛。
每名选手都有对应的编号,如10001~10012
比赛方式:分组比赛,每组6人
第一轮分为两个小组,整体按照选手编号进行抽签后顺序演讲
十个评委分别给每名选手打分,去除最高分和最低分,求的平均分为本轮选手的成绩
当小组演讲完后,淘汰组内排名最后的三个选手,前三名晋级,进入下一轮的比赛
第二轮为决赛,前三名胜出
每轮比赛过后需要显示晋级选手的信息
*/

//创建管理类
/*
功能描述:
提供菜单界面与用户交互
对演讲比赛流程进行控制
与文件的读写交互
*/

//退出系统
/*
退出功能:实现退出程序
提供功能接口:在main函数中提供分支选择,提供每个功能接口
*/

//演讲比赛功能
/*
一、功能分析
比赛流程分析:抽签→开始演讲比赛→显示第一轮比赛结果→抽签→开始演讲比赛→显示前三名结果→保存分数
二、创建选手类
1、选手类中的属性包含:选手姓名,分数;
2、头文件中创建speaker.h文件,并添加代码。
三、比赛
1、成员属性添加
四、保存分数


*/
int main()
{
	//初始化对象
	SpeechManager sm;

	/*for (map<int, Speaker>::iterator it = sm.m_Speaker.begin(); it != sm.m_Speaker.end(); it++)
	{
		cout << "选手编号: " << it->first << " 姓名: " << it->second.m_Name
			 << " 第一次分数: " << it->second.m_Score[0] << " 第二次分数: " << it->second.m_Score[1] << endl;
	}*/

	int choice = 0;

	while (true)
	{
		//随机数种子
		srand((unsigned int)time(NULL));
		//展示菜单
		sm.show_Menu();
		//提示用户选择选项
		cin >> choice;

		switch (choice)
		{
		case 1://开始比赛
			sm.startSpeech();
			break;
		case 2://查看往届比赛记录
			sm.showRecord();
			break;
		case 3://清空比赛记录
			sm.clearRecord();
			break;
		case 0://退出系统
			sm.exitSystem();
			break;
		default:
			system("cls");//清屏
			break;
		}
	}

	system("pause");
	return 0;
}

7.机房预约系统

导图

在这里插入图片描述
在这里插入图片描述

在这里插入图片描述

※※※学生验证注意事项/保存.txt文件时的编码选择

在这里插入图片描述

Identity.h

#pragma once//防止头文件重复包含
#include <iostream>//标准输入输出流
#include <string>
#include <fstream>//文件操作流
#include "globalFile.h"
#include "computerRoom.h"
#include "orderFile.h"
#include <vector>
using namespace std;

//身份抽象基类
class Identity
{
public:
	//操作菜单 
	virtual void operMenu() = 0;//纯虚函数


	//用户名
	string m_Name;
	//密码
	string m_Pwd;
};

globalFile.h

#pragma once
#include <iostream>
using namespace std;

//管理员文件
#define ADMIN_FILE     "admin.txt"

//学生文件
#define STUDENT_FILE   "student.txt"

//教师文件
#define TEACHER_FILE   "teacher.txt"

//机房信息文件
#define COMPUTER_FILE  "computerRoom.txt"

//订单文件
#define ORDER_FILE     "order.txt"

manager.h

#pragma once
#include "Identity.h"
#include "student.h"
#include "teacher.h"
#include "computerRoom.h"

//管理员类设计
class Manager:public Identity
{
public:

	//默认构造
	Manager();

	//有参构造 管理员姓名,密码
	Manager(string name, string pwd);

	//选择菜单
	virtual void operMenu();

	//添加账号
	void addPerson();

	//查看账号
	void showPerson();

	//机房信息容器
	vector<ComputerRoom>vCom;

	//查看机房信息
	void showComputer();

	//清空所有预约记录
	void cleanFile();

	//初始化容器
	void initVector();

	//学生容器
	vector<Student>vStu;

	//老师容器
	vector<Teacher>vTea;
	
	//去重函数封装
	//检测重复 参数(传入id,传入的类型是学生还是老师)  返回值(true:代表有重复,false:代表没有重复)
	bool checkRepeat(int id, int type);

};

computerRoom.h

#pragma once
#include <iostream>
using namespace std;

//机房类
class ComputerRoom
{
public:
	
	int m_ComId;//机房Id号

	int m_MaxNum;//机房最大容量
};
//因为机房信息目前版本不会有所改动,如果后期有修改功能,最好封装到一个函数中,方便维护

student.h

#pragma once
#include <iostream>
using namespace std;
#include "Identity.h"
#include "orderFile.h"

//设计学生类
class Student :public Identity
{
public:

	//默认构造
	Student();

	//有参构造 参数:学号,姓名,密码
	Student(int id, string name, string pwd);

	//菜单界面
	virtual void operMenu();

	//机房容器
	vector<ComputerRoom>vCom;

	//申请预约
	void applyOrder();

	//查看自身预约
	void showMyOrder();

	//查看所有预约
	void showAllOrder();

	//取消预约
	void cancelOrder();

	//学生学号
	int m_Id;
};

orderFile.h

#pragma once
#include <iostream>
using namespace std;
#include "globalFile.h"
#include <fstream>
#include <map>
#include <string>

class OrderFile
{
public:

	//构造函数
	OrderFile();

	//更新预约记录
	void updateOrder();
	
	//析构函数
	~OrderFile();
	
	//记录预约条数
	int m_Size;

	//记录所有预约数据的容器 key为记录条数 value为具体记录的键值对信息
	map<int, map<string, string>> m_orderData;
};

teacher.h

#pragma once
#include <iostream>
using namespace std;
#include "Identity.h"

//教师类设计
class Teacher:public Identity
{
public:

	//默认构造
	Teacher();

	//有参构造(职工编号,姓名,密码)
	Teacher(int empId, string name, string pwd);

	//菜单界面
	virtual void operMenu();

	//查看所有预约
	void showAllOrder();

	//审核预约
	void validOrder();

	//职工号
	int m_EmpID;
};

manager.cpp

#include "manager.h"
#include <algorithm>//for_each遍历算法要包含的头文件
//默认构造
Manager::Manager()
{

}

//有参构造 管理员姓名,密码
Manager::Manager(string name, string pwd)
{
	//初始化管理员信息
	this->m_Name = name;//把传进来的姓名赋给自身的姓名
	this->m_Pwd = pwd;//把传进来的密码赋给自身的密码

    //初始化容器 获取到文件中老师和学生的信息
    this->initVector();

    //初始化机房信息
    ifstream ifs;
    ifs.open(COMPUTER_FILE, ios::in);

    ComputerRoom com;
    while (ifs>>com.m_ComId&&ifs>>com.m_MaxNum)
    {
        vCom.push_back(com);
    }

    ifs.close();
    cout << "机房数量为: " << vCom.size() << endl;
}

//选择菜单
void Manager::operMenu()
{
	cout << "欢迎管理员:" << this->m_Name << "登录!" << endl;
    cout << "\t\t --------------------------------- \n";
    cout << "\t\t|                                 |\n";
    cout << "\t\t|           1.添加账号            |\n";
    cout << "\t\t|                                 |\n";
    cout << "\t\t|                                 |\n";
    cout << "\t\t|           2.查看账号            |\n";
    cout << "\t\t|                                 |\n";
    cout << "\t\t|                                 |\n";
    cout << "\t\t|           3.查看机房            |\n";
    cout << "\t\t|                                 |\n";
    cout << "\t\t|                                 |\n";
    cout << "\t\t|           4.清空预约            |\n";
    cout << "\t\t|                                 |\n";
    cout << "\t\t|                                 |\n";
    cout << "\t\t|           0.注销登录            |\n";
    cout << "\t\t|                                 |\n";
    cout << "\t\t --------------------------------- \n";
    cout << "请输入选项选择您的操作: " << endl;
}

//添加账号
/*
去重操作:添加新账号时,如果是重复的学生编号或是重复的教师职工编号,提示有误

    1.读取信息:
    要去除重复的账号,首先要先将学生和教师的账号信息获取到程序中,方可检测
    在manager.h中,添加两个容器,由于存放学生和教师的信息
    添加一个新的成员函数 coid initVector() 来初始化容器
    2.去重函数封装
*/
void Manager::addPerson()
{
    cout << "请输入添加账号类型" << endl;
    cout << "1、添加学生" << endl;
    cout << "2、添加老师" << endl;

    string fileName;//操作文件名
    string tip;//提示id号
    string errorTip;//重复错误提示

    ofstream ofs;//文件操作对象

    int select = 0;
    cin >> select;//接收用户的选项

    if (select == 1)
    {
        //添加学生
        fileName = STUDENT_FILE;
        tip = "请输入学号:";
        errorTip = "学号重复,请重新输入";
    }
    else
    {
        //添加老师
        fileName = TEACHER_FILE;
        tip = "请输入职工编号:";
        errorTip = "职工编号重复,请重新输入";
    }

    //利用追加的方式写文件
    ofs.open(fileName, ios::out | ios::app);

    int id;//学号 或 职工号
    string name;//姓名
    string pwd;//密码

    cout << tip << endl;//请输入职工编号
    //查看是否有重复
    while (true)
    {
        cin >> id;
        bool ret = checkRepeat(id, select);
        if (ret)//有重复
        {
            cout << errorTip << endl;
        }
        else
        {
            break;
        }
    }

    cout << "请输入姓名:" << endl;
    cin >> name;

    cout << "请输入密码:" << endl;
    cin >> pwd;

    //向文件中添加数据
    ofs << id << " " << name << " " << pwd << " " << endl;
    cout << "添加成功" << endl;

    ofs.close();
    this->initVector();//bug解决:在每次添加新账号时,重新初始化容器

    system("pause");
    system("cls");

    return;
}

//查看账号中要包含的打印函数
//这里用的是普通函数,还可以用仿函数
//查看学生账号
void printStudent(Student& s)
{
    cout << "学号: " << s.m_Id << " 姓名: " << s.m_Name << " 密码: " << s.m_Pwd << endl;
}

//查看老师账号
void printTeacher(Teacher& t)
{
    cout << "学号: " << t.m_EmpID << " 姓名: " << t.m_Name << " 密码: " << t.m_Pwd << endl;
}

//查看账号
void Manager::showPerson()
{
    cout << "请选择查看的内容:" << endl;
    cout << "1、查看所有学生" << endl;
    cout << "2、查看所有老师" << endl;

    int select = 0;
    cin >> select;

    if (select == 1)
    {
        //查看学生
        cout << "所有学生信息如下:" << endl;
        for_each(vStu.begin(), vStu.end(), printStudent);
    }
    else
    {
        //查看老师
        cout << "所有老师信息如下: " << endl;
        for_each(vTea.begin(), vTea.end(), printTeacher);
    }

    system("pause");
    system("cls");
}

//查看机房信息
//在项目文件夹里找到order.txt, 手动输入。
//一号机房容量为20台,二号机房为50台,三号机房为100台。
void Manager::showComputer()
{
    cout << "机房信息如下: " << endl;

    for (vector<ComputerRoom>::iterator it = vCom.begin(); it != vCom.end(); it++)
    {
        cout << "机房的Id号为: " << it->m_ComId << " 机房的最大容量为: " << it->m_MaxNum << endl;
    }

    system("pause");
    system("cls");
}

//清空所有预约记录
//功能描述:清空生成的order.txt文件
void Manager::cleanFile()
{
    cout << "是否要清空所有预约记录?" << endl;
    cout << "0、取消清空" << endl;
    cout << "1、确认清空" << endl;
    
    int select = 0;
    cin >> select;

    if (select == 1)
    {
        ofstream ofs(ORDER_FILE, ios::trunc);
        ofs.close();

        cout << "清空成功!" << endl;
    }
    else
    {
        cout << "已取消清空" << endl;
    }


    system("pause");
    system("cls");
}

//初始化容器
void Manager::initVector()
{
    //确保容器是清空状态
    vStu.clear();
    vTea.clear();

    //读取信息 学生、老师
    ifstream ifs;
    ifs.open(STUDENT_FILE, ios::in);
    if (!ifs.is_open())
    {
        cout << "文件读取失败" << endl;
        return;
    }

    Student s;
    while (ifs >> s.m_Id && ifs >> s.m_Name && ifs >> s.m_Pwd)
    {
        vStu.push_back(s);
    }

    cout << "当前学生数量为: " << vStu.size() << endl;
    ifs.close();

    //读取信息 老师
    ifs.open(TEACHER_FILE, ios::in);
    Teacher t;
    while (ifs >> t.m_EmpID && ifs >> t.m_Name && ifs >> t.m_Pwd)
    {
        vTea.push_back(t);
    }

    cout << "当前老师数量为: " << vTea.size() << endl;
    ifs.close();

}

//去重函数封装
//检测重复 参数(传入id,传入的类型是学生还是老师)  返回值(true:代表有重复,false:代表没有重复)
//bug:虽然可以检测重复的账号,但是刚刚添加的账号由于没有更新到容器中,因此不会做检测,导致刚加入的账号的学生号或职工编号,在再次添加的时候依然可以重复
//bug解决:在每次添加新账号时,重新初始化容器
bool Manager::checkRepeat(int id, int type)
{
    if (type == 1)
    {
        //检测学生
        for (vector <Student>::iterator it = vStu.begin(); it != vStu.end(); it++)
        {
            if (id == it->m_Id)
            {
                return true;
            }
        }
    }
    else
    {
        //检测老师
        for (vector <Teacher>::iterator it = vTea.begin(); it != vTea.end(); it++)
        {
            if (id == it->m_EmpID)
            {
                return true;
            }
        }
    }

    return false;
}

student.cpp

#include "student.h"

//默认构造
Student::Student()
{

}

//有参构造 参数:学号,姓名,密码
Student::Student(int id, string name, string pwd)
{
	//初始化操作
	this->m_Id=id;
	this->m_Name=name;
	this->m_Pwd=pwd;

    //初始化机房信息
    ifstream ifs;
    ifs.open(COMPUTER_FILE, ios::in);

    ComputerRoom com;
    while (ifs >> com.m_ComId && ifs >> com.m_MaxNum)
    {
        //将读取的信息放入到容器中
        vCom.push_back(com);
    }

    ifs.close();
}

//菜单界面
void Student::operMenu()
{
    cout << "欢迎学生代表:" << this->m_Name << "登录!" << endl;
    cout << "\t\t --------------------------------- \n";
    cout << "\t\t|                                 |\n";
    cout << "\t\t|         1.申请预约              |\n";
    cout << "\t\t|                                 |\n";
    cout << "\t\t|                                 |\n";
    cout << "\t\t|         2.查看我的预约          |\n";
    cout << "\t\t|                                 |\n";
    cout << "\t\t|                                 |\n";
    cout << "\t\t|         3.查看所有预约          |\n";
    cout << "\t\t|                                 |\n";
    cout << "\t\t|                                 |\n";
    cout << "\t\t|         4.取消预约              |\n";
    cout << "\t\t|                                 |\n";
    cout << "\t\t|                                 |\n";
    cout << "\t\t|         0.注销登录              |\n";
    cout << "\t\t|                                 |\n";
    cout << "\t\t --------------------------------- \n";
    cout << "请输入选项选择您的操作: " << endl;
}

//申请预约
void Student::applyOrder()
{
    cout << "机房开放的时间为周一至周五" << endl;
    cout << "请输入申请预约的日期: " << endl;
    cout << "1、周一" << endl << "2、周二" << endl << "3、周三" << endl << "4、周四" << endl << "5、周五" << endl;

    int date = 0;//日期
    int interval = 0;//时间段
    int room = 0;//机房编号

    while (true)
    {
        cin >> date;
        if (date >= 1 && date <= 5)
        {
            break;
        }
        cout << "输入有误,请重新输入" << endl;
    }

    cout << "请输入申请预约时间段: " << endl;
    cout << "1、上午" << endl << "2、下午" << endl;

    while (true)
    {
        cin >> interval;
        if (interval >= 1 && interval <= 2)
        {
            break;
        }
        cout << "输入有误,请重新输入" << endl;
    }

    cout << "机房如下: " << endl;
    for (int i = 0; i < vCom.size(); i++)
    {
        cout << vCom[i].m_ComId << "号机房容量为: " << vCom[i].m_MaxNum << endl;
    }
    cout << endl;

    while (true)
    {
        cout << "请输入1~3来选择机房:";
        cin >> room;
        if (room >= 1 && room <= 3)
        {
            break;
        }
        cout << "输入有误,请重新输入" << endl;
    }

    cout << "预约成功!审核中..." << endl;

    ofstream ofs;
    ofs.open(ORDER_FILE, ios::app);

    ofs << "date:" << date << " ";
    ofs << "interval:" << interval << " ";
    ofs << "stuId:" << this->m_Id << " ";
    ofs << "stuName:" << this->m_Name << " ";
    ofs << "roomId:" << room << " ";
    ofs << "status:" << 1 << endl;

    ofs.close();

    system("pause");
    system("cls");
}

//查看自身预约
void Student::showMyOrder()
{
    OrderFile of;

    if (of.m_Size == 0)
    {
        cout << "无预约记录!" << endl;
        system("pause");
        system("cls");
        return;
    }

    for (int i = 0; i < of.m_Size; i++)
    {
        //atoi(of.m_orderData[i]["stuId"].c_str()) 意思是先用.c_str()把string转换成char,再用atoi(...)转换成int
        if (this->m_Id == atoi(of.m_orderData[i]["stuId"].c_str()))
        {
            cout << "预约日期:周" << of.m_orderData[i]["date"] << endl;
            cout << "时间段: " << (of.m_orderData[i]["interval"] == "1" ? "上午" : "下午") << endl;
            cout << "机房号: " << of.m_orderData[i]["roomId"] << endl;
            string status = "状态: ";
            //1、审核中 2、已预约 -1、预约失败 0、取消预约
            if (of.m_orderData[i]["status"] == "1")
            {
                status += "审核中";
            }
            else if (of.m_orderData[i]["status"] == "2")
            {
                status += "已预约";
            }
            else if (of.m_orderData[i]["status"] == "-1")
            {
                status += "预约失败,审核未通过";
            }
            else
            {
                status += "预约已取消";
            }
            cout << status << endl;
        }
    }

    system("pause");
    system("cls");
    
}

//查看所有预约
void Student::showAllOrder()
{
    OrderFile of;
    if (of.m_Size == 0)
    {
        cout << "无预约记录" << endl;
        system("pause");
        system("cls");
        return;
    }

    for (int i = 0; i < of.m_Size; i++)
    {
        cout << i + 1 << "、" << endl;
        cout << "预约日期:周" << of.m_orderData[i]["date"] << endl;
        cout << "时间段: " << (of.m_orderData[i]["interval"] == "1" ? "上午" : "下午") << endl;
        cout << "学号: " << of.m_orderData[i]["stuId"] << endl;
        cout << "姓名: " << of.m_orderData[i]["stuName"] << endl;
        cout << "机房编号: " << of.m_orderData[i]["roomId"] << endl;
        string status = "状态:";
        //1、审核中 2、已预约 -1、预约失败 0、取消预约
        if (of.m_orderData[i]["status"] == "1")
        {
            status += "审核中";
        }
        else if (of.m_orderData[i]["status"] == "2")
        {
            status += "已预约";
        }
        else if (of.m_orderData[i]["status"] == "-1")
        {
            status += "预约失败,审核未通过";
        }
        else
        {
            status += "预约已取消";
        }
        cout << status << endl;
    }

    system("pause");
    system("cls");
}

//取消预约
void Student::cancelOrder()
{
    //初始化预约记录文件
    OrderFile of;

    if (of.m_Size == 0)
    {
        cout << "无预约记录" << endl;
        system("pause");
        system("cls");
        return;
    }

    cout << "审核中或预约成功的记录可以取消,请输入取消的记录" << endl;
    
    int index = 1;
    vector<int>v;//存放在最大容器中的下表编号,也是自己的预约信息的容器

    for (int i = 0; i < of.m_Size; i++)
    {
        //先判断是自身学号
        if (this->m_Id == atoi(of.m_orderData[i]["stuId"].c_str()))
        {
            //再筛选状态 审核中或预约成功
            if (of.m_orderData[i]["status"] == "1" || of.m_orderData[i]["status"] == "2")
            {
                v.push_back(i);
                cout << index++ << "、 " << endl;
                cout << "预约日期:周" << of.m_orderData[i]["date"] << endl;
                cout << "时间段: " << (of.m_orderData[i]["interval"] == "1" ? "上午" : "下午") << endl;
                cout << "机房编号: " << of.m_orderData[i]["roomId"] << endl;
                string status = "状态: ";
                if (of.m_orderData[i]["status"] == "1")
                {
                    status += "审核中";
                }
                else if (of.m_orderData[i]["status"] == "2")
                {
                    status += "审核成功";
                }
                cout << status << endl;
            }
        }
    }

    cout << "请输入数字来选择要取消的记录,0代表返回" << endl;
    int select = 0;

    while (true)
    {
        cin >> select;

        if (select >= 0 && select <= v.size())
        {
            if (select == 0)
            {
                break;
            }
            else
            {
                of.m_orderData[v[select - 1]]["status"] = "0";
                of.updateOrder();
                cout << "-------------已经取消预约-------------" << endl;
                break;
            }
        }

        cout << "输入有误,请重新输入" << endl;
    }
    system("pause");
    system("cls");
}

orderFile.cpp

#include "orderFile.h"


//构造函数
OrderFile::OrderFile()
{
	ifstream ifs;
	ifs.open(ORDER_FILE, ios::in);
	
	string date;//日期
	string interval;//时间段
	string stuId;//学生学号
	string stuName;//学生姓名
	string roomId;//机房编号
	string status;//预约状态

	this->m_Size = 0;//记录条数

	while (ifs >> date && ifs >> interval && ifs >> stuId && ifs >> stuName && ifs >> roomId && ifs >> status)
	{
		//测试
		//cout << date << endl << interval << endl << stuId << endl << stuName << endl << roomId << endl << statue << endl;
	
		string key;
		string value;
		map<string, string>m;

		//截取日期
		//date:1
		//d - 0,a - 1,t - 2,e - 3,":" - 4,"1" - 5,那么返回":"的pos值为4,即pos = 4
		int pos = date.find(":");
		if (pos != -1)
		{
			//pos在这里指的是截取字符串的长度(即,该地方所添加的参数的意思是截取字符串的长度)
		    //从位置0开始截取长度为4的字符串,也就是最终会截取完"e"为止,0~3的长度为4
			key = date.substr(0, pos);

			//date.size() = 6(即“date:1”的长度为6)
			//从位置pos + 1 = 5开始截取长度为date.size() - pos - 1 = 1的字符串,也就是最终会截取完"1"为止
			//这里也可以理解为 data.size() - (pos + 1) 或 data.size() - 1 - pos ,三种理解的方法都可以
			//5~6的长度为1
			value = date.substr(pos + 1, date.size() - pos - 1);

			//测试
			//cout << "key = " << key << endl;
			//cout << "value = " << value << endl;

			m.insert(make_pair(key, value));
		}

		//截取时间段
		//interval:1
		pos = interval.find(":");
		if (pos != -1)
		{
			
			key = interval.substr(0, pos);
			value = interval.substr(pos + 1, interval.size() - pos - 1);

			//测试
			//cout << "key = " << key << endl;
			//cout << "value = " << value << endl;

			m.insert(make_pair(key, value));
		}
		
		//截取学生学号
		//stuId:1
		pos = stuId.find(":");
		if (pos != -1)
		{

			key = stuId.substr(0, pos);
			value = stuId.substr(pos + 1, stuId.size() - pos - 1);

			//测试
			//cout << "key = " << key << endl;
			//cout << "value = " << value << endl;

			m.insert(make_pair(key, value));
		}

		//截取学生姓名
		//stuName:张三
		pos = stuName.find(":");
		if (pos != -1)
		{

			key = stuName.substr(0, pos);
			value = stuName.substr(pos + 1, stuName.size() - pos - 1);

			//测试
			//cout << "key = " << key << endl;
			//cout << "value = " << value << endl;

			m.insert(make_pair(key, value));
		}

		//截取机房编号
		//roomId:1
		pos = roomId.find(":");
		if (pos != -1)
		{

			key = roomId.substr(0, pos);
			value = roomId.substr(pos + 1, roomId.size() - pos - 1);

			//测试
			//cout << "key = " << key << endl;
			//cout << "value = " << value << endl;

			m.insert(make_pair(key, value));
		}

		//截取预约状态
		//status:1
		pos = status.find(":");
		if (pos != -1)
		{

			key = status.substr(0, pos);
			value = status.substr(pos + 1, status.size() - pos - 1);

			//测试
			//cout << "key = " << key << endl;
			//cout << "value = " << value << endl;

			m.insert(make_pair(key, value));
		}

		//将小map容器放到大map容器中
		this->m_orderData.insert(make_pair(this->m_Size, m));

		//条数+1
		this->m_Size++;
	}

	ifs.close();

	//测试最大map容器
	//for (map<int, map<string, string>>::iterator it = m_orderData.begin(); it != m_orderData.end(); it++)
	//{
	//	cout << "条数:  " << it->first + 1 << endl;
	//	for (map<string, string>::iterator mit = it->second.begin(); mit != it->second.end(); mit++)
	//	{
	//		cout << "key  = " << mit->first << " value = " << mit->second << endl;
	//	}
	//	cout << endl;
	//}
}

//析构函数
OrderFile::~OrderFile()
{

}

//更新预约记录
void OrderFile::updateOrder()
{
	if (this->m_Size == 0)
	{
		return;//预约记录0条,直接返回
	}

	ofstream ofs(ORDER_FILE, ios::out | ios::trunc);//删了重建来更新记录
	for (int i = 0; i < this->m_Size; i++)
	{
		ofs << "date:" << this->m_orderData[i]["date"] << " ";
		ofs << "interval:" << this->m_orderData[i]["interval"] << " ";
		ofs << "stuId:" << this->m_orderData[i]["stuId"] << " ";
		ofs << "stuName:" << this->m_orderData[i]["stuName"] << " ";
		ofs << "roomId:" << this->m_orderData[i]["roomId"] << " ";
		ofs << "status:" << this->m_orderData[i]["status"] << endl;
	}

	ofs.close();
}

teacher.cpp

#include "teacher.h"

//默认构造
Teacher::Teacher()
{

}

//有参构造(职工编号,姓名,密码)
Teacher::Teacher(int empId, string name, string pwd)
{
	this->m_EmpID = empId;
	this->m_Name = name;
	this->m_Pwd = pwd;
}

void Teacher::operMenu()
{
    cout << "欢迎老师:" << this->m_Name << "登录!" << endl;
    cout << "\t\t --------------------------------- \n";
    cout << "\t\t|                                 |\n";
    cout << "\t\t|         1.查看所有预约          |\n";
    cout << "\t\t|                                 |\n";
    cout << "\t\t|                                 |\n";
    cout << "\t\t|         2.审核预约              |\n";
    cout << "\t\t|                                 |\n";
    cout << "\t\t|                                 |\n";
    cout << "\t\t|         0.注销登录              |\n";
    cout << "\t\t|                                 |\n";
    cout << "\t\t --------------------------------- \n";
    cout << "请输入选项选择您的操作: " << endl;
}

//查看所有预约
void Teacher::showAllOrder()
{
    OrderFile of;
    
    if (of.m_Size == 0)
    {
        cout << "无预约记录" << endl;
        system("pause");
        system("cls");
        return;
    }

    for (int i = 0; i < of.m_Size; i++)
    {
        cout << i + 1 << "、 " << endl;
        cout << "预约日期:周" << of.m_orderData[i]["date"] << endl;
        cout << "时间段: " << of.m_orderData[i]["interval"] << endl;
        cout << "学号: " << of.m_orderData[i]["stuId"] << endl;
        cout << "姓名: " << of.m_orderData[i]["stuName"] << endl;
        cout << "机房编号: " << of.m_orderData[i]["roomId"] << endl;
        string status = "状态: ";
        //1、审核中 2、已预约 -1、预约失败 0、取消预约
        if (of.m_orderData[i]["status"] == "1")
        {
            status += "审核中";
        }
        else if (of.m_orderData[i]["status"] == "2")
        {
            status += "已预约";
        }
        else if (of.m_orderData[i]["status"] == "-1")
        {
            status += "预约失败,审核未通过";
        }
        else
        {
            status += "预约已取消";
        }
        cout << status << endl;
    }

    system("pause");
    system("cls");
}

//审核预约
void Teacher::validOrder()
{
    OrderFile of;

    if (of.m_Size == 0)
    {
        cout << "无预约记录" << endl;
        system("pause");
        system("cls");
        return;
    }

    cout << "待审核的预约记录如下: " << endl;
    vector<int>v;
    int index = 0;

    for (int i = 0; i < of.m_Size; i++)
    {
        if (of.m_orderData[i]["status"] == "1")
        {
            v.push_back(i);
            cout << ++index << "、 " << endl;
            cout << "预约日期:周" << of.m_orderData[i]["date"] << endl;
            cout << "时间段: " << (of.m_orderData[i]["interval"] == "1" ? "上午" : "下午") << endl;
            cout << "学号: " << of.m_orderData[i]["stuId"] << endl;
            cout << "姓名: " << of.m_orderData[i]["stuName"] << endl;
            cout << "机房编号: " << of.m_orderData[i]["roomId"] << endl;
            string status = "状态:审核中";
            cout << status << endl;
        }
    }

    cout << "请输入数字来审核预约记录,0代表返回" << endl;
    int select = 0;
    int ret = 0;

    while (true)
    {
        cin >> select;
        if (select >= 0 && select <= v.size())
        {
            if (select == 0)
            {
                break;
            }
            else
            {
                cout << "请输入审核结果" << endl;
                cout << "1、通过" << endl;
                cout << "2、不通过" << endl;
                cin >> ret;
                if (ret == 1)
                {
                    //通过情况
                    of.m_orderData[v[select - 1]]["status"] = "2";
                }
                else
                {
                    //不通过情况
                    of.m_orderData[v[select - 1]]["status"] = "-1" ;
                }
                of.updateOrder();
                cout << "----------------审核完毕----------------" << endl;
                break;
            }
        }
        cout << "输入有误,请重新输入" << endl;
    }

    system("pause");
    system("cls");

}

机房预约系统.cpp

//一、机房预约系统需求
/*
1.系统简介:学校现有几个规格不同的机房,由于使用时经常出现“撞车”现象,现开发一套机房预约系统,解决这一问题
2.身份简介
学生代表:申请使用机房
教师:审核学生的预约申请
管理员:给学生、教师创建账号
3、机房简介
1号机房 – 最大容量20人
2号机房 – 最多容量50人
3号机房 – 最多容量100人
4.申请简介
申请的订单每周由管理员负责清空
学生可以预约未来一周内的机房使用,预约的日期为周一至周五,预约时需要选择预约时段(上午、下午)
教师来审核预约,依据实际情况审核预约通过或者不通过
5.系统具体需求
(1)首先进入登陆界面,可选登陆身份有:学生代表、老师、管理员、退出
(2)每个身份都需要进行验证后,进入子菜单
学生需要输入:学号、姓名、登陆密码
老师需要输入:职工号、姓名、登陆密码
管理员需要输入:管理员姓名、登陆密码
(3)学生具体功能
申请预约 – 预约机房
查看自身的预约 – 查看自己的预约状态
查看所有预约 – 查看全部预约信息以及预约状态
取消预约 – 取消自身的预约,预约成功或审核中的预约均可取消
(4)教师具体功能
查看所有预约 – 查看全部预约信息以及预约状态
审核预约 – 对学生的预约进行审核
注销登录 – 退出登录
(5)管理员具体功能
添加账号 – 添加学生或教师的账号,需要检测学生编号或教师职工号是否重复
查看账号 – 可以选择查看学生或教师的全部信息
查看机房 – 查看所有机房的信息
清空预约 – 清空所有预约记录
注销登录 – 退出登录
实现注销功能
*/

//二、创建身身份类
/*
身份的基类:
在整个系统中,有三种身份,分别为:学生代表、老师以及管理员
三种身份有其共性也有其特性,因此我们可以将三种身份抽象出一个身份基类identity
在头文件下创建identity.h文件
*/

//三、登录模块
/*
全局文件添加:
不同的身份可能会用到不同的文件操作,我们可以将所有的文件名定义到一个全局的文件中
在头文件中添加globalFile.h文件

登陆函数封装:
根据用户的选择,进行不同的身份登录
在预约系统的cpp文件中添加全局函数 void LoginIn(string fileName , int type)
参数: fileName – 操作的文件名 type – 登录的身份(1代表学生,2代表老师,3代表管理员)
*/

#include <iostream>
using namespace std;
#include "globalFile.h"
#include "Identity.h"
#include <fstream>  
#include <string>
#include "student.h"
#include "teacher.h"
#include "manager.h"

//先声明来告诉编译器有这个函数,让它别报错,让它自己去找函数去
//或者就不声明,把这些函数都放在LoginIn函数的上面,这样就不用提前声明了
void managerMenu(Identity*& manager);
void studentMenu(Identity*& student);
void teacherMenu(Identity*& teacher);

//登录功能 参数1 操作文件名 参数2 操作身份类型
void LoginIn(string fileName, int type)
{
    //父类指针,用于指向子类对象
    Identity* person = NULL;

    //读文件
    ifstream ifs;
    ifs.open(fileName, ios::in);

    //判断文件是否存在
    if (!ifs.is_open())
    {
        cout << "文件不存在" << endl;
        ifs.close();
        return;
    }

    //准备接收用户信息
    int id = 0;
    string name;
    string pwd;

    //判断身份
    if (type == 1)//学生身份
    {
        cout << "请输入你的学号:" << endl;
        cin >> id;
    }
    else if (type == 2)//老师身份
    {
        cout << "请输入你的职工号:" << endl;
        cin >> id;
    }

    //管理员身份不需要什么号,只需要用户名和密码
    cout << "请输入用户名: " << endl;
    cin >> name;

    cout << "请输入密码: " << endl;
    cin >> pwd;

    if (type == 1)
    {
        //学生身份验证
        int fId;//从文件中获取的id号
        string fName;//从文件中获取的姓名
        string fPwd;//从文件中获取的密码
        while (ifs >> fId && ifs >> fName && ifs >> fPwd)
        {
            读操作验证
            ifs读取的时候,读到空格就会略过,不会读空格,读到回车就会换行开始读
            //cout << fId << endl << fName << endl << fPwd << endl;

            //与用户信息做对比
            if (fId == id && fName == name && fPwd == pwd)
            {
                cout << "学生的验证登录成功!" << endl;
                system("pause");
                system("cls");
                person = new Student(id,name,pwd);
                //学生进入下一级菜单
                studentMenu(person);
                return;
            }
        }
    }
    else if (type == 2)
    {
        //教师身份验证
        int fId;//从文件中获取id号
        string fName;//从文件中获取姓名
        string fPwd;//从文件中获取密码
        while (ifs >> fId && ifs >> fName && ifs >> fPwd)
        {
            if (fId == id && fName == name && fPwd == pwd)
            {
                cout << "教师验证登录成功!" << endl;
                system("pause");
                system("cls");
                person = new Teacher(id, name, pwd);
                //老师进入下一级菜单
                teacherMenu(person);
                return;
            }
        }
    }
    else if (type == 3)
    {
        //管理员身份验证
        string fName;//从文件中获取姓名
        string fPwd;//从文件中获取密码
        while (ifs >> fName && ifs >> fPwd)
        {
            if (name == fName && pwd == fPwd)
            {
                cout << "管理员验证登陆成功!" << endl;
                system("pause");
                system("cls");
                person = new Manager(name,pwd);
               //管理员进入下一次菜单
                managerMenu(person);
                return;
            }
        }
    }

    cout << "验证登陆失败!" << endl;
    system("pause");
    system("cls");

    return;
}

//进入管理员子菜单界面
void managerMenu(Identity * &manager)
{
    while (true)
    {
        //调用管理员子菜单
        //利用多态,用父类指针来创建子类对象,来调用共同的接口,operMenu是重写的纯虚函数,纯虚函数实现之后就可以调用子类的菜单接口
        manager->operMenu();

        //将父类指针转为子类指针,为了调用子类里其它特有的接口
        Manager* man = (Manager*)manager;

        int select = 0;

        //接受用户选择
        cin >> select;

        if (select == 1)//添加账号
        {
            cout << "添加账号" << endl;
            man->addPerson();
        }
        else if (select == 2)//查看账号
        {
            cout << "查看账号" << endl;
            man->showPerson();
        }
        else if (select == 3)//查看机房信息
        {
            cout << "查看机房" << endl;
            man->showComputer();
        }
        else if (select == 4)//清空预约
        {
            cout << "清空预约" << endl;
            man->cleanFile();
        }
        else//注销
        {
            delete manager;//销毁掉堆区对象
            cout << "注销成功" << endl;
            system("pause");
            system("cls");
            return;
        }
    }
}

//进入学生子菜单界面
void studentMenu(Identity * &student)
{
    while (true)
    {
        //调用学生子菜单
        student->operMenu();

        Student* stu = (Student*)student;//父类指针强转成子类指针

        int select = 0;
        cin >> select;//接收用户选择

        if (select == 1)//申请预约
        {
            stu->applyOrder();
        }
        else if (select == 2)//查看自身预约
        {
            stu->showMyOrder();
        }
        else if (select == 3)//查看所有人预约
        {
            stu->showAllOrder();
        }
        else if (select == 4)//取消预约
        {
            stu->cancelOrder();
        }
        else//注销登录
        {
            delete student;
            cout << "注销成功!" << endl;
            system("pause");
            system("cls");
            return;
        }
    }
}

//进入教师子菜单界面
void teacherMenu(Identity * &teacher)
{
    while (true)
    {
        //调用子菜单界面
        teacher->operMenu();

        //强转成子类指针
        Teacher* tea = (Teacher*)teacher;

        int select = 0;//接收用户选择
        cin >> select;
        if (select == 1)//查看所有预约
        {
            tea->showAllOrder();
        }
        else if (select == 2)//审核预约
        {
            tea->validOrder();
        }
        else
        {
            delete teacher;
            cout << "注销成功" << endl;
            system("pause");
            system("cls");
            return;
        }
    }
}


int main()
{
    int select = 0;//用于接收用户的选择

    while (true)
    {
        cout << "===================  欢迎来到传智播客机房预约系统  ===================" << endl;
        cout << endl << "请输入您的身份" << endl;
        cout << "\t\t --------------------------------- \n";
        cout << "\t\t|                                 |\n";
        cout << "\t\t|           1.学生代表            |\n";
        cout << "\t\t|                                 |\n";
        cout << "\t\t|                                 |\n";
        cout << "\t\t|           2.老    师            |\n";
        cout << "\t\t|                                 |\n";
        cout << "\t\t|                                 |\n";
        cout << "\t\t|           3.管 理 员            |\n";
        cout << "\t\t|                                 |\n";
        cout << "\t\t|                                 |\n";
        cout << "\t\t|           0.退    出            |\n";
        cout << "\t\t|                                 |\n";
        cout << "\t\t --------------------------------- \n";
        cout << "输入您的选择: ";

        cin >> select;
        switch (select)//根据用户选择 实现不同接口
        {
        case 1://学生身份
            LoginIn(STUDENT_FILE,1);
            break;
        case 2://老师身份
            LoginIn(TEACHER_FILE,2);
            break;
        case 3://管理员身份
            LoginIn(ADMIN_FILE,3);
            break;
        case 0://退出系统
            cout << "欢迎下一次使用" << endl;
            system("pause");
            return 0;
            break;
        default:
            cout << "输入有误,请重新选择" << endl;
            system("pause");
            system("cls");
            break;
        }
    }


    system("pause");
    return 0;
}
  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值