0
点赞
收藏
分享

微信扫一扫

c++实现职工管理系统(基于多态和双链表)

纽二 2022-03-15 阅读 78

职工管理系统可以用来管理公司内所有员工的信息


本教程主要利用C++来实现一个基于多态的职工管理系统


公司中职工分为三类:普通员工、经理、老板,显示信息时,需要显示职工编号、职工姓名、职工岗位、以及职责


普通员工职责:完成经理交给的任务
经理职责:完成老板交给的任务,并下发任务给员工
老板职责:管理公司所有事务


管理系统中需要实现的功能如下:

1退出管理程序:退出当前管理系统

2增加职工信息:实现批量添加职工功能,将信息录入到文件中。

职工信息为∶职工编号、姓名、部门编号·显示职工信息:显示公司内部所有职工的信息
删除离职职工:按照编号删除指定的职工

3修改职工信息:按照编号修改职工个人信息

4查找职工信息:按照职工的编号或者职工的姓名进行查找相关的人员信息·

5按照编号排序:按照职工编号,进行排序,排序规则由用户指定

6清空所有文档:清空文件中记录的所有职工信息(清空前需要再次确认,防止误删)
 

成品如下:

 此项目用五个文件分别实现功能

头文件1

#pragma once
#include <iostream>
#include <string>
using namespace std;

//职工抽象类
class Worker
{
public:
	//显示个人信息
	virtual void showInfo() = 0;
	//获取岗位名称
	virtual string getDeptName() = 0;

	//职工编号
	int m_ID;
	//职工姓名
	string m_Name;
	//部门编号
	int m_DeptID;
};

//普通员工类
class Employee : public Worker
{
public:
	//构造函数
	Employee(int id, string name, int dId);
	//显示个人信息
	virtual void showInfo();
	//获取岗位名称
	virtual string getDeptName();
};

//经理类
class Manager : public Worker
{
public:
	//构造函数
	Manager(int id, string name, int dId);
	//显示个人信息
	virtual void showInfo();
	//获取岗位名称
	virtual string getDeptName();
};

//老板类
class Boss : public Worker
{
public:
	//构造函数
	Boss(int id, string name, int dId);
	//显示个人信息
	virtual void showInfo();
	//获取岗位名称
	virtual string getDeptName();
};

头文件2

#pragma once  //防止头文件重复包含

#include <iostream>
#include <fstream>
#include <string>
using namespace std;

#include "worker.h"

#define FILENAME "empFile.txt"

class Node
{
	friend class LinkedList;
public:
	Worker* m_Data_Worker;

private:
	Node* preptr;
	Node* postptr;
};

class LinkedList
{
public:
	LinkedList();
	~LinkedList();
	void newNodeToEnd(int ID, string name, int dSelect);
	Node* findNode(int ID);
	Node* findNode(string name);
	bool delNode(int ID);
	void showNode();
	void showNode(Node* p);
	void sortNode(int way);
	void cleanList();
	void save();

	int nodeNum;

private:
	Node node;
	//p: 管理当前节点,pBefore: 管理前一个节点,pAfter: 管理后一个节点,ptr: 分配内存的指针,h: 头节点,e: 尾节点
	Node* pBefore, * p, * pAfter, * ptr, * h, * e;
};

class WorkerManager
{
public:
	WorkerManager();
	~WorkerManager();

	void Show_Menu();
	void exitSystem();
	void Add_Emp();
	void Show_Emp();
	void Del_Emp();
	void Mod_Emp();
	void Find_Emp();
	void Sort_Emp();
	void Clean_File();

	LinkedList list;
};

源文件1

#include "worker.h"

//普通员工类
//构造函数
Employee::Employee(int id, string name, int dId)
{
	this->m_ID = id;
	this->m_Name = name;
	this->m_DeptID = dId;
}

//显示个人信息
void Employee::showInfo()
{
	cout << "职工编号:" << this->m_ID << "\t";
	cout << "姓名:" << this->m_Name << "\t";
	cout << "岗位:" << this->getDeptName() << "\t";
	cout << "岗位职责: 完成经理交给的任务" << endl;
}

//获取岗位名称
string Employee::getDeptName()
{
	return string("员工");
}

//经理类
//构造函数
Manager::Manager(int id, string name, int dId)
{
	this->m_ID = id;
	this->m_Name = name;
	this->m_DeptID = dId;
}

//显示个人信息
void Manager::showInfo()
{
	cout << "职工编号:" << this->m_ID << "\t";
	cout << "姓名:" << this->m_Name << "\t";
	cout << "岗位:" << this->getDeptName() << "\t";
	cout << "岗位职责: 完成老板交给的任务,并下发任务给职工" << endl;
}

//获取岗位名称
string Manager::getDeptName()
{
	return string("经理");
}

//老板类
//构造函数
Boss::Boss(int id, string name, int dId)
{
	this->m_ID = id;
	this->m_Name = name;
	this->m_DeptID = dId;
}

//显示个人信息
void Boss::showInfo()
{
	cout << "职工编号:" << this->m_ID << "\t";
	cout << "姓名:" << this->m_Name << "\t";
	cout << "岗位:" << this->getDeptName() << "\t";
	cout << "岗位职责: 管理公司所有事务" << endl;
}

//获取岗位名称
string Boss::getDeptName()
{
	return string("总裁");
}

源文件2

#include "workerManager.h"

WorkerManager::WorkerManager()
{
	//1、文件不存在
	ifstream ifs;
	ifs.open(FILENAME, ios::in); //读文件

	if (!ifs.is_open())
	{
		ifs.close();
		return;
	}

	//2、文件存在 数据为空
	char ch;
	ifs >> ch;
	if (ifs.eof())
	{
		ifs.close();
		return;
	}

	//3、文件存在并且记录数据
	ifs.seekg(0);
	int id;
	string name;
	int dId;

	while (ifs >> id && ifs >> name && ifs >> dId)
	{
		list.newNodeToEnd(id, name, dId);
	}

	ifs.close();
}

WorkerManager::~WorkerManager()
{
	list.cleanList();
}

void WorkerManager::Show_Menu()
{
	cout << "********************************************" << endl;
	cout << "*********  欢迎使用职工管理系统! **********" << endl;
	cout << "*************  0.退出管理程序  *************" << endl;
	cout << "*************  1.增加职工信息  *************" << endl;
	cout << "*************  2.显示职工信息  *************" << endl;
	cout << "*************  3.删除离职职工  *************" << endl;
	cout << "*************  4.修改职工信息  *************" << endl;
	cout << "*************  5.查找职工信息  *************" << endl;
	cout << "*************  6.按照编号排序  *************" << endl;
	cout << "*************  7.清空所有文档  *************" << endl;
	cout << "********************************************" << endl;
	cout << endl;
}

void WorkerManager::exitSystem()
{
	cout << "欢迎下次使用" << endl;
	system("pause");
	exit(0);
}

void WorkerManager::Add_Emp()
{
	cout << "请输入添加职工数量: " << endl;
	int addNum = 0;//保存用户的输入数量
	cin >> addNum;

	if (addNum > 0)
	{
		//批量添加新数据
		for (int i = 0; i < addNum; i++)
		{
			int id;//职工编号
			string name;//职工姓名
			int dSelect;//部门选择

			cout << "请输入第 " << i + 1 << " 个新职工编号:" << endl;
			cin >> id;

			cout << "请输入第 " << i + 1 << " 个新职工姓名:" << endl;
			cin >> name;

			while (true)
			{
				cout << "请选择该职工岗位:" << endl;
				cout << "1、普通职工" << endl;
				cout << "2、经理" << endl;
				cout << "3、老板" << endl;
				cin >> dSelect;
				if (dSelect >= 1 && dSelect <= 3)
					break;
				cout << "您的输入有误,请重新输入!" << endl;
			}

			list.newNodeToEnd(id, name, dSelect);
		}

		cout << "成功添加" << addNum << "名新职工!" << endl;

		//保存数据到文件中
		this->list.save();

	}
	else
	{
		cout << "您的输入有误!" << endl;
	}

	//按任意键返回上级目录
	system("pause");
	system("cls");
}

void WorkerManager::Show_Emp()
{
	if (list.nodeNum == 0)
		cout << "当前暂无职工信息!" << endl;
	else
	{
		cout << "当前共 " << list.nodeNum << "名职工" << endl;
		list.showNode();
	}

	//按任意键返回上级目录
	system("pause");
	system("cls");
}

void WorkerManager::Del_Emp()
{
	//按照职工编号删除
	cout << "请输入想要删除职工编号:" << endl;
	int id = 0;
	cin >> id;

	if (list.delNode(id))
	{
		cout << "删除成功!" << endl;
		this->list.save();
	}
	else
	{
		cout << "删除失败,未找到该职工!" << endl;
	}

	//按任意键返回上级目录
	system("pause");
	system("cls");
}

void WorkerManager::Mod_Emp()
{
	cout << "请输入修改职工编号:" << endl;
	int id;
	cin >> id;
	Node* Emp = list.findNode(id);
	if (Emp != NULL)
	{
		//查找到了职工
		int newID = 0;
		string newName = "";
		int dSelect = 0;

		cout << "查到:" << id << "号职工,请输入新职工号:" << endl;
		cin >> newID;

		cout << "请输入新姓名:" << endl;
		cin >> newName;

		cout << "请输入岗位:" << endl;
		cout << "1、普通职工" << endl;
		cout << "2、经理" << endl;
		cout << "3、老板" << endl;
		cin >> dSelect;

		delete Emp->m_Data_Worker;//先释放旧数据

		switch (dSelect)
		{
		case 1:
			Emp->m_Data_Worker = new Employee(newID, newName, 1);
			break;
		case 2:
			Emp->m_Data_Worker = new Manager(newID, newName, 2);
			break;
		case 3:
			Emp->m_Data_Worker = new Boss(newID, newName, 3);
			break;
		default:
			break;
		}

		//保存
		this->list.save();

		cout << "修改成功!" << endl;

		//按任意键返回上级目录
		system("pause");
		system("cls");
	}
	else
	{
		cout << "修改失败,查无此人!" << endl;
	}
}

void WorkerManager::Find_Emp()
{
	cout << "请输入查找的方式:" << endl;
	cout << "1、按职工编号查找 " << endl;
	cout << "2、按职工姓名查找 " << endl;

	int select = 0;
	cin >> select;
	Node* Emp = NULL;

	if (select == 1)
	{
		int id;
		cout << "请输入查找的职工编号:" << endl;
		cin >> id;
		Emp = list.findNode(id);
		if (Emp != NULL)
		{
			cout << "查找成功!该员工信息如下:" << endl;
			this->list.showNode(Emp);
		}
		else
		{
			cout << "查找失败,查无此人!" << endl;
		}
	}
	else if (select == 2)
	{
		string name;
		cout << "请输入查找的职工姓名:" << endl;
		cin >> name;
		Emp = list.findNode(name);
		if (Emp != NULL)
		{
			cout << "查找成功!该员工信息如下:" << endl;
			this->list.showNode(Emp);
		}
		else
		{
			cout << "查找失败,查无此人!" << endl;
		}
	}
	//按任意键返回上级目录
	system("pause");
	system("cls");
}

void WorkerManager::Sort_Emp()
{
	if (list.nodeNum == 0)
	{
		cout << "当前暂无职工信息!" << endl;
		system("pause");
		system("cls");
	}
	else
	{
		cout << "请选择排序方式:" << endl;
		cout << "1、按职工号进行升序" << endl;
		cout << "2、按职工号进行降序" << endl;
		int select = 0;
		cin >> select;

		while (true)
		{
			if (select == 1)
			{
				list.sortNode(1);
				cout << "排序成功!排序后的结果为:" << endl;
				this->list.save();
				this->Show_Emp();
				break;
			}
			else if (select == 2)
			{
				list.sortNode(2);
				cout << "排序成功!排序后的结果为:" << endl;
				this->list.save();
				this->Show_Emp();
				break;
			}
			else
			{
				cout << "您的输入有误,请重新输入!" << endl;

				system("pause");
				system("cls");
			}
		}
	}
}

void WorkerManager::Clean_File()
{
	cout << "确定清空?" << endl;
	cout << "1、确定" << endl;
	cout << "2、返回" << endl;

	int select = 0;
	cin >> select;

	if (select == 1)
	{
		//清空文件
		ofstream ofs(FILENAME, ios::trunc); //删除文件后重新创建
		ofs.close();

		//清空链表
		if (list.nodeNum != 0)
		{
			list.cleanList();
		}

		cout << "清空成功!" << endl;
	}

	system("pause");
	system("cls");
}

//链表
//构造函数: 创建了头、尾节点
LinkedList::LinkedList()
{
	nodeNum = 0;
	h = new Node;
	e = new Node;
	h->preptr = NULL;
	e->postptr = NULL;
	h->postptr = e;
	e->preptr = h;
}

//析构函数: 释放链表
LinkedList::~LinkedList()
{
	p = h->postptr;
	pAfter = p->postptr;
	while (p != e)
	{
		delete p;
		p = pAfter;
		pAfter = pAfter->postptr;
		nodeNum--;
	}
	delete h;
	delete e;
}

//添加节点到尾部
void LinkedList::newNodeToEnd(int ID, string name, int dSelect)
{
	ptr = new Node;

	switch (dSelect)
	{
	case 1:
		ptr->m_Data_Worker = new Employee(ID, name, 1);
		break;
	case 2:
		ptr->m_Data_Worker = new Manager(ID, name, 2);
		break;
	case 3:
		ptr->m_Data_Worker = new Boss(ID, name, 3);
		break;
	}

	p = ptr;
	pAfter = e;

	p->postptr = pAfter;
	p->preptr = pBefore = pAfter->preptr;

	pBefore->postptr = p;
	pAfter->preptr = p;

	nodeNum++;
}

//查找(两种方式)
Node* LinkedList::findNode(int ID)
{
	p = h->postptr;
	pAfter = p->postptr;
	while (p != e && p->m_Data_Worker->m_ID != ID)
	{
		p = pAfter;
		pAfter = pAfter->postptr;
	}
	if (p == e)
		return NULL;
	else
		return p;
}

Node* LinkedList::findNode(string name)
{
	p = h->postptr;
	pAfter = p->postptr;
	while (p != e && p->m_Data_Worker->m_Name != name)
	{
		p = pAfter;
		pAfter = pAfter->postptr;
	}
	if (p == e)
		return NULL;
	else
		return p;
}

//删除
bool LinkedList::delNode(int ID)
{
	p = findNode(ID);
	if (p != NULL)
	{
		pAfter = p->postptr;
		pBefore = p->preptr;

		pBefore->postptr = pAfter;
		pAfter->preptr = pBefore;

		delete p;
		nodeNum--;

		return true;
	}
	else
	{
		return false;
	}
}

//显示全部节点
void LinkedList::showNode()
{
	p = h->postptr;
	pAfter = p->postptr;
	while (p != e)
	{
		p->m_Data_Worker->showInfo();
		p = pAfter;
		pAfter = pAfter->postptr;
	}
}

//显示某一节点
void LinkedList::showNode(Node* p)
{
	p->m_Data_Worker->showInfo();
}

//链表版冒泡排序
void LinkedList::sortNode(int way)
{
	Node* pBe, * p1, * p2, * pAf;
	int i = 0, j = 0;
	if (way == 1)
	{
		for (i = 0; i < nodeNum - 1; i++)
		{
			pBe = h;
			p1 = h->postptr;
			p2 = p1->postptr;
			pAf = p2->postptr;
			for (j = 0; j < nodeNum - 1 - i; j++)
			{
				if (p1->m_Data_Worker->m_ID > p2->m_Data_Worker->m_ID)
				{
					pBe->postptr = p2;
					pAf->preptr = p1;
					p1->preptr = p2;
					p1->postptr = pAf;
					p2->preptr = pBe;
					p2->postptr = p1;

					//交换p1 p2指向
					Node* temp = p1;
					p1 = p2;
					p2 = temp;
				}
				pBe = p1;
				p1 = p2;
				p2 = pAf;
				pAf = pAf->postptr;
			}
		}
	}
	else if (way == 2)
	{
		for (i = 0; i < nodeNum - 1; i++)
		{
			pBe = h;
			p1 = h->postptr;
			p2 = p1->postptr;
			pAf = p2->postptr;
			for (j = 0; j < nodeNum - 1 - i; j++)
			{
				if (p1->m_Data_Worker->m_ID < p2->m_Data_Worker->m_ID)
				{
					pBe->postptr = p2;
					pAf->preptr = p1;
					p1->preptr = p2;
					p1->postptr = pAf;
					p2->preptr = pBe;
					p2->postptr = p1;

					//交换p1 p2指向
					Node* temp = p1;
					p1 = p2;
					p2 = temp;
				}
				pBe = p1;
				p1 = p2;
				p2 = pAf;
				pAf = pAf->postptr;
			}
		}
	}
}

//清空链表
void LinkedList::cleanList()
{
	p = h->postptr;
	pAfter = p->postptr;
	while (p != e)
	{
		delete p;
		p = pAfter;
		pAfter = pAfter->postptr;
		nodeNum--;
	}
	h->postptr = e;
	e->preptr = h;
}

//保存文件
void LinkedList::save()
{
	ofstream ofs;
	ofs.open(FILENAME, ios::out);//用输出的方式打开文件 ———— 写文件

	p = h->postptr;
	pAfter = p->postptr;
	//将每个人的数据写入文件中
	while (p != e)
	{
		ofs << this->p->m_Data_Worker->m_ID << " " << this->p->m_Data_Worker->m_Name << " "
			<< this->p->m_Data_Worker->m_DeptID << endl;

		p = pAfter;
		pAfter = pAfter->postptr;
	}

	//关闭文件
	ofs.close();
}

源文件3

#include "Worker.h"

int main()
{

	//实例化对象
	WorkerManager wm;

	int choice = 0;

	while (true)
	{
		wm.Show_Menu();

		cout << "请输入您的选择:" << endl;
		cin >> choice;

		switch (choice)
		{
		case 0: //退出系统
			wm.exitSystem();
			break;
		case 1: //添加职工
			wm.Add_Emp();
			break;
		case 2: //显示职工
			wm.Show_Emp();
			break;
		case 3: //删除职工
			wm.Del_Emp();
			break;
		case 4: //修改职工
			wm.Mod_Emp();
			break;
		case 5: //查找职工
			wm.Find_Emp();
			break;
		case 6: //排序职工
			wm.Sort_Emp();
			break;
		case 7: //清空文件
			wm.Clean_File();
			break;
		default:
			system("cls");
			break;
		}
	}

	system("pause");

	return EXIT_SUCCESS;
}
举报

相关推荐

0 条评论