【C++学习笔记】练习小项目之STL实现机房预约系统
一、功能描述
二、代码实现
三、学习后记
一、功能描述
功能和要求比较多,如下图所示:     
 思维导图:   
二、代码实现
main函数:
#include<iostream>
using namespace std;
#include<string>
#include"identity.h"
#include<fstream>
#include"globalFile.h"
#include"manager.h"
#include"student.h"
#include"teacher.h"
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 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");
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->cleanFile();
}
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 << "LHX================= 欢迎来到机房预约系统 =================LXY" << endl;
cout << endl;
cout << "请输入您的身份:" << 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 << "输入您的选择:";
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;
}
管理员头文件:
#pragma once
#include<iostream>
using namespace std;
#include"identity.h"
#include<string>
#include"globalFile.h"
#include<vector>
#include"student.h"
#include"teacher.h"
#include"computerRoom.h"
class Manager: public Identity
{
public:
Manager();
Manager(string name, string password);
virtual void operMenu();
void addPerson();
void showPerson();
void showComputer();
void cleanFile();
void initVecotr();
vector<Student>vStu;
vector<Teacher>vTea;
bool checkRepeat(int id, int ttype);
vector<computerRoom>vCom;
};
管理员源文件:
#include"manager.h"
#include<string>
#include<fstream>
#include"globalFile.h"
#include"student.h"
#include"teacher.h"
#include<algorithm>
Manager::Manager()
{
}
Manager::Manager(string name, string password)
{
this->m_name = name;
this->m_password = password;
this->initVecotr();
ifstream ifs;
ifs.open(COMPUTER_FILE, ios::in);
computerRoom com;
while (ifs >> com.m_comId&&ifs >> com.m_maxNum)
{
vCom.push_back(com);
}
ifs.close();
cout << "机房的数量为:" << vCom.size() << endl;
}
void Manager::operMenu()
{
cout << "欢迎管理员:" << this->m_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 << "\t\t---------------------------------------\n";
cout << "请选择您的操作:" << endl;
}
void Manager::addPerson()
{
cout << "请输入添加账号的类型" << endl;
cout << "1.添加学生" << endl;
cout << "2.添加老师" << endl;
string fileName;
string tip;
string errortip;
ofstream ofs;
int select = 0;
cin >> select;
if (select == 1)
{
fileName = STUDENT_FILE;
tip = "请输入学号:";
errortip = "学号重复,请重新输入";
}
else
{
fileName = TEACHER_FILE;
tip = "请输入职工号:";
errortip = "职工号重复,请重新输入";
}
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->initVecotr();
}
void printStudent(Student &s)
{
cout << "学号:" << s.m_id << "\t姓名:" << s.m_name
<< "\t密码:" << s.m_password << endl;
}
void printTeacher(Teacher &t)
{
cout << "职工号:" << t.m_teacherId << "\t姓名:" << t.m_name
<< "\t密码:" << t.m_password << endl;
}
void Manager::showPerson()
{
cout << "请选择查看的内容:" << endl;
cout << "1.查看所有的学生" << endl;
cout << "2.查看所有的老师" << endl;
int select = 0;
cin >> select;
if (select == 1)
{
cout << "所有学生的信息如下:" << endl;
for_each(vStu.begin(), vStu.end(), printStudent);
}
else
{
cout << "所有老师的信息如下:" << endl;
for_each(vTea.begin(), vTea.end(), printTeacher);
}
system("pause");
system("cls");
}
void Manager::showComputer()
{
cout << "机房信息如下:" << endl;
for (vector<computerRoom>::iterator it = vCom.begin(); it != vCom.end(); it++)
{
cout << "机房编号:" << it->m_comId << " 机房最大容量:" << it->m_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::initVecotr()
{
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_password)
{
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.m_teacherId &&ifs >> t.m_name&&ifs >> t.m_password)
{
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 (id == it->m_id)
{
return true;
}
}
}
else
{
for (vector<Teacher>::iterator it = vTea.begin(); it != vTea.end(); it++)
{
if (id == it->m_teacherId)
{
return true;
}
}
}
return false;
}
学生头文件
#pragma once
#include<iostream>
using namespace std;
#include"identity.h"
#include<string>
#include"computerRoom.h"
#include<fstream>
#include"globalFile.h"
#include<vector>
#include"orderFile.h"
class Student: public Identity
{
public:
Student();
Student(int id, string name, string password);
virtual void operMenu();
void applyOrder();
void showMyOrder();
void showAllOrder();
void cancelOrder();
int m_id;
vector<computerRoom> vCom ;
};
学生源文件:
#include"student.h"
#include<string>
Student::Student()
{
}
Student::Student(int id, string name, string password)
{
this->m_id = id;
this->m_name = name;
this->m_password = password;
ifstream ifs;
ifs.open(COMPUTER_FILE, ios::in);
computerRoom com;
while (ifs >> com.m_comId&&ifs >> com.m_maxNum)
{
vCom.push_back(com);
}
}
void Student::operMenu()
{
cout << "欢迎学生代表:" << this->m_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 << "\t\t--------------------------------------------\n";
cout << "请选择您的操作:" << endl;
}
void Student::applyOrder()
{
cout << "机房开放时间为周一至周五" << endl;
cout << "请输入申请预约的时间:" << endl;
cout << "1. 周一" << endl;
cout << "2. 周二" << endl;
cout << "3. 周三" << endl;
cout << "4. 周四" << endl;
cout << "5. 周五" << endl;
int data = 0;
int interval = 0;
int room = 0;
while (true)
{
cin >> data;
if (data >= 1 && data <= 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_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:" << data << " ";
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;
}
for (int i = 0; i < of.m_size; i++)
{
if (atoi(of.m_orderData[i]["stuId"].c_str()) == this->m_id)
{
cout << "预约日期:周" << of.m_orderData[i]["date"] << " ";
cout << "时间段:" <<
(of.m_orderData[i]["interval"] == "1" ? "上午" : "下午") << " ";
cout << "机房编号:" << of.m_orderData[i]["roomId"] << " ";
string status = "状态:";
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 << "、";
cout << "预约日期:周" << of.m_orderData[i]["date"] << " ";
cout << "时间段:" <<
(of.m_orderData[i]["interval"] == "1" ? "上午" : "下午") << " ";
cout << "学号:" << of.m_orderData[i]["stuId"] << " ";
cout << "姓名:" << of.m_orderData[i]["stuName"] << " ";
cout << "机房编号:" << of.m_orderData[i]["roomId"] << " ";
string status = "状态:";
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;
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++ << "、";
cout << "预约日期:周" << of.m_orderData[i]["date"] << " ";
cout << "时间段:" <<
(of.m_orderData[i]["interval"] == "1" ? "上午" : "下午") << " ";
cout << "学号:" << of.m_orderData[i]["stuId"] << " ";
cout << "姓名:" << of.m_orderData[i]["stuName"] << " ";
cout << "机房编号:" << of.m_orderData[i]["roomId"] << " ";
string status = "状态:";
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;
}
}
}
system("pause");
system("cls");
}
老师头文件
#pragma once
#include<iostream>
using namespace std;
#include"identity.h"
#include<string>
#include"orderFile.h"
class Teacher :public Identity
{
public:
Teacher();
Teacher(int id, string name, string password);
virtual void operMenu();
void showAllOrder();
void validOrder();
int m_teacherId;
};
老师源文件:
#include"teacher.h"
Teacher::Teacher()
{
}
Teacher::Teacher(int id, string name, string password)
{
}
void Teacher::operMenu()
{
cout << "欢迎教师:" << this->m_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 << "\t\t--------------------------------------------\n";
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 << "、";
cout << "预约日期:周" << of.m_orderData[i]["date"] << " ";
cout << "时间段:" <<
(of.m_orderData[i]["interval"] == "1" ? "上午" : "下午") << " ";
cout << "学号:" << of.m_orderData[i]["stuId"] << " ";
cout << "姓名:" << of.m_orderData[i]["stuName"] << " ";
cout << "机房编号:" << of.m_orderData[i]["roomId"] << " ";
string status = "状态:";
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;
}
cout << "待审核的预约记录如下:" << endl;
vector<int> v;
int index = 0;
for (int i = 0; i < of.m_size; i++)
{
if (of.m_orderData[i]["status"] == "1")
{
v.push_back(i);
cout << ++index << " ";
cout << "预约日期:周" << of.m_orderData[i]["date"] << " ";
cout << "时间段:" <<
(of.m_orderData[i]["interval"] == "1" ? "上午" : "下午") << " ";
cout << "学号:" << of.m_orderData[i]["stuId"] << " ";
cout << "姓名:" << of.m_orderData[i]["stuName"] << " ";
cout << "机房编号:" << of.m_orderData[i]["roomId"] << " ";
string status = "状态:审核中";
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;
cin >> ret;
if (ret == 1)
{
of.m_orderData[v[select - 1]]["status"] = "2";
}
else
{
of.m_orderData[v[select - 1]]["status"] = "-1";
}
of.updateOrder();
cout << "审核完毕!" << endl;
break;
}
}
cout << "输入有误,请重新输入!" << endl;
}
system("pause");
system("cls");
}
其他:
#pragma once
#include<iostream>
using namespace std;
#include"identity.h"
#include<string>
#include"computerRoom.h"
#include<fstream>
#include"globalFile.h"
#include<vector>
#include<map>
class orderFile
{
public:
orderFile();
void updateOrder();
int m_size;
map<int, map<string, string>>m_orderData;
};
#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(":");
if (pos != -1)
{
key = date.substr(0, pos);
value = date.substr(pos + 1, date.size());
m.insert(make_pair(key, value));
}
pos = interval.find(":");
if (pos != -1)
{
key = interval.substr(0, pos);
value = interval.substr(pos + 1, interval.size());
m.insert(make_pair(key, value));
}
pos = stuId.find(":");
if (pos != -1)
{
key = stuId.substr(0, pos);
value = stuId.substr(pos + 1, stuId.size());
m.insert(make_pair(key, value));
}
pos = stuName.find(":");
if (pos != -1)
{
key = stuName.substr(0, pos);
value = stuName.substr(pos + 1, stuName.size());
m.insert(make_pair(key, value));
}
pos = roomId.find(":");
if (pos != -1)
{
key = roomId.substr(0, pos);
value = roomId.substr(pos + 1, roomId.size());
m.insert(make_pair(key, value));
}
pos = status.find(":");
if (pos != -1)
{
key = status.substr(0, pos);
value = status.substr(pos + 1, status.size());
m.insert(make_pair(key, value));
}
this->m_orderData.insert(make_pair(this->m_size, m));
this->m_size++;
}
ifs.close();
}
void orderFile::updateOrder()
{
if (this->m_size == 0)
{
return;
}
ofstream ofs;
ofs.open(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();
}
computerRoom.h
#pragma once
#include<iostream>
using namespace std;
class computerRoom
{
public:
int m_comId;
int m_maxNum;
};
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"
identity.h
#pragma once
#include<iostream>
using namespace std;
class Identity
{
public:
virtual void operMenu() = 0;
string m_name;
string m_password;
};

三、学习后记
这是最后一个练习案例了,昨天到今天花2天时间做完了这个案例,感觉是一个很综合的练习,其中涉及前面所学的很多知识,不只是STL,还有多态等知识点。突然有一种且做且珍惜的感觉,哈哈!从七月初开始学C++的教程到今天,正式学完,完结撒花!~~~ 结束也是一种新的开始! 未来继续加油吧!! 多练多写多思考! 
|