写在前面:
- 本系列专栏主要介绍C++的相关知识,思路以下面的参考链接教程为主,大部分笔记也出自该教程,笔者的原创部分主要在示例代码的注释部分。
- 除了参考下面的链接教程以外,笔者还参考了其它的一些C++教材(比如计算机二级教材和C语言教材),笔者认为重要的部分大多都会用粗体标注(未被标注出的部分可能全是重点,可根据相关部分的示例代码量和注释量判断,或者根据实际经验判断)。
- 如有错漏欢迎指出。
参考教程:黑马程序员匠心之作|C++教程从0到1入门编程,学习编程不再难_哔哩哔哩_bilibili
一、通讯录管理系统
1、系统需求
(1)通讯录管理系统中需要实现的功能如下:
①添加联系人:向通讯录中添加新人,信息包括(姓名、性别、年龄、联系电话、家庭住址)最多记录1000人。
②显示联系人:显示通讯录中所有联系人信息。
③删除联系人:按照姓名进行删除指定联系人。
④查找联系人:按照姓名查看指定联系人信息。
⑤修改联系人:按照姓名重新修改指定联系人。
⑥清空联系人:清空通讯录中所有信息。
⑦退出通讯录:退出当前使用的通讯录。
(2)菜单界面效果如下图:
2、参考代码
#include<iostream>
using namespace std;
#include<string>
#define MAX 1000//封装函数显示菜单界面
void showMenu()
{cout << "***************************" << endl;cout << "***** 1、添加联系人 *****" << endl;cout << "***** 2、显示联系人 *****" << endl;cout << "***** 3、删除联系人 *****" << endl;cout << "***** 4、查找联系人 *****" << endl;cout << "***** 5、修改联系人 *****" << endl;cout << "***** 6、清空联系人 *****" << endl;cout << "***** 0、退出通讯录 *****" << endl;cout << "***************************" << endl;
}
//设计联系人结构体
struct Person
{string m_name; //姓名int m_Sex; //性别:1-男 2-女int m_Age; //年龄string m_Phone; //电话号码string m_Addr; //地址
};
//设计通讯录结构体
struct Addressbooks
{struct Person personArray[MAX]; //通讯录中保存的联系人数组int m_Size; //通讯录当前记录联系人个数
};
//添加联系人
void addPerson(struct Addressbooks * abs)
{if (abs->m_Size == MAX-1){cout << "通讯率已满,无法添加!" << endl;return;}else{string name;cout << "请输入姓名: " << endl;cin >> name;abs->personArray[abs->m_Size].m_name = name;int sex = 0;cout << "请输入性别: " << endl;cout << "1---男 2---女 " << endl;cin >> sex;while (true){if (sex == 1 || sex == 2){abs->personArray[abs->m_Size].m_Sex = sex;break; //输入有效的性别数字,可以成功记录,退出循环,否则重新输入}else{cout << "你是不是找茬?好好输!" << endl;cin >> sex;}}int age = 0;cout << "请输入年龄: " << endl;cin >> age;abs->personArray[abs->m_Size].m_Age = age;string number;cout << "请输入联系电话: " << endl;cin >> number;abs->personArray[abs->m_Size].m_Phone = number;string address;cout << "请输入家庭住址: " << endl;cin >> address;abs->personArray[abs->m_Size].m_Addr = address;(abs->m_Size)++; //更新通讯录人数cout << "添加成功" << endl;system("pause"); //请按任意键继续system("cls"); //清屏操作}
}
//显示所有联系人
void showPreson(Addressbooks * abs)
{if ((abs->m_Size) == 0){cout << "通讯录没有联系人" << endl;}else{for (int i = 0; i < abs->m_Size; i++){cout << "姓名:" << abs->personArray[i].m_name << "\t";cout << "性别:" << (abs->personArray[i].m_Sex == 1 ? "男" : "女") << "\t";cout << "年龄:" << abs->personArray[i].m_Age << "\t";cout << "电话:" << abs->personArray[i].m_Phone << "\t";cout << "家庭住址:" << abs->personArray[i].m_Addr << endl;}}system("pause"); //请按任意键继续system("cls"); //清屏操作
}
//检测(判断)联系人是否存在
int isExist(Addressbooks * abs,string name)
{for (int i = 0; i < abs->m_Size; i++){if (abs->personArray[i].m_name == name) //如果找到用户输入的姓名{return i; //把用户在通讯录的位置返回去}}return -1; //找不到该用户,返回-1
}
//删除联系人
void deletePreson(Addressbooks * abs)
{cout << "请输入删除联系人姓名:" << endl;string name;cin >> name;if (isExist(abs, name) == -1) //先查这个人在不在{cout << "查无此人,删不了" << endl;}else //要删人,直接把这个人后面的人的数据往前移一格,覆盖这个人的数据{for (int i = isExist(abs, name); i < abs->m_Size; i++){abs->personArray[i] = abs->personArray[i + 1];}abs->m_Size--; //更新通讯录中的人员数cout << "删除成功" << endl;}system("pause"); //请按任意键继续system("cls"); //清屏操作
}
//查找联系人
void findPerson(Addressbooks * abs)
{cout << "请输入您要查找的联系人:" << endl;string name;cin >> name;int ret = isExist(abs, name);if (ret == -1) //先查这个人在不在{cout << "查无此人" << endl;}else{cout << "姓名:" << abs->personArray[ret].m_name << "\t";cout << "性别:" << (abs->personArray[ret].m_Sex == 1 ? "男" : "女") << "\t";cout << "年龄:" << abs->personArray[ret].m_Age << "\t";cout << "电话:" << abs->personArray[ret].m_Phone << "\t";cout << "家庭住址:" << abs->personArray[ret].m_Addr << endl;}system("pause"); //请按任意键继续system("cls"); //清屏操作
}
//修改联系人
void modifyPerson(Addressbooks * abs)
{cout << "请输入您要修改的联系人:" << endl;string name;cin >> name;int ret = isExist(abs, name);if (ret == -1) //先查这个人在不在{cout << "查无此人" << endl;}else{cout << "请输入修改后的姓名:" << endl;cin >> abs->personArray[ret].m_name;cout << "请输入修改后的性别(1--男,2--女):" << endl;int sex = 0;while (true){cin >> sex;if (sex == 1 || sex == 2){abs->personArray[ret].m_Sex = sex;break;}else{cout << "你输入了无效数字,重输!" << endl;}}cout << "请输入修改后的年龄:" << endl;cin >> abs->personArray[ret].m_Age ;cout << "请输入修改后的电话号码:" << endl;cin >> abs->personArray[ret].m_Phone ;cout << "请输入修改后的家庭住址:" << endl;cin >> abs->personArray[ret].m_Addr ;cout << "修改成功了捏" << endl;}system("pause"); //请按任意键继续system("cls"); //清屏操作
}
//清空联系人
void cleanPerson(Addressbooks * abs)
{abs->m_Size = 0; //把当前记录联系人数量置零,做逻辑清空操作//上面的函数全都需要m_Size参与,一旦这个值为0,很多语句都不会执行cout << "通讯录已清空" << endl;system("pause"); //请按任意键继续system("cls"); //清屏操作
}int main(){int select = 0; //创建用户选择输入的变量Addressbooks abs;//创建通讯录结构体变量abs.m_Size = 0; //初始化通讯录中当前人员个数showMenu(); //菜单调用while (true){cin >> select;switch (select){case 1:addPerson(&abs); //利用地址传递,可以修饰实参break; //添加联系人case 2:showPreson(&abs);break; //显示联系人case 3:deletePreson(&abs);break; //删除联系人case 4:findPerson(&abs);break; //查找联系人case 5:modifyPerson(&abs);break; //修改联系人case 6:cleanPerson(&abs) ;break; //清空联系人case 0:{cout << "欢迎下次使用" << endl;system("pause");return 0;}break; //退出通讯录default:break;}showMenu(); //清屏后重新显示菜单}
}
二、职工管理系统
1、系统需求
(1)公司中职工分为普通员工、经理、老板三类,显示信息时需要显示职工编号、职工姓名、职工岗位、以及职责。
(2)管理系统中需要实现的功能如下:
①退出管理程序:退出当前管理系统.
②增加职工信息:实现批量添加职工功能,将信息录入到文件中,职工信息为:职工编号、姓名、部门编号。
③显示职工信息:显示公司内部所有职工的信息。
④删除离职职工:按照编号删除指定的职工。
⑤修改职工信息:按照编号修改职工个人信息。
⑥查找职工信息:按照职工的编号或者职工的姓名进行查找相关的人员信息。
⑦按照编号排序:按照职工编号,进行排序,排序规则由用户指定。
⑧清空所有文档:清空文件中记录的所有职工信息 (清空前需要再次确认,防止误删)。
(3)系统界面效果图如下:
2、参考代码
(1)职工管理系统.cpp
#include<iostream>
using namespace std;
#include"workerManager.h"
#include"worker.h"
#include"employee.h"
#include"boss.h"
#include"manager.h"void test01()
{Worker *w1 = NULL;w1 = new Employee(1, "张三", 1);w1->showInof();delete w1;Worker *w2 = NULL;w2 = new Manager(2, "李四", 2);w2->showInof();delete w2;Worker *w3 = NULL;w3 = new Boss(3, "王五", 3);w3->showInof();delete w3;
}int main()
{/*test01();return 0;*/WorkerManager wm;int choice = 0;while (true){wm.Show_Menu();cout << "请输入您的选择:" << endl;cin >> choice;switch (choice){case 0: //退出系统wm.exitSystem();break;case 1: //增加职工wm.Add_Emp();break;case 2: //显示职工wm.Show_Emp();break;case 3: //删除职工wm.Del_Emp();break;case 4: //修改职工wm.Mod_Emp();break;case 5: //查找职工wm.Find_Emp();break;case 6: //职工排序wm.Sort_Emp();break;case 7: //清空文档wm.Clean_File();break;default:{cout << "你是不是不想干了?重输!" << endl;system("pause");system("cls");break;}}}system("pause");return 0;
}
(2)workermanager.cpp
#include"workerManager.h"
#include<string>WorkerManager::WorkerManager()
{ifstream ifs; //如果文件不存在ifs.open(FILENAME, ios::in); //读文件if (!ifs.is_open()) {//cout << "文件不存在!" << endl;m_EmpNum = 0; //初始化人数m_EmpArray = NULL; //初始化数组指针this->m_FileIsEmpty = true;ifs.close();return;}char ch; //如果文件为空ifs >> ch;if (ifs.eof()) {//cout << "文件为空!" << endl;m_EmpNum = 0; //初始化人数m_EmpArray = NULL; //初始化数组指针this->m_FileIsEmpty = true;ifs.close();return;}int num = this->get_EmpNum();//cout << "职工人数为: " << num << endl;this->m_EmpNum = num;this->m_EmpArray = new Worker*[this->m_EmpNum]; //开辟空间this->init_Emp(); //将文件中的数据存到数组中
}
int WorkerManager::get_EmpNum()
{ifstream ifs;ifs.open(FILENAME, ios::in); //打开文件,读它int id;string name;int dId;int num = 0;while (ifs >> id && ifs >> name && ifs >> dId){num++; //统计人数的变量}return num;
}
WorkerManager::~WorkerManager()
{if (this->m_EmpArray != NULL){delete[]this->m_EmpArray;this->m_EmpArray = NULL;}
}
void WorkerManager::Show_Menu()
{cout << "********************************************" << endl;cout << "********* 欢迎使用职工管理系统! **********" << endl;cout << "************* 0.退出管理程序 *************" << endl;cout << "************* 1.增加职工信息 *************" << endl;cout << "************* 2.显示职工信息 *************" << endl;cout << "************* 3.删除离职职工 *************" << endl;cout << "************* 4.修改职工信息 *************" << endl;cout << "************* 5.查找职工信息 *************" << endl;cout << "************* 6.按照编号排序 *************" << endl;cout << "************* 7.清空所有文档 *************" << endl;cout << "********************************************" << endl;cout << endl;
}
void WorkerManager::exitSystem()
{cout << "欢迎下次使用" << endl;system("pause");exit(0); //退出系统
}
void WorkerManager::Add_Emp()
{cout << "请输入需要增加职工的数量: ";int AddNum = 0;cin >> AddNum;if (AddNum > 0){int newSize = this->m_EmpNum + AddNum; //计算新空间的大小Worker ** newSpace = new Worker*[newSize]; //开辟新空间if (this->m_EmpArray != NULL) //将原空间下的内容存放到新空间下{for (int i = 0; i < m_EmpNum; i++){newSpace[i] = m_EmpArray[i];}}for (int i = 0; i < AddNum; i++) //输入新数据{int id;string name;int dSelect;cout << "请输入第" << i + 1 << "个新职工的编号: ";bool pass = true;while (true){cin >> id;for (int i = 0; i < m_EmpNum; i++){if (m_EmpArray[i]->m_Id == id){cout << "该编号已存在,请核实输入是否有误!" << endl;cout << "重新输入: ";pass = false;break;}}if (pass){break;}}cout << "请输入第" << i + 1 << "个新职工的姓名: ";cin >> name;cout << "请选择第" << i + 1 << "个新职工的岗位: " << endl<< "1、普通职工" << endl<< "2、经理" << endl<< "3、老板" << endl;cin >> dSelect;Worker * worker = NULL;switch (dSelect){case 1:worker = new Employee(id, name, 1);break;case 2:worker = new Manager(id, name, 2);break;case 3:worker = new Boss(id, name, 3);break;default:break;}newSpace[m_EmpNum + i] = worker;}delete[]this->m_EmpArray; //释放原有空间this->m_EmpArray = newSpace; //更新新空间的指向m_EmpNum = newSize; //更新新的个数cout << "添加成功了嗷" << endl; this->m_FileIsEmpty = false; //更新职工不为空的标志save();}else{cout << "你是不是想扣工资?" << endl;}system("pause");system("cls");
}
void WorkerManager::save()
{ofstream ofs;ofs.open(FILENAME, ios::out);for (int i = 0; i < this->m_EmpNum; i++){ofs << this->m_EmpArray[i]->m_Id << " "<< this->m_EmpArray[i]->m_Name << " "<< this->m_EmpArray[i]->m_Deptid << endl;}ofs.close();
}
void WorkerManager::init_Emp()
{ifstream ifs;ifs.open(FILENAME, ios::in);int id;string name;int dId;int index = 0;while (ifs >> id && ifs >> name && ifs >> dId){Worker * worker = NULL;switch (dId){case 1:worker = new Employee(id, name, dId);break;case 2:worker = new Manager(id, name, dId);break;case 3:worker = new Boss(id, name, dId);break;default:break;}this->m_EmpArray[index] = worker;index++;}ifs.close();
}
void WorkerManager::Show_Emp()
{if (this->m_FileIsEmpty){cout << "文件不存在或记录为空" << endl;}else{for (int i = 0; i < m_EmpNum; i++) //利用多态调用函数接口{this->m_EmpArray[i]->showInof();}}system("pause");system("cls");
}
int WorkerManager::IsExist(int id)
{int index = -1;for (int i = 0; i < m_EmpNum; i++){if (m_EmpArray[i]->m_Id == id){index = i;break;}}return index;
}
void WorkerManager::Del_Emp()
{if (this->m_FileIsEmpty){cout << "文件不存在或记录为空!" << endl;}else{int id = 0;cout << "请输入需要删除职工的编号: " << endl;cin >> id;int id0 = IsExist(id);if (id0 == -1){cout << "没找到该编号对应的员工,请核实是否输入正确" << endl;}else{cout << "您确定要删除吗?" << endl;cout << "姓名: " << m_EmpArray[id0]->m_Name;switch (m_EmpArray[id0]->m_Deptid){case 1:cout << " 职位:普通员工" << endl;break;case 2:cout << " 职位:经理" << endl;break;case 3:cout << " 职位:老板" << endl;break;default:break;}int select = 0;cout << "输入1即删除,输入其它数则将直接回到主界面" << endl;cin >> select;if (select == 1){for (int i = id0; i < m_EmpNum - 1; i++){m_EmpArray[i] = m_EmpArray[i + 1];}m_EmpNum--; //更新数组中记录的人员个数save(); //数据同步更新到文件中}}}system("pause");system("cls");
}
void WorkerManager::Mod_Emp()
{if (this->m_FileIsEmpty){cout << "文件不存在或记录为空!" << endl;}else{int id = 0;cout << "请输入需要修改职工的编号: " << endl;cin >> id;int id0 = IsExist(id);if (id0 == -1){cout << "没找到该编号对应的员工,请核实是否输入正确" << endl;}else{int newId = 0;string newName = "";int dSelect = 0;cout << "查到" << id << "号职工的姓名为" << m_EmpArray[id0]->m_Name;switch (m_EmpArray[id0]->m_Deptid){case 1:cout << " 职位:普通员工" << endl;break;case 2:cout << " 职位:经理" << endl;break;case 3:cout << " 职位:老板" << endl;break;default:break;}cout << "请输入新职工号(如果不是更改这项,输入原来的即可,下同理): " << endl;cin >> newId;cout << "请输入新姓名: " << endl;cin >> newName;cout << "请输入新岗位相应的编号: " << endl;cout << "1、普通职工" << endl;cout << "2、经理" << endl;cout << "3、老板" << endl;cin >> dSelect;Worker * worker = NULL;switch (dSelect){case 1:worker = new Employee(newId, newName, 1);break;case 2:worker = new Manager(newId, newName, 2);break;case 3:worker = new Boss(newId, newName, 3);break;default:break;}this->m_EmpArray[id0] = worker;cout << "修改成功了嗷" << endl;save();}}system("pause");system("cls");
}
void WorkerManager::Find_Emp()
{if (this->m_FileIsEmpty){cout << "文件不存在或记录为空!" << endl;}else{int select = 0;cout << "按照职工编号查找请输入1: " << endl;cout << "按照职工姓名查找请输入2: " << endl;cout << "筛选相应岗位的所有员工请输入3: " << endl;cin >> select;if (select == 1){cout << "输入需要查找的职工编号:";int id = 0;int j1 = 0;cin >> id;for (int i = 0; i < m_EmpNum; i++){if (m_EmpArray[i]->m_Id == id){m_EmpArray[i]->showInof();break;}j1++;}if (j1 == m_EmpNum){cout << "查无此人" << endl;}}else if (select == 2){cout << "输入需要查找的职工姓名:";string name;cin >> name;int j2 = 0;for (int i = 0; i < m_EmpNum; i++){if (m_EmpArray[i]->m_Name == name){m_EmpArray[i]->showInof();j2++;}}if (j2 == 0){cout << "查无此人" << endl;}}else if (select == 3){cout << "请输入筛选目标岗位相应的编号: " << endl;cout << "1、普通职工" << endl;cout << "2、经理" << endl;cout << "3、老板" << endl;int slt = 0;cin >> slt;switch (slt){case 1:{for (int i = 0; i < m_EmpNum; i++){if (m_EmpArray[i]->m_Deptid == 1){m_EmpArray[i]->showInof();}}break;}case 2:{for (int i = 0; i < m_EmpNum; i++){if (m_EmpArray[i]->m_Deptid == 2){m_EmpArray[i]->showInof();}}break;}case 3:{for (int i = 0; i < m_EmpNum; i++){if (m_EmpArray[i]->m_Deptid == 3){m_EmpArray[i]->showInof();}}break;}default:cout << "你输入了无效编号,再见" << endl;break;}}else{cout << "666666666666666666,6死了" << endl;}}system("pause");system("cls");
}
void WorkerManager::Sort_Emp()
{if (this->m_FileIsEmpty){cout << "文件不存在或记录为空!" << endl;}else{cout << "按照编号进行升序排序请扣1,按照编号进行降序排序请扣2,不要乱扣哦";int select;cin >> select;if (select == 1){for (int i = 0; i < m_EmpNum-1; i++){for (int j = 0; j < m_EmpNum - i - 1; j++){if (m_EmpArray[j]->m_Id > m_EmpArray[j + 1]->m_Id){Worker * temp = m_EmpArray[j + 1];m_EmpArray[j + 1] = m_EmpArray[j];m_EmpArray[j] = temp;}}}}else if (select == 2){for (int i = 0; i < m_EmpNum - 1; i++){for (int j = 0; j < m_EmpNum - i - 1; j++){if (m_EmpArray[j]->m_Id < m_EmpArray[j + 1]->m_Id){Worker * temp2 = m_EmpArray[j + 1];m_EmpArray[j + 1] = m_EmpArray[j];m_EmpArray[j] = temp2;}}}}else{cout << "建议你回炉重造" << endl;}}save();Show_Emp();
}
void WorkerManager::Clean_File()
{cout << "你确定要清空全部数据?输入1就不能后悔了哦!不想删除就随便输入一个其它数。" << endl;int select = 0;cin >> select;if (select == 1){ofstream ofs(FILENAME, ios::trunc); //清空文件(删除文件后重新创建)ofs.close(); //关闭文件if (this->m_EmpArray != NULL){for (int i = 0; i < m_EmpNum; i++){delete this->m_EmpArray[i];this->m_EmpArray[i] = NULL;}delete[] this->m_EmpArray;m_EmpArray = NULL;m_EmpNum = 0;m_FileIsEmpty = true;}cout << "清空成功!" << endl;}system("pause");system("cls");
}
(3)manager.cpp
#include"manager.h"
#include<string>void Manager::showInof()
{cout << "编号: " << m_Id<< "\t姓名:" << m_Name<< "\t岗位:" << this->getDeptname()<< "\t岗位职责: " << "完成老板交代的任务,并下发任务给普通员工" << endl;
}
string Manager::getDeptname()
{return string("经理");
}
Manager::Manager(int id, string name, int did)
{this->m_Id = id;this->m_Name = name;this->m_Deptid = did;
}
(4)employee.cpp
#include"employee.h"
#include<string>void Employee::showInof()
{cout << "编号: " << m_Id<< "\t姓名:" << m_Name<< "\t岗位:" << this->getDeptname()<< "\t岗位职责: " << "完成经理交代的任务" << endl;
}
string Employee::getDeptname()
{return string("普通员工");
}
Employee::Employee(int id, string name, int did)
{this->m_Id = id;this->m_Name = name;this->m_Deptid = did;
}
(5)boss.cpp
#include"boss.h"
#include<string>void Boss::showInof()
{cout << "编号: " << m_Id<< "\t姓名:" << m_Name<< "\t岗位:" << this->getDeptname()<< "\t岗位职责: " << "管理公司所有事物" << endl;
}
string Boss::getDeptname()
{return string("老板");
}
Boss::Boss(int id, string name, int did)
{this->m_Id = id;this->m_Name = name;this->m_Deptid = did;
}
(6)workerManager.h
#pragma once
#include<iostream>
using namespace std;
#include"worker.h"
#include"employee.h"
#include"manager.h"
#include"boss.h"#include<fstream>
#define FILENAME "empFile.txt"class WorkerManager
{
public:WorkerManager();void Show_Menu();void exitSystem();~WorkerManager();int m_EmpNum; //记录文件中的人数个数Worker ** m_EmpArray; //员工数组的指针void Add_Emp(); //增加职工---void save(); //把数据写入文件bool m_FileIsEmpty; //判断文件是否为空的标志int get_EmpNum(); //统计文件中的人数void init_Emp(); //初始化职工void Show_Emp(); //显示职工---void Del_Emp(); //删除职工---int IsExist(int id); //判断职工是否存在void Mod_Emp(); //修改职工---void Find_Emp(); //查找职工---void Sort_Emp(); //职工排序---void Clean_File(); //清空数据---
};
(7)worker.h
#pragma once
#include<iostream>
using namespace std;
#include<string>class Worker
{
public:int m_Id;string m_Name;int m_Deptid;virtual void showInof() = 0;virtual string getDeptname() = 0;
};
(8)manager.h
#pragma once
#include<iostream>
using namespace std;
#include"worker.h"class Manager :public Worker
{
public:Manager(int id, string name, int did);void showInof();string getDeptname();
};
(9)employee.h
#pragma once
#include<iostream>
using namespace std;
#include"worker.h"class Employee :public Worker
{
public:Employee(int id,string name,int did);void showInof();string getDeptname();
};
(10)boss.h
#pragma once
#include<iostream>
using namespace std;
#include"worker.h"class Boss :public Worker
{
public:Boss(int id, string name, int did);void showInof();string getDeptname();
};
三、演讲比赛流程管理系统
1、系统需求
(1)比赛规则和流程:
①学校举行一场演讲比赛,共有12个人参加。
②比赛共两轮,第一轮为淘汰赛,第二轮为决赛。
③比赛方式:分组比赛,每组6个人,选手每次要随机分组,进行比赛。
④每名选手都有对应的编号,如 10001 ~ 10012。
⑤第一轮分为两个小组,每组6个人,按照选手编号进行抽签后顺序演讲。
⑥当小组演讲完后,根据评分淘汰组内排名最后的三个选手,前三名晋级,进入下一轮的比赛。
⑦第二轮为决赛,得分前三名的胜出。
⑧每轮比赛过后需要显示晋级选手的信息。
(2)程序功能:
①开始演讲比赛:完成整届比赛的流程,每个比赛阶段需要给用户一个提示,用户按任意键后继续下一个阶段。
②查看往届记录:查看之前比赛前三名结果,每次比赛都会记录到文件中,文件用.csv后缀名保存。
③清空比赛记录:将文件中数据清空。
④退出比赛程序:可以退出当前程序。
(3)程序效果图:
2、参考代码
(1)main.cpp
#include<iostream>
using namespace std;
#include"speechManager.h"
#include<ctime>int main()
{srand((unsigned)time(NULL));speechManager m;/*for (map<int, Speaker>::iterator it = m.m_Speaker.begin(); it != m.m_Speaker.end(); it++){cout << "选手编号:" << it->first<< " 姓名: " << it->second.m_Name<< " 成绩: " << it->second.m_Score[0] << endl;}*/int choice = 0;while (true){m.show_Menu();cout << "请输入您的选择:" << endl;cin >> choice;switch (choice){case 1: //开始比赛m.startSpeech();break;case 2: //查看记录m.showRecord();break;case 3: //清空记录m.clearRecord();break;case 0: //退出程序m.Exitsystem();break;default:system("cls");break;}}system("pause");return 0;
}
(2)speaker.h
#pragma once
#include<iostream>
#include<string>
using namespace std;class Speaker
{
public:double m_Score[2]; //可能有两轮得分string m_Name; //姓名};
(3)speechManager.h
#pragma once
#include<iostream>
#include<vector>
#include<map>
#include<deque>
#include<algorithm>
#include<numeric>
#include<fstream>
#include<string>
#include"speaker.h"
using namespace std;class speechManager
{
public:speechManager(); //构造函数~speechManager(); //析构函数void show_Menu(); //菜单显示void Exitsystem(); //退出系统***vector<int>v1; //所有选手的容器12人vector<int>v2; //第一轮晋级容器6人vector<int>vVictory; //前三名容器3人map<int, Speaker>m_Speaker; //存放编号以及对应选手的容器int m_Index; //比赛轮数bool fileIsEmpty; //判断文件是否为空标志map<int, vector<string>>m_Record; //存放往届记录的容器void init_Speech(); //初始化属性void createSpeaker(); //创建选手void startSpeech(); //开始比赛***void speechDraw(); //抽签void speechContest(); //比赛void showScore(); //显示结果void saveRecord(); //保存记录void loadRecord(); //读取记录void showRecord(); //查看记录***void clearRecord(); //清空记录***
};
(4)speechManager.cpp
#include"speechManager.h"
#include<algorithm>speechManager::speechManager()
{init_Speech();createSpeaker();loadRecord();
}speechManager::~speechManager()
{}void speechManager::show_Menu()
{cout << "********************************************" << endl;cout << "************* 欢迎参加演讲比赛 ************" << endl;cout << "************* 1.开始演讲比赛 *************" << endl;cout << "************* 2.查看往届记录 *************" << endl;cout << "************* 3.清空比赛记录 *************" << endl;cout << "************* 0.退出比赛程序 *************" << endl;cout << "********************************************" << endl;cout << endl;
}void speechManager::Exitsystem()
{cout << "欢迎下次使用~" << endl;system("pause");exit(0);
}void speechManager::init_Speech()
{this->m_Index = 1; //初始化比赛轮数this->v1.clear(); //容器保证为空this->v2.clear();this->vVictory.clear();this->m_Speaker.clear();this->m_Record.clear();
}void speechManager::createSpeaker()
{string nameSeed = "ABCDEFGHIJKL";for (int i = 0; i < 12; i++){string name = "选手";name += nameSeed[i];Speaker speaker;speaker.m_Name = name;speaker.m_Score[0] = 0;v1.push_back(10001 + i);m_Speaker.insert(make_pair(i + 10001, speaker));}
}void speechManager::speechDraw()
{cout << "第" << m_Index << "轮选手抽签:" << endl;cout << "-------------------------------" << endl;cout << "抽签后演讲顺序如下:" << endl;if (m_Index == 1){random_shuffle(v1.begin(), v1.end());for (vector<int>::iterator it = v1.begin(); it != v1.end(); it++){cout << *it << " ";}cout << endl;}if (m_Index == 2){random_shuffle(v2.begin(), v2.end());for (vector<int>::iterator it = v2.begin(); it != v2.end(); it++){cout << *it << " ";}cout << endl;}cout << "----------------------------------" << endl;system("pause");cout << endl;
}
void speechManager::speechContest()
{cout << "第" << m_Index << "轮比赛开始:" << endl;cout << "-------------------------------" << endl;multimap<double, int, greater<int>> groupScore;int num = 0;vector<int>v_Src;if (m_Index == 1){v_Src = v1;}if (m_Index == 2){v_Src = v2;}for (vector<int>::iterator it = v_Src.begin(); it != v_Src.end(); it++){num++;deque<double>sc;for (int i = 0; i < 10; i++) //十个评委打分{double cc = (rand() % 400 + 600) / 10.f;//cout << cc << " ";sc.push_back(cc);}sort(sc.begin(),sc.end(),greater<double>());sc.pop_back(); //去除最低分sc.pop_front(); //去除最高分double avg = accumulate(sc.begin(), sc.end(), 0.0f) / (double)sc.size();//cout << "编号: " << *it << " 选手: " << this->m_Speaker[*it].m_Name << " 获取平均分为: " << avg << endl; //打印分数groupScore.insert(make_pair(avg, *it));//m_Speaker[*it] 中括号内为key值,不是简单的数组下标this->m_Speaker[*it].m_Score[this->m_Index - 1] = avg;if (num % 6 == 0){int i = 0;cout << "第" << num / 6 << "小组比赛名次:" << endl;for (multimap<double, int, greater<int>>::iterator it2 = groupScore.begin(); it2 != groupScore.end(); it2++, i++) {cout << "编号:" << it2->second << " 姓名:" << m_Speaker[it2->second].m_Name<< " 成绩:" << m_Speaker[it2->second].m_Score[m_Index - 1] << endl;if (m_Index == 1 && i < 3) {v2.push_back(it2->second);}if (m_Index == 2 && i < 3){vVictory.push_back(it2->second);}}groupScore.clear();}}cout << "------------- 第" << this->m_Index << "轮比赛完毕 ------------- " << endl;system("pause");
}
void speechManager::showScore()
{cout << "-------第" << m_Index << "轮晋级选手信息如下-------" << endl;vector<int>v_Src;if (m_Index == 1){v_Src = v2;}if (m_Index == 2){v_Src = vVictory;}for (vector<int>::iterator it = v_Src.begin(); it != v_Src.end(); it++){cout << "选手编号:" << *it << " 姓名:" << m_Speaker[*it].m_Name << " 得分:" << m_Speaker[*it].m_Score[m_Index - 1] << endl;}system("pause");system("cls");this->show_Menu();
}
void speechManager::saveRecord()
{ofstream ofs;ofs.open("speech.csv", ios::out | ios::app); //用追加的方式写文件for (vector<int>::iterator it = vVictory.begin(); it != vVictory.end(); it++){ofs << *it << "," << this->m_Speaker[*it].m_Score[1] << ",";}ofs << endl;ofs.close();cout << "记录成功保存" << endl;
}void speechManager::startSpeech()
{speechDraw();speechContest();showScore();m_Index++;speechDraw();speechContest();showScore();saveRecord();init_Speech();createSpeaker();loadRecord();cout << "本届比赛完毕" << endl;this->fileIsEmpty = false;system("pause");system("cls");
}void speechManager::loadRecord()
{ifstream ifs("speech.csv", ios::in);//没有文件情况if (!ifs.is_open()){//cout<< "文件不存在" << endl;this->fileIsEmpty = true;ifs.close();return;}//文件清空清空char ch;ifs >> ch;if (ifs.eof()){//cout << "文件为空" << endl;this->fileIsEmpty = true;ifs.close();return;}this->fileIsEmpty = false;ifs.putback(ch); //将上面读取的单个字符放回int index = 0;string data;while (ifs >> data){//cout << data << endl;vector<string>v;int pos = -1; //查逗号位置的变量(写文件时数据用逗号分隔了)int start = 0;while (true){pos = data.find(",", start);if (pos == -1){break;}string temp = data.substr(start, pos - start);//cout << temp << endl;v.push_back(temp);start = pos + 1;}this->m_Record.insert(make_pair(index, v));index++;}ifs.close();
}
void speechManager::showRecord()
{if (this->fileIsEmpty){cout << "文件不存在或记录为空!" << endl;}else {for (int index = 0; index < this->m_Record.size(); index++){cout << "第" << index + 1 << "届 冠军编号:" << m_Record[index][0] << " 得分:" << m_Record[index][1]<< " 亚军编号:" << m_Record[index][2] << " 得分:" << m_Record[index][3]<< " 季军编号:" << m_Record[index][4] << " 得分:" << m_Record[index][5] << endl;}}system("pause");system("cls");
}void speechManager::clearRecord()
{cout << "确认清空?" << endl;cout << "1、确认" << endl;cout << "2、返回" << endl;int select = 0;cin >> select;if (select == 1){//打开模式 ios::trunc 如果存在删除文件并重新创建ofstream ofs("speech.csv", ios::trunc);ofs.close();//初始化属性this->init_Speech();//创建选手this->createSpeaker();//获取往届记录this->loadRecord();cout << "清空成功!" << endl;}system("pause");system("cls");
}
四、机房预约系统
1、系统需求
(1)学校现有几个规格不同的机房,由于使用时经常出现"撞车"现象,现开发一套机房预约系统,解决这一问题。
(2)分别有三种身份使用该程序:
①学生代表:申请使用机房。
②教师:审核学生的预约申请。
③管理员:给学生、教师创建账号。
(3)机房总共有3间:
①1号机房最多允许20人同时使用。
②2号机房最多允许50人同时使用。
③3号机房最多允许100人同时使用。
(4)申请的订单每周由管理员负责清空;学生可以预约未来一周内的机房使用,预约的日期为周一至周五,预约时需要选择预约时段(上午、下午);教师来审核预约,依据实际情况审核预约通过或者不通过。
(5)系统具体需求:
①首先进入登录界面,可选登录身份有:学生代表、老师、管理员(、退出)。
②每个身份都需要进行验证后才能进入子菜单:
[1]学生需要输入:学号、姓名、登录密码。
[2]老师需要输入:职工号、姓名、登录密码。
[3]管理员需要输入:管理员姓名、登录密码。
③学生具体功能:
[1]申请预约——预约机房。
[2]查看自身的预约——查看自己的预约状态。
[3]查看所有预约——查看全部预约信息以及预约状态。
[4]取消预约——取消自身的预约,预约成功或审核中的预约均可取消。
[5]注销登录——退出登录。
④教师具体功能:
[1]查看所有预约——查看全部预约信息以及预约状态。
[2]审核预约——对学生的预约进行审核。
[3]注销登录——退出登录.
⑤管理员具体功能:
[1]添加账号——添加学生或教师的账号,需要检测学生编号或教师职工号是否重复。
[2]查看账号——可以选择查看学生或教师的全部信息。
[3]查看机房——查看所有机房的信息。
[4]清空预约——清空所有预约记录。
[5]注销登录——退出登录。
2、参考代码
(1)main.cpp
#include<iostream>
#include<string>
#include<fstream>
#include<numeric>
using namespace std;
#include"Identity.h"
#include"globalFile.h"
#include"manager.h"
#include"student.h"
#include"teacher.h"void managerMenu(Identity * &manager)
{while (true){Manager * man = (Manager *)manager;manager->opermenu();int choice = 0;cin >> choice;switch (choice){case 1: //添加账号{cout << "添加账号" << endl;man->addPerson();break;}case 2: //查看账号{cout << "查看账号" << endl;man->showPerson();break;}case 3: //查看机房{cout << "查看机房" << endl;man->showComputer();break;}case 4: //清空预约{cout << "清空预约" << endl;man->cleanFile();break;}case 0: //注销登录{delete manager;cout << "注销成功" << endl;system("pause");system("cls");return;break;}default:{cout << "输入无效数字,请重输" << endl;system("pause");system("cls");break;}}}
}
void studentMenu(Identity * &student)
{while (true){Student * man = (Student *)student;student->opermenu();int choice = 0;cin >> choice;switch (choice){case 1: //申请预约{cout << "申请预约" << endl;man->applyOrder();break;}case 2: //查看我的预约{cout << "查看我的预约" << endl;man->showMyOrder();break;}case 3: //查看所有预约{cout << "查看所有预约" << endl;man->showALLOrder();break;}case 4: //取消预约{cout << "取消预约" << endl;man->cancelOrder();break;}case 0: //注销登录{delete student;cout << "注销成功" << endl;system("pause");system("cls");return;break;}default:{cout << "输入无效数字,请重输" << endl;system("pause");system("cls");break;}}}
}
void teacherMenu(Identity * &teacher)
{while (true){Teacher * man = (Teacher *)teacher;teacher->opermenu();int choice = 0;cin >> choice;switch (choice){case 1: //查看所有预约{cout << "查看所有预约" << endl;man->showALLOrder();break;}case 2: //审核预约{cout << "查看我的预约" << endl;man->validOrder();break;}case 0: //注销登录{delete teacher;cout << "注销成功" << endl;system("pause");system("cls");return;break;}default:{cout << "输入无效数字,请重输" << endl;system("pause");system("cls");break;}}}
}void LoginTn(string fileName, int type)
{Identity * person = NULL;ifstream ifs;ifs.open(fileName, ios::in);if (!ifs.is_open()){cout << "文件不存在" << endl;ifs.close();return;}string name;string password;int id;if (type == 1){cout << "请输入您的学生学号:";cin >> id;}if (type == 2){cout << "请输入您的教师工号:";cin >> id;}cout << "请输入您的账号(姓名):";cin >> name;cout << "请输入您的账号密码:";cin >> password;int ID;string PASSWORD;string NAME;if (type == 1){while (ifs >> ID && ifs >> NAME && ifs >> PASSWORD){if (ID == id && NAME == name && PASSWORD == password){cout << "学生验证登录成功" << endl;system("pause");system("cls");person = new Student(id, name, password); //创建学生对象studentMenu(person); //进入学生代表子菜单return;}}}else if (type == 2){while (ifs >> ID && ifs >> NAME && ifs >> PASSWORD){if (ID == id && NAME == name && PASSWORD == password){cout << "教师验证登录成功" << endl;system("pause");system("cls");person = new Teacher(id, name, password); //创建教师对象teacherMenu(person); //进入教师子菜单return;}}}else if (type == 3){while (ifs >> NAME && ifs >> PASSWORD){if (NAME == name && PASSWORD == password){cout << "管理员验证登录成功" << endl;system("pause");system("cls");person = new Manager(name, password); //创建管理员对象managerMenu(person); //进入管理员子菜单return;}}}cout << "验证登录失败!" << endl;system("pause");system("cls");return;
}int main()
{while (true){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 << "\t\t -------------------------------\n";cout << "输入您的选择: ";int select = 0;cin >> select;switch (select){case 1: //学生代表LoginTn(STUDENT_FILE, 1);break;case 2: //老师LoginTn(TEACHER_FILE, 2);break;case 3: //管理员LoginTn(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;
}
(2)manager.cpp
#include"manager.h"
#include"computerRoom.h"
#include<algorithm>
#include<numeric>Manager::Manager()
{}
Manager::Manager(string name, string pwd) //有参构造
{this->m_Name = name;this->m_Pwd = pwd;initVector();initComputerRoom();
}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 << "1、添加学生" << endl << "2、添加老师" << endl;string fileName;ofstream ofs;string tip;string errortip;int select = 0;while (true){cin >> select;if (select == 1){fileName = STUDENT_FILE;tip = "请输入学生学号:";errortip = "该学生已有账号!";break;}if (select == 2){fileName = TEACHER_FILE;tip = "请输入职工编号:";errortip = "该职工已有账号!";break;}cout << "输入无效数字,请重新输入" << endl;}ofs.open(fileName, ios::out);int id;string name;string password;cout << tip;cin >> id;bool ret = checkRepeat(id, select);if (!ret){cout << errortip << endl;}else{cout << "请输入姓名:";cin >> name;cout << "请输入密码:";cin >> password;ofs << id << " " << name << " " << password << endl;cout << "添加成功!" << endl;}system("pause");system("cls");ofs.close();initVector();
}
void Manager::initVector()
{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_Pwd){vStu.push_back(s);}//cout << "当前学生数量为:" << vStu.size() << endl;//读取教师文件信息ifs.open(TEACHER_FILE, ios::in);if (!ifs.is_open()){cout << "读取文件失败" << endl;return;}Teacher t;while (ifs >> t.m_EmpId&&ifs >> t.m_Name&&ifs >> t.m_Pwd){vTea.push_back(t);}//cout << "当前教师数量为:" << vTea.size() << endl;ifs.close();
}
bool Manager::checkRepeat(int id, int type)
{if (type == 1){vector<Student>::iterator it = vStu.begin();for (; it != vStu.end(); it++){if (it->m_Id == id){break;}}if (it == vStu.end()){return true;}}else if (type == 2){vector<Teacher>::iterator it = vTea.begin();for (; it != vTea.end(); it++){if (it->m_EmpId == id){break;}}if (it == vTea.end()){return true;}}return false;
}
void Manager::showPerson() //查看账号
{cout << "请选择查看内容:" << endl << "1、查看所有学生" << endl<< "2、查看所有老师" << endl;int select = 0;while (true){cin >> select;if (select == 1){for (vector<Student>::iterator it = vStu.begin(); it != vStu.end(); it++){cout << "学号:" << it->m_Id << " 姓名:" << it->m_Name << " 密码:" << it->m_Pwd << endl;}system("pause");system("cls");break;}if (select == 2){for (vector<Teacher>::iterator it = vTea.begin(); it != vTea.end(); it++){cout << "职工号:" << it->m_EmpId << " 姓名:"<< it->m_Name << " 密码:" << it->m_Pwd << endl;}system("pause");system("cls");break;}cout << "输入无效数字,请重新输入" << endl;}
}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::initComputerRoom()
{vCom.clear();ifstream ifs;ifs.open(COMPUTER_FILE, ios::in);int id;int maxnum;while (ifs >> id && ifs >> maxnum){ComputerRoom com;com.m_ComId = id;com.m_MaxNum = maxnum;vCom.push_back(com);}ifs.close();
}
(3)orderFile.cpp
#include"orderFile.h"OrderFile::OrderFile()
{ifstream ifs(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));}m_orderData.insert(make_pair(m_Size,m));m_Size++;}ifs.close();
}void OrderFile::updateOrder()
{if (this->m_Size == 0){return;}ofstream ofs(ORDER_FILE, ios::out | ios::trunc);for (int i = 0; i < 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();
}
(4)student.cpp
#include"student.h"Student::Student()
{}
Student::Student(int id, string name, string pwd) //有参构造
{this->m_Id = id;this->m_Name = name;this->m_Pwd = pwd;ifstream ifs (COMPUTER_FILE, ios::in);ComputerRoom com;while (ifs >> com.m_ComId&&ifs >> com.m_MaxNum){vCom.push_back(com);}ifs.close();
}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 << "2、周二" << endl << "3、周三" << endl << "4、周四" << endl << "5、周五" << endl;int choice = 0;while (true){cin >> choice;if (choice == 1 || choice == 2 || choice == 3 || choice == 4 || choice == 5){break;}cout << "你输入了无效数字,请重新输入" << endl;}cout << "请输入申请预约的时间段:(输入对应数字编号!)" << endl;cout << "1、上午" << endl << "2、下午" << endl;int choice2 = 0;while (true){cin >> choice2;if (choice2 == 1 || choice2 == 2){break;}cout << "你输入了无效数字,请重新输入" << endl;}cout << "请选择机房:(输入对应数字编号!)" << endl;cout << "1、一号机房容量:" << vCom[0].m_MaxNum << endl;cout << "2、二号机房容量:" << vCom[1].m_MaxNum << endl;cout << "3、三号机房容量:" << vCom[2].m_MaxNum << endl;int choice3 = 0;while (true){cin >> choice3;if (choice3 == 1 || choice3 == 2 || choice == 3){break;}cout << "你输入了无效数字,请重新输入" << endl;}cout << "预约成功!请耐心等待审核……" << endl;ofstream ofs;ofs.open(ORDER_FILE, ios::app);ofs << "date:" << choice << " "; //日期ofs << "interval:" << choice2 << " "; //时间段ofs << "stuId:" << this->m_Id << " "; //学生编号ofs << "stuName:" << this->m_Name << " "; //学生姓名ofs << "roomId:" << choice3 << " "; //预约机房号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;}int i = 0;for (; i < of.m_Size; i++){if (atoi(of.m_orderData[i]["stuId"].c_str()) == this->m_Id){cout << "预约日期:周" << of.m_orderData[i]["date"] << " 时段:" << (of.m_orderData[i]["interval"] == "1" ? "上午" : "下午") << " 机房:";cout << of.m_orderData[i]["roomId"] << " 状态:";if (atoi(of.m_orderData[i]["status"].c_str()) == 1){cout << "审核中" << endl;}if (atoi(of.m_orderData[i]["status"].c_str()) == 2){cout << "已预约" << endl;}if (atoi(of.m_orderData[i]["status"].c_str()) == -1){cout << "预约失败" << endl;}if (atoi(of.m_orderData[i]["status"].c_str()) == 0){cout << "取消的预约" << 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"] << " 时段:"<< (of.m_orderData[i]["interval"] == "1" ? "上午" : "下午") << " 机房:";cout << of.m_orderData[i]["roomId"] << " 状态:";if (atoi(of.m_orderData[i]["status"].c_str()) == 1){cout << "审核中" << endl;}if (atoi(of.m_orderData[i]["status"].c_str()) == 2){cout << "已预约" << endl;}if (atoi(of.m_orderData[i]["status"].c_str()) == -1){cout << "预约失败" << endl;}if (atoi(of.m_orderData[i]["status"].c_str()) == 0){cout << "取消的预约" << 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 (atoi(of.m_orderData[i]["stuId"].c_str()) == this->m_Id&&(atoi(of.m_orderData[i]["status"].c_str()) == 1|| atoi(of.m_orderData[i]["status"].c_str()) == 2)){v.push_back(i);cout << index++ << "、";cout << "预约日期:周" << of.m_orderData[i]["date"] << " 时段:"<< (of.m_orderData[i]["interval"] == "1" ? "上午" : "下午") << " 机房:";cout << of.m_orderData[i]["roomId"] << " 状态:";if (atoi(of.m_orderData[i]["status"].c_str()) == 1){cout << "审核中" << endl;}if (atoi(of.m_orderData[i]["status"].c_str()) == 2){cout << "已预约" << endl;}}}int select = 0;cout << "请输入需要取消的记录,0代表返回" << endl;while (true){cin >> select;if (select == 0){system("pause");system("cls");return;}else{of.m_orderData[v[select - 1]]["status"] = "0";break;}cout << "输入有误,请重新输入" << endl;}cout << "已取消预约" << endl;of.updateOrder();system("pause");system("cls");
}
(5)teacher.cpp
#include"teacher.h"Teacher::Teacher()
{}
Teacher::Teacher(int empid, string name, string pwd) //有参构造
{this->m_EmpId = empid;this->m_Name = name;this->m_Pwd = pwd;
}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"] << " 时段:"<< (of.m_orderData[i]["interval"] == "1" ? "上午" : "下午") << " 机房:";cout << of.m_orderData[i]["roomId"] << " 状态:";if (atoi(of.m_orderData[i]["status"].c_str()) == 1){cout << "审核中" << endl;}if (atoi(of.m_orderData[i]["status"].c_str()) == 2){cout << "已预约" << endl;}if (atoi(of.m_orderData[i]["status"].c_str()) == -1){cout << "预约失败" << endl;}if (atoi(of.m_orderData[i]["status"].c_str()) == 0){cout << "取消的预约" << endl;}}system("pause");system("cls");
}
void Teacher::validOrder() //审核预约
{OrderFile of;cout << "待审核的预约记录如下:" << endl;int index = 1;vector<int>v;for (int i = 0; i < of.m_Size; i++){if (atoi(of.m_orderData[i]["status"].c_str()) == 1){cout << index++ << "、";cout << "预约日期:周" << of.m_orderData[i]["date"] << " 时段:"<< (of.m_orderData[i]["interval"] == "1" ? "上午" : "下午") << " 机房:";cout << of.m_orderData[i]["roomId"] << " 状态:审核中" << endl;v.push_back(i);}}if (index == 1){cout << "无需要审核的预约记录" << endl;system("pause");system("cls");return;}cout << "请输入审核的预约记录,0代表返回" << endl;int choice = 0;while (true){cin >> choice;if (choice == 0){system("pause");system("cls");return;}if (choice < index&&choice>0){break;}cout << "你输入了无效数字,请重新输入" << endl;}cout << "1、通过" << endl << "2、不通过" << endl;int select = 0;while (true){cin >> select;if (select == 0){break;}if (select == 1){of.m_orderData[v[choice - 1]]["status"] = "2";cout << "审核完毕!" << endl;break;}if (select == 2){of.m_orderData[v[choice - 1]]["status"] = "-1";cout << "审核完毕!" << endl;break;}cout << "你输入了无效数字,请重新输入" << endl;}of.updateOrder();system("pause");system("cls");
}
(6)computerRoom.h
#pragma once
#include<iostream>
using namespace std;class ComputerRoom
{
public:int m_ComId; //机房id号int m_MaxNum; //机房最大容量
};
(7)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"
(8)Identity.h
#pragma once
#include<iostream>
#include<string>
using namespace std;class Identity //身份抽象类
{
public:string m_Name; //用户名string m_Pwd; //密码virtual void opermenu() = 0; //操作菜单};
(9)manager.h
#pragma once
#include<iostream>
#include<string>
#include<fstream>
#include<vector>
#include"Identity.h"
#include"globalFile.h"
#include"student.h"
#include"teacher.h"
#include"computerRoom.h"
#include<algorithm>
using namespace std;class Manager :public Identity
{
public:Manager();Manager(string name, string pwd); //有参构造virtual void opermenu(); //菜单界面void addPerson(); //添加账号void showPerson(); //查看账号void showComputer(); //查看机房信息void cleanFile(); //清空预约记录//初始化容器void initVector();//学生容器vector<Student> vStu;//教师容器vector<Teacher> vTea;//检测重复 参数:(传入id,传入类型) 返回值:(true 代表有重复,false代表没有重复)bool checkRepeat(int id, int type);//机房容器vector<ComputerRoom> vCom;void initComputerRoom(); //初始化机房信息
};
(10)orderFile.h
#pragma once
#include<iostream>
#include<fstream>
#include<string>
using namespace std;
#include <map>
#include "globalFile.h"class OrderFile
{
public://构造函数OrderFile();//更新预约记录void updateOrder();//记录的容器 key --- 记录的条数 value --- 具体记录的键值对信息map<int, map<string, string>> m_orderData;//预约记录条数int m_Size;
};
(11)student.h
#pragma once
#include<iostream>
#include<string>
#include"Identity.h"
#include"globalFile.h"
#include"computerRoom.h"
#include"orderFile.h"
#include<fstream>
#include<vector>
using namespace std;class Student :public Identity
{
public:Student();Student(int id, string name, string pwd); //有参构造virtual void opermenu(); //菜单界面void applyOrder(); //申请预约void showMyOrder(); //查看我的预约void showALLOrder(); //查看所有预约void cancelOrder(); //取消预约int m_Id; //学生学号//机房容器vector<ComputerRoom> vCom;
};
(12)teacher.h
#pragma once
#include<iostream>
#include<string>
#include<vector>
#include"orderFile.h"
#include"Identity.h"
#include"globalFile.h"
using namespace std;class Teacher :public Identity
{
public:Teacher();Teacher(int empid, string name, string pwd); //有参构造virtual void opermenu(); //菜单界面void showALLOrder(); //查看所有预约void validOrder(); //审核预约int m_EmpId; //教师工号
};