使用C++对上述问题编写完整代码
时间: 2024-02-29 11:06:24 浏览: 109
好的,下面给出使用 C++ 编写的完整代码:
```cpp
#include <iostream>
#include <string>
#include <vector>
using namespace std;
// 定义本科生类
class Undergraduate {
public:
Undergraduate(string name, string clazz) : name_(name), clazz_(clazz) {}
string GetName() const {
return name_;
}
string GetClazz() const {
return clazz_;
}
private:
string name_;
string clazz_;
};
// 定义研究生类
class Graduate {
public:
Graduate(string name, string clazz) : name_(name), clazz_(clazz) {}
string GetName() const {
return name_;
}
string GetClazz() const {
return clazz_;
}
private:
string name_;
string clazz_;
};
// 定义导师类
class Teacher {
public:
Teacher(string name, string title) : name_(name), title_(title) {}
string GetName() const {
return name_;
}
string GetTitle() const {
return title_;
}
void AddStudent(Undergraduate* student) {
students_.push_back(student);
}
void AddStudent(Graduate* student) {
students_.push_back(student);
}
bool RemoveStudent(Undergraduate* student) {
for (auto it = students_.begin(); it != students_.end(); ++it) {
if (*it == student) {
students_.erase(it);
return true;
}
}
return false;
}
bool RemoveStudent(Graduate* student) {
for (auto it = students_.begin(); it != students_.end(); ++it) {
if (*it == student) {
students_.erase(it);
return true;
}
}
return false;
}
vector<Undergraduate*> GetUndergraduates() const {
vector<Undergraduate*> undergraduates;
for (auto student : students_) {
if (auto undergraduate = dynamic_cast<Undergraduate*>(student)) {
undergraduates.push_back(undergraduate);
}
}
return undergraduates;
}
vector<Graduate*> GetGraduates() const {
vector<Graduate*> graduates;
for (auto student : students_) {
if (auto graduate = dynamic_cast<Graduate*>(student)) {
graduates.push_back(graduate);
}
}
return graduates;
}
private:
string name_;
string title_;
vector<void*> students_;
};
// 定义导师广义表类
class TeacherList {
public:
// 建立导师广义表
void BuildTeacherList() {
// 添加导师
teachers_.push_back(new Teacher("张三", "副教授"));
teachers_.push_back(new Teacher("李四", "教授"));
// 添加本科生
auto zhangsan = teachers_[0];
zhangsan->AddStudent(new Undergraduate("小明", "计算机科学与技术"));
zhangsan->AddStudent(new Undergraduate("小红", "软件工程"));
zhangsan->AddStudent(new Undergraduate("小刚", "物联网工程"));
auto lisi = teachers_[1];
lisi->AddStudent(new Undergraduate("小芳", "计算机科学与技术"));
lisi->AddStudent(new Undergraduate("小李", "软件工程"));
lisi->AddStudent(new Undergraduate("小王", "物联网工程"));
// 添加研究生
auto zhangsan_graduate = new Graduate("王五", "计算机科学与技术");
zhangsan->AddStudent(zhangsan_graduate);
zhangsan_graduate->AddStudent(new Undergraduate("小张", "计算机科学与技术"));
zhangsan_graduate->AddStudent(new Undergraduate("小刘", "软件工程"));
auto lisi_graduate = new Graduate("赵六", "物联网工程");
lisi->AddStudent(lisi_graduate);
lisi_graduate->AddStudent(new Undergraduate("小周", "计算机科学与技术"));
lisi_graduate->AddStudent(new Undergraduate("小吴", "软件工程"));
}
// 插入本科生或研究生到广义表的相应位置
void InsertStudent(Teacher* teacher, void* student) {
teacher->AddStudent(student);
}
// 删除某本科生或研究生从广义表中
bool RemoveStudent(Teacher* teacher, void* student) {
return teacher->RemoveStudent(student);
}
// 查询导师、本科生、研究生的基本情况
void QueryInfo() const {
for (auto teacher : teachers_) {
cout << "导师:" << teacher->GetName() << ",职称:" << teacher->GetTitle() << endl;
auto undergraduates = teacher->GetUndergraduates();
cout << "本科生:" << endl;
for (auto undergraduate : undergraduates) {
cout << "姓名:" << undergraduate->GetName() << ",班级:" << undergraduate->GetClazz() << endl;
}
auto graduates = teacher->GetGraduates();
cout << "研究生:" << endl;
for (auto graduate : graduates) {
cout << "姓名:" << graduate->GetName() << ",班级:" << graduate->GetClazz() << endl;
auto undergraduate_students = graduate->GetUndergraduates();
cout << "本科生:" << endl;
for (auto student : undergraduate_students) {
cout << "姓名:" << student->GetName() << ",班级:" << student->GetClazz() << endl;
}
}
cout << endl;
}
}
// 统计某导师带了多少个研究生和本科生
void CountStudents(Teacher* teacher) const {
auto undergraduates = teacher->GetUndergraduates();
auto graduates = teacher->GetGraduates();
cout << "导师:" << teacher->GetName() << ",职称:" << teacher->GetTitle() << endl;
cout << "本科生数量:" << undergraduates.size() << endl;
cout << "研究生数量:" << graduates.size() << endl << endl;
}
// 输出某导师所带学生的情况
void PrintTeacherStudents(Teacher* teacher) const {
cout << "导师:" << teacher->GetName() << ",职称:" << teacher->GetTitle() << endl;
auto undergraduates = teacher->GetUndergraduates();
cout << "本科生:" << endl;
for (auto undergraduate : undergraduates) {
cout << "姓名:" << undergraduate->GetName() << ",班级:" << undergraduate->GetClazz() << endl;
}
auto graduates = teacher->GetGraduates();
cout << "研究生:" << endl;
for (auto graduate : graduates) {
cout << "姓名:" << graduate->GetName() << ",班级:" << graduate->GetClazz() << endl;
auto undergraduate_students = graduate->GetUndergraduates();
cout << "本科生:" << endl;
for (auto student : undergraduate_students) {
cout << "姓名:" << student->GetName() << ",班级:" << student->GetClazz() << endl;
}
}
cout << endl;
}
// 退出程序
void Exit() {
for (auto teacher : teachers_) {
delete teacher;
}
teachers_.clear();
}
private:
vector<Teacher*> teachers_;
};
int main() {
TeacherList teacher_list;
teacher_list.BuildTeacherList();
while (true) {
cout << "请选择操作:" << endl;
cout << "1. 插入本科生或研究生到广义表的相应位置" << endl;
cout << "2. 删除某本科生或研究生从广义表中" << endl;
cout << "3. 查询导师、本科生、研究生的基本情况" << endl;
cout << "4. 统计某导师带了多少个研究生和本科生" << endl;
cout << "5. 输出某导师所带学生的情况" << endl;
cout << "6. 退出程序" << endl;
int option;
cin >> option;
switch (option) {
case 1: {
cout << "请输入导师姓名:";
string teacher_name;
cin >> teacher_name;
cout << "请输入学生姓名:";
string student_name;
cin >> student_name;
cout << "请输入学生班级:";
string clazz;
cin >> clazz;
cout << "请选择学生类型(1:本科生,2:研究生):";
int type;
cin >> type;
if (type == 1) {
auto student = new Undergraduate(student_name, clazz);
for (auto teacher : teacher_list.GetTeachers()) {
if (teacher->GetName() == teacher_name) {
teacher_list.InsertStudent(teacher, student);
break;
}
}
} else if (type == 2) {
auto student = new Graduate(student_name, clazz);
for (auto teacher : teacher_list.GetTeachers()) {
if (teacher->GetName() == teacher_name) {
teacher_list.InsertStudent(teacher, student);
break;
}
}
} else {
cout << "输入有误,请重新选择操作!" << endl;
}
break;
}
case 2: {
cout << "请输入导师姓名:";
string teacher_name;
cin >> teacher_name;
cout << "请输入学生姓名:";
string student_name;
cin >> student_name;
cout << "请选择学生类型(1:本科生,2:研究生):";
int type;
cin >> type;
bool found = false;
if (type == 1) {
for (auto teacher : teacher_list.GetTeachers()) {
if (teacher->GetName() == teacher_name) {
for (auto undergraduate : teacher->GetUndergraduates()) {
if (undergraduate->GetName() == student_name) {
teacher_list.RemoveStudent(teacher, undergraduate);
found = true;
break;
}
}
}
if (found) {
break;
}
}
} else if (type == 2) {
for (auto teacher : teacher_list.GetTeachers()) {
if (teacher->GetName() == teacher_name) {
for (auto graduate : teacher->GetGraduates()) {
if (graduate->GetName() == student_name) {
teacher_list.RemoveStudent(teacher, graduate);
found = true;
break;
}
}
}
if (found) {
break;
}
}
} else {
cout << "输入有误,请重新选择操作!" << endl;
}
if (!found) {
cout << "未找到对应导师或学生,请检查输入信息!" << endl;
}
break;
}
case 3: {
teacher_list.QueryInfo();
break;
}
case 4: {
cout << "请输入导师姓名:";
string teacher_name;
cin >> teacher_name;
bool found = false;
for (auto teacher : teacher_list.GetTeachers()) {
if (teacher->GetName() == teacher_name) {
teacher_list.CountStudents(teacher);
found = true;
break;
}
}
if (!found) {
cout << "未找到对应导师,请检查输入信息!" << endl;
}
break;
}
case 5: {
cout << "请输入导师姓名:";
string teacher_name;
cin >> teacher_name;
bool found = false;
for (auto teacher : teacher_list.GetTeachers()) {
if (teacher->GetName() == teacher_name) {
teacher_list.PrintTeacherStudents(teacher);
found = true;
break;
}
}
if (!found) {
cout << "未找到对应导师,请检查输入信息!" << endl;
}
break;
}
case 6: {
teacher_list.Exit();
cout << "程序已退出,谢谢使用!" << endl;
return 0;
}
default: {
cout << "输入有误,请重新选择操作!" << endl;
break;
}
}
}
return 0;
}
```
这个代码中,我们定义了三个类:本科生类、研究生类和导师类。本科生和研究生都有姓名和班级属性,导师有姓名和职称属性。导师类中包含了一个存储本科生和研究生指针的动态数组,可以实现添加、删除、查询和统计学生的功能。导师广义表类中包含了一个存储导师指针的动态数组,可以实现建立导师广义表的功能。在主函数中,我们通过一个循环来不断接收用户的操作选择,并调用相应的函数来实现对导师广义表的修改和查询。
阅读全文