用c++语言写出这道题,一个班级 的学生被分给几个老师,每个老师带领 n 个学生,如果老师还带研究生,那么研 究生也可直接负责本科生。 本科生导师制问题中的数据元素具有如下形式且需要用树状形式表达: ⑴ 导师带研究生:(老师,((研究生 1,(本科生 1, …, 本科生 m)), … )) ⑵ 导师不带研究生: (老师,(本科生 1, …, 本科生 m)) 导师的自然情况只包括姓名、职称; 研究生的自然情况只包括姓名、班级; 本科生的自然情况只包括姓名、班级。 实现要求: 1)插入:将某位本科生或研究生插入到广义表的相应位置; 2)删除:将某本科生或研究生从广义表中删除; 3)查询:查询导师、本科生(研究生)的情况; 4)统计:某导师带了多少个研究生和本科生; 5)输出:将某导师所带学生情况输出。
时间: 2024-03-31 20:32:48 浏览: 80
好的,以下是用C++语言实现本科生导师制问题的代码:
```cpp
#include <iostream>
#include <vector>
#include <string>
using namespace std;
// 学生类
class Student {
public:
string name; // 姓名
string className; // 班级
Student(string n, string c) : name(n), className(c) {}
};
// 研究生类
class GraduateStudent : public Student {
public:
GraduateStudent(string n, string c) : Student(n, c) {}
};
// 导师类
class Teacher {
public:
string name; // 姓名
string title; // 职称
vector<Student*> students; // 学生集合
vector<GraduateStudent*> graduateStudents; // 研究生集合
Teacher(string n, string t) : name(n), title(t) {}
};
// 广义表节点类
class Node {
public:
Teacher* teacher; // 导师
vector<Node*> children; // 子节点集合
Node(Teacher* t) : teacher(t) {}
};
// 广义表类
class GeneralList {
public:
Node* root; // 根节点
GeneralList() {}
~GeneralList() {
clear(root);
}
// 插入本科生节点
bool insertStudent(string teacherName, string studentName, string className) {
Teacher* teacher = findTeacher(teacherName);
if (teacher == nullptr) {
return false;
}
Student* student = new Student(studentName, className);
teacher->students.push_back(student);
return true;
}
// 插入研究生节点
bool insertGraduateStudent(string teacherName, string studentName, string className) {
Teacher* teacher = findTeacher(teacherName);
if (teacher == nullptr) {
return false;
}
GraduateStudent* graduateStudent = new GraduateStudent(studentName, className);
teacher->graduateStudents.push_back(graduateStudent);
return true;
}
// 删除本科生节点
bool deleteStudent(string teacherName, string studentName) {
Teacher* teacher = findTeacher(teacherName);
if (teacher == nullptr) {
return false;
}
for (auto it = teacher->students.begin(); it != teacher->students.end(); it++) {
if ((*it)->name == studentName) {
delete (*it);
teacher->students.erase(it);
return true;
}
}
return false;
}
// 删除研究生节点
bool deleteGraduateStudent(string teacherName, string studentName) {
Teacher* teacher = findTeacher(teacherName);
if (teacher == nullptr) {
return false;
}
for (auto it = teacher->graduateStudents.begin(); it != teacher->graduateStudents.end(); it++) {
if ((*it)->name == studentName) {
delete (*it);
teacher->graduateStudents.erase(it);
return true;
}
}
return false;
}
// 查询导师信息
void findTeacher(string teacherName) {
Teacher* teacher = findTeacher(teacherName);
if (teacher == nullptr) {
cout << "未找到该导师" << endl;
return;
}
cout << "姓名:" << teacher->name << " 职称:" << teacher->title << endl;
cout << "本科生集合:" << endl;
for (auto& student : teacher->students) {
cout << "姓名:" << student->name << " 班级:" << student->className << endl;
}
cout << "研究生集合:" << endl;
for (auto& graduateStudent : teacher->graduateStudents) {
cout << "姓名:" << graduateStudent->name << " 班级:" << graduateStudent->className << endl;
}
}
// 统计导师带了多少个研究生和本科生
void count(string teacherName) {
Teacher* teacher = findTeacher(teacherName);
if (teacher == nullptr) {
cout << "未找到该导师" << endl;
return;
}
cout << "本科生数量:" << teacher->students.size() << endl;
cout << "研究生数量:" << teacher->graduateStudents.size() << endl;
}
// 输出导师带的学生情况
void output(string teacherName) {
Teacher* teacher = findTeacher(teacherName);
if (teacher == nullptr) {
cout << "未找到该导师" << endl;
return;
}
cout << teacher->name << ":" << endl;
for (auto& student : teacher->students) {
cout << " " << student->name << " " << student->className << endl;
}
for (auto& graduateStudent : teacher->graduateStudents) {
cout << " " << graduateStudent->name << " " << graduateStudent->className << "(研究生)" << endl;
for (auto& student : teacher->students) {
cout << " " << student->name << " " << student->className << "(本科生)" << endl;
}
}
}
private:
// 查找导师
Teacher* findTeacher(string teacherName) {
return findTeacher(root, teacherName);
}
// 在子树中查找导师
Teacher* findTeacher(Node* node, string teacherName) {
if (node == nullptr) {
return nullptr;
}
if (node->teacher->name == teacherName) {
return node->teacher;
}
for (auto& child : node->children) {
Teacher* teacher = findTeacher(child, teacherName);
if (teacher != nullptr) {
return teacher;
}
}
return nullptr;
}
// 清空子树
void clear(Node* node) {
if (node == nullptr) {
return;
}
for (auto& child : node->children) {
clear(child);
}
delete node;
}
};
int main() {
GeneralList generalList;
// 插入节点
Teacher* teacher1 = new Teacher("张三", "教授");
Teacher* teacher2 = new Teacher("李四", "副教授");
Teacher* teacher3 = new Teacher("王五", "讲师");
generalList.root = new Node(teacher1);
generalList.root->children.push_back(new Node(teacher2));
generalList.root->children.push_back(new Node(teacher3));
generalList.insertStudent("张三", "张一", "一班");
generalList.insertStudent("张三", "张二", "二班");
generalList.insertGraduateStudent("张三", "李一", "研一班");
generalList.insertGraduateStudent("张三", "李二", "研二班");
generalList.insertStudent("李四", "李一", "一班");
generalList.insertStudent("李四", "李二", "二班");
generalList.insertGraduateStudent("李四", "王一", "研一班");
generalList.insertGraduateStudent("李四", "王二", "研二班");
generalList.insertStudent("王五", "赵一", "一班");
generalList.insertStudent("王五", "赵二", "二班");
// 查询节点
generalList.findTeacher("张三");
// 统计节点
generalList.count("张三");
// 输出节点
generalList.output("张三");
// 删除节点
generalList.deleteStudent("张三", "张一");
generalList.deleteGraduateStudent("张三", "李一");
generalList.output("张三");
return 0;
}
```
以上代码实现了插入、删除、查询、统计和输出功能。其中,广义表通过树状结构实现,每个节点包含一个导师和子节点集合,子节点表示该导师的下属导师。另外,本科生和研究生分别通过Student和GraduateStudent类实现,导师通过Teacher类实现。可以根据需要自行扩展功能。
阅读全文