何时何地使用 switch 语句而不是嵌套的 if-else 语句?
When and where to use switch statement instead of nested if-else statement?
我对 C++
中的结构有疑问,我写了下面的代码,我想给每个学生分配不同的课程,我一直在尝试进行不同的分配,但无法弄清楚正确的做法。
这是我的代码:
#include <iostream>
using namespace std;
struct Course {
string CourseCode;
string CourseName;
string LecturerName;
int CourseNumber;
};
struct Student {
string StudentID;
string StudentName;
string Nickname;
Course info;
};
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()
{
Student* ptr[5];
Student details[5];
for (int i = 0; i < 2; i++) {
ptr[i] = &details[i];
}
int m, option, stuNo, courNo, n;
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;
for (m = 0; m < courNo; m++) {
cout << "\nEnter course " << m + 1 << " details: " << endl;
cout << "Course Code: ";
cin >> ptr[m]->info.CourseCode;
cout << "Course Name: ";
cin >> ptr[m]->info.CourseName;
cout << "Lecturer Name: ";
cin >> ptr[m]->info.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: " << ptr[m]->info.CourseCode << endl;
cout << "Course Name: " << ptr[m]->info.CourseName << endl;
cout << "Lecturer Name: " << ptr[m]->info.LecturerName << endl;
}
}
else if (option == 3) {
cout << "\nHow many students do you want to enter? [Maximum 5]: ";
cin >> stuNo;
for (m = 0, n=0; m < stuNo; m++, n++) {
cout << "\nEnter student " << m + 1 << " details: " << endl;
cout << "ID: ";
cin >> ptr[m]->StudentID;
cout << "Name: ";
cin >> ptr[m]->StudentName;
cout << "Nickname: ";
cin >> ptr[m]->Nickname;
cout << "Course number from the list: ";
cin >> n; // This gives same output for both students
}
}
else if (option == 4) {
cout << "\nThe students details you entered are:" << endl;
for (m = 0; m < stuNo; m++) {
cout << "\nStudent " << m + 1 << ": " << endl;
cout << "ID: " << ptr[m]->StudentID << endl;
cout << "Name: " << ptr[m]->StudentName << endl;
cout << "Nickname: " << ptr[m]->Nickname << endl;
cout << "Course " << ptr[n-1]->info.CourseName<< endl;
}
}
else if (option == 5) {
cout << "\nEnter the student number that has been saved in option 1,2: ";
cin >> n;
cout << "\nEnter the new name: ";
cin >> ptr[n-1]->StudentName;
}
else if (option == 6) {
cout << "\nStudent "<< n <<" new name: ";
cout << ptr[n - 1]->StudentName;
cout << 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;
}
if (option == 7)
cout << "End of program!" << endl;
}
我知道根据我的代码分配给学生的课程的输出是相同的,因为我只有 n
,如何更改它以使其对每个学生都不同
这是输出:
=================================
MENU
=================================
1- Add new course
2- Display course offered
3- Add new students
4- Display student list
5- Modify student name
6- Display student's name after modification
7- Exit Program
=================================
Choose an option from the menu: 1
How many courses do you want to enter? [Maximum 2]: 2
Enter course 1 details:
Course Code: 111
Course Name: co1
Lecturer Name: lect1
Enter course 2 details:
Course Code: 222
Course Name: co2
Lecturer Name: lect2
=====================================================
Choose an option from the menu: 3
How many students do you want to enter? [Maximum 5]: 2
Enter student 1 details:
ID: 1111
Name: student1
Nickname: stud1
Course number from the list: 2
Enter student 2 details:
ID: 2222
Name: student2
Nickname: stud2
Course number from the list: 1
=====================================================
Choose an option from the menu: 4
The students details you entered are:
Student 1:
ID: 1111
Name: student1
Nickname: stud1
Course co2
Student 2:
ID: 2222
Name: student2
Nickname: stud2
Course co2
=====================================================
Choose an option from the menu: 7
End of program!
问题是您将可用 Course
和 Student
的列表存储在同一个列表中。 Course
s 和 Student
s 应该有一个单独的列表,然后根据需要让这两个列表相互交叉引用。
例如,将Student
的Course info;
成员改为std::string CourseCode;
,然后就可以在Course
s列表中查找CourseCode
需要的时候。
试试像这样的东西:
#include <iostream>
#include <string>
#include <limits>
using namespace std;
struct Course {
string CourseCode;
string CourseName;
string LecturerName;
int CourseNumber; // <-- what is this for???
};
struct Student {
string StudentID;
string StudentName;
string Nickname;
string CourseCode;
};
void menu() {
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;
}
string getCourseName(const string &CourseCode, const Course *courses, int courNo) {
for(int m = 0; m < courNo; ++m) {
if (courses[m].CourseCode == CourseCode)
return courses[m].CourseName;
}
return "";
}
int main()
{
Student students[5];
Course courses[2];
int n, m, option, stuNo = 0, courNo = 0, modNo = 0;
menu();
do
{
cout << "Choose an option from the menu: ";
cin >> option;
switch (option) {
case 1:
if (courNo >= 2) {
cout << "\nCourse list is full";
break;
}
cout << "\nHow many courses do you want to enter? [Maximum " << 2 - courNo << "]: ";
cin >> n;
for (m = 0; m < n; ++m) {
cout << "\nEnter course " << m + 1 << " details: " << endl;
cout << "Course Code: ";
cin >> courses[courNo].CourseCode;
cout << "Course Name: ";
cin >> courses[courNo].CourseName;
cout << "Lecturer Name: ";
cin >> courses[courNo].LecturerName;
++courNo;
}
break;
case 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;
}
break;
case 3:
if (stuNo >= 5) {
cout << "\nStudent list is full";
break;
}
cout << "\nHow many students do you want to enter? [Maximum " << 5 - stuNo << "]: ";
cin >> n;
for (m = 0; m < n; ++m) {
cout << "\nEnter student " << m + 1 << " details: " << endl;
cout << "ID: ";
cin >> students[stuNo].StudentID;
cout << "Name: ";
cin >> students[stuNo].StudentName;
cout << "Nickname: ";
cin >> students[stuNo].Nickname;
cout << "Course number from the list: ";
cin >> students[stuNo].CourseCode;
++stuNo;
}
break;
case 4:
cout << "\nThe students details you entered are:" << endl;
for (m = 0; m < stuNo; ++m) {
cout << "\nStudent " << m + 1 << ": " << endl;
cout << "ID: " << students[m].StudentID << endl;
cout << "Name: " << students[m].StudentName << endl;
cout << "Nickname: " << students[m].Nickname << endl;
cout << "Course: " << students[m].CourseCode << " " << getCourseName(students[m].CourseCode, courses, courNo) << endl;
}
break;
case 5:
cout << "\nEnter the student number that has been saved in option 1,2: ";
cin >> n;
if (n < 1 || n > stuNo) {
cout << "\nInvalid student number";
break;
}
cout << "\nEnter the new name: ";
cin >> students[n-1].StudentName;
modNo = n;
break;
case 6:
if (modNo < 1 || modNo > stuNo) {
cout << "\nNo student modified";
break;
}
cout << "\nStudent " << modNo << " new name: ";
cout << students[modNo-1].StudentName << endl;
modNo = 0;
break;
case 7:
break;
default:
cout << "Invalid number! Please re-enter a choice from the menu." << endl;
break;
}
cout << "=====================================================" << endl;
}
while (option != 7);
cout << "End of program!" << endl;
}
我对 C++
中的结构有疑问,我写了下面的代码,我想给每个学生分配不同的课程,我一直在尝试进行不同的分配,但无法弄清楚正确的做法。
这是我的代码:
#include <iostream>
using namespace std;
struct Course {
string CourseCode;
string CourseName;
string LecturerName;
int CourseNumber;
};
struct Student {
string StudentID;
string StudentName;
string Nickname;
Course info;
};
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()
{
Student* ptr[5];
Student details[5];
for (int i = 0; i < 2; i++) {
ptr[i] = &details[i];
}
int m, option, stuNo, courNo, n;
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;
for (m = 0; m < courNo; m++) {
cout << "\nEnter course " << m + 1 << " details: " << endl;
cout << "Course Code: ";
cin >> ptr[m]->info.CourseCode;
cout << "Course Name: ";
cin >> ptr[m]->info.CourseName;
cout << "Lecturer Name: ";
cin >> ptr[m]->info.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: " << ptr[m]->info.CourseCode << endl;
cout << "Course Name: " << ptr[m]->info.CourseName << endl;
cout << "Lecturer Name: " << ptr[m]->info.LecturerName << endl;
}
}
else if (option == 3) {
cout << "\nHow many students do you want to enter? [Maximum 5]: ";
cin >> stuNo;
for (m = 0, n=0; m < stuNo; m++, n++) {
cout << "\nEnter student " << m + 1 << " details: " << endl;
cout << "ID: ";
cin >> ptr[m]->StudentID;
cout << "Name: ";
cin >> ptr[m]->StudentName;
cout << "Nickname: ";
cin >> ptr[m]->Nickname;
cout << "Course number from the list: ";
cin >> n; // This gives same output for both students
}
}
else if (option == 4) {
cout << "\nThe students details you entered are:" << endl;
for (m = 0; m < stuNo; m++) {
cout << "\nStudent " << m + 1 << ": " << endl;
cout << "ID: " << ptr[m]->StudentID << endl;
cout << "Name: " << ptr[m]->StudentName << endl;
cout << "Nickname: " << ptr[m]->Nickname << endl;
cout << "Course " << ptr[n-1]->info.CourseName<< endl;
}
}
else if (option == 5) {
cout << "\nEnter the student number that has been saved in option 1,2: ";
cin >> n;
cout << "\nEnter the new name: ";
cin >> ptr[n-1]->StudentName;
}
else if (option == 6) {
cout << "\nStudent "<< n <<" new name: ";
cout << ptr[n - 1]->StudentName;
cout << 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;
}
if (option == 7)
cout << "End of program!" << endl;
}
我知道根据我的代码分配给学生的课程的输出是相同的,因为我只有 n
,如何更改它以使其对每个学生都不同
这是输出:
=================================
MENU
=================================
1- Add new course
2- Display course offered
3- Add new students
4- Display student list
5- Modify student name
6- Display student's name after modification
7- Exit Program
=================================
Choose an option from the menu: 1
How many courses do you want to enter? [Maximum 2]: 2
Enter course 1 details:
Course Code: 111
Course Name: co1
Lecturer Name: lect1
Enter course 2 details:
Course Code: 222
Course Name: co2
Lecturer Name: lect2
=====================================================
Choose an option from the menu: 3
How many students do you want to enter? [Maximum 5]: 2
Enter student 1 details:
ID: 1111
Name: student1
Nickname: stud1
Course number from the list: 2
Enter student 2 details:
ID: 2222
Name: student2
Nickname: stud2
Course number from the list: 1
=====================================================
Choose an option from the menu: 4
The students details you entered are:
Student 1:
ID: 1111
Name: student1
Nickname: stud1
Course co2
Student 2:
ID: 2222
Name: student2
Nickname: stud2
Course co2
=====================================================
Choose an option from the menu: 7
End of program!
问题是您将可用 Course
和 Student
的列表存储在同一个列表中。 Course
s 和 Student
s 应该有一个单独的列表,然后根据需要让这两个列表相互交叉引用。
例如,将Student
的Course info;
成员改为std::string CourseCode;
,然后就可以在Course
s列表中查找CourseCode
需要的时候。
试试像这样的东西:
#include <iostream>
#include <string>
#include <limits>
using namespace std;
struct Course {
string CourseCode;
string CourseName;
string LecturerName;
int CourseNumber; // <-- what is this for???
};
struct Student {
string StudentID;
string StudentName;
string Nickname;
string CourseCode;
};
void menu() {
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;
}
string getCourseName(const string &CourseCode, const Course *courses, int courNo) {
for(int m = 0; m < courNo; ++m) {
if (courses[m].CourseCode == CourseCode)
return courses[m].CourseName;
}
return "";
}
int main()
{
Student students[5];
Course courses[2];
int n, m, option, stuNo = 0, courNo = 0, modNo = 0;
menu();
do
{
cout << "Choose an option from the menu: ";
cin >> option;
switch (option) {
case 1:
if (courNo >= 2) {
cout << "\nCourse list is full";
break;
}
cout << "\nHow many courses do you want to enter? [Maximum " << 2 - courNo << "]: ";
cin >> n;
for (m = 0; m < n; ++m) {
cout << "\nEnter course " << m + 1 << " details: " << endl;
cout << "Course Code: ";
cin >> courses[courNo].CourseCode;
cout << "Course Name: ";
cin >> courses[courNo].CourseName;
cout << "Lecturer Name: ";
cin >> courses[courNo].LecturerName;
++courNo;
}
break;
case 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;
}
break;
case 3:
if (stuNo >= 5) {
cout << "\nStudent list is full";
break;
}
cout << "\nHow many students do you want to enter? [Maximum " << 5 - stuNo << "]: ";
cin >> n;
for (m = 0; m < n; ++m) {
cout << "\nEnter student " << m + 1 << " details: " << endl;
cout << "ID: ";
cin >> students[stuNo].StudentID;
cout << "Name: ";
cin >> students[stuNo].StudentName;
cout << "Nickname: ";
cin >> students[stuNo].Nickname;
cout << "Course number from the list: ";
cin >> students[stuNo].CourseCode;
++stuNo;
}
break;
case 4:
cout << "\nThe students details you entered are:" << endl;
for (m = 0; m < stuNo; ++m) {
cout << "\nStudent " << m + 1 << ": " << endl;
cout << "ID: " << students[m].StudentID << endl;
cout << "Name: " << students[m].StudentName << endl;
cout << "Nickname: " << students[m].Nickname << endl;
cout << "Course: " << students[m].CourseCode << " " << getCourseName(students[m].CourseCode, courses, courNo) << endl;
}
break;
case 5:
cout << "\nEnter the student number that has been saved in option 1,2: ";
cin >> n;
if (n < 1 || n > stuNo) {
cout << "\nInvalid student number";
break;
}
cout << "\nEnter the new name: ";
cin >> students[n-1].StudentName;
modNo = n;
break;
case 6:
if (modNo < 1 || modNo > stuNo) {
cout << "\nNo student modified";
break;
}
cout << "\nStudent " << modNo << " new name: ";
cout << students[modNo-1].StudentName << endl;
modNo = 0;
break;
case 7:
break;
default:
cout << "Invalid number! Please re-enter a choice from the menu." << endl;
break;
}
cout << "=====================================================" << endl;
}
while (option != 7);
cout << "End of program!" << endl;
}