IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> C++知识库 -> C++黑马 机房预约系统 -> 正文阅读

[C++知识库]C++黑马 机房预约系统

1、机房预约系统.cpp


1、机房预约系统.cpp

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


//教师子菜单
void teacherMenu(Identity*&teacher1)
{
	while (true)
	{
		teacher1->operMenu();//调用教师菜单

		teacher*tea = (teacher*)teacher1;

		int select = 0;
		cin >> select;
		if (select == 1)
		{
			tea->showAllOrder();//查看所有预约
		}
		else if (select == 2)
		{
			tea->validOrder();//审核预约
		}
		else
		{

			//注销操作
			//注销操作
			delete tea;
			cout << "注销成功" << endl;
			system("pause");
			system("cls");
			return;
		}
	}
}


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

		student*stu = (student*)student1;//将父类指针转为子类指针

		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 student1;
			cout << "注销成功" << endl;
			system("pause");
			system("cls");
			return;
		}
	}
}
//管理员子菜单界面
//指针的引用,把identity*当作一个类,类名后加&,就是引用这个类的对象,
//这里是以引用的方式来接收identity类的指针实参
//加不加&的区别就在于,不加重新开辟空间,并创建一个名为manager的指针,
//这个指针内部存放的数据是对实参数据的拷贝(拷贝了一块地址编号)
//加了&,如果不考虑引用的本质,那么就相当于给实参指针起了第二个名字,叫做manager,
//我们可以通过manager这个名字直接去调用实参
//形参是n级指针,实参也是n级指针时,形参指针不会指向实参,而是进行对实参的拷贝,
//如果想让指针指向指针,那么形参指针必须比实参指针高一级

//多态的巧妙之处就在于,可以在不确定要调用那个子类函数时使用多态
void managerMenu(Identity* &manager)
{
	while (true)
	{
		//调用管理员子菜单
		manager->operMenu();

		//将父类指针转为 子类指针,调用子类其他接口
		Manager*man = (Manager*)manager;

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

		if (select == 1)
		{
			//添加账号
			man->addPerson();
		}
		else if (select == 2)
		{
			//查看账号
			man->showPerson();
		}
		else if (select == 3)
		{
			//查看机房
			man->showComputer();
		}
		else if (select == 4)
		{
			//清空预约
			man->cleanFile();
		}
		else
		{
			//注销操作
			delete manager;
			cout << "注销成功" << endl;
			system("pause");
			system("cls");
			return;
		}
	}
}
//登录函数,参数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;
	}

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

	//判断身份
	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)
		{
			if (id == fId && name == fName && pwd == fPwd)
			{
				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 (id == fId && name == fName && pwd == fPwd)
			{
				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;

}
int main()
{
	int select = 0;
	while (true)

	{
		
		cout << "============欢迎来到机房预约系统==========" << endl;
		cout << "请输入您的身份" << endl;
		cout << "\t\t|-------------------|" << endl;
		cout << "\t\t|----1、学生--------|" << endl;
		cout << "\t\t|----2、教师--------|" << endl;
		cout << "\t\t|----3、管理员------|" << endl;
		cout << "\t\t|----0、退出--------|" << endl;
		cout << "\t\t|-------------------|" << endl;
		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("cls");//清零操作			
			break;
		}
		
	}
	
	system("pause");
	return 0;

}

2、Identity.h

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

//身份抽象类,基类
class Identity
{
public:

	//操作菜单
	virtual void operMenu() = 0;//纯虚函数


	//属性

	string m_Name;//姓名
	string m_Pwd;//密码
};

3、student.h

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

//学生类
class student :public Identity
{
public:

	//默认构造
	student();
	
	//有参构造
	student(int id, string name, string pwd);

	//菜单
	virtual void operMenu();

	//申请预约
	void applyOrder();

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

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

	//取消预约
	void cancelOrder();

	//学生属性
	int m_Id;//学号

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

};

4、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|-----------------------|" << endl;
	cout << "\t\t|----1、申请预约--------|" << endl;
	cout << "\t\t|----2、查看自身预约----|" << endl;
	cout << "\t\t|----3、查看所有预约----|" << endl;
	cout << "\t\t|----4、取消预约--------|" << endl;
	cout << "\t\t|----0、注销登录--------|" << endl;
	cout << "\t\t|-----------------------|" << endl;
	cout << "请选择您的操作:" << endl;;
}

//申请预约
void student::applyOrder()
{
	cout << "请输入您申请预约的时间:" << endl;
	cout << "1、周一" << endl;
	cout << "2、周二" << endl;
	cout << "3、周三" << endl;
	cout << "4、周四" << endl;
	cout << "5、周五" << endl;
	int data = 0;//预约日期
	int interval = 0;//预约时间段
	int room = 0;//机房编号
	 
	while (true)
	{
		cin >> data;
		if (data >= 1 && data <= 5)
		{
			break;
		}
		cout << "输入有误" << endl;
	}

	cout << "请输入您选择的时间段" << endl;
	cout << "1、上午" << endl;
	cout << "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;
	}
	while (true)
	{
		cin >> room;
		if (room >= 1 && room <= 3)
		{
			break;
		}
		cout << "输入有误,请重新输入" << endl;
	}

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

	//将预约信息写入文件
	ofstream ofs;
	ofs.open(ORDER_FILE, ios::app);
	//ofs << endl;
	ofs << "data:" << data<<" ";
	ofs << "interval:" << interval << " ";
	ofs << "stuName:" << this->m_Name << " ";
	ofs << "stuId:" << this->m_Id << " ";
	ofs << "roomId:" << room<< " ";
	ofs << "status:" << 1 << endl;

	ofs.close();
	system("pause");
	system("cls");

	
}

//查看自身预约
void student::showMyOrder()
{
	OrderFile ord;
	if (ord.m_size == 0)
	{
		cout << "无预约记录" << endl;
		system("pause");
		system("cls");
		return;
	}

	
	for (int i = 0; i < ord.m_size; i++)
	{
		//string转int
		//利用.c_str()将string转为const char*
		//利用atoi()将const char*转为int	
		if (this->m_Id == atoi(ord.m_orderData[i]["stuId"].c_str()))//找到自身预约
		{
			cout << "预约日期:周" << ord.m_orderData[i]["data"] << " ";
			cout << "预约时间段:" << (ord.m_orderData[i]["interval"]=="1"?"上午":"下午") << " ";
			cout << "机房号:" << ord.m_orderData[i]["roomId"] << " ";
			string status="预约状态:";
			if (ord.m_orderData[i]["status"] == "1")
			{
				status += "审核中";
			}
			else if (ord.m_orderData[i]["status"] == "2")
			{
				status += "预约成功";
			}
			else if (ord.m_orderData[i]["status"] == "-1")
			{
				status += "预约失败,审核未通过";
			}
			else 
			{
				status += "预约已取消";
			}
			cout << status << endl;

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

//查看所有预约
void student::showAllOrder()
{
	OrderFile ord;
	if (ord.m_size == 0)
	{
		cout << "无预约记录" << endl;
		system("pause");
		system("cls");
		return;
	}
	for (int i = 0; i < ord.m_size; i++)
	{
		cout << i + 1 << "、";
		cout << "预约日期:周" << ord.m_orderData[i]["data"] << " ";
		cout << "预约时间段:" << (ord.m_orderData[i]["interval"] == "1" ? "上午" : "下午") << " ";
		cout << "机房号:" << ord.m_orderData[i]["roomId"] << " ";
		cout << "学号:" << ord.m_orderData[i]["stuId"] << " ";
		cout << "姓名:" << ord.m_orderData[i]["stuName"] << " ";

		string status = "预约状态:";
		if (ord.m_orderData[i]["status"] == "1")
		{
			status += "审核中";
		}
		else if (ord.m_orderData[i]["status"] == "2")
		{
			status += "预约成功";
		}
		else if (ord.m_orderData[i]["status"] == "-1")
		{
			status += "预约失败,审核未通过";
		}
		else
		{
			status += "预约已取消";
		}
		cout << status << endl;
		
	}
	system("pause");
	system("cls");
}

//取消预约
void student::cancelOrder()
{
	OrderFile ord;
	if (ord.m_size == 0)
	{
		cout << "无预约记录" << endl;
		system("pause");
		system("cls");
		return;
	}

	cout << "预约成功和审核中的记录可以取消,请输入要取消的记录" << endl;
	vector<int>v;//存放最大容器里满足条件的下标编号
	int index = 1;
	for (int i = 0; i < ord.m_size; i++)
	{
		//先判断是否是自身学号
		if (this->m_Id == atoi(ord.m_orderData[i]["stuId"].c_str()))
		{
			//预约成功/审核中的可以取消
			if (ord.m_orderData[i]["status"] == "1" || ord.m_orderData[i]["status"] == "2")
			{
				v.push_back(i);
				cout << index++ << "、";
				cout << "预约日期:周" << ord.m_orderData[i]["data"] << " ";
				cout << "预约时间段:" << (ord.m_orderData[i]["interval"] == "1" ? "上午" : "下午") << " ";
				cout << "机房号:" << ord.m_orderData[i]["roomId"] << " ";
				cout << "学号:" << ord.m_orderData[i]["stuId"] << " ";
				cout << "姓名:" << ord.m_orderData[i]["stuName"] << " ";

				string status = "预约状态:";
				if (ord.m_orderData[i]["status"] == "1")
				{
					status += "审核中";
				}
				else if (ord.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
			{
				ord.m_orderData[v[select - 1]]["status"] = "0";
				ord.updateOrder();
				cout << "已取消预约" << endl;

				break;
			}
		}
		
	}

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

5、teacher.h

#pragma once
#include<iostream>
using namespace std;
#include"Identity.h"
#include"orderFile.h"
#include<vector>
class teacher :public Identity//教师类
{
public:
	//默认构造
	teacher();

	//有参构造
	teacher(int empId, string name, string pwd);

	//菜单
	virtual  void operMenu();
	//查看所有预约
	void showAllOrder();
	//审核预约
	void validOrder();
	
	//属性
	int empId;//职工号
};

6、teacher.cpp

#include "teacher.h"
teacher::teacher()
{

}
//有参构造
teacher::teacher(int empId, string name, string pwd)
{
	this->empId = empId;
	this->m_Name = name;
	this->m_Pwd = pwd;
}
//菜单
void teacher::operMenu()
{
	cout << "欢迎" << this->m_Name << "登录" << endl;
	cout << "\t\t|--------------------------------|" << endl;
	cout << "\t\t|---------1、查看所有预约--------|" << endl;
	cout << "\t\t|---------2、审核预约------------|" << endl;
	cout << "\t\t|---------0、注销登录------------|" << endl;
	cout << "\t\t|--------------------------------|" << endl;
	cout << "请选择您的操作:" << endl;;
}
//查看所有预约
void teacher::showAllOrder()
{
	OrderFile ord;
	if (ord.m_size == 0)
	{
		cout << "无预约记录" << endl;
		system("pause");
		system("cls");
		return;
	}
	for (int i = 0; i < ord.m_size; i++)
	{
		cout << i + 1 << "、";
		cout << "预约日期:周" << ord.m_orderData[i]["data"] << " ";
		cout << "预约时间段:" << (ord.m_orderData[i]["interval"] == "1" ? "上午" : "下午") << " ";
		cout << "机房号:" << ord.m_orderData[i]["roomId"] << " ";
		cout << "学号:" << ord.m_orderData[i]["stuId"] << " ";
		cout << "姓名:" << ord.m_orderData[i]["stuName"] << " ";

		string status = "预约状态:";
		if (ord.m_orderData[i]["status"] == "1")
		{
			status += "审核中";
		}
		else if (ord.m_orderData[i]["status"] == "2")
		{
			status += "预约成功";
		}
		else if (ord.m_orderData[i]["status"] == "-1")
		{
			status += "预约失败,审核未通过";
		}
		else
		{
			status += "预约已取消";
		}
		cout << status << endl;

	}
	system("pause");
	system("cls");
}
//审核预约
void teacher::validOrder()
{
	OrderFile ord;
	if (ord.m_size == 0)
	{
		cout << "无预约记录" << endl;
		system("pause");
		system("cls");
		return;
	}
	cout << "待审核的记录如下:" << endl;
	vector<int>v;//存放最大容器里满足条件的下标编号
	int index = 1;
	for (int i = 0; i < ord.m_size; i++)
	{
		//先判断是否是审核中
		if (ord.m_orderData[i]["status"] == "1" )
		{
			v.push_back(i);
			cout << index++ << "、";
			cout << "预约日期:周" << ord.m_orderData[i]["data"] << " ";
			cout << "预约时间段:" << (ord.m_orderData[i]["interval"] == "1" ? "上午" : "下午") << " ";
			cout << "机房号:" << ord.m_orderData[i]["roomId"] << " ";
			cout << "学号:" << ord.m_orderData[i]["stuId"] << " ";
			cout << "姓名:" << ord.m_orderData[i]["stuName"] << " ";

			string status = "预约状态:";
			if (ord.m_orderData[i]["status"] == "1")
			{
				status += "审核中";
			}

			cout << status << endl;
		}
	}
	cout << "请输入要审核的记录" << 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)
				{
					ord.m_orderData[v[select - 1]]["status"] = "2";
				}
				else
				{
					ord.m_orderData[v[select - 1]]["status"] = "-1";
				}
				ord.updateOrder();
				cout << "审核完毕" << endl;
				
				break;
		
			}
		}
		
	}
	system("pause");
	system("cls");

}

7、manager.h

#pragma once
#include<iostream>
using namespace std;
#include"Identity.h"
#include<fstream>
#include<vector>
#include"student.h"
#include"teacher.h"
#include"computerRoom.h"
#include<algorithm>

class Manager :public Identity
{
public:
	//默认构造
	Manager();
	//有参构造
	Manager(string name, string pwd);
	//选择菜单
	virtual  void operMenu();
	
	//新建账户
	void addPerson();
	//查看账户
	void showPerson();
	//查看机房信息
	void showComputer();

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

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

	//检测重复,参数1:检测学号 学生/教师    参数2:检测类型,教师/学生
	bool checkRepeat(int id, int type);

	//学生容器
	vector<student>vStu;//获取文件中的学生数据进行比对,看是否重复添加

	//教师容器
	vector<teacher>vTea;//获取文件中的教师数据进行比对,看是否重复添加

	//机房容器
	vector<computerRoom>vCom;
};

8、manager.cpp

#include"manager.h"
#include"globelFile.h"
//默认构造
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|-----------------------|" << endl;
	 cout << "\t\t|----1、添加账号--------|" << endl;
	 cout << "\t\t|----2、查看账号--------|" << endl;
	 cout << "\t\t|----3、查看机房--------|" << endl;
	 cout << "\t\t|----4、清空预约--------|" << endl;
	 cout << "\t\t|----0、注销登录--------|" << endl;
	 cout << "\t\t|-----------------------|" << endl;
	 cout << "请选择您的操作:" << endl;
}

//新建账户
void Manager::addPerson()
{
	cout << "请选择您要添加的账户类型" << endl;
	cout << "1、添加学生" << endl;
	cout << "2、添加老师" << endl;

	string fileName;//操作的文件名
	string tip;//提示的Id号
	string errorTip;//重复输入提示

	ofstream ofs;//文件操作对象,写入操作

	int select;
	cin >> select;

	if (select == 1)
	{
		fileName = STUDENT_FILE;
		tip = "请输入学号";
		errorTip = "学号重复,请重新输入";

	}
	else if (select == 2)
	{
		fileName = TEACHER_FILE;
		tip = "请输入职工号";
		errorTip = "职工号重复,请重新输入";
	}
	else
	{
		cout << "输入有误,请重新输入" << endl;
		system("pause");
		system("cls");
		return;
	}
	//用out的方式进行写文件,并且用追加的方式写
	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;
	system("pause");
	system("cls");
	ofs.close();

	//调用初始化容器接口,重新获取文件中的数据
	this->initVector();

}


void printStudent(student&s)
{
	cout << "学号:" << s.m_Id << " 姓名:" << s.m_Name << " 密码:" << s.m_Pwd << endl;
}
void printTeacher(teacher&t)
{
	cout << "职工号:" << t.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");
}
//查看机房信息
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");
}
//清空预约记录记录
void Manager::cleanFile()
{
	ofstream ofs;
	ofs.open(ORDER_FILE, ios::trunc);
	ofs.close();
	cout << "清空成功" << endl;

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


//检测重复,参数1:检测学号 学生/教师    参数2:检测类型,教师/学生
bool Manager::checkRepeat(int id, int type)
{
	if (type == 1)
	{
		//检测学生id是否重复
		for (vector<student>::iterator it = vStu.begin(); it != vStu.end(); ++it)
		{
			if (id == it->m_Id)
			{
				return true;
			}
		}

	}
	else
	{
		//检测老师的Id是否重复
		for (vector<teacher>::iterator it = vTea.begin(); it != vTea.end(); ++it)
		{
			if (id == it->empId)
			{
				return true;
			}
		}

	}
	return false;
}


//初始化容器
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);
	if (!ifs.is_open())
	{
		cout << "文件读取失败" << endl;
		return;
	}

	teacher t;
	while (ifs >> t.empId&&ifs >> t.m_Name&&ifs >> t.m_Pwd)
	{
		vTea.push_back(t);
	}

	cout << "老师的个数为" << vTea.size() << endl;
	ifs.close();
}

9、orderFile.h

#pragma once
#include<iostream>
#include<vector>
#include"computerRoom.h"
#include"globelFile.h"
#include<map>
#include<fstream>
using namespace std;
#include<string>
class	OrderFile
{
public:

	//构造函数
	OrderFile();

	//更新预约记录
	void updateOrder();

	int m_size;//记录条数

	//记录所有信息的容器,key:记录条数;value:记录键值对信息
	map<int, map<string, string>>m_orderData;


};

10、orderFile.cpp

#include"orderFile.h"

//截取关键字
void separate(string&date, map<string, string>&m)
{
	//m.clear();
	string key;
	string value;
	int pos = date.find(":");
	if (pos != -1)
	{
		key = date.substr(0, pos);
		value = date.substr(pos + 1, date.size() - pos - 1);
		m.insert(make_pair(key, value));
	}

	
}


//构造函数
OrderFile::OrderFile()
{
	ifstream ifs;
	ifs.open(ORDER_FILE, ios::in);
	string data;
	string interval;
	string stuName;
	string stuId;
	string roomId;//机房编号
	string status;

	this->m_size = 0;

	while (ifs >> data && ifs >> interval && ifs >> stuName && ifs >> stuId &&
		ifs >> roomId && ifs >> status)
	{
		/*cout << data << endl;
		cout << interval << endl;
		cout << stuName << endl;
		cout << stuId << endl;
		cout << roomId << endl;
		cout << status << endl;
		cout << endl;*/
		map<string, string>m;

		string key;
		string value;

		
		/*int pos = data.find(":");
		if (pos != -1)
		{
			key = data.substr(0, pos);
			value = data.substr(pos + 1, data.size() - pos - 1);
			m.insert(make_pair(key, value));
		}
		pos = interval.find(":");
		if (pos != -1)
		{
			key = interval.substr(0, pos);
			value = interval.substr(pos + 1, interval.size() - pos - 1);
			m.insert(make_pair(key, value));
		}
		pos = stuName.find(":");
		if (pos != -1)
		{
			key = stuName.substr(0, pos);
			value = stuName.substr(pos + 1, stuName.size() - pos - 1);
			m.insert(make_pair(key, value));
		}

		pos = stuId.find(":");
		if (pos != -1)
		{
			key = stuId.substr(0, pos);
			value = stuId.substr(pos + 1, stuId.size() - pos - 1);
			m.insert(make_pair(key, value));
		}

		pos = roomId.find(":");
		if (pos != -1)
		{
			key = roomId.substr(0, pos);
			value = roomId.substr(pos + 1, roomId.size() - pos - 1);
			m.insert(make_pair(key, value));
		}
		pos = status.find(":");
		if (pos != -1)
		{
			key = status.substr(0, pos);
			value = status.substr(pos + 1, status.size() - pos - 1);
			m.insert(make_pair(key, value));
		}*/
		separate(data,m);//截取日期
		separate(interval, m);
		separate(stuName, m);
		separate(stuId, m);
		separate(roomId, m);
		separate(status, m);
		
		//把小map容器放入到大的map容器里
		this->m_orderData.insert(make_pair(this->m_size, m));
		this->m_size++;

		
		
	}

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

		}
		cout << endl;

	}*/

	ifs.close();

}


//更新预约记录
void OrderFile::updateOrder()
{
	if (this->m_size == 0)
	{
		return;
	}
	ofstream ofs;
	ofs.open(ORDER_FILE, ios::out || ios::trunc);
	for (int i = 0; i < this->m_size; ++i)
	{
		ofs << "data:" << this->m_orderData[i]["data"] << " ";
		ofs << " interval:" << this->m_orderData[i][" interval"] << " ";
		ofs << "stuName:" << this->m_orderData[i]["stuName"] << " ";
		ofs << "stuId:" << this->m_orderData[i]["stuId"] << " ";
		ofs << "roomId:" << this->m_orderData[i]["roomId"] << " ";
		ofs << "status:" << this->m_orderData[i]["status"] << endl;
	}

}

11、globalFile.h

#pragma once

#define ADMIN_FILE  "admin.txt"//管理员文件
#define STUDENT_FILE  "student.txt"//学生文件
#define TEACHER_FILE  "teacher.txt"//老师文件
#define COMPUTER_FILE  "computer.txt"//机房信息文件
#define ORDER_FILE  "order.txt"//订单文件

12、computerRoom.h

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

class computerRoom
{
public:
	int m_comID;//机房ID号
	int m_maxNum;//机房最大容量
};


  C++知识库 最新文章
【C++】友元、嵌套类、异常、RTTI、类型转换
通讯录的思路与实现(C语言)
C++PrimerPlus 第七章 函数-C++的编程模块(
Problem C: 算法9-9~9-12:平衡二叉树的基本
MSVC C++ UTF-8编程
C++进阶 多态原理
简单string类c++实现
我的年度总结
【C语言】以深厚地基筑伟岸高楼-基础篇(六
c语言常见错误合集
上一篇文章      下一篇文章      查看所有文章
加:2022-03-12 17:14:26  更:2022-03-12 17:16:58 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年11日历 -2024/11/24 4:54:05-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码