请用C++编写一个学生、教师、管理员登录系统,其中每个身份都需要进行验证后进入子菜单,具体功能如下: 学生: 申请预约:预约机房 查看自己的预约:查看自己预约的机房及预约状态 查看所有预约:查看全部预约信息及预约状态 取消预约:取消自己预约的机房,预约成功或审核中的预约均可取消 注销登录:退出登录 教师: 查看所有预约:查看全部预约信息及预约状态 审核预约:一对一审核预约,可以通过或者不通过 注销登录:退出登录 管理员: 添加账号:添加学生或教师的账号,需要检测学生编号或教师职工号是否重复 查看账号:可选择查看学生或教师的全部信息 查看机房:查看所有机房的信息 清空预约:清空所有预约记录 注销登录:退出登录
时间: 2024-03-07 19:50:32 浏览: 115
用c++编写的一个学生信息管理系统,能进行各种操作,采用菜单录入方式。
4星 · 用户满意度95%
好的,以下是我给出的代码实现,包括学生、教师和管理员的功能:
```c++
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
// 学生结构体
struct Student {
string id; // 学号
string name; // 姓名
string password; // 密码
vector<int> roomNum; // 预约机房编号
vector<int> status; // 预约状态,0表示已经取消,1表示预约成功,2表示预约审核中
};
// 教师结构体
struct Teacher {
string id; // 职工号
string name; // 姓名
string password; // 密码
};
// 机房结构体
struct Room {
int roomNum; // 机房编号
int capacity; // 机房容量
};
// 管理员结构体
struct Admin {
string name; // 姓名
string password; // 密码
};
// 学生数组
vector<Student> studentVec;
// 教师数组
vector<Teacher> teacherVec;
// 机房数组
vector<Room> roomVec;
// 管理员数组
vector<Admin> adminVec;
// 学生登录函数
void studentLogin()
{
cout << "请输入学号:" << endl;
string id;
cin >> id;
cout << "请输入姓名:" << endl;
string name;
cin >> name;
cout << "请输入密码:" << endl;
string password;
cin >> password;
// 遍历学生数组,查找是否有该学生
for (vector<Student>::iterator it = studentVec.begin(); it != studentVec.end(); it++) {
if (it->id == id && it->name == name && it->password == password) {
cout << "登录成功!欢迎" << name << "同学!" << endl;
// 进入学生子菜单
while (true) {
cout << "======================" << endl;
cout << "请选择功能:" << endl;
cout << "1. 申请预约" << endl;
cout << "2. 查看自己的预约" << endl;
cout << "3. 查看所有预约" << endl;
cout << "4. 取消预约" << endl;
cout << "5. 注销登录" << endl;
cout << "======================" << endl;
int choice;
cin >> choice;
if (choice == 1) { // 申请预约
int roomNum;
cout << "请输入要预约的机房编号:" << endl;
cin >> roomNum;
// 遍历机房数组,查找是否有该机房
bool isExist = false;
for (vector<Room>::iterator it = roomVec.begin(); it != roomVec.end(); it++) {
if (it->roomNum == roomNum) {
isExist = true;
if (it->capacity == 0) {
cout << "对不起,该机房已经预约满了!" << endl;
break;
} else {
// 预约该机房
it->capacity--;
Student s = *it;
s.status.push_back(2);
s.roomNum.push_back(roomNum);
cout << "预约成功!" << endl;
break;
}
}
}
if (!isExist) {
cout << "对不起,查无此机房!" << endl;
}
} else if (choice == 2) { // 查看自己的预约
for (vector<Student>::iterator it = studentVec.begin(); it != studentVec.end(); it++) {
if (it->id == id) {
cout << "您的预约情况如下:" << endl;
for (int i = 0; i < it->roomNum.size(); i++) {
if (it->status[i] == 1) {
cout << "预约成功的机房编号为:" << it->roomNum[i] << endl;
} else if (it->status[i] == 2) {
cout << "审核中的机房编号为:" << it->roomNum[i] << endl;
} else {
cout << "已取消的机房编号为:" << it->roomNum[i] << endl;
}
}
break;
}
}
} else if (choice == 3) { // 查看所有预约
for (vector<Student>::iterator it = studentVec.begin(); it != studentVec.end(); it++) {
cout << "学号:" << it->id << ",姓名:" << it->name << ",预约情况如下:" << endl;
for (int i = 0; i < it->roomNum.size(); i++) {
if (it->status[i] == 1) {
cout << "预约成功的机房编号为:" << it->roomNum[i] << endl;
} else if (it->status[i] == 2) {
cout << "审核中的机房编号为:" << it->roomNum[i] << endl;
} else {
cout << "已取消的机房编号为:" << it->roomNum[i] << endl;
}
}
}
} else if (choice == 4) { // 取消预约
int roomNum;
cout << "请输入要取消预约的机房编号:" << endl;
cin >> roomNum;
// 遍历学生数组,查找是否有该学生
for (vector<Student>::iterator it = studentVec.begin(); it != studentVec.end(); it++) {
if (it->id == id) {
// 遍历该学生预约的机房编号数组,查找是否有该机房编号
for (int i = 0; i < it->roomNum.size(); i++) {
if (it->roomNum[i] == roomNum) {
if (it->status[i] == 1) {
cout << "该机房已经预约成功,无法取消预约!" << endl;
} else if (it->status[i] == 2) {
it->status[i] = 0;
it->roomNum.erase(it->roomNum.begin() + i);
cout << "取消预约成功!" << endl;
} else {
cout << "该机房已经取消预约,无需重复操作!" << endl;
}
break;
}
}
break;
}
}
} else if (choice == 5) { // 注销登录
cout << "注销登录成功!" << endl;
break;
} else {
cout << "输入有误,请重新输入!" << endl;
}
}
break;
}
}
}
// 教师登录函数
void teacherLogin()
{
cout << "请输入职工号:" << endl;
string id;
cin >> id;
cout << "请输入姓名:" << endl;
string name;
cin >> name;
cout << "请输入密码:" << endl;
string password;
cin >> password;
// 遍历教师数组,查找是否有该教师
for (vector<Teacher>::iterator it = teacherVec.begin(); it != teacherVec.end(); it++) {
if (it->id == id && it->name == name && it->password == password) {
cout << "登录成功!欢迎" << name << "老师!" << endl;
// 进入教师子菜单
while (true) {
cout << "======================" << endl;
cout << "请选择功能:" << endl;
cout << "1. 查看所有预约" << endl;
cout << "2. 审核预约" << endl;
cout << "3. 注销登录" << endl;
cout << "======================" << endl;
int choice;
cin >> choice;
if (choice == 1) { // 查看所有预约
for (vector<Student>::iterator it = studentVec.begin(); it != studentVec.end(); it++) {
cout << "学号:" << it->id << ",姓名:" << it->name << ",预约情况如下:" << endl;
for (int i = 0; i < it->roomNum.size(); i++) {
if (it->status[i] == 1) {
cout << "预约成功的机房编号为:" << it->roomNum[i] << endl;
} else if (it->status[i] == 2) {
cout << "审核中的机房编号为:" << it->roomNum[i] << endl;
} else {
cout << "已取消的机房编号为:" << it->roomNum[i] << endl;
}
}
}
} else if (choice == 2) { // 审核预约
int roomNum;
cout << "请输入要审核的机房编号:" << endl;
cin >> roomNum;
// 遍历学生数组,查找是否有该学生
for (vector<Student>::iterator it = studentVec.begin(); it != studentVec.end(); it++) {
// 遍历该学生预约的机房编号数组,查找是否有该机房编号
for (int i = 0; i < it->roomNum.size(); i++) {
if (it->roomNum[i] == roomNum && it->status[i] == 2) {
cout << "请输入审核结果(1表示通过,0表示不通过):" << endl;
int result;
cin >> result;
if (result == 1) {
it->status[i] = 1;
cout << "审核通过!" << endl;
} else if (result == 0) {
it->status[i] = 0;
cout << "审核不通过!" << endl;
} else {
cout << "输入有误,请重新输入!" << endl;
}
break;
}
}
}
} else if (choice == 3) { // 注销登录
cout << "注销登录成功!" << endl;
break;
} else {
cout << "输入有误,请重新输入!" << endl;
}
}
break;
}
}
}
// 管理员登录函数
void adminLogin()
{
cout << "请输入姓名:" << endl;
string name;
cin >> name;
cout << "请输入密码:" << endl;
string password;
cin >> password;
// 遍历管理员数组,查找是否有该管理员
for (vector<Admin>::iterator it = adminVec.begin(); it != adminVec.end(); it++) {
if (it->name == name && it->password == password) {
cout << "登录成功!欢迎" << name << "管理员!" << endl;
// 进入管理员子菜单
while (true) {
cout << "======================" << endl;
cout << "请选择功能:" << endl;
cout << "1. 添加账号" << endl;
cout << "2. 查看账号" << endl;
cout << "3. 查看机房" << endl;
cout << "4. 清空预约" << endl;
cout << "5. 注销登录" << endl;
cout << "======================" << endl;
int choice;
cin >> choice;
if (choice == 1) { // 添加账号
cout << "请选择要添加的账号类型(1表示学生,2表示教师):" << endl;
int type;
cin >> type;
if (type == 1) { // 添加学生账号
cout << "请输入学号:" << endl;
string id;
cin >> id;
cout << "请输入姓名:" << endl;
string name;
cin >> name;
cout << "请输入密码:" << endl;
string password;
cin >> password;
// 检查学生编号是否重复
bool isExist = false;
for (vector<Student>::iterator it = studentVec.begin(); it != studentVec.end(); it++) {
if (it->id == id) {
isExist = true;
break;
}
}
if (isExist) {
cout << "该学号已经存在,请重新输入!" << endl;
continue;
}
Student s;
s.id = id;
s.name = name;
s.password = password;
studentVec.push_back(s);
cout << "添加学生账号成功!" << endl;
} else if (type == 2) { // 添加教师账号
cout << "请输入职工号:" << endl;
string id;
cin >> id;
cout << "请输入姓名:" << endl;
string name;
cin >> name;
cout << "请输入密码:" << endl;
string password;
cin >> password;
// 检查教师职工号是否重复
bool isExist = false;
阅读全文