C++数据结构||为我院设计一个简单的教师信息管理程序。对我院教师进行管理,包括插入、删除、查找、排序等功能。教师信息包括姓名、性别、出生年月、工作年月、学历、职务、住址、电话等信息。

0.先序条件

在文件夹中新建名称"教师信息.txt"的文本文件存储教师信息
visual studio 2017

1.背景

数据结构课程设计的作业

为我院设计一个简单的教师信息管理程序。对我院教师进行管理,包括插入、删除、查找、排序等功能。教师信息包括姓名、性别、出生年月、工作年月、学历、职务、住址、电话等信息。

基本要求:
(1)新增一名教师:将新增教师按姓名以字典方式插入教师管理文件中。
(2)删除一名教师:从教师管理文件中删除一名教师。
(3)查询:从教师管理文件中查询符合某些条件的教师。
(4)修改:检索某个教师,对其某些属性进行修改。
(5)排序:按某种需要对教师管理文件进行排序。
【实现提示】
  教师数不必很多,便于一次读入内存,所有操作不经过内外存交换。
(1)由键盘输入教师信息,以文件方式保存。程序执行时先将文件读入内存。
(2)对教师信息中的"姓名"按字典顺序进行排序。
(3)对排序后的教师进行增、删、查询、修改等操作。

2.0 Teacher.h的头文件

2.1 sTeacher 的结构体

#include
#include
#include
using namespace std;
struct sTeacher	//sTeacher结构体存储Teacher的信息
{
	string name;	//姓名
	string sex;	//性别
	string birdata;	//出生年月日
	string workdata;	//工作年月日
	string level;	//学历
	string post;	//职位
	string adr;	//地址
	string tel;	//电话
};

2.2 Teacher类和链表类定义

class Teacher
{
public:
	Teacher();
	~Teacher() {};
	void setdata();	//设置教师信息
	sTeacher getdata();	//获得教师信息
	void setnext(Teacher*m_next);	//将m_next指向next域
	void showdata();	//显示教师信息
	Teacher *getnext();	//获得下一个节点地址指针
	string getname();		//获取单个教师的某项信息,下同
	string getsex();
	string getbirdata();	
	string getworkdata();
	string getlevel();
	string getpost();
	string getadr();
	string gettel();
	void setname(string pname);	//设置单个教师的某项信息,下同
	void setsex(string psex);
	void setbirdata(string pbirdata);
	void setworkdata(string pworkdata);
	void setlevel(string plevel);
	void setpost(string ppost);
	void setadr(string padr);
	void settel(string ptel);
	int input(ifstream & is);	//文件操作
	void output(ofstream &os);
private:
	Teacher  *next;
	struct sTeacher m_Teacher;
};
class ClinkList
{
private:
	Teacher *head;
	Teacher Node;
public:
	ClinkList();
	~ClinkList();
	void CreatList();
	Teacher*GetNode(int index);	//获得第index个节点的指针
	void Insert(int index);	//在第index个位置插入一个教师信息
	void Delete(int index);	//删除第index个位置的教师信息
	int lenth();	//求链表长度(教师个数)
	void Ranking();	//对现有教师姓名进行字典从大到小排序
	void ShowList();	//输出所有教师信息
	void Seachbyname(string panme); //按对应信息查找教师信息,下同
	void Seachbylevel(string plevel);
	void Seachbypost(string ppost);
	void Seachbytel(string ptel);
	void Save(string filename);	//保存文件
	void Load(string filename);	//读取文件
};

2.3 成员函数

Teacher::Teacher()   //构造函数
{
	next = NULL;
}
ClinkList::ClinkList()	//构造函数
{
	head = &Node;
	Node.setnext(NULL);
}
ClinkList::~ClinkList()	//析构函数
{
	cout << "这是一个析构函数";
}
void Teacher::setdata()	//设置教师信息
{
	cout << "姓名:" << endl;
	cin >> m_Teacher.name;
	cout << "性别:" << endl;
	cin >> m_Teacher.sex;
	cout << "出生年月:" << endl;
	cin >> m_Teacher.birdata;
	cout << "工作年月:" << endl;
	cin >> m_Teacher.workdata;
	cout << "学历:" << endl;
	cin >> m_Teacher.level;
	cout << "职务:" << endl;
	cin >> m_Teacher.post;
	cout << "地址:" << endl;
	cin >> m_Teacher.adr;
	cout << "电话:" << endl;
	cin >> m_Teacher.tel;
}
void Teacher::setnext(Teacher*m_next)	//将m_next指向next域
{
	next = m_next;
}
void Teacher::showdata()	//显示教师信息
{
	const char*pname = m_Teacher.name.c_str();
	const char*psex = m_Teacher.sex.c_str();
	const char*plevel = m_Teacher.level.c_str();
	const char*ppost = m_Teacher.post.c_str();
	const char*padr = m_Teacher.adr.c_str();
	const char*ptel = m_Teacher.tel.c_str();
	cout << "教师姓名:" << pname << '\t' << "教师性别:" << psex<< endl;
	cout << "教师出生年月:" << m_Teacher.birdata;
	cout << "教师工作年月:" << m_Teacher.workdata;
	cout << "教师学历:" << plevel << '\t' << "教师职务:" << ppost << '\t' << "教师家庭住址:" << padr << '\t' << "教师电话:" << ptel << endl;
}
Teacher*Teacher::getnext()	//获得下一个节点地址指针
{
	return next;
}
string Teacher::gettel()	//获取单个教师的某项信息,下同
{
	return m_Teacher.tel;
}
string Teacher::getsex()
{
	return m_Teacher.sex;
}
string Teacher::getpost()
{
	return m_Teacher.post;
}
string Teacher::getlevel()
{
	return m_Teacher.level;
}
string Teacher::getworkdata()
{
	return m_Teacher.workdata;
}
string Teacher::getbirdata()
{
	return m_Teacher.birdata;
}
string Teacher::getadr()
{
	return m_Teacher.adr;
}
string Teacher::getname()
{
	return m_Teacher.name;
}
void Teacher::setname(string pname)		//设置单个教师的某项信息,下同
{
	m_Teacher.name = pname;
}
void Teacher::setadr(string padr)
{
	m_Teacher.adr = padr;
}
void Teacher::setbirdata(string pbirdata)
{
	m_Teacher.birdata = pbirdata;
}
void Teacher::setlevel(string plevel)
{
	m_Teacher.level = plevel;
}
void Teacher::setpost(string ppost)
{
	m_Teacher.post = ppost;
}
void Teacher::setsex(string psex)
{
	m_Teacher.sex = psex;
}
void Teacher::settel(string ptel)
{
	m_Teacher.tel = ptel;
}
void Teacher::setworkdata(string pworkdata)
{
	m_Teacher.workdata = pworkdata;
}
int Teacher::input(ifstream & is)	//从文件读取
{
	is >> m_Teacher.name >> m_Teacher.sex >> m_Teacher.birdata >> m_Teacher.workdata >> m_Teacher.level >> m_Teacher.post >> m_Teacher.adr >> m_Teacher.tel;
	if (m_Teacher.name == "") return 0;
	else return 1;
}
void Teacher::output(ofstream &os)	//向文件写
{
	os << m_Teacher.name <<
		" " << m_Teacher.sex <<
		" " << m_Teacher.birdata <<
		" " << m_Teacher.workdata <<
		" " << m_Teacher.level <<
		" " << m_Teacher.post <<
		" " << m_Teacher.adr <<
		" " << m_Teacher.tel << endl;
}
void ClinkList::CreatList()
{
	int temp = 0;
	Teacher *pTemp = NULL;
	Teacher *pNode = head;
	pTemp = pNode;
	while (1)
	{
		pTemp = new Teacher;
		cout << "请输入下一个结点的内容!" << endl;
		pTemp->setdata();
		cout << "如果想继续输入下一个学生的信息请输入 1,否则输入 0" << endl;
		cin >> temp;
		pNode->setnext(pTemp);
		if (temp == 0)
		{
			break;
		}
		if (temp == 1)
		{
			pNode = pTemp;
		}
		else
			cout << "输入有误!请重新输入!" << endl;
	}
}
Teacher*ClinkList::GetNode(int index)
{
	Teacher*pNode = head->getnext();
	int temp = 0;
	while (pNode != NULL)
	{
		if (index == temp)
		{
			return pNode;
		}
		pNode = pNode->getnext();
		++temp;
	}
	return pNode;
}
void ClinkList::Insert(int index)
{
	Teacher *pNode = GetNode(index - 1);
	Teacher *ptemp = new Teacher;
	ptemp->setdata();
	ptemp->setnext(pNode->getnext());
	pNode->setnext(ptemp);
}
void ClinkList::Ranking()
{
	Teacher *p ,*q;
	for(p = head->getnext();p!=NULL;p = p->getnext())
		for (q = p->getnext(); q != NULL; q = q->getnext())
			
			if (p->getname() > q->getname())
			{
				string t11 = p->getadr(); string t12 = q->getadr();
				string t21 = p->getbirdata(); string t22 = q->getbirdata();
				string t31 = p->getlevel(); string t32 = q->getlevel();
				string t41 = p->getname(); string t42 = q->getname();
				string t51 = p->getpost(); string t52 = q->getpost();
				string t61 = p->getsex(); string t62 = q->getsex();
				string t71 = p->gettel(); string t72 = q->gettel();
				string t81 = p->getworkdata(); string t82 = q->getworkdata();
				string t1 = p->getadr(); p->getadr() = q->getadr(); q->getadr() = t1;
				string t2 = p->getbirdata(); p->getbirdata() = q->getbirdata(); q->getbirdata() = t2;
				string t3 = p->getlevel(); p->getlevel() = q->getlevel(); q->getlevel() = t3;
				p->setadr(t12); q->setadr(t11);
				p->setbirdata(t22); q->setbirdata(t21);
				p->setlevel(t32); q->setlevel(t31);
				p->setname(t42); q->setname(t41);
				p->setpost(t52); q->setpost(t51);
				p->setsex(t62); q->setsex(t61);
				p->settel(t72); q->settel(t71);
				p->setworkdata(t82); q->setworkdata(t81);
			}
}
void ClinkList::Delete(int index)
{
	Teacher *p = head, *q = NULL;
	int count = 0;
	while (p != NULL && count < index)
	{
		p = p->getnext();
		count++;
	}
	q = p->getnext();
	p->setnext(q->getnext());
	delete q;
}
int ClinkList::lenth()
{
	int count = 0;
	Teacher *p = head->getnext();
	while (p != NULL)
	{
		p = p->getnext();
		count++;
	}
	return count;
}
void ClinkList::ShowList()
{          
	int i = 1;
	Teacher *pTemp = head->getnext();    
	if (pTemp == NULL)
	{
		cout << "这是个空链" << endl;
	}
	do
	{
		cout << i<<":";
		pTemp->showdata();
		pTemp = pTemp->getnext();
		i++;
	} while (pTemp != NULL);
}
void ClinkList::Seachbyname(string pname)
{
	Teacher *p = new Teacher;
	int count = 0;
	for (p = head->getnext(); p != NULL; p = p->getnext())
	{
		count++;
		if (p->getname() == pname)
			p->showdata();
	}
	if (count == 0)
	{
		cout << "未找到相关教师信息!" << endl;
	}
}
void ClinkList::Seachbytel(string ptel)
{
	Teacher *p = new Teacher;
	int count = 0;
	for (p = head->getnext(); p != NULL; p = p->getnext())
	{
		count++;
		if (p->gettel() == ptel)
			p->showdata();
	}
	if (count == 0)
	{
		cout << "未找到相关教师信息!" << endl;
	}
}
void ClinkList::Seachbylevel(string plevel)
{
	Teacher *p = new Teacher;
	int count = 0;
	for (p = head->getnext(); p != NULL; p = p->getnext())
	{
		count++;
		if (p->getlevel() == plevel)
			p->showdata();
	}
	if (count == 0)
	{
		cout << "未找到该教师信息!" << endl;
	}
}
void ClinkList::Seachbypost(string ppost)
{
	Teacher *p = new Teacher;
	int count = 0;
	for (p = head->getnext(); p != NULL; p = p->getnext())
	{
		count++;
		if (p->getpost() == ppost)
			p->showdata();
	}
	if (count == 0)
	{
		cout << "未找到相关教师信息!" << endl;
	}
}
void ClinkList::Save(string filename)
{
	ofstream os(filename.c_str(), fstream::out);
	Teacher *pTemp = head->getnext();
	if (pTemp == NULL)
	{
		return;
	}
	do
	{
		pTemp->output(os);
		pTemp = pTemp->getnext();
	} while (pTemp != NULL);
	os.close();
}
void ClinkList::Load(string filename)
{
	ifstream is(filename.c_str(), ios::in);
	if (!is)  return;
	char c;
	int temp = 0;
	int ret = 0;
	Teacher *pTemp = NULL;
	Teacher *pNode = head;
	pTemp = pNode;
	while (is.peek() != EOF)
	{
		pTemp = new Teacher;
		ret = pTemp->input(is);
		if (!ret)
		{
			delete pTemp;
			break;
		}
		pNode->setnext(pTemp);
		pNode = pTemp;
	}
	is.close();
}

3.主函数

#include"teacher.h"
int main()
{
	ClinkList*plist = new ClinkList;
	int choice = 0;
	do {
		cout << "@——@——@——@——@——@——@——@——@——@" << endl;
		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 << "||    0.退出系统。                         ||" << endl;
		cout << "@——@——@——@——@——@——@——@——@——@" << endl;
		cin >> choice;
		switch (choice)
		{
		case 1:
			plist->CreatList();
			plist->ShowList();
			break;
		case 2:
		{
			plist->Insert(plist->lenth());
			plist->ShowList();
			break;
		}
		case 3:
		{
			plist->Ranking();
			plist->ShowList();
			break;
		}
		case 4:
		{
			int choose;
			cout << "请输入您需要的查询方式" << endl;
			cout << "1.根据教师姓名查询" << endl;
			cout << "2.根据教师学历查询" << endl;
			cout << "3.根据教师职务查询" << endl;
			cout << "4.根据教师电话查询" << endl;
			cin >> choose;
			switch (choose)
			{
			case 1:
			{
				string pname;
				cout << "请输入您想查找的教师姓名" << endl;
				cin >> pname;
				plist->Seachbyname(pname);
				break;
			}
			case 2:
			{
				string plevel;
				cout << "请输入您想要查找的教师学历" << endl;
				cin >> plevel;
				plist->Seachbylevel(plevel);
				break;
			}
			case 3:
			{
				string ppost;
				cout << "请输入您需要查询的教师职位" << endl;
				cin >> ppost;
				plist->Seachbypost(ppost);
				break;
			}
			case 4:
				string ptel;
				cout << "请输入您需要查询的教师电话" << endl;
				cin >> ptel;
				plist->Seachbytel(ptel);
				break;
			}
			break;
		}
		case 5:
		{
			plist->ShowList();
			int p;
			cout << "请输入您需要删除的教师编号:" << endl;
			cin >> p;
			plist->Delete(p - 1);
			plist->ShowList();
			break;
		}
		case 6:
		{
			plist->ShowList();
			int p;
			cout << "请输入您需要修改的教师编号:" << endl;
			cin >> p;
			plist->Delete(p - 1);
			plist->Insert(plist->lenth());
			plist->ShowList();
			break;
		}
		case 7:
		{
			plist->Load("教师信息.txt");
			break;
		}
		case 8:
		{
			if (plist->lenth() > 0)
			{
				plist->Save("教师信息.txt");
			}
			break;
		}
		case 0:
			exit(0);
		}
	} while (choice != 0);
	system("pause");
	return 0;
}

你可能感兴趣的:(C++数据结构||为我院设计一个简单的教师信息管理程序。对我院教师进行管理,包括插入、删除、查找、排序等功能。教师信息包括姓名、性别、出生年月、工作年月、学历、职务、住址、电话等信息。)