// 登录成功,进入学生菜单 Student student = { id, password,{} }; int choice; do { studentMenu(); cin >> choice; switch (choice) { case 1: {// 查看可选课程 showCourseList(); break; } case 2: { // 选课 cout << "请输入要选的课程编号:" << endl; string code; cin >> code; for (size_t i = 0; i < courses.size(); i++) { if (courses[i].code == code) { // 找到课程,添加到已选课程列表 student.courses.push_back(courses[i]); cout << "选课成功!" << endl; break; } if (i == courses.size() - 1) { // 未找到课程 cout << "没有找到对应的课程,请重新输入!" << endl; } } break; } case 3: { // 查看已选课程 if (student.courses.empty()) { cout << "您还没有选课!" << endl; } else { cout << "已选课程:" << endl; for (size_t i = 0; i < student.courses.size(); i++) { cout << i + 1 << ". " << student.courses[i].name << "(编号:" << student.courses[i].code << ",地点:" << student.courses[i].location << ",学分:" << student.courses[i].credit << ")" << endl; } } break; } case 0: // 退出 break; default: cout << "无效的选择,请重新输入!" << endl; break; }代码内容
时间: 2024-04-07 17:27:59 浏览: 120
这段代码主要实现了学生登录成功后的菜单,包括查看可选课程、选课、查看已选课程等功能。
具体来说,程序会先根据学生的 id 和 password 创建一个 Student 对象,其中 courses 属性为空,表示未选课程。之后通过 do-while 循环显示学生菜单,让用户输入选择,然后根据用户的选择使用 switch 语句来调用对应的函数。
如果用户选择查看可选课程,程序会调用 showCourseList 函数来显示可选课程列表。如果用户选择选课,程序会让用户输入要选的课程编号,然后通过遍历 courses 列表来查找对应的课程,如果找到了课程则将其添加到学生的 courses 列表中,否则提示用户未找到对应的课程。如果用户选择查看已选课程,则会遍历学生的 courses 列表,逐个输出已选课程的信息。如果学生的 courses 列表为空,则提示用户还未选课。
如果用户输入的选择既不是 1、2、也不是 0,那么程序会输出无效的选择信息并重新显示学生菜单,直到用户输入 0 退出程序为止。
总的来说,这段代码的设计思路比较清晰,将不同的学生功能分别封装成不同的函数,通过 switch 语句来根据用户的选择调用对应的函数,使得代码结构更加清晰明了。同时,通过使用 Student 对象来存储学生信息和已选课程列表,也方便了学生功能的实现。
相关问题
bool studentLogin(vector<Student>& userInfos, string username, string password) { for (Student userInfo : userInfos) { if (userInfo.id == username && userInfo.password == password) { cout << "登录成功!" << endl; // 登录成功,进入学生菜单 Student student = { username, password,{} }; int choice; do { studentMenu(); cin >> choice; switch (choice) { case 1: {// 查看可选课程 showCourseList(); break; } case 2: { // 选课 cout << "请输入要选的课程编号:" << endl; string code; cin >> code; for (size_t i = 0; i < courses.size(); i++) { if (courses[i].code == code) { // 找到课程,添加到已选课程列表 student.courses.push_back(courses[i]); cout << "选课成功!" << endl; break; } if (i == courses.size() - 1) { // 未找到课程 cout << "没有找到对应的课程,请重新输入!" << endl; } } break; } case 3: { // 查看已选课程 if (student.courses.empty()) { cout << "您还没有选课!" << endl; } else { cout << "已选课程:" << endl; for (size_t i = 0; i < student.courses.size(); i++) { cout << i + 1 << ". " << student.courses[i].name << "(编号:" << student.courses[i].code << ",地点:" << student.courses[i].location << ",学分:" << student.courses[i].credit << ")" << endl; } } break; } case 0: // 退出 break; default: cout << "无效的选择,请重新输入!" << endl; break; } } else {cout<<"登录失败!!" 怎么修改
这段代码缺少大括号,应该在 `} else {` 后面添加大括号,形成完整的 if-else 结构。修改后的代码如下所示:
```
bool studentLogin(vector<Student>& userInfos, string username, string password) {
for (Student userInfo : userInfos) {
if (userInfo.id == username && userInfo.password == password) {
cout << "登录成功!" << endl;
// 登录成功,进入学生菜单
Student student = { username, password,{} };
int choice;
do {
studentMenu();
cin >> choice;
switch (choice) {
case 1: { // 查看可选课程
showCourseList();
break;
}
case 2: { // 选课
cout << "请输入要选的课程编号:" << endl;
string code;
cin >> code;
for (size_t i = 0; i < courses.size(); i++) {
if (courses[i].code == code) {
// 找到课程,添加到已选课程列表
student.courses.push_back(courses[i]);
cout << "选课成功!" << endl;
break;
}
if (i == courses.size() - 1) {
// 未找到课程
cout << "没有找到对应的课程,请重新输入!" << endl;
}
}
break;
}
case 3: { // 查看已选课程
if (student.courses.empty()) {
cout << "您还没有选课!" << endl;
}
else {
cout << "已选课程:" << endl;
for (size_t i = 0; i < student.courses.size(); i++) {
cout << i + 1 << ". " << student.courses[i].name << "(编号:" << student.courses[i].code <<
",地点:" << student.courses[i].location << ",学分:" << student.courses[i].credit << ")" << endl;
}
}
break;
}
case 0: // 退出
break;
default:
cout << "无效的选择,请重新输入!" << endl;
break;
}
} while (choice != 0);
return true;
}
}
cout << "登录失败!" << endl;
return false;
}
```
请用C++编写一个学生、教师、管理员登录系统,其中每个身份都需要进行验证后进入子菜单,具体功能如下: 学生: 申请预约:预约机房 查看自己的预约:查看自己预约的机房及预约状态 查看所有预约:查看全部预约信息及预约状态 取消预约:取消自己预约的机房,预约成功或审核中的预约均可取消 注销登录:退出登录 教师: 查看所有预约:查看全部预约信息及预约状态 审核预约:一对一审核预约,可以通过或者不通过 注销登录:退出登录 管理员: 添加账号:添加学生或教师的账号,需要检测学生编号或教师职工号是否重复 查看账号:可选择查看学生或教师的全部信息 查看机房:查看所有机房的信息 清空预约:清空所有预约记录 注销登录:退出登录
好的,以下是我给出的代码实现,包括学生、教师和管理员的功能:
```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;
阅读全文
相关推荐












