爆肝10+小时做出来了(我是新手,比较拉)希望大家好好看看啊
/*
题目信息:
学校中现有3个机房,分别为大型、中型、小型机房,大型可容纳100人,中型可容纳50人,小型可容纳20人。
因为管理混乱,所以开发了机房预约系统。每位用户可登录三种账号类型,分别是学生代表、老师、管理员。
学生代表与管理员登录时需输入姓名与编号,老师需输入姓名、编号、学生统一编号。
机房周末关闭,预约时需输入周一~周五、还需选择上午或是下午与机房型号。
学生代表可预约机房(只可预约本人份)、查看自己所有预约 、查看所有人的预约、取消本身预约、退出登录。
老师可批量为学生预约机房、查看学生所有预约、批量取消预约、退出登录。
管理员可添加学生代表、老师、管理员账号、查看账号信息、依据编号查找账号信息、查看机房预约信息、清空机房预约、退出登录。
*/
#include<iostream>
#include<fstream>
#include<string>
#include<vector>//又是一堆头文件
using namespace std;
class guanli{//管理员类
public://为了方便访问把成员属性都放在public下了,但最好放在private下控制权限
int app(){//登录的成员函数
fstream f;
this->op = 0;//将op属性初始化为0来判断是否登录成功
f.open("管理员名单.txt",ios::in);//遍历管理员名单查找该管理员信息
string k, j, n;
int size1 = this->name.size(),pos;//拿到名字大小
while(getline(f,k)){
j = k.substr(6,size1);//截取子串
pos = k.find(this->number);//找到编号
if (j == this->name && pos != -1){ //子串相同与编号一起找到时就登录成功
op = 1;//方便返回值
cout << "登录成功" << endl;
break;
}
}
if (op == 0){//没有找到
cout << "登录失败" << endl;
return 1;
}
else{
return 0;
}
f.close();
}
void add(int choice){//添加账号的函数
fstream f;
string num, nam, n, o = "名单.txt", k;
int pos;
bool p1 = 0;
if (choice == 1){
n = "学生代表";
}
else if (choice == 2){
n = "老师";
}
else if(choice == 3){
n = "管理员";
}
else {
cout << "输入错误" << endl;
return;
}
n += o;//形成文件名方便打开
f.open(n.c_str(),ios::in);//以写入形式先查看编号是否重复
cout << "请输入他的姓名:";
cin >> nam;
cout << "请输入他的编号:";
cin >> num;
while(getline(f,k)){//一行行读入
pos = k.find(num);//寻找编号
if (pos != -1){
p1 = 1;
break;
}
}
if (p1){
cout << "编号重复!" << endl;
return;//找到就结束程序
}
f.close();
f.open(n.c_str(),ios::app);//追加形式打开
f << "姓名:" << nam << " 编号:" << num;//写入姓名与编号
if (choice == 2){//老师还需写入学生编号
int s_num;
cout << "请输入他的学生编号(大于100):";
cin >> s_num;
f << " 学生编号:" << s_num;
}
f << endl;
cout << "输入成功!" << endl;
f.close();
}
void look(int choice){//查看账号
string n, o = "名单.txt", k;
fstream f;
bool p = 1;
if (choice == 1){
n = "学生代表";
}
else if (choice == 2){
n = "老师";
}
else if(choice == 3){
n = "管理员";
}//根据选择查看账号
n += o;
f.open(n.c_str(),ios::in);
while (getline(f,k)){
p = 0;//用以判断文件是否为空
cout << k << endl;
}
if (p){
cout << "此文件为空" << endl;
}
f.close();
}
void find(int choice){//查找账号
string n, o = "名单.txt", k;
fstream f;
string nam,c, b;
bool p1 = 1;
if (choice == 1){
n = "学生代表";
}
else if (choice == 2){
n = "老师";
}
else if(choice == 3){
n = "管理员";
}
n += o;
f.open(n.c_str(),ios::in);//日常打开文件
cout << "请输入他的名字:" << endl;//依据名字查找
cin >> nam;
int size1 = nam.size();
while(getline(f,k)){
c = k.substr(6,size1);
if (c == nam){
cout << k << endl;
p1 = 0;//用以判断此人是否存在
break;
}
}
if (p1){
cout << "查无此人" << endl;
}
}
void clear(){//清空机房文件的函数
fstream f;
vector<string>day;
vector<string>time;
vector<string>size;
string d,k,s,n;
day.push_back("周一");
day.push_back("周二");
day.push_back("周三");
day.push_back("周四");
day.push_back("周五");
time.push_back("上午");
time.push_back("下午");
size.push_back("大型");
size.push_back("小型");
size.push_back("中型");//emmm,本人技术实在有限,只能暴力遍历+建立30个同级文件夹下的机房预约名单来实现功能
for (int i = 0; i < 5; i++){
for (int j = 0; j < 2; j++){
for (int o = 0; o < 3; o++){
d = day[i];
d += time[j];
d += size[o];
d += "机房预约名单.txt";
f.open(d.c_str(),ios::out);//以out形式打开相当于重写文件,然后直接关闭就可以清空文件。
f.close();
}
}
}
cout << "清除成功!" << endl;
}
string name;
string number;
int op;//三个属性
};
class student{
public:
int app(){//同样的登录,与管理员差不多,不做讲解
fstream f;
this->op = 0;
f.open("学生代表名单.txt",ios::in);
string k, j, n;
int size1 = this->name.size(),pos;
while(getline(f,k)){
j = k.substr(6,size1);
pos = k.find(this->number);
if (j == this->name && pos != -1){
op = 1;
cout << "登录成功" << endl;
break;
}
}
if (op == 0){
cout << "登录失败" << endl;
return 1;
}
else{
return 0;
}
f.close();
}
void yuyue(){//预约的函数
fstream f;
int ans = 0;
string day, k, time, size, norm = "机房预约名单.txt";
cout << "请选择您要预约的时间(输入周一、周二等):";
cin >> day;
cout << "请选择上午或是下午:";
cin >> time;
cout << "请选择机房型号(分为大型,中型,小型):";
cin >> size;
day += time;
day += size;
day += norm;//传统打开文件
f.open(day.c_str(),ios::in);
while(getline(f,k)){
ans++;//算出有多少人已经预约了
}
f.close();
f.open(day.c_str(),ios::app);
if ((ans < 30) | (ans < 50 && size != "小型") | (ans < 100 && size == "大型")){//小于30人不管什么机房都成功,小于50人大于30人不是小型机房就成功,大于50人小于100人只有大型才成功
f << ans+1 << ". 姓名:" << this->name << " 编号:" << this->number << endl;
f.close();
cout << "预约成功!" << endl;//写入并显示
}
else{
f.close();
cout << "预约失败,已满人。" << endl;
}
}
void lookmyself(){//查找自己预约信息的函数
fstream f;
bool p = 1;
vector<string>day;
vector<string>time;
vector<string>size;
string d,k,s,n;
day.push_back("周一");
day.push_back("周二");
day.push_back("周三");
day.push_back("周四");
day.push_back("周五");
time.push_back("上午");
time.push_back("下午");
size.push_back("大型");
size.push_back("小型");
size.push_back("中型");
for (int i = 0; i < 5; i++){
for(int j = 0; j < 2; j++){
for (int o = 0; o < 3; o++){//暴力遍历
d = day[i];
d += time[j];
d += size[o];
n = d;
n += "机房预约名单.txt";
f.open(n.c_str(),ios::in);
while(getline(f,k)){
int pos = k.find(" 编号:");//找到 编号的位置
s = k.substr(pos+7);//加7后指向:后数字
if (s == this->number){
p = 0;//判断是否有预约
cout << d << "机房:" << endl << k << endl;
}
}
f.close();
}
}
}
if (p){
cout << "你还没有预约哦" << endl;
}
}
void lookall(){//查看所有人预约信息
fstream f;
bool p1 = 1;
vector<string>day;
vector<string>time;
vector<string>size;
string d,k,s,n;
day.push_back("周一");
day.push_back("周二");
day.push_back("周三");
day.push_back("周四");
day.push_back("周五");
time.push_back("上午");
time.push_back("下午");
size.push_back("大型");
size.push_back("小型");
size.push_back("中型");//暴力遍历
for (int i = 0; i < 5; i++){
for(int j = 0; j < 2; j++){
for (int o = 0; o < 3; o++){
d = day[i];
d += time[j];
d += size[o];
n = d;
n += "机房预约名单.txt";
f.open(n.c_str(),ios::in);
f.seekg(0,ios::end);///这边用了一个不太一样的方式,将文件中指针从开头偏移至末尾。
streampos fp = f.tellg();//求出偏移了多少位置
if (int (fp) == 0){//没有偏移时就为空
cout << d << "机房暂时为空" << endl;
f.close();
continue;
}
cout << d << "机房名单:" << endl;
f.seekg(0,ios::beg);//偏移回去。若不回去将不会打印任何东西
while(getline(f,k)){
p1 = 0;
cout << k << endl;
}
f.close();
}
}
}
}
void del(){//取消预约函数
fstream f;
int ans = 0;
string day, time, size,k;
vector<string>v;//用来重写的容器
bool p1 = 1;
cout << "请选择您要取消预约的时间:";
cin >> day;
cout << "请选择上午或是下午:";
cin >> time;
cout << "请选择机房型号(分为大型,中型,小型):";
cin >> size;
day += time;
day += size;
day += "机房预约名单.txt";
f.open(day.c_str(),ios::in);//传统输入信息打开文件
while(getline(f,k)){
if(k.find(this->name) != -1){//查找账号名字 ,找到时不放进vector容器
p1 = 0;//判断当前账号是否预约
continue;
}
v.push_back(k);
ans++;//记录行数
}
if (p1){//未预约时
cout << "您没有预约当天的机房哦" << endl;
return;
}
f.close();
f.open(day.c_str(),ios::out);//传统清空
f.close();
f.open(day.c_str(),ios::app);
for (int i = 0; i < ans; i++){
f << i+1 << ". " << v[i].substr(3) << endl;//一行行写入,但此刻vector中没有该账号数据
}
cout << "删除成功" << endl;
f.close();
}
bool op;
string name;
string number;
};
class teacher{
public:
bool op;
string name;
string number;
string s_number;
int app(){//登录函数
fstream f;
this->op = 0;
f.open("老师名单.txt",ios::in);
string k, j, n;
int size1 = this->name.size(),pos,pos2;
while(getline(f,k)){
j = k.substr(6,size1);
pos = k.find(this->number);
pos2 = k.find(this->s_number);//s_number为学生编号
if (j == this->name && pos != -1 && pos2 != -1){//三个一起满足时登录成功
op = 1;
cout << "登录成功" << endl;
break;
}
}
if (op == 0){
cout << "登录失败" << endl;
return 1;
}
else{
return 0;
}
f.close();
}
void yuyue(){//批量预约函数
fstream f;
string day, k, time, size, norm = "机房预约名单.txt";
cout << "请选择您要预约的时间(输入周一、周二等):";
cin >> day;
cout << "请选择上午或是下午:";
cin >> time;
cout << "请选择机房型号(分为大型,中型,小型):";
cin >> size;
day += time;
day += size;
day += norm;
f.open(day.c_str(),ios::in);//传统打开文件
int people, cap,ans = 0,num;
while(getline(f,k)){
ans++;
}
cout << "请输入批量预约人数:";
cin >> people;//写入批量预约人数
if (size == "大型") cap = 100;//依据size识别容量
else if (size == "中型") cap = 50;
else if (size == "小型") cap = 30;
if (people + ans > cap){
cout << "预约失败,人太多了" << endl;
f.close();
return;
}
cout << "预约成功!" << endl;
f.close();
f.open(day.c_str(),ios::app);//以写入方式打开
for (int i = 1; i <= people; i++){
f << ans+i << ". 姓名:" << this->name << "老师班上的学生 编号:" << this->s_number << endl;
}
f.close();
}
void lookmystudent(){//查看学生预约信息
fstream f;
bool p = 1;
vector<string>day;
vector<string>time;
vector<string>size;
string d,k,s,n;
day.push_back("周一");
day.push_back("周二");
day.push_back("周三");
day.push_back("周四");
day.push_back("周五");
time.push_back("上午");
time.push_back("下午");
size.push_back("大型");
size.push_back("小型");
size.push_back("中型");
for (int i = 0; i < 5; i++){
for(int j = 0; j < 2; j++){
for (int o = 0; o < 3; o++){//传统暴力遍历
p = 1;//重置p来判断是否预约
d = day[i];
d += time[j];
d += size[o];
n = d;//这边留着d来打印,n来打开文件
n += "机房预约名单.txt";
f.open(n.c_str(),ios::in);
cout << d << "机房名单:" << endl;
while(getline(f,k)){
int pos = k.find(" 编号:");
s = k.substr(pos+7);
if (s == this->s_number){//用学生编号查找
p = 0;
cout << k << endl;//输出
}
}
if (p){
cout << "无预约" << endl;
}
f.close();
}
}
}
}
void del(){//批量取消预约
fstream f;
int ans = 0;
string day, time, size,k;
vector<string>v;
bool p1 = 1;//用来判断是否预约
cout << "请选择您要取消预约的时间:";
cin >> day;
cout << "请选择上午或是下午:";
cin >> time;
cout << "请选择机房型号(分为大型,中型,小型):";
cin >> size;
day += time;
day += size;
day += "机房预约名单.txt";
f.open(day.c_str(),ios::in);//日常打开文件
while(getline(f,k)){
if(k.find(this->s_number) != -1){
p1 = 0;
continue;
}
v.push_back(k);
ans++;//原理同学生预约,不多解释
}
if (p1){//判断并输出
cout << "您没有预约当天的机房哦" << endl;
return;
}
f.close();
f.open(day.c_str(),ios::out);//传统清空
f.close();
f.open(day.c_str(),ios::app);
for (int i = 0; i < ans; i++){
f << i+1 << ". " << v[i].substr(3) << endl;//一行行写入
}
cout << "删除成功" << endl;
f.close();
}
};
int main(){
int choice1;
cout << "欢迎来到机房管理预约系统!" << endl;
while(1){
cout << "请选择您的身份:" << endl;
cout << "1.学生代表" << endl;
cout << "2.老师" << endl;
cout << "3.管理员" << endl;
cout << "0.退出" << endl;//菜单页
cin >> choice1;
if (choice1 == 0){
cout << "下次再见";
break;
}
else if (choice1 == 1){
student s;
cout << "请输入您的姓名:";
cin >> s.name;//直接写入,不多解释
cout << "请输入您的编号:";
cin >> s.number;
int choice2, yu = s.app();
if (yu){//yu为1时代表未成功,重新登录,原因见以上登录函数返回值
continue;
}
while (1){
int choice6;
cout << "请选择您要选择的操作:" << endl;
cout << "1.申请预约" << endl;
cout << "2.查看自身预约" << endl;
cout << "3.查看所有预约" << endl;
cout << "4.取消预约" << endl;
cout << "5.退出登录" << endl;//5个功能的菜单页
cin >> choice6;
if (choice6 == 1){
s.yuyue();//调用yuyue函数
}
else if (choice6 == 2){
s.lookmyself();//调用lookmyself查看
}
else if (choice6 == 5){
cout << "已退出" << endl;
break;
}
else if (choice6 == 3){
s.lookall();//同调用
}
else if (choice6 == 4){
s.del();//同调用
}
}
}
else if (choice1 == 2){
teacher t;
cout << "请输入您的姓名:";
cin >> t.name;
cout << "请输入您的编号:";
cin >> t.number;
cout << "请输入您的学生编号:";
cin >> t.s_number;//与学生登录唯一区别就是多了学生编号
int choice7, yu = t.app();
if (yu){
continue;
}
while(1){
cout << "请选择您的操作:" << endl;
cout << "1.批量为学生预约" << endl;
cout << "2.查看自己学生预约信息" << endl;
cout << "3.批量取消预约" << endl;
cout << "4.退出登录" << endl;//4个功能菜单页
cin >> choice7;
if (choice7 == 4){
cout << "已退出" << endl;
break;
}
else if (choice7 == 1){
t.yuyue();
}
else if (choice7 == 2){
t.lookmystudent();
}
else if (choice7 == 3){
t.del();
}//调用函数,不多解释
}
}
else if (choice1 == 3){
guanli g;
cout << "请输入您的姓名:";
cin >> g.name;
cout << "请输入您的编号:";
cin >> g.number;
int choice2, yu = g.app();
if (yu){
continue;
}//同学生代表登录
while(1){
cout << "请选择您要选择的操作:" << endl;
cout << "1.添加账号" << endl;
cout << "2.查看账号" << endl;
cout << "3.查找账号" << endl;
cout << "4.查看机房信息" << endl;
cout << "5.清空机房预约名单" << endl;
cout << "6.退出登录" << endl;
cin >> choice2;
if (choice2 == 1){
int choice3;
cout << "请选择您要添加的账号类型:" << endl;
cout << "1.学生代表" << endl;
cout << "2.老师" << endl;
cout << "3.管理员" << endl;
cin >> choice3;
g.add(choice3);
}
else if (choice2 == 6){
cout << "已退出" << endl;
break;
}
else if (choice2 == 2){
int choice4;
cout << "请选择您要查看的账号类型:" << endl;
cout << "1.学生代表" << endl;
cout << "2.老师" << endl;
cout << "3.管理员" << endl;
cin >> choice4;
g.look(choice4);
}
else if (choice2 == 3){
int choice5;
cout << "请选择您要查找的账号类型:" << endl;
cout << "1.学生代表" << endl;
cout << "2.老师" << endl;
cout << "3.管理员" << endl;
cin >> choice5;
g.find(choice5);
}
else if (choice2 == 4){
student().lookall();//这边借用学生代表类的匿名函数实现一波功能
}
else if (choice2 == 5){
g.clear();
}//都是调用函数,不多解释
}
}
}
return 0;
} //感谢看到这边的朋友们!!!