⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 message.cpp

📁 教职员工及非教职员工数据库管理系统
💻 CPP
字号:
// Message.cpp: implementation of the CMessage class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "Message.h"

#define TEACHER 1
#define NTEACHER 2

//学院信息
string strCollInfo[] = {"沈阳航空工业学院", "东  北  大  学", "沈 阳 工 业 大 学", 
						"沈 阳 建 筑 大 学", "沈 阳 理 工 大 学", "辽  宁  大  学"};
//系别信息
string strDepInfo[] = {"计算机科学与技术",  "数控机床技术", "软 件 工 程",
 						"信 息 工 程","机 器 翻 译", "计算机应用技术"};
//工种
string strWorkType[] = {"管理", "服务", "试用", "实习"};

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

//管理类的构造函数
CMessage::CMessage()
{
	head = new Node;
	head->next = NULL;
}

//管理类的析构函数
CMessage::~CMessage()
{
	ClearPerson();//释放除头结点的所有结点的空间
	delete head;//释放头结点的空间
}

//将传递过来的字符串数组中的信息以指定格式逐个显示,返回用户选择的字符串
string JudString(string strtemp[], int len)
{
	string strage;
	int temp = 0;

	do
		{
			cout << "请选择所在学院:";
			for (int i=0; i<len; i++)
			{
				if (i % 2 == 0)
				{
					cout << "\t\n";
				}
				cout << "\t" << i+1 << ". " << strtemp[i];
			}
			cout << "\n\t" << i+1 << ". " << "退出" << endl;
			cout << "请输入:";
			cin >> temp;

			if (cin.fail())//非合法数字的处理
			{
				cin.clear();
				cin >> strage;
				cout << "选择错误,请重新输入!" << endl;

				continue;
			}

			if (temp > 0 && temp < len+1)//
				break;
			if (temp == len+1)//len+1
				return "";

			cout << "选择错误,请重新输入!" << endl;
		}while(1);
	return strtemp[temp -1];
}

//人员年龄录入,要求必须是合法数字(0-150之间)
int InputAge()
{
	int age = 0;//数字型的年龄
	string strage = "";//人员年龄
	do 
	{
		cout << "请输入人员年龄: ";
		cin >> age;

		if (cin.fail())//非合法数字的处理
		{
			cin.clear();
			cin >> strage;
			cout << "选择错误,请重新输入!" << endl;

			continue;
		}
		if (age > 0 && age < 150)//0-150之间
			break;

		cout << "选择错误,请重新输入!" << endl;
		
	} while(1);

	return age;
}

//人员性别信息录入
string InputSex()
{
	string strsex = "";//人员性别信息
	string strage = "";//人员年龄

	int temp = 0;//临时变量
	do 
	{
		cout << "请选择人员性别: 1,男\t2,女\t3,退出" << endl;
		cout << "请输入:" ;
		cin >> temp;

		if (cin.fail())//非合法数字的处理
		{
			cin.clear();
			cin >> strage;
			cout << "选择错误,请重新输入!" << endl;

			continue;
		}
		if (temp == 1)//1说明为男
		{
			strsex = "男";
			break;
		}
		if (temp == 2)//2说明为女
		{
			strsex = "女";
			break;
		}
		if (temp == 3)//3说明为退出
			return "";

		cout << "选择错误,请重新输入!" << endl;
		
	} while(1);

	return strsex;
}

//人员类型的选择: 教职工,非教职工
string InputType()
{
	string strt = "";//教职工或非教职工
	do
	{
		cout << "请选择要插入人员的类型:" << endl;
		cout << "1,教职工\t2,非教职工\t3,退出" << endl;
		cout << "请输入:";
		cin >> strt;

		if (strt == "1" || strt == "2")//1或2说明是正确选择了
			break;

		if (strt == "3")//3说明是退出
			return "";

		cout << "选择错误,请重新输入!" << endl;
	}while(1);//否则继续输入直到正确或退出

	return strt;
}
//插入人员信息
void CMessage::InsertPerson()
{
	string strt = "";//教职工或非教职工
	string strname = "";//人员姓名信息
	string strsex = "";//人员性别信息
	string strage = "";//人员年龄
	string strcoll = "";//信息1
	string strdep = "";//信息2

	int age = 0;//数字型的年龄
	int wa = 0;//工龄信息
	int temp = 0;//临时变量

	//人员类型的选择: 教职工,非教职工
	strt = InputType();
	if (strt == "")
		return;

	//人员姓名信息录入
	cout << "请输入人员姓名: ";
	cin >> strname;

	//人员年龄录入,要求必须是合法数字(0-150之间)
	age = InputAge();
	
	//人员性别信息录入
	strsex = InputSex();
	if (strsex == "")
		return;

	if (strt == "1")
	{
		//教职工的学院信息录入
		strcoll = JudString(strCollInfo, 6);
		if (strcoll == "")
			return ;

		//教职工的所在系信息录入		
		strdep = JudString(strDepInfo, 6);
		if (strdep == "")
			return ;

		//创建教职工对象
		CTeacher *teacher = new CTeacher;

		//新对象信息添加
		teacher->SetName(strname);
		teacher->SetAge(age);
		teacher->SetSex(strsex);
		teacher->SetCollege(strcoll);
		teacher->SetDepartment(strdep);

		//创建Node新结点
		Node *node = new Node;
		node->Type = TEACHER;//派生类类型赋值
		node->pPer = teacher;//基类指针赋值

		//将新结点插入链表
		node->next = head->next;
		head->next = node;
	}
	else
	{
		//非教职工的工种信息录入
		strcoll = JudString(strWorkType, 4);
		if (strcoll == "")
		return ;
		
		//非教职工的工龄信息录入,要求工龄在0~80之间
		do 
		{
			cout << "请输入非教职工的工龄:" ;
			cin >> wa;

			if (cin.fail())//非合法数字的处理
			{
				cin.clear();
				cin >> strage;
				cout << "选择错误,请重新输入!" << endl;

				continue;
			}

			if (wa >= 0 && wa < 80)//在0~80之间
				break;

			cout << "选择错误,请重新输入!" << endl;
			
		} while(1);

		//创建非教职工对象
		CNTeacher *nteacher = new CNTeacher;

		//新对象信息添加
		nteacher->SetName(strname);
		nteacher->SetAge(age);
		nteacher->SetSex(strsex);
		nteacher->SetWorkType(strcoll);
		nteacher->SetWorkAge(wa);

		//创建Node新结点
		Node *node = new Node;
		node->Type = NTEACHER;//派生类类型赋值
		node->pPer = nteacher;//基类指针赋值

		//将新结点插入链表
		node->next = head->next;
		head->next = node;	
	}

	//询问是否继续添加
	cout << "添加成功 .(*^__^*)." << endl;

	cout << "继续添加请输入:Y或y(否则任意字符)..." << endl;
	cin >> strt;

	if (strt == "Y" || strt == "y")//Y和y都是继续添加,否则为不添加
	{
		InsertPerson();//循环调用
	}
	else
	{
		return;//否则返回
	}
}

//删除指定人员信息或删除全部的人员信息
void CMessage::DeletePerson()
{
	string strname = "";//要删除人员姓名信息
	string strdelete = "";//教职工或非教职工

	do
	{
		cout << "请选择删除类型:" << endl;
		cout << "1,单个删除\t2,全部删除\t3,退出" << endl;
		cout << "请选择:";
		cin >> strdelete;

		if (strdelete == "1" || strdelete == "2")//1或2说明是正确选择了
			break;

		if (strdelete == "3")//3说明是退出
			return;

		cout << "选择错误,请重新输入!" << endl;
	}while(1);

	if (strdelete == "1")
	{
		//指定人员姓名
		cout << "请输入要删除的人员姓名:";
		cin >> strname;

		Node *p, *q;//p为前指针,q为后指针
		p = head;//前指针初始化
		q = p->next;//后指针初始化

		//遍历链表
		while (q != NULL)
		{
			//找到与指定信息相同的结点
			if (q->pPer->GetName() == strname)
				break;

			//同时向后移动
			p = q;
			q = q->next;
		}

		if (q != NULL)//若找到指定信息
		{
			//进行删除
			p->next = q->next;

			cout << "删除掉的人员信息为:" << q->pPer->GetName() << endl;

			delete q;//空间释放
			cout << "删除成功!" << endl;//提示成功
		}
		else//若没找到指定信息
		{
			cout << "没有要删除信息!" << endl;//提示失败信息 
		}
	}
	if (strdelete == "2")
	{
		cout << "真的要全部删除吗???(Y或N,y或n)" << endl;
		cin >> strname;

		if (strname == "Y" || strname == "y")//Y和y都是继续添加,否则为不添加
		{
			ClearPerson();
		}
		else
		{
			return;//否则返回
		}
	}
}

//某个教职工人员信息显示
void PrintTeacher(CTeacher* pTeacher)
{
	cout << "\t" << pTeacher->GetName() << "\t";
	cout << pTeacher->GetAge() << "\t";
	cout << pTeacher->GetSex() << "\t";
	cout << pTeacher->GetCollege() << "\t";
	cout << pTeacher->GetDepartment() << endl;
}
//某个非教职工信息显示
void PrintNTeacher(CNTeacher* pnTeacher)
{
	cout << "\t" << pnTeacher->GetName() << "\t";
	cout << pnTeacher->GetAge() << "\t";
	cout << pnTeacher->GetSex() << "\t\t";
	cout << pnTeacher->GetWorkType() << "\t\t";
	cout << pnTeacher->GetWorkAge() << endl;
}
void PrintTotNt(Node* q)
{
	//以下为指定信息显示
	cout << "\t--------------------------------------------------------------" << endl;

	if (q->Type == TEACHER)
	{
		//若是教职工则定义教职工指针
		CTeacher *pTeacher = (CTeacher*)q->pPer;

		cout << "\t姓名\t年龄\t性别\t\t学院\t\t系别" << endl;
		cout << "\t--------------------------------------------------------------" << endl;

		PrintTeacher(pTeacher);//调用教职工打印函数
	}
	else
	{
		//若是非教职工则定义非教职工指针
		CNTeacher *pnTeacher = (CNTeacher*)q->pPer;

		cout << "\t姓名\t年龄\t性别\t\t工种\t\t工龄" << endl;
		cout << "\t--------------------------------------------------------------" << endl;

		PrintNTeacher(pnTeacher);//调用非教职工打印函数
	}

	cout << "\t--------------------------------------------------------------" << endl;
}

//按姓名查找信息
int CMessage::FindPersonName()
{	
	string strname = "";//要查询的人员姓名信息
	int key = 0;

	//指定人员姓名
	cout << "请输入要查找的人员姓名:";
	cin >> strname;

	Node *q = head->next;//初始化指针

	//遍历链表
	while (q != NULL)
	{
		//找到与指定信息相同的结点
		if (q->pPer->GetName() == strname)
		{
			PrintTotNt(q);
			key = 1;
		}

		//向后移动
		q = q->next;
	}
	
	return key;
}

//按类型查找信息
int CMessage::FindPersonType()
{
	string strt = "";//要查询的人员类型信息
	int temp = 0;
	int key = 0;

	//人员类型的选择: 教职工,非教职工
	do
	{
		cout << "请选择要查找的人员类型:" << endl;
		cout << "1,教职工\t2,非教职工\t" << endl;
		cout << "请输入:";
		cin >> strt;

		if (strt == "1")
		{
			temp = 1;
			break;
		}
		if (strt == "2")
		{
			temp = 2;
			break;
		}

		cout << "选择错误,请重新输入!" << endl;
	}while(1);//否则继续输入直到正确或退出
	
	Node *q = head->next;//初始化指针

	//遍历链表
	while (q != NULL)
	{
		//找到与指定信息相同的结点
		if (q->Type == temp)
		{
			PrintTotNt(q);
			key = 1;
		}

		//向后移动
		q = q->next;
	}
	
	return key;
}

//按性别查找信息
int CMessage::FindPersonSex()
{
	string strsex = "";//要查询的人员性别信息
	int key = 0;

	//指定人员性别
	strsex = InputSex();

	Node *q = head->next;//初始化指针

	//遍历链表
	while (q != NULL)
	{
		//找到与指定信息相同的结点
		if (q->pPer->GetSex() == strsex)
		{
			PrintTotNt(q);
			key = 1;
		}

		//向后移动
		q = q->next;
	}
	return key;
}

//按学院查找信息
int CMessage::FindPersonColl()
{
	string strcoll = "";//要查询的人员学院信息
	int key = 0;

	//指定人员性别
	strcoll = JudString(strCollInfo, 6);

	Node *q = head->next;//初始化指针

	//遍历链表
	while (q != NULL)
	{
		//找到与指定信息相同的结点
		if (q->Type == TEACHER)
		{
			//若是教职工则定义教职工指针
			CTeacher *pTeacher = (CTeacher*)q->pPer;

			//判断是否为指定的信息
			if (pTeacher->GetCollege() == strcoll)
			{
				PrintTotNt(q);
				key = 1;
			}
		}
		//向后移动
		q = q->next;
	}
	return key;
}

//按工种查找信息
int CMessage::FindPersonWT()
{
	string strwt = "";//要查询的人员学院信息
	int key = 0;

	//指定人员性别
	strwt = JudString(strWorkType, 4);

	Node *q = head->next;//初始化指针

	//遍历链表
	while (q != NULL)
	{
		//找到与指定信息相同的结点
		if (q->Type == NTEACHER)
		{
			//若是教职工则定义教职工指针
			CNTeacher *pnTeacher = (CNTeacher*)q->pPer;

			//判断是否为指定的信息
			if (pnTeacher->GetWorkType() == strwt)
			{
				PrintTotNt(q);
				key = 1;
			}
		}
		//向后移动
		q = q->next;
	}
	return key;
}

//查找某个人的信息
void CMessage::FindPerson()
{
	char strkey[10] = "\0";//用于输入选项
	int key = 0;

	//进入查询系统
	do 
	{
		cout << "\n  -----职工信息查询系统-----" << endl;
		cout << "\t1,姓    名\t2,工作类型" << endl;
		cout << "\t3,性    别\t4,所在学院" << endl;
		cout << "\t5,所属工种\t6,退出系统" << endl;

		//菜单选择录入
		cout << "请选择查询方式:";
		cin >> strkey;

		//分项处理
		switch(strkey[0])
		{
		case '1':
			key = FindPersonName();//按姓名查找信息
			break;
		case '2':
			key = FindPersonType();//按类型查找信息
			break;
		case '3':
			key = FindPersonSex();//按性别查找信息
		    break;
		case '4':
			key = FindPersonColl();//按学院查找信息
		    break;
		case '5':
			key = FindPersonWT();//按工种查找信息
			break;
		case '6':
			return;//退出系统
		default:
			cout << "输入有误,请重新输入!" << endl;
		    break;
		}
		if (key == 0)
		{
			cout << "没有要查找信息!" << endl;
		}
		cout << "按回车键返回...";
		getchar();
		getchar();

		cout << endl;
		cout << "-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-" << endl;
		cout << endl;

	}while (1);		
}

//释放链表
void CMessage::ClearPerson()
{
	Node *p = head;
	Node *q = head->next;

	//遍历链表:逐个结点释放空间
	while (q != NULL)
	{
		p->next = q->next;

		delete q;//删除结点
		q = p->next;
	}
}

//显示所有人员信息
void CMessage::ShowPerson()
{
	Node *q;
	q = head->next;

	//判断链表是否为空
	if (q == NULL)
	{
		cout << "暂时没有人员信息!" << endl;
		return;
	}

	cout << "\t--------------------------------------------------------------" << endl;
	cout << "\t姓名\t年龄\t性别\t\t信息1\t\t信息2" << endl;
	cout << "\t--------------------------------------------------------------" << endl;

	//遍历链表
	while (q != NULL)
	{
		if (q->Type == TEACHER)
		{
			//若是教职工则定义教职工指针
			CTeacher *pTeacher = (CTeacher*)q->pPer;

			PrintTeacher(pTeacher);//调用教职工打印函数
		}
		else
		{
			//若是非教职工则定义非教职工指针
			CNTeacher *pnTeacher = (CNTeacher*)q->pPer;

			PrintNTeacher(pnTeacher);//调用非教职工打印函数
		}

		q = q->next;
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -