主要思想是利用全局函数实现对身份的验证,如果身份验证成功就调用该身份的构造函数,实现该身份使用其功能(构造函数实现该身份的功能页面)。
本程序使用的文档放在保存文件夹的统计目录下。
实现界面:
全局文件:
#pragma once
#define FILE_student "student.txt"
#define FILE_teacher "teacher.txt"
#define FILE_manager "manager.txt"
#define FILE_room "room.txt"
#define FILE_order "order.txt"
身份基类:
#include<iostream>
#pragma once
using namespace std;
class identity
{
virtual void opermenu()=0;
public:
string I_name;
string I_keyword;
};
学生类:
#include<iostream>
#pragma once
using namespace std;
#include"identity.h"
#include<vector>
#include"room.h"
#include"orderfile.h"
class student :public identity
{
public:
student(string name, string keyword, int id);
void opermenu();
void addmyorder();
void openallorder();
void cancelorder();
int S_id;
vector<room> stuv;
};
教师类:
#include<iostream>
#pragma once
using namespace std;
#include"identity.h"
#include"orderfile.h"
#include<vector>
class teacher :public identity
{
public:
teacher(string name, string keyword, int id);
void opermenu();
void exammyorder();
void openallorder();
int T_id;
};
管理员类:
#include<iostream>
#pragma once
using namespace std;
#include"identity.h"
#include"globalfile.h"
#include"room.h"
#include<vector>
class manager:public identity
{
public:
manager(string name, string keyword);
void opermenu();
void addmen();
void lookmen();
void lookroom();
void clearorder();
vector<room> vc;
};
机房类:
#include<iostream>
#pragma once
using namespace std;
class room
{
public:
int comid;
int comcapacity;
};
管理文件类:
#pragma once
#include<iostream>
#include<fstream>
#include"globalfile.h"
using namespace std;
#include<map>
class orderfile
{
public:
orderfile();
void updateorder();
int m_size;
map<int, map<string, string>> m_order;
};
学生类的实现:
#include"student.h"
#include<fstream>
#include"globalfile.h"
student::student(string name, string keyword, int id)
{
this->I_name = name;
this->I_keyword = keyword;
this->S_id = id;
ifstream ifs;
ifs.open(FILE_room, ios::in);
room r;
while (ifs >> r.comid && ifs >> r.comcapacity)
{
stuv.push_back(r);
}
}
void student::opermenu()
{
cout << "欢迎同学:" << this->I_name << "登录" << endl;
cout << "\t\t\t\t--------------------—————-----------------------" << endl;
cout << "\t\t\t\t------------------1.添加预约-------------------------" << endl;
cout << "\t\t\t\t------------------2.查看预约-------------------------" << endl;
cout << "\t\t\t\t------------------3.取消预约-------------------------" << endl;
cout << "\t\t\t\t------------------0.注销账号-------------------------" << endl;
cout << "\t\t\t\t--------------------—————-----------------------" << endl;
}
void student::addmyorder()
{
cout << "请选择你申请的时间段" << endl;
cout << "1.周一" << endl;
cout << "2.周二" << endl;
cout << "3.周三" << endl;
cout << "4.周四" << endl;
cout << "5.周五" << endl;
int date;
while (1)
{
cin >> date;
if (date >= 1 && date <= 5)
{
break;
}
cout << "输入错误,重新输入" << endl;
}
cout << "请选择你申请的时间" << endl;
cout << "1.上午" << endl;
cout << "2.下午" << endl;
int time;
while (1)
{
cin >> time;
if (time >= 1 && time <= 2)
{
break;
}
cout << "输入错误,重新输入" << endl;
}
cout << "请输入你想申请的机房" << endl;
cout << "1.一号机房" << endl;
cout << "2.二号机房" << endl;
cout << "3.三号机房" << endl;
int choice;
cin >> choice;
while (1)
{
if (choice >= 1 && choice <= 3)
{
break;
}
cout << "输入错误,重新输入" << endl;
}
cout << "预约成功!审核中!" << endl;
ofstream ofs;
ofs.open(FILE_order, ios::out | ios::app);
ofs << "date:" << date<<" ";
ofs << "time:" << time << " ";
ofs << "id:" << this->S_id << " ";
ofs << "name:" << this->I_name << " ";
ofs << "choice:" << choice << " ";
ofs << "status:" << 1 << endl;
ofs.close();
system("pause");
system("cls");
return;
}
void student::openallorder()
{
orderfile of;
if (of.m_size == 0)
{
cout << "无预约记录" << endl;
system("pause");
system("cls");
return;
}
for (int i = 0; i < of.m_size; i++)
{
if (atoi(of.m_order[i]["id"].c_str()) == this->S_id)//转换为int类型
{
cout << "时间(周):" << of.m_order[i]["date"] << " ";
cout << "时间段:" << (of.m_order[i]["time"] == "1" ? "上午" : "下午") << " ";
cout << "学号:" << of.m_order[i]["id"] << " ";
cout << "姓名:" << of.m_order[i]["name"]<< " ";
cout << "机房号:" << of.m_order[i]["choice"] << " ";
cout << "状态:";
if (of.m_order[i]["status"]=="1")
{
cout << "审核中" << endl;
}
else if (of.m_order[i]["status"] == "2")
{
cout << "审核通过" << endl;
}
else
{
cout << "未通过" << endl;
}
}
}
system("pause");
system("cls");
return;
}
void student::cancelorder()
{
orderfile of;
if (of.m_size == 0)
{
cout << "无预约记录" << endl;
system("pause");
system("cls");
return;
}
vector<int> v;
int index=1;
for (int i = 0; i < of.m_size; i++)
{
if (atoi(of.m_order[i]["id"].c_str() )== this->S_id)
{
if (of.m_order[i]["status"] == "1" || of.m_order[i]["status"] == "2")
{
v.push_back(i);
cout << index++ << " " << endl;
cout << "时间(周):" << of.m_order[i]["date"] << endl;
cout << "时间段:" << (of.m_order[i]["time"] == "1" ? "上午" : "下午") << endl;;
cout << "学号:" << of.m_order[i]["id"] << endl;
cout << "姓名:" << of.m_order[i]["name"] << endl;
cout << "机房号:" << of.m_order[i]["choice"] << endl;;
cout << "状态:";
if (of.m_order[i]["status"] == "1")
{
cout << "审核中" << endl;
}
else if (of.m_order[i]["status"] == "2")
{
cout << "审核通过" << endl;
}
}
}
}
if (index-1 == 0)
{
cout << "无可取消记录" << endl;
system("pause");
system("cls");
return;
}
cout << "请输入取消记录,0代表换回" << endl;
int select;
while (1)
{
cin >> select;
if (select >= 0 && select <= v.size())
{
if (select == 0)
{
cout << "退出成功" << endl;
system("pause");
system("cls");
return;
}
else
{
of.m_order[v[select - 1]]["status"] = "0";
of.updateorder();
cout << "已经取消预约" << endl;
system("pause");
system("cls");
return;
}
cout << "输入错误重新输入" << endl;
}
}
}
教师类的实现:
#include"teacher.h"
teacher::teacher(string name, string keyword, int id)
{
}
void teacher::openallorder()
{
orderfile of;
if (of.m_size == 0)
{
cout << "无预约记录" << endl;
system("pause");
system("cls");
return;
}
for (int i = 0; i < of.m_size; i++)
{
cout << "时间(周):" << of.m_order[i]["date"] << " ";
cout << "时间段:" << (of.m_order[i]["time"] == "1" ? "上午" : "下午") << " ";;
cout << "学号:" << of.m_order[i]["id"] << " ";
cout << "姓名:" << of.m_order[i]["name"] << " ";
cout << "机房号:" << of.m_order[i]["choice"] << " ";
cout << "状态:";
if (of.m_order[i]["status"] == "1")
{
cout << "审核中" << endl;
}
else if (of.m_order[i]["status"] == "2")
{
cout << "审核通过" << endl;
}
else
{
cout << "未通过" << endl;
}
}
system("pause");
system("cls");
return;
}
void teacher::exammyorder()
{
orderfile of;
if (of.m_size == 0)
{
cout << "无预约记录" << endl;
system("pause");
system("cls");
return;
}
vector<int> v;
int index = 0;
for (int i = 0; i < of.m_size; i++)
{
if (of.m_order[i]["status"] =="1" )
{
v.push_back(i);
cout << ++index << " " << endl;
cout << "时间(周):" << of.m_order[i]["date"] << " ";
cout << "时间段:" << (of.m_order[i]["time"] == "1" ? "上午" : "下午") << " ";
cout << "学号:" << of.m_order[i]["id"] << " ";
cout << "姓名:" << of.m_order[i]["name"] << " ";
cout << "机房号:" << of.m_order[i]["choice"] << " ";;
cout << "状态:" << "审核中" << endl;
}
}
if (index== 0)
{
cout << "无可审核记录" << endl;
system("pause");
system("cls");
return;
}
cout << "请输入取消记录,0代表换回" << endl;
int select;
int ret;
while (1)
{
cin >> select;
if (select >= 0 && select <= v.size())
{
if (select == 0)
{
cout << "退出成功" << endl;
system("pause");
system("cls");
return;
}
else
{
cout << "请选择是否通过" << endl;
cout << "1.通过" << endl;
cout << "2.不通过" << endl;
cin >> ret;
if (ret == 1)
{
of.m_order[v[select - 1]]["status"] ="2";
}
else if (ret == 2)
{
of.m_order[v[select - 1]]["status"] ="3";
}
else
{
cout << "审核失败,已经退出" << endl;
}
of.updateorder();
cout << "审核成功" << endl;
break;
}
}
cout << "输入错误重新输入" << endl;
}
system("pause");
system("cls");
return;
}
void teacher::opermenu()
{
cout << "欢迎教师:" << this->I_name << "登录" << endl;
cout << "\t\t\t\t--------------------—————-----------------------" << endl;
cout << "\t\t\t\t------------------1.查看预约-------------------------" << endl;
cout << "\t\t\t\t------------------2.审核预约-------------------------" << endl;
cout << "\t\t\t\t------------------0.注销账号-------------------------" << endl;
cout << "\t\t\t\t--------------------—————-----------------------" << endl;
}
管理员类的实现:
#include"manager.h"
#include<fstream>
manager::manager(string name, string keyword)
{
this->I_name = name;
this->I_keyword = keyword;
ifstream ifs;
ifs.open(FILE_room, ios::in);
room r;
while (ifs >> r.comid && ifs >> r.comcapacity)
{
vc.push_back(r);
}
}
void manager::addmen()
{
cout << "请输入你需要添加的类型" << endl;
cout << "1.学生" << endl;
cout << "2.老师" << endl;
int choice;
cin >> choice;
string filename;
int id;
if (choice == 1)
{
filename = FILE_student;
cout << "请输入添加学生的学号" << endl;
cin >> id;
ifstream ifs;
ifs.open(filename, ios::in);
string E_name;
string E_keyword;
int E_id;
while (ifs >> E_id && ifs >> E_name && ifs >> E_keyword)
{
if (E_id == id)
{
cout << "你所输入的学号重复,添加失败" << endl;
system("pause");
system("cls");
return;
}
}
}
else
{
filename = FILE_teacher;
cout << "请输入职工编号" << endl;
cin >> id;
ifstream ifs;
ifs.open(filename, ios::in);
string E_name;
string E_keyword;
int E_id;
while (ifs >> E_id && ifs >> E_name && ifs >> E_keyword)
{
if (E_id == id)
{
cout << "你所输入的职工编号重复,添加失败" << endl;
system("pause");
system("cls");
return;
}
}
}
string name;
string keyword;
cout << "请输入添加姓名" << endl;
cin >> name;
cout << "请输入添加密码" << endl;
cin >> keyword;
ofstream ofs;
ofs.open(filename, ios::out | ios::app);
ofs << id << " " << name << " " << keyword << endl;
ofs.close();
cout << "添加成功" << endl;
system("pause");
system("cls");
return;
}
void manager::lookmen()
{
cout << "请输入你需要查看账号的类型" << endl;
cout << "1.学生" << endl;
cout << "2.老师" << endl;
int choice;
cin >> choice;
string filename;
if (choice == 1)
{
filename = FILE_student;
ifstream ifs;
ifs.open(filename, ios::in);
string name;
string keyword;
int id;
int sum=0;
while (ifs >> id && ifs >> name && ifs >> keyword)
{
cout << "学号:" << id <<" "<< "名字:" << name << " " << "密码:" << keyword << endl;;
sum++;
}
if (sum == 0)
{
cout << "当前没有学生账号" << endl;
}
else
{
cout << "当前学生总数为:" << sum << endl;
}
}
else
{
filename = FILE_teacher;
ifstream ifs;
ifs.open(filename, ios::in);
string name;
string keyword;
int id;
int sum = 0;
while (ifs >> id && ifs >> name && ifs >> keyword)
{
cout << "职工编号:" << id << " " << "名字:" << name << " " << "密码:" << keyword << endl;;
sum++;
}
if (sum == 0)
{
cout << "当前没有老师账号" << endl;
}
else
{
cout << "当前老师总数为:" << sum << endl;
}
}
system("pause");
system("cls");
return;
}
void manager::lookroom()
{
for (vector<room>::iterator it = vc.begin(); it != vc.end(); it++)
{
cout << it->comid << " " << it->comcapacity << endl;
}
system("pause");
system("cls");
return;
}
void manager::clearorder()
{
ifstream ifs;
ifs.open(FILE_order, ios::trunc);
cout << "清除成功" << endl;
ifs.close();
system("pause");
system("cls");
return;
}
void manager::opermenu()
{
cout << "欢迎管理员:" << this->I_name << "进入管理系统" << endl;
cout << endl;
cout << "\t\t\t\t--------------------—————-----------------------" << endl;
cout << "\t\t\t\t------------------1.添加账号-------------------------" << endl;
cout << "\t\t\t\t------------------2.查看账号-------------------------" << endl;
cout << "\t\t\t\t------------------3.查看机房-------------------------" << endl;
cout << "\t\t\t\t------------------4.清理预约-------------------------" << endl;
cout << "\t\t\t\t------------------0.注销账号-------------------------" << endl;
cout << "\t\t\t\t--------------------—————-----------------------" << endl;
}
管理文件类的实现:
#include"orderfile.h"
orderfile::orderfile()
{
ifstream ifs;
ifs.open(FILE_order, ios::in);
string date;
string time;
string id;
string name;
string choice;
string status;
while (ifs >> date && ifs >> time && ifs >> id && ifs >> name && ifs >> choice && ifs >> status)
{
string key;
string value;
map<string, string>m;
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));
}
pos = time.find(":");
if (pos != -1)
{
key = time.substr(0, pos);
value = time.substr(pos + 1, time.size() - pos - 1);
m.insert(make_pair(key, value));
}
pos = id.find(":");
if (pos != -1)
{
key = id.substr(0, pos);
value = id.substr(pos + 1, id.size() - pos - 1);
m.insert(make_pair(key, value));
}
pos = name.find(":");
if (pos != -1)
{
key = name.substr(0, pos);
value = name.substr(pos + 1, name.size() - pos - 1);
m.insert(make_pair(key, value));
}
pos = choice.find(":");
if (pos != -1)
{
key = choice.substr(0, pos);
value = choice.substr(pos + 1, choice.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));
}
this->m_order.insert(make_pair(this->m_size, m));
this->m_size++;
}
}
void orderfile::updateorder()
{
if (this->m_size == 0)
{
return;
}
ofstream ofs(FILE_order, ios::out | ios::trunc);
for(int i=0;i<this->m_size;i++)
{
ofs << "date:" << this->m_order[i]["date"] << " ";
ofs << "time:" << this->m_order[i]["time"] << " ";
ofs << "id:" << this->m_order[i]["id"] << " ";
ofs << "name:" << this->m_order[i]["name"] << " ";
ofs << "choice:" << this->m_order[i]["choice"] << " ";
ofs << "status:" << this->m_order[i]["status"] << endl;
}
ofs.close();
}
主函数:
#include<iostream>
#pragma once
using namespace std;
#include"globalfile.h"
#include"identity.h"
#include<fstream>
#include<string>
#include"student.h"
#include"teacher.h"
#include"manager.h"
void managermenu(identity*&manage)
{
manager* man = (manager*)manage;
while (1)
{
int choice;
man->opermenu();
cout << "请输入你需要的功能" << endl;
cin >> choice;
if (choice == 1)
{
man->addmen();
}
else if (choice == 2)
{
man->lookmen();
}
else if (choice == 3)
{
man->lookroom();
}
else if (choice == 4)
{
man->clearorder();
}
else
{
int option;
cout << "请确定是否要注销账号" << endl;
cout << "1.确定" << endl;
cout << "2.放弃注销" << endl;
cin >> option;
if (option == 1)
{
delete manage;
cout << "注销成功" << endl;
system("pause");
system("cls");
return;
}
system("pause");
system("cls");
}
}
}
void studentmenu(identity*& Student)
{
student* stu = (student*)Student;
int choice;
while (1)
{
stu->opermenu();
cout << "请输入你需要的功能" << endl;
cin >> choice;
if (choice == 1)
{
stu->addmyorder();
}
else if (choice == 2)
{
stu->openallorder();
}
else if (choice == 3)
{
stu->cancelorder();
}
else
{
cout << "请确认是否注销" << endl;
cout << "1.确定" << endl;
cout << "2.放弃注销" << endl;
int option;
cin >> option;
if (option == 1)
{
delete stu;
cout << "注销成功" << endl;
system("pause");
system("cls");
return;
}
system("pause");
system("cls");
}
}
}
void teachermenu(identity*& Teacher)
{
teacher* TE = (teacher*)Teacher;
int choice;
while (1)
{
TE->opermenu();
cout << "请输入你需要的功能" << endl;
cin >> choice;
if (choice == 1)
{
TE->openallorder();
}
else if (choice == 2)
{
TE->exammyorder();
}
else
{
cout << "请确认是否注销" << endl;
cout << "1.确定" << endl;
cout << "2.放弃注销" << endl;
int option;
cin >> option;
if (option == 1)
{
delete TE;
cout << "注销成功" << endl;
system("pause");
system("cls");
return;
}
system("pause");
system("cls");
}
}
}
void loadingin(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;
string name;
string keyword;
if (type == 1)
{
cout << "请输入你的学号" << endl;
cin >> id;
}
else if (type == 2)
{
cout << "请输入你的职工编号" << endl;
cin >> id;
}
cout << "请输入你的用户名" << endl;
cin >> name;
cout << "请输入你的登录密码" << endl;
cin >> keyword;
if (type == 1)
{
//学生验证
int passid;
string passname;
string passkeyword;
while (ifs >> passid && ifs >> passname && ifs >> passkeyword)
{
if (passid == id && passname == name && passkeyword == keyword)
{
cout << "身份验证成功,按任意键进入菜单" << endl;
system("pause");
system("cls");
person = new student(name, keyword, id);
studentmenu(person);
return;
}
}
}
else if(type == 2)
{
//老师验证
int passid;
string passname;
string passkeyword;
while (ifs >> passid && ifs >> passname && ifs >> passkeyword)
{
if (passid == id && passname == name && passkeyword == keyword)
{
cout << "身份验证成功,按任意键进入菜单" << endl;
system("pause");
system("cls");
person = new teacher(name, keyword, id);
teachermenu(person);
return;
}
}
}
else if (type == 3)
{
//管理员验证
string passname;
string passkeyword;
while (ifs >> passname && ifs >> passkeyword)
{
if ( passname == name && passkeyword == keyword)
{
cout << "身份验证成功,按任意键进入菜单" << endl;
system("pause");
system("cls");
person = new manager(name, keyword);
managermenu(person);
return;
}
}
}
cout << "登录失败,密码错误" << endl;
system("pause");
system("cls");
return ;
}
int main()
{
while (1)
{
cout << "请输入你的身份" << endl;
cout << "\t\t\t\t--------------------—————-----------------------" << endl;
cout << "\t\t\t\t------------------1.学生-----------------------------" << endl;
cout << "\t\t\t\t------------------2.老师-----------------------------" << endl;
cout << "\t\t\t\t------------------3.管理员---------------------------" << endl;
cout << "\t\t\t\t------------------0.退出-----------------------------" << endl;
cout << "\t\t\t\t--------------------—————-----------------------" << endl;
int choice;
cin >> choice;
switch (choice)
{
//学生
case 1:loadingin(FILE_student,choice); break;
//老师
case 2:loadingin(FILE_teacher, choice); break;
//管理员
case 3:loadingin(FILE_manager, choice); break;
case 0:exit(0); break;
default:cout << "输入错误,请重新输入" << endl;
system("pause");
system("cls");
break;
}
}
system("pause");
return 0;
}