高校有教师、学生、课程三类实体,老师可以增加、删除课程、 学生可以选课、退选课程:教师可以增加、删除课程答疑辅导信息 学生可以查看信息。 基本功能要求: (1)实现描述教师的类Teacher,其中定义了增加课程的函数 Add(可以增加新的课程),定义了删除课程的函数Delete(可以 删除课程),查询已有课程函数Search,并显示查询结果。 2)实现描述学生的类Student,其中定义了选修课程的函数 Select(可以选修新的课程),定义了退选课程的函数Unselect (可以退选课程),查询已有选修
时间: 2024-03-25 15:35:41 浏览: 14
课程函数 Search,并显示查询结果。
下面是一个简单的 C++ 代码实现,包含了上述功能要求:
```
#include <iostream>
#include <vector>
#include <string>
using namespace std;
class Teacher {
private:
vector<string> courses; // 存储课程名称
public:
void Add(string course) {
courses.push_back(course);
cout << "课程 " << course << " 已添加!" << endl;
}
void Delete(string course) {
for (auto it = courses.begin(); it != courses.end(); it++) {
if (*it == course) {
courses.erase(it);
cout << "课程 " << course << " 已删除!" << endl;
return;
}
}
cout << "课程 " << course << " 不存在!" << endl;
}
void Search() {
if (courses.empty()) {
cout << "当前没有任何课程!" << endl;
return;
}
cout << "当前已有的课程:" << endl;
for (auto course : courses) {
cout << "- " << course << endl;
}
}
};
class Student {
private:
vector<string> selected_courses; // 存储已选课程名称
public:
void Select(string course) {
selected_courses.push_back(course);
cout << "已选修课程 " << course << "!" << endl;
}
void Unselect(string course) {
for (auto it = selected_courses.begin(); it != selected_courses.end(); it++) {
if (*it == course) {
selected_courses.erase(it);
cout << "已退选课程 " << course << "!" << endl;
return;
}
}
cout << "未选修课程 " << course << "!" << endl;
}
void Search() {
if (selected_courses.empty()) {
cout << "当前没有任何选修课程!" << endl;
return;
}
cout << "当前已选修的课程:" << endl;
for (auto course : selected_courses) {
cout << "- " << course << endl;
}
}
};
int main() {
Teacher teacher;
Student student;
// 教师增加课程
teacher.Add("数学");
teacher.Add("英语");
teacher.Add("物理");
teacher.Add("化学");
// 教师删除课程
teacher.Delete("物理");
teacher.Delete("体育");
// 教师查询已有课程
teacher.Search();
// 学生选修课程
student.Select("数学");
student.Select("语文");
student.Select("英语");
// 学生退选课程
student.Unselect("语文");
student.Unselect("物理");
// 学生查询已选修课程
student.Search();
return 0;
}
```
注意,这只是一个简单的代码示例,没有考虑到实际应用中可能会遇到的各种情况和异常处理。在实际开发中,需要根据具体需求进行更加详细的设计和实现。