具体需求:
1.首先进入登陆界面,选项有:学生、老师、管理员、退出
2.每个身份需要进行验证后进入子菜单,每种身份用一个对应文件存放其账号姓名和密码。
3.学生具体功能
申请预约,预约机房
查看自身的预约
查看所有的预约
取消审核中和申请成功的预约
退出登录
4教师具体功能
查看所有预约
审核预约
退出登录
5.管理员具体功能
添加各种账号
查看账号
查看机房
退出登录
Identity.h 抽象类
#pragma once
#include<iostream>
using namespace std;
class identity
{
public:
virtual void openMenu()=0;//显示对应菜单
virtual void Enter() = 0;
virtual ~identity(){}
string m_name;//名字
string m_pwd;//密码
int m_sno;//账号
};
Student.h
#pragma once
#include<iostream>
#include"Identity.h"
#define file_c "ComputerRoom.txt"
#define file_r "Record.txt"
#include"GetComputerRoom.h"
using namespace std;
class student:public identity
{
public:
student();
student(string name, int sno, string pwd);
void openMenu();//学生界面
void applicate();//申请预约
void own_applicate();//查看自己的预约,返回记录条数
void all_applicate();//查看所有预约
void cancle();//取消预约
void Enter();//进入学生子系统
public:
GetFile p;
};
Student.cpp
#include "Student.h"
student::student()
{
this->m_pwd = "";
this->m_sno = 0;
this->m_name = "";
}
student::student(string name, int sno, string pwd)
{
this->m_name = name;
this->m_pwd = pwd;
this->m_sno = sno;
}
void student::openMenu()
{
cout << "欢迎" << this->m_name << "登录" << endl;
cout << " ------------------------------" << endl;
cout << " | |" << endl;
cout << " | 1.申请预约 |" << endl;
cout << " | |" << endl;
cout << " | 2.查看我的预约 |" << endl;
cout << " | |" << endl;
cout << " | 3.查看所有预约 |" << endl;
cout << " | |" << endl;
cout << " | 4.取消预约 |" << endl;
cout << " | |" << endl;
cout << " | 0.退出登录 |" << endl;
cout << " | |" << endl;
cout << " ------------------------------" << endl;
}
void student::applicate()
{
if (look_file(file_c) != 1)//机房记录文件非正常时
{
cout << "全部机房故障!暂时不可以申请!" << endl;
return;
}
cout << "机房开放时间为周一到周五" << endl;
cout << "请选择申请的时间:" << endl;
cout << "1.周一 2.周二 3.周三 4.周四 5.周五" << endl;
int week;
while (true)
{
cin >> week;
if (week > 0 && week < 6)
break;
cout << "请重新选择:" << endl;
}
cout << "请选择申请的时间段:" << endl;
cout << "1.上午 2.下午" << endl;
int day;
while (true)
{
cin >> day;
if (day == 1 || day == 2)
break;
cout << "请重新选择:" << endl;
}
cout << "请选择机房:" << endl;
int c = 1;
int number;
ifstream ifs(file_c, ios::in);
int cno, cnum;//机房编号和数目
while (ifs >> cno >> cnum)
{
cout << c << ". " << cno << "号机房容量:" << cnum << endl;
++c;
}
ifs.close();
while (true)
{
cin >> number;
if (number > 0 && number < c)
break;
cout << "请重新选择:" << endl;
}
ifs.open(file_c, ios::in);
c = 1;
while (ifs >> cno >> cnum)//找到对应的机房编号
{
if (c == number)
break;
++c;
}
ifs.close();
ofstream ofs(file_r, ios::app);
cout << "预约成功!" << endl;
ofs << m_sno << '\t' << m_name << '\t' << week << '\t' << day << '\t' << cno << '\t' << "审核中" << endl;
ofs.close();
}
void student::own_applicate()
{
p.save_to_record();
if (p.re_num == 0)
{
cout << "您暂无预约!" << endl;
return;
}
for (int i = 0; i < p.re_num; ++i)
{
if (p.record[i]->c_sno == this->m_sno)
{
p.Print(i);
}
}
}
void student::all_applicate()
{
p.save_to_record();
if (p.re_num == 0)
{
cout << "暂无记录!" << endl;
return;
}
for (int i = 0; i < p.re_num; ++i)
{
p.Print(i);
}
}
void student::cancle()
{
p.save_to_record();
int me(0);
cout << "请选择取消的预约编号:" << endl;
for (int i = 0; i < p.re_num; ++i)
{
if (p.record[i]->c_sno == this->m_sno &&
(p.record[i]->c_state =="审核中" || p.record[i]->c_state =="预约成功!"))
{
++me;
cout << me << ". ";
p.Print(i);
}
}
if (me == 0)
{
cout << "无可取消的预约!" << endl;
return;
}
int x;
cin >> x;
if (x < 1 || x > me)
{
cout << "无此选项,取消失败!" << endl;
return;
}
int rno(0);//存放所删记录在文件中的位置
for (int i = 0; i < p.re_num; ++i)
{
if (p.record[i]->c_sno == this->m_sno &&
(p.record[i]->c_state == "审核中" || p.record[i]->c_state == "预约成功!"))
{
--x;
if (x == 0)
{
rno = i;
break;
}
}
}
p.record[rno]->c_state = "预约已取消";
if (!p.save_to_file())
{
cout << "取消预约失败!请重试!" << endl;
return;
}
cout << "成功取消预约!" << endl;
}
void student::Enter()
{
int choose;
while (true)
{
system("cls");
openMenu();
cout << "请选择您要进行的操作:";
cin >> choose;
switch (choose)
{
case 1:
applicate();
system("pause");
break;
case 2:
own_applicate();
system("pause");
break;
case 3:
all_applicate();
system("pause");
break;
case 4:
cancle();
system("pause");
break;
case 0:
cout << "退出成功!" << endl;
system("pause");
return;
default:
cout << "无此选项!" << endl;
system("pause");
break;
}
}
}
Teacher.h
#pragma once
#include<iostream>
#include<string>
#include<fstream>
#include"Identity.h"
#include"GetComputerRoom.h"
using namespace std;
class teacher:public identity
{
public:
teacher();
teacher(string name, int sno, string pwd);
void openMenu();//教师子界面
void all_applicate();//查看所有记录
void make_decision();//审核预约
void Enter();//进入教师子系统
GetFile p;
};
Teacher.cpp
#include "Teacher.h"
teacher::teacher()
{
this->m_name = "";
this->m_pwd = "";
this->m_sno = 0;
}
teacher::teacher(string name, int sno, string pwd)
{
this->m_name = name;
this->m_pwd = pwd;
this->m_sno = sno;
}
void teacher::openMenu()
{
cout << "欢迎" << this->m_name << "登录!" << endl;
cout << " ------------------------------" << endl;
cout << " | |" << endl;
cout << " | 1.查看所有预约 |" << endl;
cout << " | |" << endl;
cout << " | 2.审核预约 |" << endl;
cout << " | |" << endl;
cout << " | 0.退出登录 |" << endl;
cout << " | |" << endl;
cout << " ------------------------------" << endl;
}
void teacher::all_applicate()
{
p.save_to_record();
if (p.re_num == 0)
{
cout << "暂无记录!" << endl;
return;
}
for (int i = 0; i < p.re_num; ++i)
{
p.Print(i);
}
}
void teacher::make_decision()//只会显示审核中状态的记录
{
p.save_to_record();
if (p.re_num == 0)
{
cout << "暂无预约需要审核!" << endl;
return;
}
int n = 0;
for (int i = 0; i < p.re_num; ++i)
{
if (p.record[i]->c_state == "审核中")
{
++n;
cout << n << ". ";
p.Print(i);
}
}
if (n == 0)
{
cout << "暂无预约需要审核!" << endl;
return;
}
cout << "请选择需要操作的记录:";
int tno;
cin >> tno;
if (tno<1 || tno>n)
{
cout << "选择错误!" << endl;
return;
}
cout << "请选择:" << endl;
cout << "1.通过 2.不通过" << endl;
int new_state;
cin >> new_state;
if (new_state != 1 && new_state != 2)
{
cout << "错误!" << endl;
return;
}
for (int i = 0; i < p.re_num; ++i)
{
if (p.record[i]->c_state == "审核中")
{
--tno;
if (tno == 0)
{
n = i;//找到所在位置
break;
}
}
}
if (new_state == 1)
p.record[n]->c_state = "预约成功!";
else
p.record[n]->c_state = "预约失败!";
if (!p.save_to_file())
cout << "审核失败!" << endl;
else
cout << "审核成功!" << endl;
}
void teacher::Enter()
{
int choose;
while (true)
{
system("cls");
openMenu();
cout << "请选择您要进行的操作:";
cin >> choose;
switch (choose)
{
case 1:
all_applicate();
system("pause");
break;
case 2:
make_decision();
system("pause");
break;
case 0:
cout << "退出成功!" << endl;
system("pause");
return;
default:
cout << "无此选项!" << endl;
system("pause");
break;
}
}
}
administator.h
#pragma once
#include<iostream>
#include<iomanip>
#include"Identity.h"
#include"File.h"
#include"Student.h"
#include"Teacher.h"
#define file_a "admini.txt"
#define file_s "student.txt"
#define file_t "teacher.txt"
#define file_c "ComputerRoom.txt"
#define file_r "Record.txt"
using namespace std;
class administator :public identity
{
public:
administator();//默认构造
administator(string name, int sno, string pwd);//有参构造
void openMenu();//管理员子窗口
void add();//添加新用户
void Look_user();//查看用户,不显示密码
void Look_computerRoom();//查看机房
void clear_appliaction();//清空预约
void Enter();//进入子窗口
~administator();
identity* id;//添加对象
};
administator.cpp
#include "administator.h"
administator::administator()
{
this->m_name = "";
this->m_pwd = "";
this->m_sno = 0;
this->id = NULL;
}
administator::administator(string name, int sno, string pwd)
{
this->m_name = name;
this->m_pwd = pwd;
this->m_sno = sno;
this->id = NULL;
}
void administator::openMenu()
{
cout << "欢迎" << this->m_name << "登录" << endl;
cout << " ------------------------------" << endl;
cout << " | |" << endl;
cout << " | 1.添加账号 |" << endl;
cout << " | |" << endl;
cout << " | 2.查看账号 |" << endl;
cout << " | |" << endl;
cout << " | 3.查看机房 |" << endl;
cout << " | |" << endl;
cout << " | 4.清空预约 |" << endl;
cout << " | |" << endl;
cout << " | 0.退出登录 |" << endl;
cout << " | |" << endl;
cout << " ------------------------------" << endl;
}
void administator::add()
{
int account;
cout << "请选择添加账号的类型:" << endl;
cout << "1.学生 2.老师 3.管理员" << endl;
cin >> account;
string filename;
switch (account)
{
case 1:
filename = file_s;
id = new student;
break;
case 2:
filename = file_t;
id = new teacher;
break;
case 3:
filename = file_a;
id = new administator;
break;
default:
cout << "选择错误!" << endl;
return;
}
int sno;
string name;
string pwd;
string pwd2;
cout << "请输入新增账号:";
while (true)
{
cin >> sno;
if (if_Repeat(sno, filename))
cout << "帐号重复,请重新输入:";
else break;
}
cout << "请输入姓名:";
cin >> name;
cout << "请输入密码(20个字符以内):";
while (true)
{
pwd = Password();
if (pwd == "")
{
cout << "密码不能为空!请重新输入:";
continue;
}
cout << "请确认您的密码:";
pwd2 = Password();
if (pwd == pwd2)
break;
else
cout << "两次密码不相符,请重新设置:";
}
id->m_name = name;
id->m_pwd = pwd;
id->m_sno = sno;
cout << id->m_name << ' ' << id->m_pwd << ' ' << id->m_sno << endl;
if (add_id(filename, id->m_sno, id->m_name, id->m_pwd))
cout << "添加成功!" << endl;
else
cout << "添加失败,请重试!" << endl;
}
void administator::Look_user()
{
cout << "请选择查看的对象:" << endl;
cout << "1.学生 2.老师 3.管理员" << endl;
int look;
string filename;
while (1)
{
cin >> look;
if (look < 1 || look>3)
cout << "选择错误,请重新选择:";
else
break;
}
if (look == 1)
filename = file_s;
else if (look == 2)
filename = file_t;
else filename = file_a;
if (look_file(filename) == -1)
{
cout << "文件打开失败!" << endl;
return;
}
else if (look_file(filename) == 0)
{
cout << "记录为空!" << endl;
return;
}
ifstream ifs(filename, ios::in);
int sno;
string name;
string pwd;
while (ifs >> sno >> name >> pwd)
{
cout << "账号:" << left << setw(10) << sno << '\t';
cout << "姓名:" << left << setw(10) << name << endl;
}
ifs.close();
}
void administator::Look_computerRoom()
{
int sno, num;
if (look_file(file_c) == -1)
{
cout << "文件打开失败!" << endl;
return;
}
else if (look_file(file_c) == 0)
{
cout << "记录为空!" << endl;
return;
}
ifstream ifs(file_c, ios::in);
while (ifs >> sno >> num)
{
cout << sno << "号机房" << " " << "容量:" << num << endl;
}
ifs.close();
}
void administator::clear_appliaction()
{
if (look_file(file_r) == -1)
{
cout << "文件打开失败!" << endl;
return;
}
ofstream ofs(file_r, ios::binary);
ofs.close();
cout << "文件已清空!" << endl;
}
void administator::Enter()
{
int choose;
while (true)
{
system("cls");
openMenu();
cout << "请选择您要进行的操作:";
cin >> choose;
switch (choose)
{
case 1:
add();
system("pause");
break;
case 2:
Look_user();
system("pause");
break;
case 3:
Look_computerRoom();
system("pause");
break;
case 4:
clear_appliaction();
system("pause");
break;
case 0:
cout << "退出成功!" << endl;
system("pause");
return;
default:
cout << "无此选项!" << endl;
system("pause");
break;
}
}
}
administator::~administator()
{
if (id != NULL)
{
delete id;
id = NULL;
}
}
File.h
#pragma once
#include<iostream>
#include<fstream>
#include<string>
#include<conio.h>
using namespace std;
int look_file(string path);//文件状态
int total(string path);//统计文件记录个数
bool if_Repeat(int sno, string path);//判断账号是否重复
string Password();//密码操作,实现输入为加密的*
bool add_id(string path, int sno, string name, string pwd);//添加新用户
File.cpp
#include "File.h"
int look_file(string path)
{
ifstream ifs(path, ios::in);
if (!ifs.is_open())//文件打不开
return -1;
char s;
ifs >> s;
if (ifs.eof())//文件为空
{
ifs.close();
return 0;
}
ifs.close();//正常
return 1;
}
int total(string path)
{
ifstream ifs(path, ios::in);
if (look_file(path) != 1)
return 0;
int n = 0;
string buf;
while (getline(ifs, buf))//一行一行的读
{
if (buf != "")//过滤掉空白行
++n;
}
return n;
}
bool if_Repeat(int sno, string path)
{
if (look_file(path) != 1)
return false;//不重复
int s;
string name;
string pwd;
ifstream ifs(path, ios::in);
while (ifs >> s >> name >> pwd)
{
if (s == sno)
{
return true;//重复!
}
}
return false;
}
string Password()
{
char mm[20];//存放密码,限长20
int mm_num = 0;//字符个数
while (true)
{
char ch;
ch = _getch();//从控制台读取一个字符,但不显示在屏幕上
if (ch == 8)//退格键ascll码为8
{
if (mm_num != 0)//有格可退
{
cout << char(8) << " " << char(8);//实现退一格操作,用空格显示
--mm_num;
}
}
else if (ch == '\r')//输入回车时结束
{
mm[mm_num] = '\0';
cout << endl;
break;
}
else if (ch == 32 || ch == '\t')//输入为空格或制表符时略过
continue;
else
{
cout << "*";
mm[mm_num] = ch;
++mm_num;
}
}
return mm;
}
bool add_id(string path, int sno, string name, string pwd)
{
ofstream ofs(path, ios::app);
if (!ofs.is_open())
return false;
ofs << sno << '\t' << name << '\t' << pwd << endl;
return true;
}
GetComputerRoom.h
#pragma once
#include<iostream>
#include<fstream>
#include<string>
#include<iomanip>
#include"ComputerRoom.h"
#include"File.h"
#define file "Record.txt"
using namespace std;
class GetFile
{
public:
GetFile();
void save_to_record();//将文件中的记录读到record
bool save_to_file();//将record存到文件
void Print(int i);//打印
~GetFile();
computerroom** record;//存放所有记录
int re_num;//记录数目
};
GetComputerRoom.cpp
#include "GetComputerRoom.h"
GetFile::GetFile()
{
this->record = NULL;
this->re_num = 0;
}
void GetFile::save_to_record()
{
if (record != NULL)//若p中有数据则先删
{
for (int i = 0; i < re_num; ++i)
delete record[i];
delete[]record;
record = NULL;
re_num = 0;
}
if (look_file(file) != 1)
return;
ifstream ifs(file, ios::in);
re_num = total(file);
record = new computerroom * [re_num];
computerroom c;
int i = 0;
while (ifs >> c.c_sno >> c.c_name >> c.c_week >> c.c_day >> c.c_cno >> c.c_state)
{
computerroom* com = new computerroom(c.c_sno, c.c_name, c.c_week, c.c_day, c.c_cno, c.c_state);
record[i] = com;
++i;
}
}
bool GetFile::save_to_file()
{
ofstream ofs(file, ios::binary);
if (!ofs.is_open())
return false;
for (int i = 0; i < re_num; ++i)
{
ofs << record[i]->c_sno << '\t';
ofs << record[i]->c_name << '\t';
ofs << record[i]->c_week << '\t';
ofs << record[i]->c_day << '\t';
ofs << record[i]->c_cno << '\t';
ofs << record[i]->c_state << endl;
}
return true;
}
void GetFile::Print(int i)
{
cout << "学号:" << left << setw(10) << record[i]->c_sno;
cout << "姓名:" << left << setw(6) << record[i]->c_name;
cout << "预约日期:周" << record[i]->c_week << '\t';
if (record[i]->c_day == 1)
cout << "时段:上午" << '\t';
else cout << "时段:下午" << '\t';
cout << "机房:" << record[i]->c_cno << '\t' << "状态:" << record[i]->c_state << endl;
}
GetFile::~GetFile()
{
if (record != NULL)
{
for (int i = 0; i < re_num; ++i)
delete record[i];
delete[]record;
record = NULL;
}
}
ComputerRoom.h
#include<iostream>
#include<string>
using namespace std;
class computerroom
{
public:
computerroom(int sno, string name, int week, int day, int cno, string state);
computerroom();
int c_sno;
string c_name;
int c_week;
int c_day;
int c_cno;
string c_state;
};
ComputerRoom.cpp
#include "ComputerRoom.h"
computerroom::computerroom(int sno, string name, int week, int day, int cno, string state)
{
this->c_cno = cno;
this->c_day = day;
this->c_name = name;
this->c_sno = sno;
this->c_state = state;
this->c_week = week;
}
computerroom::computerroom()
{
c_sno = 0;
c_name = "";
c_week = 0;
c_day = 0;
c_cno = 0;
c_state = "";
}
Window.h
#pragma once
#include<iostream>
#include<fstream>
#include"Student.h"
#include"Teacher.h"
#include"administator.h"
#include"File.h"
using namespace std;
class window
{
public:
window();
void show();
void get();//获取账号密码
void student_window();//学生子系统
void teacher_window();//老师子系统
void adnimi_window();//管理员子系统
bool judge(string path);//判断账号对否
~window();
public:
int w_sno;
string w_name;
string w_pwd;
identity* person;
};
Window.cpp
#include "Window.h"
window::window()
{
this->w_sno = 0;
this->w_name = "";
this->w_pwd = "";
person = NULL;
}
void window::show()
{
cout << "============= 欢迎进入某某某某某某大学机房预约系统 ===============" << endl;
cout << "请选择您的身份进行登录" << endl;
cout << " ------------------------------" << endl;
cout << " | |" << endl;
cout << " | 1.学 生 |" << endl;
cout << " | |" << endl;
cout << " | 2.教 师 |" << endl;
cout << " | |" << endl;
cout << " | 3.管理员 |" << endl;
cout << " | |" << endl;
cout << " | 0.退 出 |" << endl;
cout << " | |" << endl;
cout << " ------------------------------" << endl;
}
void window::get()
{
cout << "请输入您的账号:";
cin >> this->w_sno;
cout << "请输入您的密码:";
this->w_pwd = Password();
}
void window::student_window()
{
if (judge(file_s))
{
person = new student(w_name, w_sno, w_pwd);
system("cls");
person->Enter();
}
}
void window::teacher_window()
{
if (judge(file_t))
{
person = new teacher(w_name, w_sno, w_pwd);
system("cls");
person->Enter();
}
}
void window::adnimi_window()
{
if (judge(file_a))
{
person = new administator(w_name, w_sno, w_pwd);
system("cls");
person->Enter();
}
}
bool window::judge(string path)//账号姓名密码
{
get();
if (look_file(path) != 1)
{
cout << "暂无记录!" << endl;
system("pause");
return false;
}
ifstream ifs(path, ios::in);
int sno;
string name;
string pwd;
while (ifs >> sno >> name >> pwd)
{
if (sno == w_sno && pwd == w_pwd)
{
w_name = name;
return true;
}
}
cout << "账号或密码错误!" << endl;
system("pause");
return false;
}
window::~window()
{
if (person != NULL)
{
delete person;
person = NULL;
}
}
主函数.cpp
#include<iostream>
#include"Window.h"
#include"administator.h"
#include"File.h"
using namespace std;
int main()
{
window w;
int x;
while (true)
{
system("cls");
w.show();
cin >> x;
switch (x)
{
case 1:
w.student_window();
break;
case 2:
w.teacher_window();
break;
case 3:
w.adnimi_window();
break;
case 0:
cout << "欢迎下次使用!" << endl;
exit(0);
default:
cout << "无此选项" << endl;
system("pause");
break;
}
}
}
综上,代码比较粗滥冗长,一些状况没有测试到,基础功能大都实现。
几张运行结果截图: