用c++语言写出这道题,在高校的教学改革中,有很多学校实行了本科生导师制。一个班级 的学生被分给几个老师,每个老师带领 n 个学生,如果老师还带研究生,那么研 究生也可直接负责本科生。 本科生导师制问题中的数据元素具有如下形式: ⑴ 导师带研究生:(老师,((研究生 1,(本科生 1, …, 本科生 m)), … )) ⑵ 导师不带研究生: (老师,(本科生 1, …, 本科生 m)) 导师的自然情况只包括姓名、职称; 研究生的自然情况只包括姓名、班级; 本科生的自然情况只包括姓名、班级。 实现要求: 1)插入:将某位本科生或研究生插入到广义表的相应位置; 2)删除:将某本科生或研究生从广义表中删除; 3)查询:查询导师、本科生(研究生)的情况; 4)统计:某导师带了多少个研究生和本科生; 5)输出:将某导师所带学生情况输出
时间: 2024-03-31 16:35:40 浏览: 84
以下是一个使用 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 Teacher {
public:
string name;
string title;
vector<Student*> students;
vector<pair<string, vector<Student*>>> graduateStudents;
Teacher(string n, string t) : name(n), title(t) {}
};
class AdvisorSystem {
public:
vector<Teacher*> teachers;
void insertStudent(Student* s, Teacher* t, int idx = -1);
void insertGraduateStudent(Student* s, Teacher* t);
void deleteStudent(Student* s, Teacher* t);
void deleteGraduateStudent(Student* s, Teacher* t);
void queryTeacher(Teacher* t);
void queryStudent(Student* s);
void queryGraduateStudent(Student* s);
void countStudents(Teacher* t);
void outputTeacherStudents(Teacher* t);
};
void AdvisorSystem::insertStudent(Student* s, Teacher* t, int idx) {
if (idx == -1) {
t->students.push_back(s);
} else {
t->students.insert(t->students.begin() + idx, s);
}
}
void AdvisorSystem::insertGraduateStudent(Student* s, Teacher* t) {
for (auto& gs : t->graduateStudents) {
if (gs.first == s->className) {
gs.second.push_back(s);
return;
}
}
t->graduateStudents.push_back(make_pair(s->className, vector<Student*>{s}));
}
void AdvisorSystem::deleteStudent(Student* s, Teacher* t) {
auto it = find(t->students.begin(), t->students.end(), s);
if (it != t->students.end()) {
t->students.erase(it);
}
}
void AdvisorSystem::deleteGraduateStudent(Student* s, Teacher* t) {
for (auto& gs : t->graduateStudents) {
if (gs.first == s->className) {
auto it = find(gs.second.begin(), gs.second.end(), s);
if (it != gs.second.end()) {
gs.second.erase(it);
if (gs.second.empty()) {
t->graduateStudents.erase(find(t->graduateStudents.begin(), t->graduateStudents.end(), gs));
}
}
return;
}
}
}
void AdvisorSystem::queryTeacher(Teacher* t) {
cout << "Teacher: " << t->name << ", Title: " << t->title << endl;
}
void AdvisorSystem::queryStudent(Student* s) {
cout << "Student: " << s->name << ", Class: " << s->className << endl;
}
void AdvisorSystem::queryGraduateStudent(Student* s) {
cout << "Graduate Student: " << s->name << ", Class: " << s->className << endl;
}
void AdvisorSystem::countStudents(Teacher* t) {
int nStudents = t->students.size();
int nGraduateStudents = 0;
for (auto& gs : t->graduateStudents) {
nGraduateStudents += gs.second.size();
}
cout << "Teacher " << t->name << " has " << nStudents << " undergraduate students and " << nGraduateStudents << " graduate students." << endl;
}
void AdvisorSystem::outputTeacherStudents(Teacher* t) {
cout << "Teacher " << t->name << " has " << t->students.size() << " undergraduate students and " << t->graduateStudents.size() << " graduate student classes." << endl;
cout << "Undergraduate students: ";
for (auto s : t->students) {
cout << s->name << " ";
}
cout << endl;
cout << "Graduate students: ";
for (auto& gs : t->graduateStudents) {
cout << gs.first << ": ";
for (auto s : gs.second) {
cout << s->name << " ";
}
cout << endl;
}
}
int main() {
AdvisorSystem advisor;
// 初始化学生和老师
Student* s1 = new Student{"Alice", "Class1"};
Student* s2 = new Student{"Bob", "Class1"};
Student* s3 = new Student{"Charlie", "Class2"};
Student* s4 = new Student{"David", "Class2"};
Teacher* t1 = new Teacher{"Emily", "Professor"};
Teacher* t2 = new Teacher{"Frank", "Associate Professor"};
// 建立导师与学生的关系
advisor.insertStudent(s1, t1);
advisor.insertStudent(s2, t1);
advisor.insertStudent(s3, t2);
advisor.insertStudent(s4, t2);
// 建立研究生与学生的关系
Student* s5 = new Student{"Eva", "GraduateClass1"};
Student* s6 = new Student{"Frank", "GraduateClass2"};
advisor.insertGraduateStudent(s5, t1);
advisor.insertGraduateStudent(s6, t2);
// 查询导师和学生的关系
advisor.queryTeacher(t1);
advisor.queryStudent(s1);
advisor.queryStudent(s2);
advisor.queryGraduateStudent(s5);
// 统计导师带领的学生人数
advisor.countStudents(t1);
advisor.countStudents(t2);
// 输出导师带领的学生情况
advisor.outputTeacherStudents(t1);
advisor.outputTeacherStudents(t2);
// 删除学生和研究生
advisor.deleteStudent(s1, t1);
advisor.deleteGraduateStudent(s5, t1);
// 再次输出导师带领的学生情况
advisor.outputTeacherStudents(t1);
// 释放内存
delete s1;
delete s2;
delete s3;
delete s4;
delete s5;
delete s6;
delete t1;
delete t2;
return 0;
}
```
在这个示例代码中,我们定义了三个类 `Student`、`Teacher` 和 `AdvisorSystem`。`Student` 和 `Teacher` 类与上一个示例代码相同,用于表示学生和导师。`AdvisorSystem` 类用于提供插入、删除、查询、统计和输出等功能。在 `main()` 函数中,我们创建了四个学生对象和两个老师对象,并将它们之间的关系建立起来。然后,我们查询了每个老师对象的本科生和研究生,统计了他们带领的学生人数,并输出了带领的学生情况。接着,我们删除了一个本科生和一个研究生,并再次输出了带领的学生情况。最后,我们释放了所有对象的内存。
阅读全文