C++学生管理系统课程分配问题修复及switch替代if-else场景说明
问题1:switch语句替代嵌套if-else的适用场景
- 所有判断均为单个整型、字符型、枚举类型的等值判断场景,比如菜单选项选择、状态机状态跳转等,switch的分支逻辑比嵌套if-else更清晰易读
- 分支数量≥3个的等值判断场景,switch会被编译器优化为跳转表结构,执行效率远高于逐次条件判断的嵌套if-else
- 存在多个等值分支共用同一段处理逻辑的场景,可直接利用switch的case穿透特性简化代码,无需重复写多段相同的if判断逻辑
- 嵌套if-else层级超过3层时,用switch拆分逻辑可以大幅降低代码维护成本,减少漏写判断条件的概率
问题2:C++学生课程管理系统课程分配异常修复方案
问题根因
当前代码的错误来自3个核心逻辑问题:
- 没有单独存储公共课程列表,错误地把课程信息存在了学生结构体的
info属性中,相当于每个学生自己存了一套课程数据,逻辑完全错误 - 没有给每个学生存储自己选中的课程编号,输入的课程编号直接覆盖了全局变量
n,只有最后一次输入的课程编号会被保留 - 展示学生课程时,统一读取全局变量
n的最后一次值取值,导致所有学生的课程都显示为最后一次输入的课程值
修复步骤
- 单独定义
Course类型的数组存储所有公共课程,不要和学生结构体绑定 - 在
Student结构体中新增int selectedCourseIdx属性,存储每个学生自己选中的课程下标 - 录入学生选课信息时,把输入的课程编号转成下标后存入对应学生的
selectedCourseIdx属性中,不要覆盖循环变量 - 展示学生信息时,读取当前学生自身存储的
selectedCourseIdx,到公共课程数组中取出对应课程信息展示
修复后完整代码
#include <iostream> #include <string> using namespace std; struct Course { string CourseCode; string CourseName; string LecturerName; }; struct Student { string StudentID; string StudentName; string Nickname; int selectedCourseIdx; // 存储学生选中的课程下标 }; void menu(void) { cout << "=================================" << endl; cout << " MENU " << endl; cout << "=================================" << endl; cout << " 1- Add new course" << endl; cout << " 2- Display course offered" << endl; cout << " 3- Add new students" << endl; cout << " 4- Display student list" << endl; cout << " 5- Modify student name" << endl; cout << " 6- Display student's name after modification" << endl; cout << " 7- Exit Program" << endl; cout << "=================================" << endl << endl; } int main() { Course courses[2]; // 单独存储公共课程列表,最大2门 Student students[5]; // 存储学生信息,最大5人 int m, option, stuNo = 0, courNo = 0, opn; menu(); cout << "Choose an option from the menu: "; cin >> option; while (option != 7) { if (option == 1) { cout << "\nHow many courses do you want to enter? [Maximum 2]: "; cin >> courNo; if (courNo > 2) courNo = 2; for (m = 0; m < courNo; m++) { cout << "\nEnter course " << m + 1 << " details: " << endl; cout << "Course Code: "; cin >> courses[m].CourseCode; cout << "Course Name: "; cin >> courses[m].CourseName; cout << "Lecturer Name: "; cin >> courses[m].LecturerName; } } else if (option == 2) { cout << "\nThe courses details you entered are:" << endl; for (m = 0; m < courNo; m++) { cout << "\nCourse " << m + 1 << ": " << endl; cout << "Course Code: " << courses[m].CourseCode << endl; cout << "Course Name: " << courses[m].CourseName << endl; cout << "Lecturer Name: " << courses[m].LecturerName << endl; } } else if (option == 3) { cout << "\nHow many students do you want to enter? [Maximum 5]: "; cin >> stuNo; if (stuNo > 5) stuNo = 5; for (m = 0; m < stuNo; m++) { cout << "\nEnter student " << m + 1 << " details: " << endl; cout << "ID: "; cin >> students[m].StudentID; cout << "Name: "; cin >> students[m].StudentName; cout << "Nickname: "; cin >> students[m].Nickname; cout << "Course number from the list: "; cin >> opn; students[m].selectedCourseIdx = opn - 1; // 转成数组下标存储 } } else if (option == 4) { cout << "\nThe students details you entered are:" << endl; for (m = 0; m < stuNo; m++) { int cIdx = students[m].selectedCourseIdx; cout << "\nStudent " << m + 1 << ": " << endl; cout << "ID: " << students[m].StudentID << endl; cout << "Name: " << students[m].StudentName << endl; cout << "Nickname: " << students[m].Nickname << endl; if (cIdx >=0 && cIdx < courNo) { cout << "Course " << courses[cIdx].CourseName<< endl; } else { cout << "Course: Invalid course selection" << endl; } } } else if (option == 5) { cout << "\nEnter the student number that has been saved: "; cin >> opn; opn -= 1; if (opn >=0 && opn < stuNo) { cout << "\nEnter the new name: "; cin >> students[opn].StudentName; } else { cout << "Invalid student number" << endl; } } else if (option == 6) { cout << "\nEnter the student number to query: "; cin >> opn; opn -= 1; if (opn >=0 && opn < stuNo) { cout << "Student "<< opn + 1 <<" new name: " << students[opn].StudentName << endl; } else { cout << "Invalid student number" << endl; } } else cout << "Invalid number! Please re-enter a choice from the menu." << endl; cout << "=====================================================" << endl; cout << "\nChoose an option from the menu: "; cin >> option; } cout << "End of program!" << endl; return 0; }
内容的提问来源于stack exchange,提问作者ADAM78
相关产品推荐
相关产品推荐

