用c++写代码本科生导师制问题 问题描述:在高校的教学改革中,有很多学校实行了本科生导师制。一个班级的学生被分给几个老师,每个老师带领n个学生,如果老师还带研究生,那么研究生也可直接负责本科生。 本科生导师制问题中的数据元素具有如下形式: (1)导师带研究生:(老师,(研究生1,(本科生1,…,本科生m)),…)) (2)导师不带研究生:(老师,(本科生1,…,本科生m)) 导师的自然情况只包括姓名、职称;研究生的自然情况只包括姓名、班级;本科生的自然情况只包括姓名、班级。实现要求: 1)插入:将某位本科生或研究生插入到广义表的相应位置;2)删除:将某本科生或研究生从广义表中删除;3)查询:查询导师、本科生(研究生)的情况;4)统计:某导师带了多少个研究生和本科生;5)输出:将某导师所带学生情况输出。
时间: 2024-03-31 22:33:05 浏览: 97
好的,下面是C++代码实现本科生导师制问题的功能:
```c++
#include <iostream>
#include <vector>
#include <string>
using namespace std;
// 定义本科生结构体
struct Undergraduate {
string name;
string classNo;
};
// 定义研究生结构体
struct Graduate {
string name;
string classNo;
};
// 定义导师结构体
struct Teacher {
string name;
string title;
vector<Graduate> graduateList;
vector<Undergraduate> undergraduateList;
};
// 定义广义表结构体
struct GList {
Teacher teacher;
vector<GList> childList;
};
// 插入本科生
void insertUndergraduate(Teacher& teacher, Undergraduate undergraduate) {
teacher.undergraduateList.push_back(undergraduate);
}
// 插入研究生
void insertGraduate(Teacher& teacher, Graduate graduate, vector<Undergraduate> undergraduateList) {
teacher.graduateList.push_back(graduate);
for (int i = 0; i < undergraduateList.size(); i++) {
insertUndergraduate(teacher, undergraduateList[i]);
}
}
// 删除本科生
void deleteUndergraduate(Teacher& teacher, string name) {
for (int i = 0; i < teacher.undergraduateList.size(); i++) {
if (teacher.undergraduateList[i].name == name) {
teacher.undergraduateList.erase(teacher.undergraduateList.begin() + i);
break;
}
}
}
// 删除研究生
void deleteGraduate(Teacher& teacher, string name) {
for (int i = 0; i < teacher.graduateList.size(); i++) {
if (teacher.graduateList[i].name == name) {
teacher.graduateList.erase(teacher.graduateList.begin() + i);
break;
}
}
}
// 查询导师情况
void queryTeacher(Teacher teacher) {
cout << "导师姓名:" << teacher.name << endl;
cout << "导师职称:" << teacher.title << endl;
cout << "导师带领的研究生有:" << endl;
for (int i = 0; i < teacher.graduateList.size(); i++) {
cout << teacher.graduateList[i].name << "(" << teacher.graduateList[i].classNo << ")" << endl;
}
cout << "导师带领的本科生有:" << endl;
for (int i = 0; i < teacher.undergraduateList.size(); i++) {
cout << teacher.undergraduateList[i].name << "(" << teacher.undergraduateList[i].classNo << ")" << endl;
}
}
// 查询本科生情况
void queryUndergraduate(Teacher teacher, string name) {
for (int i = 0; i < teacher.undergraduateList.size(); i++) {
if (teacher.undergraduateList[i].name == name) {
cout << "姓名:" << teacher.undergraduateList[i].name << endl;
cout << "班级:" << teacher.undergraduateList[i].classNo << endl;
cout << "所属导师:" << teacher.name << endl;
break;
}
}
}
// 查询研究生情况
void queryGraduate(Teacher teacher, string name) {
for (int i = 0; i < teacher.graduateList.size(); i++) {
if (teacher.graduateList[i].name == name) {
cout << "姓名:" << teacher.graduateList[i].name << endl;
cout << "班级:" << teacher.graduateList[i].classNo << endl;
cout << "所属导师:" << teacher.name << endl;
break;
}
}
}
// 统计导师带领的学生人数
void countStudent(Teacher teacher) {
int countGraduate = teacher.graduateList.size();
int countUndergraduate = teacher.undergraduateList.size();
cout << "导师带领了" << countGraduate << "个研究生和" << countUndergraduate << "个本科生。" << endl;
}
// 输出导师带领的学生情况
void outputStudent(Teacher teacher) {
cout << "导师" << teacher.name << "带领的学生情况如下:" << endl;
cout << "研究生:" << endl;
for (int i = 0; i < teacher.graduateList.size(); i++) {
cout << teacher.graduateList[i].name << "(" << teacher.graduateList[i].classNo << ")" << endl;
}
cout << "本科生:" << endl;
for (int i = 0; i < teacher.undergraduateList.size(); i++) {
cout << teacher.undergraduateList[i].name << "(" << teacher.undergraduateList[i].classNo << ")" << endl;
}
}
int main() {
// 构造广义表
GList gList;
Teacher teacher1 = {"张三", "教授", {}, {}};
Teacher teacher2 = {"李四", "副教授", {}, {}};
Teacher teacher3 = {"王五", "讲师", {}, {}};
Graduate graduate1 = {"小明", "研究生1班"};
Graduate graduate2 = {"小红", "研究生2班"};
Undergraduate undergraduate1 = {"小明明", "本科生1班"};
Undergraduate undergraduate2 = {"小红红", "本科生2班"};
vector<Undergraduate> undergraduateList1 = {undergraduate1, undergraduate2};
vector<Undergraduate> undergraduateList2 = {undergraduate2, undergraduate1};
insertGraduate(teacher1, graduate1, undergraduateList1);
insertGraduate(teacher1, graduate2, undergraduateList2);
insertUndergraduate(teacher2, undergraduate1);
insertUndergraduate(teacher2, undergraduate2);
insertUndergraduate(teacher3, undergraduate1);
insertUndergraduate(teacher3, undergraduate2);
gList.teacher = teacher1;
GList child1;
child1.teacher = teacher2;
GList child2;
child2.teacher = teacher3;
gList.childList = {child1, child2};
// 测试功能
queryTeacher(teacher1);
queryUndergraduate(teacher2, "小明明");
queryGraduate(teacher1, "小明");
countStudent(teacher1);
outputStudent(teacher1);
return 0;
}
```
阅读全文