机房预约系统(C++)

各位读者朋友,大家好!本篇文章主要内容是将黑马程序员(C++)的机房预约系统的源代码展现给大家。原码的大部分与黑马程序员老师写的代码一致,我只是对其中的一小部分做了优化,比如用户输入自己的选择不再程序服务范围内,便可以让用户重新选择等等。各位读者朋友如果在阅读过程中发现问题或者有更好的建议,可以在评论区告诉我或者给我留言,我一定第一时间改进,谢谢大家!

黑马程序员C++视频:黑马程序员匠心之作|C++教程从0到1入门编程,学习编程不再难

C++基础编程部分(1~83)如果你有C语言基础,你可以简单地过一下这部分知识点。

C++核心编程部分(84~146)这部分是C++的核心部分,希望各位读者仔细学习这部分。主要内容有内存分区模型、引用、函数提高部分、类和对象、文件操作等。

C++提高编程部分(167-263)这部分是C++进阶部分,主要内容是STL(标准模板库)中一些重要容器和关键算法。

1. 系统需求

  • 分别有三种身份使用该程序:
    • 学生代表:申请使用机房
    • 教师:审核学生的预约申请
    • 管理员:给学生、教师创建账号
  • 机房总共有3间:
    • 1号机房:最大容量20人
    • 2号机房:最大容量50人
    • 3号机房:最大容量100人
  • 申请的订单每周由管理员负责清空
  • 学生可以预约未来一周内的机房使用,预约的日期为周一至周五,预约时需要选择预约时段(上午、下午)
  • 教师来审核预约,依据实际情况审核预约通过或者不通过

2. 程序功能

  • 首先进入登录界面,可选登录身份有:
    • 学生代表
    • 老师
    • 管理员
    • 退出
  • 每个身份都需要进行验证,通过验证后,进入子菜单
    • 学生需要输入:学号、姓名、登录密码
    • 老师需要输入:职工号、姓名、登录密码
  • 学生具体功能
    • 申请预约 — 预约机房
    • 查看自身的预约 — 查看自己的预约状态
    • 查看所有预约 — 查看全部预约信息以及预约状态
    • 取消预约 — 取消自身的预约,预约成功或审核中的预约均可取消
    • 注销登录 — 退出登录
  • 教师具体功能
    • 查看所有预约 — 查看全部预约信息以及预约状态
    • 审核预约 — 对学生的预约进行审核
    • 注销登录 — 退出登录
  • 管理员具体功能
    • 添加账号 — 添加学生或教师的账号,需要检测学生编号或教师职工号是否重复
    • 查看账号 — 可以选择查看学生或教师的全部信息
    • 查看机房 — 查看所有机房的信息
    • 清空预约 — 清空所有预约记录
    • 注销登录 — 退出登录

3. 代码

3.1 computerRoom.h

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

class ComputerRoom//机房类
{
public:
	int m_ComId;//机房编号
	int m_MaxNum;//机房最大容量
};

3.2 globalFile.h

#pragma once

#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"//预约信息文件

3.3 identity.h

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

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

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

3.4 manager.h

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

class Manager :public Identity//管理员类
{
public:
	Manager();//默认构造

	Manager(string name, string pwd);//有参构造  参数:姓名、密码

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

	void addPerson();//添加账号

	void showPerson();//查看账号

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

	void clearFile();//清空预约信息

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

	void initComputerRoom();//初始化机房信息

	bool checkRepeat(int id, int type);//检测重复-参数:学号或职工号,身份类型

	vector<Student>vStu;//存放学生信息的容器

	vector<Teacher>vTea;//存放老师信息的容器

	vector<ComputerRoom>vCom;//存放机房信息的容器
};

3.5 orderFile.h

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


class OrderFile
{
public:
	OrderFile();//构造函数

	void updateOrder();//更新预约

	int m_Size;//预约的条数

	//key:第几条预约信息,value:当前这条预约的详细内容
	map<int, map<string, string>>m_OrderData;//记录所有预约的容器
};

3.6 student.h

#pragma once
#include<iostream>
#include<cstdlib>//可以防止 system不明确
using namespace std;
#include"identity.h"
#include<string>
#include<vector>
#include"computerRoom.h"
#include<fstream>
#include"globalFile.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();//取消预约

	void initComputerRoom();//初始化机房信息

	int m_Id;//学号

	vector<ComputerRoom>vCom;//存放机房信息的容器
};

3.7 teacher.h

#pragma once
#include<iostream>
using namespace std;
#include"identity.h"
#include<string>
#include<fstream>
#include"globalFile.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 m_EmpId;//职工编号
};

3.8 manager.cpp

#include"manager.h"

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

}

Manager::Manager(string name, string pwd)//有参构造  参数:姓名、密码
{
	this->m_Name = name;
	this->m_Pwd = pwd;

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

	this->initComputerRoom();//初始化机房信息
}

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

void Manager::addPerson()//添加账号
{
	cout << "请选择您要添加的账号类型:" << endl;
	cout << "1-添加学生" << endl;
	cout << "2-添加教师" << endl;

	string fileName;
	string tip;
	string errorTip;//重复的错误提示

	ofstream ofs;

	int select = 0;//用户的选择

	while (true)
	{
		cin >> select;

		if (select == 1)//添加学生
		{
			fileName = STUDENT_FILE;
			tip = "请输入您要添加的学生学号:";
			errorTip = "学号重复!请重新输入:";
			break;
		}
		else if (select == 2)//添加教师
		{
			fileName = TEACHER_FILE;
			tip = "请输入您要添加的教师编号:";
			errorTip = "职工编号重复!请重新输入:";
			break;
		}
		else
		{
			cout << "您的输入有误!请重新输入:" << endl;
		}
	}
	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 << "\t姓名:" << s.m_Name << "\t密码:" << s.m_Pwd << endl;
}

void printTeacher(Teacher& t)
{
	cout << "职工编号:" << t.m_EmpId << "\t姓名:" << t.m_Name << "\t密码:" << t.m_Pwd << endl;
}

void Manager::showPerson()//查看账号
{
	cout << "请选择您要查看的信息类型:" << endl;
	cout << "1-查看所有学生信息" << endl;
	cout << "2-查看所有教师信息" << endl;

	int select = 0;

	while (true)
	{
		cin >> select;
		if (select == 1)//查看学生信息
		{
			cout << "所有学生信息如下:" << endl;
			
			for_each(vStu.begin(), vStu.end(), printStudent);

			break;
		}
		else if (select == 2)//查看老师信息
		{
			cout << "所有教师信息如下:" << endl;

			for_each(vTea.begin(), vTea.end(), printTeacher);

			break;
		}
		else
		{
			cout << "您的输入有误!请重新输入:" << endl;
		}
	}
	system("pause");
	system("cls");
}

void Manager::showComputer()//查看机房信息
{
	cout << "所有机房信息如下:" << endl;
	for (vector<ComputerRoom>::iterator it = vCom.begin(); it != vCom.end(); it++)
	{
		cout << "机房编号:" << it->m_ComId << "\t机房最大容量:" << it->m_MaxNum << endl;
	}
	system("pause");
	system("cls");
}

void Manager::clearFile()//清空预约信息
{
	cout << "您确定要清空所有的预约信息吗?" << endl;
	cout << "1-确定" << endl;
	cout << "2-返回" << endl;
	int select = 0;
	while (true)
	{
		cin >> select;
		if (select == 1)
		{
			ofstream ofs;
			ofs.open(ORDER_FILE, ios::trunc);
			ofs.close();

			cout << "清空预约信息成功!" << endl;
			break;
		}
		else if (select == 2)
		{
			break;
		}
		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();
}

bool Manager::checkRepeat(int id, int type)//检测重复-参数:学号或职工号,身份类型
{
	if (type == 1)//学生
	{
		for (vector<Student>::iterator it = vStu.begin(); it != vStu.end(); it++)
		{
			if (it->m_Id == id)
			{
				return true;
			}
		}
	}
	else//老师
	{
		for (vector<Teacher>::iterator it = vTea.begin(); it != vTea.end(); it++)
		{
			if (it->m_EmpId == id)
			{
				return true;
			}
		}
	}
	return false;
}

void Manager::initComputerRoom()//初始化机房信息
{
	ifstream ifs;
	ifs.open(COMPUTER_FILE, ios::in);//读文件

	ComputerRoom c;

	while (ifs >> c.m_ComId && ifs >> c.m_MaxNum)
	{
		vCom.push_back(c);
	}
	cout << "当前机房数量为:" << vCom.size() << endl;
	ifs.close();
}

3.9 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)
	{
		string key;
		string value;

		map<string, string>m;

		int pos = date.find(":");//date
		if (pos != -1)
		{
			key = date.substr(0, pos);
			value = date.substr(pos + 1, date.size() - (pos + 1));
			m.insert(make_pair(key, value));
		}
		pos = interval.find(":");//interval
		if (pos != -1)
		{
			key = interval.substr(0, pos);
			value = interval.substr(pos + 1, interval.size() - (pos + 1));
			m.insert(make_pair(key, value));
		}
		pos = stuId.find(":");//stuId
		if (pos != -1)
		{
			key = stuId.substr(0, pos);
			value = stuId.substr(pos + 1, stuId.size() - (pos + 1));
			m.insert(make_pair(key, value));
		}
		pos = stuName.find(":");//stuName
		if (pos != -1)
		{
			key = stuName.substr(0, pos);
			value = stuName.substr(pos + 1, stuName.size() - (pos + 1));
			m.insert(make_pair(key, value));
		}
		pos = roomId.find(":");//roomId
		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(":");//status
		if (pos != -1)
		{
			key = status.substr(0, pos);
			value = status.substr(pos + 1, status.size() - (pos + 1));
			m.insert(make_pair(key, value));
		}
		//将小map容器放到大map容器中
		this->m_OrderData.insert(make_pair(this->m_Size, m));
		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 << "value = " << endl;
		for (map<string, string>::iterator mit = (*it).second.begin(); mit != (*it).second.end(); mit++)
		{
			cout << "key = " << mit->first << " value = " << mit->second << "  ";
		}
		cout << endl;
	}*/
}

void OrderFile::updateOrder()//更新预约
{
	if (this->m_Size == 0)//预约记录为0
	{
		return;
	}

	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();
}

3.10 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;

	this->initComputerRoom();//初始化机房信息
}

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

void Student::applyOrder()//申请预约
{
	int date = 0;//哪天
	int interval = 0;//时间段
	int room;//机房编号

	cout << "机房开放时间为周一至周五" << endl;
	cout << "请选择您要预约的时间:" << endl;
	cout << "\t1-周一" << endl;
	cout << "\t2-周二" << endl;
	cout << "\t3-周三" << endl;
	cout << "\t4-周四" << endl;
	cout << "\t5-周五" << endl;
	while (true)
	{
		cin >> date;
		if (date >= 1 && date <= 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_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 << "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;
	}
	int index = 0;
	for (int i = 0; i < of.m_Size; i++)
	{
		//string => int:    string 利用 .c_str() 转为C语言风格的字符串 const char *
		//然后:  atoi( const char * ) => int
		if (this->m_Id == atoi(of.m_OrderData[i]["stuId"].c_str()))
		{
			index += 1;
			cout << index << "、";
			string mydate;
			if (of.m_OrderData[i]["date"] == "1")
			{
				mydate = "周一";
			}
			else if (of.m_OrderData[i]["date"] == "2")
			{
				mydate = "周二";
			}
			else if (of.m_OrderData[i]["date"] == "3")
			{
				mydate = "周三";
			}
			else if (of.m_OrderData[i]["date"] == "4")
			{
				mydate = "周四";
			}
			else
			{
				mydate = "周五";
			}
			cout << "预约日期:" << mydate;
			cout << "\t时间段:" << (of.m_OrderData[i]["interval"] == "1" ? "上午" : "下午");
			cout << "\t机房编号:" << of.m_OrderData[i]["roomId"];
			string status = "\t状态:";
			//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 << "、";

		string mydate;
		if (of.m_OrderData[i]["date"] == "1")
		{
			mydate = "周一";
		}
		else if (of.m_OrderData[i]["date"] == "2")
		{
			mydate = "周二";
		}
		else if (of.m_OrderData[i]["date"] == "3")
		{
			mydate = "周三";
		}
		else if (of.m_OrderData[i]["date"] == "4")
		{
			mydate = "周四";
		}
		else
		{
			mydate = "周五";
		}
		cout << "预约日期:" << mydate;
		cout << "\t时间段:" << (of.m_OrderData[i]["interval"] == "1" ? "上午" : "下午");
		cout << "\t学号:" << of.m_OrderData[i]["stuId"];
		cout << "\t姓名:" << of.m_OrderData[i]["stuName"];
		cout << "\t机房编号:" << of.m_OrderData[i]["roomId"];
		string status = "\t状态:";
		//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;

	vector<int>v;//存放大map容器的下标
	int index = 1;
	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++ << "、";
				string mydate;
				if (of.m_OrderData[i]["date"] == "1")
				{
					mydate = "周一";
				}
				else if (of.m_OrderData[i]["date"] == "2")
				{
					mydate = "周二";
				}
				else if (of.m_OrderData[i]["date"] == "3")
				{
					mydate = "周三";
				}
				else if (of.m_OrderData[i]["date"] == "4")
				{
					mydate = "周四";
				}
				else
				{
					mydate = "周五";
				}
				cout << "预约日期:" << mydate;
				cout << "\t时间段:" << (of.m_OrderData[i]["interval"] == "1" ? "上午" : "下午");
				cout << "\t机房编号:" << of.m_OrderData[i]["roomId"];
				string status = "\t状态:";//1:审核中   2:预约成功
				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");
}

void Student::initComputerRoom()//初始化机房信息
{
	ifstream ifs;
	ifs.open(COMPUTER_FILE, ios::in);//读文件

	ComputerRoom c;

	while (ifs >> c.m_ComId && ifs >> c.m_MaxNum)
	{
		vCom.push_back(c);
	}
	ifs.close();
}

3.11 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---------------------------------" << endl;
	cout << "\t|\t 1-查看所有预约\t\t|" << endl;
	cout << "\t|\t               \t\t|" << endl;
	cout << "\t|\t 2-审 核 预  约\t\t|" << endl;
	cout << "\t|\t               \t\t|" << endl;
	cout << "\t|\t 其它--注销登录\t\t|" << endl;
	cout << "\t---------------------------------" << endl;
	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 << "、";

		string mydate;
		if (of.m_OrderData[i]["date"] == "1")
		{
			mydate = "周一";
		}
		else if (of.m_OrderData[i]["date"] == "2")
		{
			mydate = "周二";
		}
		else if (of.m_OrderData[i]["date"] == "3")
		{
			mydate = "周三";
		}
		else if (of.m_OrderData[i]["date"] == "4")
		{
			mydate = "周四";
		}
		else
		{
			mydate = "周五";
		}
		cout << "预约日期:" << mydate;
		cout << "\t时间段:" << (of.m_OrderData[i]["interval"] == "1" ? "上午" : "下午");
		cout << "\t学号:" << of.m_OrderData[i]["stuId"];
		cout << "\t姓名:" << of.m_OrderData[i]["stuName"];
		cout << "\t机房编号:" << of.m_OrderData[i]["roomId"];
		string status = "\t状态:";
		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;
	}

	vector<int>v;
	int index = 0;
	cout << "待审核的预约信息记录如下:" << endl;
	for (int i = 0; i < of.m_Size; i++)
	{
		if (of.m_OrderData[i]["status"] == "1")
		{
			v.push_back(i);
			cout << ++index << "、";

			string mydate;
			if (of.m_OrderData[i]["date"] == "1")
			{
				mydate = "周一";
			}
			else if (of.m_OrderData[i]["date"] == "2")
			{
				mydate = "周二";
			}
			else if (of.m_OrderData[i]["date"] == "3")
			{
				mydate = "周三";
			}
			else if (of.m_OrderData[i]["date"] == "4")
			{
				mydate = "周四";
			}
			else
			{
				mydate = "周五";
			}
			cout << "预约日期:" << mydate;
			cout << "\t时间段:" << (of.m_OrderData[i]["interval"] == "1" ? "上午" : "下午");
			cout << "\t学生学号:" << of.m_OrderData[i]["stuId"];
			cout << "\t学生姓名:" << of.m_OrderData[i]["stuName"];
			cout << "\t机房编号:" << of.m_OrderData[i]["roomId"];
			string status = "\t状态:审核中";
			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;

				while (true)
				{
					cin >> ret;
					if (ret == 1)//审核通过
					{
						of.m_OrderData[v[select - 1]]["status"] = "2";
						break;
					}
					else if (ret == 2)//审核不通过
					{
						of.m_OrderData[v[select - 1]]["status"] = "-1";
						break;
					}
					cout << "您的输入有误!请重新输入:" << endl;
				}

				of.updateOrder();
				cout << "审核完毕!" << endl;
				break;
			}
		}
		cout << "您的输入有误!请重新输入:" << endl;
	}
	system("pause");
	system("cls");
}

3.12 机房预约系统.cpp

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

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

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;
		}
	}
}

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->clearFile();
		}
		else//注销登录
		{
			delete manager;
			cout << "注销登录成功!" << endl;
			system("pause");
			system("cls");
			return;
		}
	}
}

//登录功能   参数:操作的文件名、登录人员的身份类型
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;//从文件中读出的学号
		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 Student(id, name, pwd);
				studentMenu(person);//进入学生子菜单
				return;
			}
		}
	}
	else if (type == 2)//教师身份验证
	{
		int fId;//从文件中读出的职工编号
		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 (fName == name && fPwd == pwd)
			{
				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|                              |" << endl;
		cout << "\t\t|         1-学生代表           |" << endl;
		cout << "\t\t|                              |" << endl;
		cout << "\t\t|         2- 老   师           |" << endl;
		cout << "\t\t|                              |" << endl;
		cout << "\t\t|         3- 管 理 员          |" << endl;
		cout << "\t\t|                              |" << endl;
		cout << "\t\t|         0-退出系统           |" << endl;
		cout << "\t\t|                              |" << 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("pause");
			system("cls");
			break;
		}
	}

	system("pause");
	return 0;
}

3.13 admin.txt

admin 123

3.14 computerRoom.txt

1 20
2 50
3 100

3.15 order.txt

date:5 interval:2 stuId:1 stuName:张三 roomId:1 status:-1
date:3 interval:1 stuId:2 stuName:李四 roomId:2 status:2

3.16 student.txt

1 张三 123 
2 李四 111

3.17 teacher.txt

1 杨 123 
机房预约系统是一种实现对计算机机房使用的预约管理的软件系统,其核心代码主要是由C语言编写的。系统分为前台和后台两个部分,前台主要实现用户的登录、预约申请、预约查询和取消预约等操作,后台主要实现机房信息和预约信息的管理等功能。 系统的代码主要包括以下几个模块: 1.用户模块。主要包括用户登录、注册、修改密码等功能,用户名和密码需要保存在数据库中。 2.机房模块。主要包括机房信息的管理,包括机房编号、机房名称、机器数量、状态等信息,这些信息需要保存在数据库中。 3.预约模块。主要包括预约申请、预约查询和取消预约等功能。预约申请需要填写申请日期、机房号、预约时间段等信息。预约查询需要输入用户名和日期,查询预约记录;取消预约需要输入预约记录的ID。 4.管理员模块。主要包括管理员登录、机房管理、用户管理等功能,管理员登录需要验证管理员用户名和密码,机房管理需要管理员输入机房编号、机房状态等信息,用户管理需要管理员输入用户名、密码等信息,同时可以查询和删除用户和预约信息。 总结来说,机房预约系统的C代码主要实现了用户登录、机房信息维护、预约信息管理和管理员管理等多个模块的功能,可实现对计算机机房使用的预约管理,能够有效地提高计算机机房的使用率和管理效率。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值