C++使用模板类实现简单的人事管理系统

写在前面

这几天我一直在做我们《C++与数据结构》课程的大作业——c++人事管理系统。在写的过程中遇到了很多困难,也让我深刻地体会到“纸上得来终觉浅,绝知此事要躬行”。特别是编程,虽然我们看数据结构书感觉其中的很多数据结构很简单,但是实际写起来就会发现很多意想不到的问题,唯有不断地练习不断地思考才能取得长久的进步。

项目要求

编写一个简单的人事管理系统,要求功能如下
用链表存储、文件输入、输出。
具有建立、插入、删除、查询和打印功能。
平台不限、链表可用公共模板类。
管理的人员有:
本科生:姓名、性别、年龄、身份证号码、高考总分
脱产研究生:姓名、性别、年龄、身份证号码、专业
在职研究生:姓名、性别、年龄、身份证号码、学号、专业、工资
职工:姓名、性别、年龄、身份证号码、工资、岗位
教师:姓名、性别、年龄、身份证号码、工资、专业

项目思路

我最开始想的是:

  • 建立一个基类Person类,其中包含最基本的姓名、性别、年龄、身份证号码;
  • 从Person类中派生出本科生Student类,脱产研究生ReleasedUndergraduate类,在职研究生ServingUndergraduate类,职工Staff类以及教师Teacher类,其中每个类在继承了基类的成员基础上,各自添加自己特有的成员;
  • 在每一个类中定义getInfo函数,用来从获取从控制台输入的各种信息;
  • 为了更加熟悉链表这一数据类型,我没有使用STL中的list容器,而是自己写了一个LinkList链表类,包含了Node结点和链表LinkList,其中Node结点包含了data数据域和* next指针;Linklist包含了* head头结点和链表长度size
  • 为了能够一次性处理五种不同的人群(本科生、脱产、在职、职工、老师),所以我想把LinkList写成一个模板类,使用template< typename T>
  • 项目要求的建立、查找、删除、打印等功能在链表中实现

后来我发现一个很严峻的问题:不同于网上大部分涉及到链表结构的源码,我如果要用这个LinkList的模板的话,我传进去的参数本身是一个数据结构体,而不是单一的数据类型变量(如int,string);而且这些不同的数据结构体其本身包含的数据成员也是不同的。 换句话说,编译器是不知道我传进去的结构体具体有哪些成员。这样使得我不知道有些功能怎么通过类的成员函数来实现。比如我想写一个类的成员函数实现通过姓名查找的功能,我起初的写法是

void LinkList<T>::findByName(const string& name) {
	Node<T>* pcur = head->next;
	while (pcur != NULL) {
		if (name == pcur->data.name)
			……
	}	
}

很明显这样是非法的。
后来我采用的方法是在LinkList类外写一个查找函数,然后将链表和要查找的姓名字符串作为参数传入,再进行比较,比如

void findStudentName(LinkList<Student>& listForStudent, Student stu)
{
	Node<Student>* pcur = listForStudent.head->next;
	bool flag = false;	//查找成功标志
	while (pcur != NULL) {
		if (stu.name == pcur->data.name) {
			cout << "\t\t*-----------------------------------------------------*" << endl;
			cout << "查找到姓名为 " << stu.name << " 的相关信息: " << endl;
			cout << pcur->data;
			flag = true;
			break;
		}
		pcur = pcur->next;	//下一数据
	}
	if (flag == false) {
		cout << "没有找到相关人员信息!" << endl;
	}
}

但这样写的弊端,我自认为有以下几点:

  • 为了五种不同的数据类型,要写五个类似的函数;不够泛型化
  • 没有在数据结构类内部完成,不够简洁;在以后的调用中不够方便

通过这样在类外写函数的方法,我实现了查找删除两大功能。
其余功能的实现要点:

  • 数组长度:类成员函数,直接通过size成员可以得到
  • 末尾添加数据append:类成员函数,通过操作指针得到;
  • 在指定位置添加数据:类成员函数,先遍历至指定位置,再操作指针,改变相关结点的next指针得到;
  • 删除remove:先遍历判断,再设置相关操作指针并改变相关结点的next,注意要delete释放内存
  • 打印print: 类成员函数,data虽然有很多项,不能通过成员运算符进行逐个输出(和上述情况是一个原因),但是可以通过重载输出运算符<<直接print整个data数据域

除此之外,还要做一个菜单。一级菜单选定要处理的人员类型;二级菜单是针对链表的操作

感想心得

这是我到目前为止写的行数最长的一个demo,虽然难度比较小,网上相关的代码也比较多,但是“麻雀虽小,五脏俱全”。使我对c++的模板编程、链表数据结构有了更深的了解,实现功能的喜悦与成就感也愈发强烈。

项目源码

/*用所学过的知识编写一个简单的人事管理系统,要求功能如下
用链表存储、文件输入、输出。
具有建立、插入、删除、查询和打印功能。
平台不限、链表可用公共模板类。
管理的人员有:
本科生:姓名、性别、年龄、身份证号码、高考总分
脱产研究生:姓名、性别、年龄、身份证号码、专业
在职研究生:姓名、性别、年龄、身份证号码、学号、专业、工资
职工:姓名、性别、年龄、身份证号码、工资、岗位
教师:姓名、性别、年龄、身份证号码、工资、专业
*/
#include <iostream>
#include <string>
#include <fstream>

using namespace std;
int first, second;	//一、二级菜单选项

//Person基类
class Person {
public:
	string name;
	char sex;
	int age;
	string idNum;
public:
	Person() = default;
	Person(const string& nam, char s, int ag, const string& id) : name(nam), sex(s), age(ag), idNum(id) {}

	void getInfo();	//获取相关信息
};

//Person类获取相关信息getInfo
void Person::getInfo()
{
	cout << "请输入姓名:";
	cin >> name;
	cout << "性别(m/f):";
	cin >> sex;
	cout << "年龄: ";
	cin >> age;
	cout << "身份证号码: ";
	cin >> idNum;
}

//本科生类
class Student : public Person{
private:
	int score;
public:
	Student() = default;
	Student(const string& nam, char s, int ag, const string& id, int sc):Person(nam, s, ag, id), score(sc) {}

	void getInfo();	//获取相关信息
	friend ostream & operator<<(ostream &, Student &);	//重载<<
};

//Student类重载<<
ostream & operator<<(ostream &os, Student &stu)
{
	os << "姓名: " << stu.name << endl;
	os << "性别: " << stu.sex << endl;
	os << "年龄: " << stu.age << endl;
	os << "身份证号码: " << stu.idNum << endl;
	os << "高考总分: " << stu.score << endl;
	return os;
}

//Student类获取相关信息getInfo
void Student::getInfo()
{
	Person::getInfo();
	cout << "高考总分: ";
	cin >> score;
}

//脱产研究生类
class ReleasedUndergraduate : public Person{
private:
	string major;
public:
	ReleasedUndergraduate() = default;
	ReleasedUndergraduate(const string& nam, char s, int ag, const string& id, const string& maj) : Person(nam, s, ag, id), major(maj) {}

	void getInfo();	//获取相关信息
	friend ostream & operator<<(ostream &, ReleasedUndergraduate &);	//重载<<
};

//ReleasedUndergraduate类重载<<
ostream & operator<<(ostream &os, ReleasedUndergraduate &ru)
{
	os << "姓名: " << ru.name << endl;
	os << "性别: " << ru.sex << endl;
	os << "年龄: " << ru.age << endl;
	os << "身份证号码: " << ru.idNum << endl;
	os << "专业: " << ru.major << endl;
	return os;
}

//ReleasedUndergraduate类获取相关信息getInfo
void ReleasedUndergraduate::getInfo()
{
	Person::getInfo();
	cout << "专业: ";
	cin >> major;
}

//在职研究生类
class ServingUndergraduate : public Person {
private:
	string major;
	int studentNum;
	int wage;
public:
	ServingUndergraduate() = default;
	ServingUndergraduate(const string& nam, char s, int ag, const string& id, const string& maj, int stuNum, int wag) : Person(nam, s, ag, id), major(maj), studentNum(stuNum), wage(wag) {}

	void getInfo();
	friend ostream & operator<<(ostream &, ServingUndergraduate &);	//重载<<
};

//ServingUndergraduate类重载<<
ostream & operator<<(ostream &os, ServingUndergraduate &su)
{
	os << "姓名: " << su.name << endl;
	os << "性别: " << su.sex << endl;
	os << "年龄: " << su.age << endl;
	os << "身份证号码: " << su.idNum << endl;
	os << "学号: " << su.sex << endl;
	os << "专业: " << su.major << endl;
	os << "工资: " << su.wage << endl;
	return os;
}

//ServingUndergraduate类获取相关信息
void ServingUndergraduate::getInfo()
{
	Person::getInfo();
	cout << "学号: ";
	cin >> studentNum;
	cout << "专业: ";
	cin >> major;
	cout << "工资: ";
	cin >> wage;
}

//职工类
class Staff : public Person{
private:
	int wage;
	string job;
public:
	Staff() = default;
	Staff(const string& nam, char s, int ag, const string& id, int wag, const string& j) : Person(nam, s, ag, id), wage(wag), job(j) {}

	void getInfo();
	friend ostream & operator<<(ostream &, Staff &);	//重载<<
};

//Staff类重载<<
ostream & operator<<(ostream &os, Staff &st)
{
	os << "姓名: " << st.name << endl;
	os << "性别: " << st.sex << endl;
	os << "年龄: " << st.age << endl;
	os << "身份证号码: " << st.idNum << endl;
	os << "工资: " << st.wage << endl;
	os << "岗位: " << st.job << endl;
	return os;
}

//Staff类获取相关信息getInfo
void Staff::getInfo()
{
	Person::getInfo();
	cout << "工资: ";
	cin >> wage;
	cout << "岗位: ";
	cin >> job;
}

//教师类
class Teacher : public Person {
private:
	int wage;
	string major;
public:
	Teacher() = default;
	Teacher(const string& nam, char s, int ag, const string& id, const string& maj, int wag) : Person(nam, s, ag, id), major(maj), wage(wag) {}

	void getInfo();
	friend ostream & operator<<(ostream &, Teacher &);	//重载<<
};

//Teacher类重载<<
ostream & operator<<(ostream &os, Teacher &tea)
{
	os << "姓名: " << tea.name << endl;
	os << "性别: " << tea.sex << endl;
	os << "年龄: " << tea.age << endl;
	os << "身份证号码: " << tea.idNum << endl;
	os << "工资: " << tea.wage << endl;
	os << "专业: " << tea.major << endl;
	return os;
}

//Teacher类获取相关信息getInfo
void Teacher::getInfo()
{
	Person::getInfo();
	cout << "工资: ";
	cin >> wage;
	cout << "专业: ";
	cin >> major;
}

//Node结构模板类
template <typename T>
struct Node {
	T data;	//数据域
	struct Node* next;	//指针域
};

//链表模板类
template <typename T>
class LinkList : public Node<T>
{
public:
	Node<T>* head;	//头结点
	int size = 0;		//链表长度

public:
	LinkList();	//构造函数
	~LinkList();//析构函数
	void insert(const T& value, const int& pos);	//指定位置插入
	void append(const T& value);	//末尾插入
	//bool removeByValue(const T& value);		//指定值删除
	int length() const;						//链表长度
	//T findByName(const string& name);		//按姓名查找
	void clear();	//清空链表
	void print();	//打印链表
};

//构造函数LinkList
template <typename T>
LinkList<T>::LinkList()
{
	head = new Node<T>;
	head->next = NULL;
}

//指定位置插入insert
template <typename T>
void LinkList<T>::insert(const T& value, const int& pos)
{
	if (pos < this->size) {	//插入序号在表内
		auto *newnode = new Node<T>;	//生成新结点
		newnode->data = value;			//赋值
		newnode->next = NULL;			//next指针

		Node<T> *pcur = head;			//定义操作指针
		for (int i = 0; i < pos; i++) {	//遍历找到第pos个结点
			pcur = pcur->next;
		}

		newnode->next = pcur->next;
		pcur->next = newnode;
		this->size++;	//链表长度加1
	}
	else {
		cout << "插入位置错误!" << endl;
	}
}

//链表开头插入append
template <typename T>
void LinkList<T>::append(const T& value)
{
	auto *newnode = new Node<T>;	//生成新结点
	newnode->data = value;
	newnode->next = head->next;
	head->next = newnode;
	this->size++;
}

//链表长度length
template <typename T>
int LinkList<T>::length() const
{
	return this->size;
}

//清空链表clear
template <typename T>
void LinkList<T>::clear()
{
	Node<T>* pcur = head->next;	//定义一个操作指针
	while (pcur != NULL) {	//遍历整个链表
		Node<T>* pnext = pcur->next;
		delete[]pcur;		//释放当前结点
		pcur = pnext;
	}
	head->next = NULL;		//处理头结点的next指针指向NULL
	size = 0;				//将链表长度清0
}

//打印链表print
template <typename T>
void LinkList<T>::print()
{
	Node<T>* pcur = head->next;	//定义一个操作指针
	cout << "\t\t*-----------------------------------------------------*" << endl;
	cout << "\t\t\t\t      当前链表\t           " << endl;
	while (pcur != NULL) {		//遍历整个链表
		cout << pcur->data << endl;
		pcur = pcur->next;
	}
}

//析构函数~LinkList
template <typename T>
LinkList<T>::~LinkList()
{
	clear();		//清空链表
	delete head;	//释放头结点
}

//按姓名查找(五种类型)
void findStudentName(LinkList<Student>& listForStudent, Student stu)
{
	Node<Student>* pcur = listForStudent.head->next;
	bool flag = false;	//查找成功标志
	while (pcur != NULL) {
		if (stu.name == pcur->data.name) {
			cout << "\t\t*-----------------------------------------------------*" << endl;
			cout << "查找到姓名为 " << stu.name << " 的相关信息: " << endl;
			cout << pcur->data;
			flag = true;
			break;
		}
		pcur = pcur->next;	//下一数据
	}
	if (flag == false) {
		cout << "没有找到相关人员信息!" << endl;
	}
}

void findRUName(LinkList<ReleasedUndergraduate>& listForRU, ReleasedUndergraduate ru)
{
	Node<ReleasedUndergraduate>* pcur = listForRU.head->next;
	bool flag = false;	//查找成功标志
	while (pcur != NULL) {
		if (ru.name == pcur->data.name) {
			cout << "\t\t*-----------------------------------------------------*" << endl;
			cout << "查找到姓名为 " << ru.name << " 的相关信息: " << endl;
			cout << pcur->data;
			flag = true;
			break;
		}
		pcur = pcur->next;	//下一数据
	}
	if (flag == false) {
		cout << "没有找到相关人员信息!" << endl;
	}
}

void findSUName(LinkList<ServingUndergraduate>& listForSU, ServingUndergraduate su)
{
	Node<ServingUndergraduate>* pcur = listForSU.head->next;
	bool flag = false;	//查找成功标志
	while (pcur != NULL) {
		if (su.name == pcur->data.name) {
			cout << "\t\t*-----------------------------------------------------*" << endl;
			cout << "查找到姓名为 " << su.name << " 的相关信息: " << endl;
			cout << pcur->data;
			flag = true;
			break;
		}
		pcur = pcur->next;	//下一数据
	}
	if (flag == false) {
		cout << "没有找到相关人员信息!" << endl;
	}
}

void findStaffName(LinkList<Staff>& listForStaff, Staff st)
{
	Node<Staff>* pcur = listForStaff.head->next;
	bool flag = false;	//查找成功标志
	while (pcur != NULL) {
		if (st.name == pcur->data.name) {
			cout << "\t\t*-----------------------------------------------------*" << endl;
			cout << "查找到姓名为 " << st.name << " 的相关信息: " << endl;
			cout << pcur->data;
			flag = true;
			break;
		}
		pcur = pcur->next;	//下一数据
	}
	if (flag == false) {
		cout << "没有找到相关人员信息!" << endl;
	}
}

void findTeacherName(LinkList<Teacher>& listForTeacher, Teacher tea)
{
	Node<Teacher>* pcur = listForTeacher.head->next;
	bool flag = false;	//查找成功标志
	while (pcur != NULL) {
		if (tea.name == pcur->data.name) {
			cout << "\t\t*-----------------------------------------------------*" << endl;
			cout << "查找到姓名为 " << tea.name << " 的相关信息: " << endl;
			cout << pcur->data;
			flag = true;
			break;
		}
		pcur = pcur->next;	//下一数据
	}
	if (flag == false) {
		cout << "没有找到相关人员信息!" << endl;
	}
}

//按姓名删除(五种类型)
void removeByStudentName(LinkList<Student>& listForStudent, Student stu)
{
	Node<Student>* pcur = listForStudent.head;
	bool flag = false;	//查找成功标志
	while (pcur != NULL) {
		if (stu.name == pcur->next->data.name) {
			cout << "\t\t*-----------------------------------------------------*" << endl;
			cout << "删除姓名为 " << stu.name << " 的相关信息: " << endl;
			Node<Student>* pdel = pcur->next;
			cout << pdel->data;
			pcur->next = pcur->next->next;
			delete pdel;
			flag = true;
			listForStudent.size--;
			cout << "删除成功!" << endl;
			break;
		}
		pcur = pcur->next;	//下一数据
	}
	if (flag == false) {
		cout << "没有找到相关人员信息!" << endl;
	}
}

void removeByRUName(LinkList<ReleasedUndergraduate>& listForRU, ReleasedUndergraduate ru)
{
	Node<ReleasedUndergraduate>* pcur = listForRU.head;
	bool flag = false;	//查找成功标志
	while (pcur != NULL) {
		if (ru.name == pcur->next->data.name) {
			cout << "\t\t*-----------------------------------------------------*" << endl;
			cout << "删除姓名为 " << ru.name << " 的相关信息: " << endl;
			Node<ReleasedUndergraduate>* pdel = pcur->next;
			cout << pdel->data;
			pcur->next = pcur->next->next;
			delete pdel;
			flag = true;
			listForRU.size--;
			cout << "删除成功!" << endl;
			break;
		}
		pcur = pcur->next;	//下一数据
	}
	if (flag == false) {
		cout << "没有找到相关人员信息!" << endl;
	}
}

void removeBySUName(LinkList<ServingUndergraduate>& listForSU, ServingUndergraduate su)
{
	Node<ServingUndergraduate>* pcur = listForSU.head;
	bool flag = false;	//查找成功标志
	while (pcur != NULL) {
		if (su.name == pcur->next->data.name) {
			cout << "\t\t*-----------------------------------------------------*" << endl;
			cout << "删除姓名为 " << su.name << " 的相关信息: " << endl;
			Node<ServingUndergraduate>* pdel = pcur->next;
			cout << pdel->data;
			pcur->next = pcur->next->next;
			delete pdel;
			flag = true;
			listForSU.size--;
			cout << "删除成功!" << endl;
			break;
		}
		pcur = pcur->next;	//下一数据
	}
	if (flag == false) {
		cout << "没有找到相关人员信息!" << endl;
	}
}

void removeByStaffName(LinkList<Staff>& listForStaff, Staff sta)
{
	Node<Staff>* pcur = listForStaff.head;
	bool flag = false;	//查找成功标志
	while (pcur != NULL) {
		if (sta.name == pcur->next->data.name) {
			cout << "\t\t*-----------------------------------------------------*" << endl;
			cout << "删除姓名为 " << sta.name << " 的相关信息: " << endl;
			Node<Staff>* pdel = pcur->next;
			cout << pdel->data;
			pcur->next = pcur->next->next;
			delete pdel;
			flag = true;
			listForStaff.size--;
			cout << "删除成功!" << endl;
			break;
		}
		pcur = pcur->next;	//下一数据
	}
	if (flag == false) {
		cout << "没有找到相关人员信息!" << endl;
	}
}

void removeByTeacherName(LinkList<Teacher>& listForTeacher, Teacher tea)
{
	Node<Teacher>* pcur = listForTeacher.head;
	bool flag = false;	//查找成功标志
	while (pcur != NULL) {
		if (tea.name == pcur->next->data.name) {
			cout << "\t\t*-----------------------------------------------------*" << endl;
			cout << "删除姓名为 " << tea.name << " 的相关信息: " << endl;
			Node<Teacher>* pdel = pcur->next;
			cout << pdel->data;
			pcur->next = pcur->next->next;
			delete pdel;
			flag = true;
			listForTeacher.size--;
			cout << "删除成功!" << endl;
			break;
		}
		pcur = pcur->next;	//下一数据
	}
	if (flag == false) {
		cout << "没有找到相关人员信息!" << endl;
	}
}

//一级菜单menu1
void menu1()
{
	cout << "\n\n\t\t  ***************************************************" << endl;
	cout << "\t\t\t\t      人事管理系统\t           " << endl;
	cout << "\t\t*-----------------------------------------------------*" << endl;
	cout << "\t\t*             1:本科生                                *" << endl;
	cout << "\t\t*             2:脱产研究生                            *" << endl;
	cout << "\t\t*             3:在职研究生                            *" << endl;
	cout << "\t\t*             4:职工                                  *" << endl;
	cout << "\t\t*             5:教师                                  *" << endl;
	cout << "\t\t*             0:退出                                  *" << endl;
	cout << "\t\t  ***************************************************" << endl;
	cout << "请输入操作:";
}

//二级菜单
void menu2()
{
	cout << "1: 链表长度\t2:插入数据(末尾)\t3:插入数据(指定位置)" << endl;
	cout << "4: 删除数据\t5:查找数据\t6:打印链表" << endl;
	cout << "7:写入文件\t0: 返回上一级" << endl;
	cout << "请输入操作:";
}

//本科生管理界面
void studentManagementSystsm()
{
	cout << "进入本科生管理界面,链表已建立!" << endl;
	LinkList<Student> listForStudent;
	while (1) {
		menu2();
		Student stu;
		cin >> second;
		if (second == 1) {	//链表长度
			cout << "当前长度为:" << listForStudent.length() << endl;
		}
		else if (second == 2) {	//末尾插入
			stu.getInfo();
			listForStudent.append(stu);
		}
		else if (second == 3) {	//指定位置插入
			stu.getInfo();
			int pos;
			cout << "请输入插入位置:";
			cin >> pos;
			listForStudent.insert(stu, pos);
		}
		else if (second == 4) {	//删除数据
			cout << "请输入要删除数据的姓名:";
			cin >> stu.name;
			removeByStudentName(listForStudent, stu);
		}
		else if (second == 5) {	//查找数据
			cout << "请输入要查找的姓名:";
			cin >> stu.name;
			findStudentName(listForStudent, stu);
		}
		else if (second == 6) {	//打印链表
			listForStudent.print();
		}
		else if (second == 7) {	//写入文件
			ofstream fout("student.txt");	
			Node<Student>* pcur = listForStudent.head->next;
			while(pcur != NULL){
				fout << pcur->data << endl;
				pcur = pcur->next;
			}
			fout.close();
			cout << "写入成功!" << endl;
		}
		else if (second == 0) {	//返回上一级
			break;
		}
		else {
			cout << "输入数据错误!" << endl;
			continue;
		}
	}
}

//脱产研究生管理界面
void ruManagementSystem()
{
	cout << "进入脱产研究生管理界面,链表已建立!" << endl;
	LinkList<ReleasedUndergraduate> listForRU;
	while (1) {
		menu2();
		ReleasedUndergraduate ru;
		cin >> second;
		if (second == 1) {	//链表长度
			cout << "当前长度为:" << listForRU.length() << endl;
		}
		else if (second == 2) {	//末尾插入
			ru.getInfo();
			listForRU.append(ru);
		}
		else if (second == 3) {	//指定位置插入
			ru.getInfo();
			int pos;
			cout << "请输入插入位置:";
			cin >> pos;
			listForRU.insert(ru, pos);
		}
		else if (second == 4) {	//删除数据
			cout << "请输入要删除数据的姓名:";
			cin >> ru.name;
			removeByRUName(listForRU, ru);
		}
		else if (second == 5) {	//查找数据
			cout << "请输入要查找的姓名:";
			cin >> ru.name;
			findRUName(listForRU, ru);
		}
		else if (second == 6) {	//打印链表
			listForRU.print();
		}
		else if (second == 7) {	//写入文件
			ofstream fout("releasedUndergraduate.txt");
			Node<ReleasedUndergraduate>* pcur = listForRU.head->next;
			while (pcur != NULL) {
				fout << pcur->data << endl;
				pcur = pcur->next;
			}
			fout.close();
			cout << "写入成功!" << endl;
		}
		else if (second == 0) {	//返回上一级
			break;
		}
		else {
			cout << "输入数据错误!" << endl;
			continue;
		}
	}
}

//在职研究生管理界面
void suManagementSystem()
{
	cout << "进入脱产研究生管理界面,链表已建立!" << endl;
	LinkList<ServingUndergraduate> listForSU;
	while (1) {
		menu2();
		ServingUndergraduate su;
		cin >> second;
		if (second == 1) {	//链表长度
			cout << "当前长度为:" << listForSU.length() << endl;
		}
		else if (second == 2) {	//末尾插入
			su.getInfo();
			listForSU.append(su);
		}
		else if (second == 3) {	//指定位置插入
			su.getInfo();
			int pos;
			cout << "请输入插入位置:";
			cin >> pos;
			listForSU.insert(su, pos);
		}
		else if (second == 4) {	//删除数据
			cout << "请输入要删除数据的姓名:";
			cin >> su.name;
			removeBySUName(listForSU, su);
		}
		else if (second == 5) {	//查找数据
			cout << "请输入要查找的姓名:";
			cin >> su.name;
			findSUName(listForSU, su);
		}
		else if (second == 6) {	//打印链表
			listForSU.print();
		}
		else if (second == 7) {	//写入文件
			ofstream fout("servingUndergraduate.txt");
			Node<ServingUndergraduate>* pcur = listForSU.head->next;
			while (pcur != NULL) {
				fout << pcur->data << endl;
				pcur = pcur->next;
			}
			fout.close();
			cout << "写入成功!" << endl;
		}
		else if (second == 0) {	//返回上一级
			break;
		}
		else {
			cout << "输入数据错误!" << endl;
			continue;
		}
	}
}

//职工管理界面
void staffManagementSystem()
{
	cout << "进入职工管理界面,链表已建立!" << endl;
	LinkList<Staff> listForStaff;
	while (1) {
		menu2();
		Staff sta;
		cin >> second;
		if (second == 1) {	//链表长度
			cout << "当前长度为:" << listForStaff.length() << endl;
		}
		else if (second == 2) {	//末尾插入
			sta.getInfo();
			listForStaff.append(sta);
		}
		else if (second == 3) {	//指定位置插入
			sta.getInfo();
			int pos;
			cout << "请输入插入位置:";
			cin >> pos;
			listForStaff.insert(sta, pos);
		}
		else if (second == 4) {	//删除数据
			cout << "请输入要删除数据的姓名:";
			cin >> sta.name;
			removeByStaffName(listForStaff, sta);
		}
		else if (second == 5) {	//查找数据
			cout << "请输入要查找的姓名:";
			cin >> sta.name;
			findStaffName(listForStaff, sta);
		}
		else if (second == 6) {	//打印链表
			listForStaff.print();
		}
		else if (second == 7) {	//写入文件
			ofstream fout("staff.txt");
			Node<Staff>* pcur = listForStaff.head->next;
			while (pcur != NULL) {
				fout << pcur->data << endl;
				pcur = pcur->next;
			}
			fout.close();
			cout << "写入成功!" << endl;
		}
		else if (second == 0) {	//返回上一级
			break;
		}
		else {
			cout << "输入数据错误!" << endl;
			continue;
		}
	}
}

//教师管理界面
void teacherManagementSystem()
{
	cout << "进入教师管理界面,链表已建立!" << endl;
	LinkList<Teacher> listForTeacher;
	while (1) {
		menu2();
		Teacher tea;
		cin >> second;
		if (second == 1) {	//链表长度
			cout << "当前长度为:" << listForTeacher.length() << endl;
		}
		else if (second == 2) {	//末尾插入
			tea.getInfo();
			listForTeacher.append(tea);
		}
		else if (second == 3) {	//指定位置插入
			tea.getInfo();
			int pos;
			cout << "请输入插入位置:";
			cin >> pos;
			listForTeacher.insert(tea, pos);
		}
		else if (second == 4) {	//删除数据
			cout << "请输入要删除数据的姓名:";
			cin >> tea.name;
			removeByTeacherName(listForTeacher, tea);
		}
		else if (second == 5) {	//查找数据
			cout << "请输入要查找的姓名:";
			cin >> tea.name;
			findTeacherName(listForTeacher, tea);
		}
		else if (second == 6) {	//打印链表
			listForTeacher.print();
		}
		else if (second == 7) {	//写入文件
			ofstream fout("teacher.txt");
			Node<Teacher>* pcur = listForTeacher.head->next;
			while (pcur != NULL) {
				fout << pcur->data << endl;
				pcur = pcur->next;
			}
			fout.close();
			cout << "写入成功!" << endl;
		}
		else if (second == 0) {	//返回上一级
			break;
		}
		else {
			cout << "输入数据错误!" << endl;
			continue;
		}
	}
}

//主函数
int main()
{
	while (1) {
		menu1();
		cin >> first;
		if (first == 0) {
			cout << "系统已退出!" << endl;
			break;
		}
		else if (first == 1) {	//本科生操作
			studentManagementSystsm();
		}
		else if (first == 2) {	//脱产研究生操作
			ruManagementSystem();
		}
		else if (first == 3) {	//在职研究生操作
			suManagementSystem();
		}
		else if (first == 4) {	//职工操作
			staffManagementSystem();
		}
		else if (first == 5) {	//教师操作
			teacherManagementSystem();
		}
		else {
			cout << "输入错误!" << endl;
		}
	}

	return 0;
}

结果展示

输入

1
2
fff
f
33
333
444
2
ttt
m
44
555
666
5
fff
4
fff
6
2
yyy
f
22
333
444
6
7

输出

在这里插入图片描述在这里插入图片描述在这里插入图片描述

发布了59 篇原创文章 · 获赞 3 · 访问量 1793

猜你喜欢

转载自blog.csdn.net/Felix_hyfy/article/details/103863275