根据上述问题和要求,用C++编写完整代码
时间: 2024-03-11 20:47:07 浏览: 89
好的,以下是一个简单的 C++ 实现,仅供参考。
```cpp
#include <iostream>
#include <string>
#include <vector>
using namespace std;
// 本科生节点
struct Undergraduate {
string name; // 姓名
string classs; // 班级
};
// 研究生节点
struct Graduate {
string name; // 姓名
string classs; // 班级
vector<Undergraduate> undergraduates; // 本科生列表
};
// 导师节点
struct Teacher {
string name; // 姓名
string title; // 职称
vector<Graduate> graduates; // 研究生列表
vector<Undergraduate> undergraduates; // 本科生列表
};
// 导师广义表
vector<Teacher> teacherList;
// 查找导师节点
int findTeacher(string name) {
for (int i = 0; i < teacherList.size(); i++) {
if (teacherList[i].name == name) {
return i;
}
}
return -1;
}
// 插入本科生节点
bool insertUndergraduate(string teacherName, Undergraduate undergraduate) {
int index = findTeacher(teacherName);
if (index != -1) {
teacherList[index].undergraduates.push_back(undergraduate);
return true;
}
return false;
}
// 插入研究生节点
bool insertGraduate(string teacherName, string graduateName, Graduate graduate) {
int index = findTeacher(teacherName);
if (index != -1) {
for (int i = 0; i < teacherList[index].graduates.size(); i++) {
if (teacherList[index].graduates[i].name == graduateName) {
teacherList[index].graduates[i].undergraduates.push_back(graduate.undergraduates[0]);
return true;
}
}
graduate.undergraduates.clear();
teacherList[index].graduates.push_back(graduate);
return true;
}
return false;
}
// 删除本科生节点
bool deleteUndergraduate(string teacherName, string undergraduateName) {
int index = findTeacher(teacherName);
if (index != -1) {
for (int i = 0; i < teacherList[index].undergraduates.size(); i++) {
if (teacherList[index].undergraduates[i].name == undergraduateName) {
teacherList[index].undergraduates.erase(teacherList[index].undergraduates.begin() + i);
return true;
}
}
}
return false;
}
// 删除研究生节点
bool deleteGraduate(string teacherName, string graduateName) {
int index = findTeacher(teacherName);
if (index != -1) {
for (int i = 0; i < teacherList[index].graduates.size(); i++) {
if (teacherList[index].graduates[i].name == graduateName) {
teacherList[index].graduates.erase(teacherList[index].graduates.begin() + i);
return true;
}
}
}
return false;
}
// 查询导师信息
void queryTeacher(string teacherName) {
int index = findTeacher(teacherName);
if (index != -1) {
cout << "姓名:" << teacherList[index].name << endl;
cout << "职称:" << teacherList[index].title << endl;
}
}
// 查询本科生信息
void queryUndergraduate(string teacherName, string undergraduateName) {
int index = findTeacher(teacherName);
if (index != -1) {
for (int i = 0; i < teacherList[index].undergraduates.size(); i++) {
if (teacherList[index].undergraduates[i].name == undergraduateName) {
cout << "姓名:" << teacherList[index].undergraduates[i].name << endl;
cout << "班级:" << teacherList[index].undergraduates[i].classs << endl;
}
}
}
}
// 查询研究生信息
void queryGraduate(string teacherName, string graduateName) {
int index = findTeacher(teacherName);
if (index != -1) {
for (int i = 0; i < teacherList[index].graduates.size(); i++) {
if (teacherList[index].graduates[i].name == graduateName) {
cout << "姓名:" << teacherList[index].graduates[i].name << endl;
cout << "班级:" << teacherList[index].graduates[i].classs << endl;
}
}
}
}
// 统计研究生和本科生数量
void countStudent(string teacherName) {
int index = findTeacher(teacherName);
if (index != -1) {
int graduateCount = teacherList[index].graduates.size();
int undergraduateCount = teacherList[index].undergraduates.size();
cout << "研究生数量:" << graduateCount << endl;
cout << "本科生数量:" << undergraduateCount << endl;
}
}
// 打印导师的学生列表
void printStudent(string teacherName) {
int index = findTeacher(teacherName);
if (index != -1) {
cout << "研究生列表:" << endl;
for (int i = 0; i < teacherList[index].graduates.size(); i++) {
cout << " 姓名:" << teacherList[index].graduates[i].name << endl;
cout << " 班级:" << teacherList[index].graduates[i].classs << endl;
cout << " 本科生列表:" << endl;
for (int j = 0; j < teacherList[index].graduates[i].undergraduates.size(); j++) {
cout << " 姓名:" << teacherList[index].graduates[i].undergraduates[j].name << endl;
cout << " 班级:" << teacherList[index].graduates[i].undergraduates[j].classs << endl;
}
}
cout << "本科生列表:" << endl;
for (int i = 0; i < teacherList[index].undergraduates.size(); i++) {
cout << " 姓名:" << teacherList[index].undergraduates[i].name << endl;
cout << " 班级:" << teacherList[index].undergraduates[i].classs << endl;
}
}
}
// 主函数
int main() {
while (true) {
int choice;
cout << "请选择操作:" << endl;
cout << "1. 建立导师广义表" << endl;
cout << "2. 插入本科生节点" << endl;
cout << "3. 插入研究生节点" << endl;
cout << "4. 删除本科生节点" << endl;
cout << "5. 删除研究生节点" << endl;
cout << "6. 查询导师信息" << endl;
cout << "7. 查询本科生信息" << endl;
cout << "8. 查询研究生信息" << endl;
cout << "9. 统计研究生和本科生数量" << endl;
cout << "10. 打印导师的学生列表" << endl;
cout << "11. 退出程序" << endl;
cin >> choice;
if (choice == 1) {
Teacher teacher;
cout << "请输入导师姓名:";
cin >> teacher.name;
cout << "请输入导师职称:";
cin >> teacher.title;
teacherList.push_back(teacher);
}
else if (choice == 2) {
string teacherName;
Undergraduate undergraduate;
cout << "请输入导师姓名:";
cin >> teacherName;
cout << "请输入本科生姓名:";
cin >> undergraduate.name;
cout << "请输入本科生班级:";
cin >> undergraduate.classs;
if (!insertUndergraduate(teacherName, undergraduate)) {
cout << "导师不存在!" << endl;
}
}
else if (choice == 3) {
string teacherName, graduateName;
Graduate graduate;
cout << "请输入导师姓名:";
cin >> teacherName;
cout << "请输入研究生姓名:";
cin >> graduateName;
cout << "请输入研究生班级:";
cin >> graduate.classs;
cout << "请输入本科生姓名:";
cin >> graduate.undergraduates[0].name;
cout << "请输入本科生班级:";
cin >> graduate.undergraduates[0].classs;
if (!insertGraduate(teacherName, graduateName, graduate)) {
cout << "导师不存在!" << endl;
}
}
else if (choice == 4) {
string teacherName, undergraduateName;
cout << "请输入导师姓名:";
cin >> teacherName;
cout << "请输入本科生姓名:";
cin >> undergraduateName;
if (!deleteUndergraduate(teacherName, undergraduateName)) {
cout << "导师或本科生不存在!" << endl;
}
}
else if (choice == 5) {
string teacherName, graduateName;
cout << "请输入导师姓名:";
cin >> teacherName;
cout << "请输入研究生姓名:";
cin >> graduateName;
if (!deleteGraduate(teacherName, graduateName)) {
cout << "导师或研究生不存在!" << endl;
}
}
else if (choice == 6) {
string teacherName;
cout << "请输入导师姓名:";
cin >> teacherName;
queryTeacher(teacherName);
}
else if (choice == 7) {
string teacherName, undergraduateName;
cout << "请输入导师姓名:";
cin >> teacherName;
cout << "请输入本科生姓名:";
cin >> undergraduateName;
queryUndergraduate(teacherName, undergraduateName);
}
else if (choice == 8) {
string teacherName, graduateName;
cout << "请输入导师姓名:";
cin >> teacherName;
cout << "请输入研究生姓名:";
cin >> graduateName;
queryGraduate(teacherName, graduateName);
}
else if (choice == 9) {
string teacherName;
cout << "请输入导师姓名:";
cin >> teacherName;
countStudent(teacherName);
}
else if (choice == 10) {
string teacherName;
cout << "请输入导师姓名
阅读全文