机房资源在现代社会中起着越来越重要的作用,为了高效地管理和利用机房资源,我们设计了一个基于C++的机房预约系统。该系统能够实现学生或教职工对机房进行预约、查询和管理等操作,提高机房资源的利用率和管理效率。
当前,大学机房资源管理普遍存在以下问题:
因此,我们的机房预约系统有以下主要需求:
基于以上需求,我们设计了如下系统模块:
我们使用C++语言进行系统开发,主要采用以下技术和方法:
经过实际测试,我们的机房预约系统实现了预约、查询和管理等功能,并且能够准确检测出预约冲突并及时提示用户。系统界面操作简单直观,用户易于上手。通过系统统计功能,管理员能够方便地获取机房使用情况和预约记录,便于管理和决策。
首先,选择身份后进入系统
若选择学生身份,输入用户名密码之后进入学生子菜单,可申请或查看、取消预约
预约流程:
查看预约:
若选择教师身份,输入职工号,用户名和密码后,进入教师子菜单
在这里审核预约:
若选择管理员身份,输入用户名和密码后进入管理员子菜单
可添加学生或老师账号
可查看学生或老师的账号
查看机房或清空预约
本次开发了一个基于C++的机房预约系统,实现了高效的机房资源管理。然而,目前系统还存在一些局限性,比如用户界面可视化程度不高,操作不够友好。后续也希望读者提出宝贵的意见,我会进一步改进系统的用户界面,增加更多的功能模块,提高系统的稳定性和可扩展性。
总之,基于C++的机房预约系统是一项具有实际意义和应用前景的工作。通过不断优化和改进,该系统将在实际运行中发挥更大的作用,提高机房资源的利用率和管理效率。
用于定义身份的抽象类:identity.h
#pragma once
#include
#include
using namespace std;
class Identity {
public:
string name;
string pwd;
virtual void operMenu() = 0;
};
继承它的子类:manager.h
#pragma once
#include
#include"identity.h"
#include"student.h"
#include"teacher.h"
#include"computerroom.h"
#include"orders.h"
using namespace std;
class Manager : public Identity {
public:
vector<Student> vStu;
vector<Teacher> vTea;
vector<ComputerRoom> vCom;
Manager() = default;
Manager(string name, string pwd);
//菜单界面
void operMenu();
//添加账号
void addPerson();
//查看账号
void showPerson();
//查看机房信息
void showComputer();
//清空预约记录
void cleanFile();
//初始化容器
void initVector();
//检查是否重复
bool isRepeat(int id, int type);
};
类的实现:manager.cpp
#include "manager.h"
Manager::Manager(string name, string pwd) {
this->name = name;
this->pwd = pwd;
initVector();
}
void Manager::operMenu() {
cout << "欢迎管理员:" << this->name << "登录!" << endl;
cout << "\t\t-------------------------------------------\n";
cout << "\t\t| |\n";
cout << "\t\t| 1.添加账号 |\n";
cout << "\t\t| |\n";
cout << "\t\t| 2.查看账号 |\n";
cout << "\t\t| |\n";
cout << "\t\t| 3.查看机房 |\n";
cout << "\t\t| |\n";
cout << "\t\t| 4.清空预约 |\n";
cout << "\t\t| |\n";
cout << "\t\t| 0.注销登录 |\n";
cout << "\t\t-------------------------------------------\n";
cout << "请输入你的选择:";
}
void Manager::addPerson() {
cout << "请输入添加账号的类型" << endl;
cout << "1.添加学生" << endl;
cout << "2.添加老师" << endl;
int choice;
cin >> choice;
string fileName, tip;
ofstream ofs;
if (choice == 1) {
fileName = STUDENT_FILE;
cout << "请输入学号" << endl;
tip = "输入的学号重复";
}
else if (choice == 2) {
fileName = TEACHER_FILE;
cout << "请输入职工号" << endl;
tip = "输入的职工号重复,请重新输入";
}
ofs.open(fileName, ios::out | ios::app);
int id;
string name, pwd;
while (true) {
cin >> id;
if (!isRepeat(id, choice)) break;
cout << tip << endl;
}
cout << "请输入姓名:";
cin >> name;
cout << "请输入密码:";
cin >> pwd;
ofs << id << " " << name << " " << pwd << " " << endl;
cout << "添加成功!" << endl;
//刷新vector
if (choice == 1) {
Student s;
s.id = id;
s.name = name;
s.pwd = pwd;
vStu.push_back(s);
}
else if (choice == 2) {
Teacher t;
t.empId = id;
t.name = name;
t.pwd = pwd;
vTea.push_back(t);
}
system("pause");
system("cls");
}
void Manager::showPerson() {
cout << "请选择查看内容:" << endl;
cout << "1.显示学生信息" << endl;
cout << "2.显示老师信息" << endl;
int choice;
cin >> choice;
if (choice == 1) {
for (int i = 0; i < vStu.size(); i++)
cout << "学号:" << vStu[i].id << " 姓名:"
<< vStu[i].name << "密码:" << vStu[i].pwd << endl;
}
else if (choice == 2) {
for (int i = 0; i < vTea.size(); i++)
cout << "职工号:" << vTea[i].empId << " 姓名:"
<< vTea[i].name << "密码:" << vTea[i].pwd << endl;
}
system("pause");
system("cls");
}
void Manager::showComputer() {
cout << "机房信息如下:" << endl;
for (int i = 0; i < vCom.size(); i++) {
cout << "机房编号:" << vCom[i].roomId
<< " 机房容量:" << vCom[i].maxNum << endl;
}
system("pause");
system("cls");
}
void Manager::cleanFile() {
ofstream ofs(ORDER_FILE, ios::trunc);
ofs.close();
cout << "清空成功" << endl;
system("pause");
system("cls");
}
void Manager::initVector() {
vStu.clear();
vTea.clear();
vCom.clear();
//读取信息
//读取学生文件
ifstream ifs(STUDENT_FILE, ios::in);
if (!ifs.is_open()) {
cout << "文件读取失败!" << endl;
return;
}
Student s;
while (ifs >> s.id && ifs >> s.name && ifs >> s.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.name && ifs >> t.pwd) {
vTea.push_back(t);
}
//cout << "当前老师数量为:" << vTea.size() << endl;
ifs.close();
//读取机房文件
ifs.open(COMPUTER_FILE, ios::in);
if (!ifs.is_open()) {
cout << "文件读取失败!" << endl;
return;
}
ComputerRoom com;
while (ifs >> com.roomId && ifs >> com.maxNum) {
vCom.push_back(com);
}
ifs.close();
}
bool Manager::isRepeat(int id, int type) {
//检查学生
if (type == 1)
for (int i = 0; i < vStu.size(); i++)
if (id == vStu[i].id) return true;
else if (type == 2)
for (int i = 0; i < vTea.size(); i++)
if (id == vTea[i].empId) return true;
return false;
}
第二个子类:teacher.h
#pragma once
#include
#include
#include"identity.h"
#include"orders.h"
using namespace std;
class Teacher : public Identity {
public:
int empId; //职工号
Teacher();
Teacher(int empId, string name, string pwd);
//菜单界面
void operMenu();
//查看所有预约
void showAllOrder();
//审核预约
void validOrder();
};
它的实现:teacher.cpp
#include "teacher.h"
Teacher::Teacher() {
}
Teacher::Teacher(int id, string name, string pwd) {
this->empId = id;
this->name = name;
this->pwd = pwd;
}
void Teacher::operMenu() {
cout << "欢迎教师:" << this->name << "登录!" << endl;
cout << "\t\t-------------------------------------------\n";
cout << "\t\t| |\n";
cout << "\t\t| 1.查看所有预约 |\n";
cout << "\t\t| |\n";
cout << "\t\t| 2.审核预约 |\n";
cout << "\t\t| |\n";
cout << "\t\t| 0.注销登录 |\n";
cout << "\t\t-------------------------------------------\n";
cout << "请输入你的选择:";
}
void Teacher::showAllOrder() {
Orders ord;
ord.showOrders();
}
void Teacher::validOrder() {
Orders ord;
if (ord.orderCnt == 0) cout << "无预约记录" << endl;
else {
cout << "目前所有预约记录如下:" << endl;
ord.showOrders();
cout << "请选择要审核的记录:" << endl;
int choice = 0;
while (choice < 1 || choice > ord.orderCnt) {
cin >> choice;
if (choice < 1 || choice > ord.orderCnt) cout << "请输入正确的预约记录编号" << endl;
}
int res = 0;
cout << "请输入审核结果:" << endl;
cout << "1.通过" << endl;
cout << "2.不通过" << endl;
cin >> res;
ord.validStatus(choice, res);
}
}
第三个子类:student.h
#pragma once
#include
#include
#include
#include
#include"identity.h"
#include"computerroom.h"
#include"globalfile.h"
#include"orders.h"
using namespace std;
class Student : public Identity {
public:
int id;
vector<ComputerRoom> vCom;
Student() = default;
Student(int id, string name, string pwd);
//菜单界面
void operMenu();
//申请预约
void applyOrder();
//查看自身预约
void showMyOrder();
//查看所有预约
void showAllOrder();
//取消预约
void cancelOrder();
//初始化容器
void initVector();
};
它的实现:student.cpp
#include"student.h"
Student::Student(int id, string name, string pwd) {
this->id = id;
this->name = name;
this->pwd = pwd;
//初始化机房信息
initVector();
}
void Student::operMenu() {
cout << "欢迎学生:" << this->name << "登录!" << endl;
cout << "\t\t-------------------------------------------\n";
cout << "\t\t| |\n";
cout << "\t\t| 1.申请预约 |\n";
cout << "\t\t| |\n";
cout << "\t\t| 2.查看我的预约 |\n";
cout << "\t\t| |\n";
cout << "\t\t| 3.查看所有人预约 |\n";
cout << "\t\t| |\n";
cout << "\t\t| 4.取消预约 |\n";
cout << "\t\t| |\n";
cout << "\t\t| 0.注销登录 |\n";
cout << "\t\t-------------------------------------------\n";
cout << "请输入你的选择:";
}
void Student::applyOrder() {
int date = 0, interval = 0, room = 0;
cout << "机房开放时间为周一至周五" << endl;
cout << "请输入申请预约的时间" << endl;
cout << "1.周一" << endl;
cout << "2.周二" << endl;
cout << "3.周三" << endl;
cout << "4.周四" << endl;
cout << "5.周五" << endl;
while (date < 1 || date > 5) {
cin >> date;
if (date < 1 || date > 5) cout << "请输入正确的时间" << endl;
}
cout << "请输入申请预约的时间段" << endl;
cout << "1.上午" << endl;
cout << "2.下午" << endl;
while (interval < 1 || interval > 2) {
cin >> interval;
if (interval < 1 || interval > 2) cout << "请输入正确的时间段" << endl;
}
cout << "请输入申请预约的机房" << endl;
cout << "机房信息如下:" << endl;
for (int i = 0; i < vCom.size(); i++) {
cout << "机房编号:" << vCom[i].roomId
<< " 机房容量:" << vCom[i].maxNum << endl;
}
while (room < 1 || room > vCom.size()) {
cin >> room;
if (room < 1 || room >= vCom.size()) cout << "请输入正确的机房号" << endl;
}
cout << "预约成功!审核中" << endl;
//将审核信息写入到文件中
ofstream ofs(ORDER_FILE, ios::app);
ofs << "date:" << date << " " << "interval:" << interval << " "
<< "student_id:" << this->id << " "
<< "student_name:" << this->name << " "
<< "room_id:" << room << " "
<< "status:" << 1 << endl;
ofs.close();
}
void Student::showMyOrder() {
Orders ord;
if (ord.orderCnt == 0) cout << "预约记录为空" << endl;
else ord.showOneOrder(this->id);
}
void Student::showAllOrder() {
Orders ord;
ord.showOrders();
}
void Student::cancelOrder() {
Orders ord;
if (ord.orderCnt == 0) cout << "无预约记录" << endl;
else {
cout << "目前您所有的预约记录如下所示:" << endl;
ord.showOneOrder(this->id);
cout << "请选择要取消的预约记录编号:";
int input = 0;
while (input <= 0 || input > ord.orderCnt) {
cin >> input;
if (input <= 0 || input > ord.orderCnt) cout << "输入序号有误,请重新输入" << endl;
}
ord.setStatusCancel(input, this->id);
}
}
void Student::initVector() {
ifstream ifs(COMPUTER_FILE, ios::in);
if (!ifs.is_open()) {
cout << "文件读取失败!" << endl;
return;
}
ComputerRoom com;
while (ifs >> com.roomId && ifs >> com.maxNum) {
vCom.push_back(com);
}
ifs.close();
}
用于记录机房信息的类:computerroom.h
#pragma once
#include
#include
#include
#include"globalfile.h"
using namespace std;
class ComputerRoom {
public:
int roomId;
int maxNum;
};
用于预约操作的类:orders.h
#pragma once
#include
#include
#include
#include
#include
#include"globalfile.h"
#include"identity.h"
#include"student.h"
using namespace std;
class Orders {
public:
//记录预约条数
int orderCnt;
Orders();
//更新预约记录
void updateOrder();
//初始化预约信息
void initFile();
//查看预约
void showOrders();
//查看某人的预约
void showOneOrder(int id);
//设为取消预约
void setStatusCancel(int item, int id);
//审核
void validStatus(int item, int res);
private:
//记录预约信息
map<int, map<string, string>> mOrder;
};
orders.cpp
#include "orders.h"
Orders::Orders() {
this->initFile();
}
void Orders::updateOrder() {
if (this->orderCnt == 0) return;
ofstream ofs(ORDER_FILE, ios::trunc);
for (auto it = mOrder.begin(); it != mOrder.end(); it++) {
ofs << "date:" << it->second["date"];
ofs << " interval:" << it->second["interval"];
ofs << " student_id:" << it->second["student_id"];
ofs << " student_name:" << it->second["student_name"];
ofs << " room_id:" << it->second["room_id"];
ofs << " status:" << it->second["status"] << endl;
}
ofs.close();
}
void Orders::initFile() {
ifstream ifs(ORDER_FILE, ios::in);
if (!ifs.is_open()) {
cout << "文件不存在!" << endl;
return;
}
//将每一条预约记录写入到内存区
string date, interval, stuId, stuName, roomId, status;
this->orderCnt = 0;
while (ifs >> date && ifs >> interval && ifs >> stuId && ifs >> stuName && ifs >> roomId && ifs >> status) {
map<string, string> m;
m.insert(make_pair(date.substr(0, date.find(":")), date.substr(date.find(":") + 1, date.size() - 1)));
m.insert(make_pair(interval.substr(0, interval.find(":")), interval.substr(interval.find(":") + 1, interval.size() - 1)));
m.insert(make_pair(stuId.substr(0, stuId.find(":")), stuId.substr(stuId.find(":") + 1, stuId.size() - 1)));
m.insert(make_pair(stuName.substr(0, stuName.find(":")), stuName.substr(stuName.find(":") + 1, stuName.size() - 1)));
m.insert(make_pair(roomId.substr(0, roomId.find(":")), roomId.substr(roomId.find(":") + 1, roomId.size() - 1)));
m.insert(make_pair(status.substr(0, status.find(":")), status.substr(status.find(":") + 1, status.size() - 1)));
mOrder.insert(make_pair(++orderCnt, m));
}
}
void Orders::showOrders() {
for (auto it = mOrder.begin(); it != mOrder.end(); it++) {
cout << "第" << it->first << "条预约记录" << endl;
cout << "预约时间:周" << it->second["date"];
cout << " 预约时间段:" << (it->second["interval"] == "1" ? "上午" : "下午");
cout << " 学号:" << it->second["student_id"];
cout << " 姓名:" << it->second["student_name"];
cout << " 机房:" << it->second["room_id"];
cout << " 预约状态:";
if (it->second["status"] == "0") cout << "预约已取消" << endl;
else if (it->second["status"] == "1") cout << "审核中" << endl;
else if (it->second["status"] == "2") cout << "审核通过" << endl;
else if (it->second["status"] == "-1") cout << "审核未通过!" << endl;
cout << endl;
}
}
void Orders::showOneOrder(int id) {
bool flag = false;
for (auto it = mOrder.begin(); it != mOrder.end(); it++) {
if (atoi(it->second["student_id"].c_str()) == id) {
flag = true;
cout << "预约时间:周" << it->second["date"];
cout << " 预约时间段:" << (it->second["interval"] == "1" ? "上午" : "下午");
cout << " 学号:" << it->second["student_id"];
cout << " 姓名:" << it->second["student_name"];
cout << " 机房:" << it->second["room_id"];
cout << " 预约状态:";
if (it->second["status"] == "0") cout << "预约已取消" << endl;
else if (it->second["status"] == "1") cout << "审核中" << endl;
else if (it->second["status"] == "2") cout << "审核通过" << endl;
else if (it->second["status"] == "-1") cout << "审核未通过!" << endl;
}
}
if(!flag) cout << "未找到预约记录!" << endl;
}
void Orders::setStatusCancel(int item, int id) {
int input = 0;
cout << "是否确认取消预约?" << endl;
cout << "1.确认" << endl;
cout << "2.返回" << endl;
cin >> input;
if (input == 2) return;
for (auto it = mOrder.begin(); it != mOrder.end(); it++) {
if (atoi(it->second["student_id"].c_str()) == id && --item == 0) {
if (it->second["status"] == "0") cout << "预约已取消" << endl;
else it->second["status"] = "0";
updateOrder();
}
}
}
void Orders::validStatus(int item, int res) {
int input = 0;
cout << "是否确认审核结果?" << endl;
cout << "1.确认" << endl;
cout << "2.返回" << endl;
cin >> input;
if (input == 2) return;
int status = 1;
if (res == 1) status = 2;
else if (res == 2) status = -1;
mOrder[item]["status"] = to_string(status);
updateOrder();
cout << "审核已完成" << endl;
}
专门用于文件名的宏定义:filename.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"
最后,main.cpp
#include
#include
#include"identity.h"
#include"globalfile.h"
#include"student.h"
#include"teacher.h"
#include"manager.h"
using namespace std;
void menu() {
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| 2.老 师 |\n";
cout << "\t\t| |\n";
cout << "\t\t| 3.管 理 员 |\n";
cout << "\t\t| |\n";
cout << "\t\t| 0.退 出 |\n";
cout << "\t\t-------------------------------------------\n";
cout << "请输入您的选择:";
}
void managerMenu(Identity*& manager) {
while (true) {
manager->operMenu();
//将父类指针转换为子类指针,调用子类里其他接口
Manager* man = (Manager*)manager;
int choice;
cin >> choice;
if (choice == 1) {
//添加账号
man->addPerson();
} else if (choice == 2) {
//查看账号
man->showPerson();
} else if (choice == 3) {
//查看机房
man->showComputer();
} else if (choice == 4) {
//清空预约
man->cleanFile();
} else if (choice == 0) {
//注销登录
delete manager;
cout << "注销成功!" << endl;
system("pause");
system("cls");
return;
}
}
}
void TeacherMenu(Identity*& teacher) {
while (true) {
//调用教师子菜单
teacher->operMenu();
Teacher* tea = (Teacher*)teacher;
int choice;
cin >> choice;
if (choice == 1) {
//查看所有预约
tea->showAllOrder();
} else if (choice == 2) {
//审核预约
tea->validOrder();
} else if (choice == 0) {
//注销登录
delete teacher;
cout << "注销成功!" << endl;
system("pause");
system("cls");
return;
}
system("pause");
system("cls");
}
}
void studentMenu(Identity*& student) {
while (true) {
//调用学生子菜单
student->operMenu();
Student* stu = (Student*)student;
int choice;
cin >> choice;
if (choice == 1) {
//申请预约
stu->applyOrder();
} else if (choice == 2) {
//查看我的预约
stu->showMyOrder();
} else if (choice == 3) {
//查看所有人预约
stu->showAllOrder();
} else if (choice == 4) {
//取消预约
stu->cancelOrder();
} else if (choice == 0) {
//注销登录
delete student;
cout << "注销成功!" << endl;
system("pause");
system("cls");
return;
}
system("pause");
system("cls");
}
}
void logIn(string fileName, int type) {
Identity* person = NULL;
ifstream ifs(fileName, ios::in);
if (!ifs.is_open()) {
cout << "文件不存在" << endl;
ifs.close();
return;
}
int id = 0;
string name, pwd;
if (type == 1) {
cout << "请输入学号" << endl;
cin >> id;
} else if (type == 2) {
cout << "请输入职工号" << endl;
cin >> id;
}
cout << "请输入用户名" << endl;
cin >> name;
cout << "请输入密码" << endl;
cin >> pwd;
int fId;
string fName, fPwd; //下面对比从文件中读取的id,姓名和密码
if (type == 1) {
//学生登录验证
while (ifs >> fId && ifs >> fName && ifs >> fPwd) {
if (fId == id && fName == name && fPwd == pwd) {
system("cls");
cout << "学生验证登录成功!" << endl;
person = new Student(id, name, pwd);
studentMenu(person);
return;
}
}
} else if (type == 2) {
//教师登录验证
while (ifs >> fId && ifs >> fName && ifs >> fPwd) {
if (fId == id && fName == name && fPwd == pwd) {
system("cls");
cout << "教师验证登录成功!" << endl;
person = new Teacher(id, name, pwd);
TeacherMenu(person);
return;
}
}
} else if (type == 3) {
while (ifs >> fName && ifs >> fPwd) {
if (fName == name && fPwd == pwd) {
system("cls");
cout << "管理员验证登录成功!" << endl;
person = new Manager(name, pwd);
managerMenu(person);
return;
}
}
}
cout << "验证登录失败!" << endl;
system("pause");
system("cls");
return;
}
int main() {
int choice = 0;
while (true) {
menu();
cin >> choice;
switch (choice) {
case 1:
logIn(STUDENT_FILE, 1);
break;
case 2:
logIn(TEACHER_FILE, 2);
break;
case 3:
logIn(ADMIN_FILE, 3);
break;
case 0:
cout << "欢迎下次使用!" << endl;
system("pause");
exit(0);
break;
default:
cout << "输入有误,请重新选择!" << endl;
system("pause");
break;
}
system("cls");
}
return 0;
}