【C++】职工管理系统源码分享

//职工管理系统.cpp
#include"WorkerManager.h"
#include"Worker.h"
#include"employee.h"
#include"manager.h"
#include"boss.h"
#include<cstdlib>

int main()
{
	int input = 0;
	WorkerManager wm;
	
	/*Worker* wk = NULL;
	wk = new employee(1,"张三",1);
	wk->showInfo();
	delete wk;*/

	do
	{
		wm.show_menu();
		cout << "请输入你的选择 : ";
		cin >> input;
		switch (input)
		{
			case 0:
				wm.exitsystem();
				break;
			case 1:
				wm.addemp();
				wm.save();
				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_emp();
				break;
			default:
				break;
		}
		system("cls");
	}while (input);

	return 0;
}
//boss.h
#pragma once
#include"Worker.h"

class boss : public Worker
{
public:
	boss(int id, string name, int deptid);

	virtual void showInfo();

	virtual string get_deptname();

};
//employee.h
#pragma once
#include"Worker.h"

class employee : public Worker
{
public:
	employee(int id,string name,int deptid);

	virtual void showInfo();

	virtual string get_deptname();

};
//manger.h
#pragma once
#include"Worker.h"

class manager : public Worker
{
public:
	manager(int id, string name, int deptid);

	virtual void showInfo();

	virtual string get_deptname();

};
//Worker.h
#pragma once
#include<iostream>
#include<string>
using namespace std;

class Worker
{
public:

	virtual void showInfo() = 0;

	virtual string get_deptname() = 0;

	int m_id;
	string m_name;
	int m_deptid;
};
//WorkerManager.h
#pragma once
#include<iostream>
using namespace std;
#include"Worker.h"
#include<fstream>

class WorkerManager
{
public:
	WorkerManager();

	void show_menu();

	void exitsystem();

	~WorkerManager();

	int m_Empnum;

	Worker** m_Emparray;

	void addemp();

	void save();

	bool m_FileIsEmpty;

	int get_Empnum();

	void init_emp();

	void show_emp();

	void del_emp();

	int isExeit(int id);

	void mod_emp();

	void find_emp();

	void sort_emp();

	void clean_emp();
};
//boss.cpp
#include"boss.h"

boss::boss(int id, string name, int deptid)
{
	this->m_id = id;
	this->m_name = name;
	this->m_deptid = deptid;
}

void boss::showInfo()
{
	cout << "职工编号: " << this->m_id << "\t职工名字: " << this->m_name << "\t职工岗位:" << this->get_deptname() << "\t岗位职责:管理公司所有事务" << endl;

}

string boss::get_deptname()
{

	return string("老板");
}
//employee.cpp
#include"employee.h"

employee::employee(int id, string name, int deptid)
{
	this->m_id = id;
	this->m_name = name;
	this->m_deptid = deptid;
}

void employee::showInfo()
{
	cout << "职工编号: " << this->m_id << "\t职工名字: " << this->m_name << "\t职工岗位:" << this->get_deptname() << "\t岗位职责:完成经理交给的任务" << endl;
}

string employee::get_deptname()
{
	return string("员工");
}
//WorkerManger.cpp
#include"WorkerManager.h"
#include"employee.h"
#include"manager.h"
#include"boss.h"


WorkerManager::WorkerManager()
{
	ifstream ifs;
	ifs.open("empFile.txt", ios::in);
	if (!ifs.is_open())
	{
		//cout << "文件不存在" << endl;
		this->m_Empnum = 0;
		this->m_Emparray = NULL;
		this->m_FileIsEmpty = true;
		ifs.close();
		return;
	}

	char ch;
	ifs >> ch;
	if (ifs.eof())
	{
		//cout << "文件为空!" << endl;
		this->m_Empnum = 0;
		this->m_Emparray = NULL;
		this->m_FileIsEmpty = true;
		ifs.close();
		return;
	}

	int num = this->get_Empnum();
	//cout << "职工人数为:" << num << endl;
	this->m_Empnum = num;
	this->m_Emparray = new Worker*[this->m_Empnum];
	this->init_emp();

}
WorkerManager::~WorkerManager()
{
	if (this->m_Emparray != NULL)
	{
		delete[] this->m_Emparray;
		this->m_Emparray = NULL;
	}
}

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::addemp()
{
	int addnum = 0;
	cout << "请输入添加职工的数量:" << endl;
	cin >> addnum;
	if (addnum > 0)
	{
		int newsize = this->m_Empnum + addnum;
		Worker** newSpace= new Worker*[newsize];
		if (this->m_Emparray != NULL)
		{
			for (int i = 0; i < this->m_Empnum; i++)
			{
				newSpace[i] = this->m_Emparray[i];
			}
		}
		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;

			cout << "请选择该职工岗位:" << endl;
			cout << "1、普通职工" << endl;
			cout << "2、经理" << endl;
			cout << "3、老板" << endl;
			cin >> dselect;
			Worker* worker = NULL;
			switch (dselect)
			{
			case 1:
				worker = new employee(id, name, 1);
				break;
			case 2:
				worker = new manager(id, name, 2);
				break;
			case 3:
				worker = new boss(id, name, 3);
				break;
			default:
				break;
			}
			newSpace[this->m_Empnum + i] = worker;
		}
		delete[] this->m_Emparray;
		this->m_Emparray = newSpace;
		this->m_Empnum = newsize;
		cout << "成功添加" << addnum << " 名新职工!" << endl;
		this->save();
	}
	else
	{
		cout << "输入有误!" << endl;
	}
}

void WorkerManager::save()
{
	ofstream ofs;
	ofs.open("empFile.txt", ios::out);
	for (int i = 0; i < this->m_Empnum; i++)
	{
		ofs << this->m_Emparray[i]->m_id << " " << this->m_Emparray[i]->m_name << " " << this->m_Emparray[i]->m_deptid << endl;
	}
	ofs.close();
}

int WorkerManager::get_Empnum()
{
	ifstream ifs;
	ifs.open("empFile.txt", ios::in);
	int id;
	string name;
	int did;
	int num = 0;
	while (ifs >> id && ifs >> name && ifs >> did)
	{
		num++;
	}
	return num;
}

void WorkerManager::init_emp()
{
	ifstream ifs;
	ifs.open("empFile.txt", ios::in);
	int id;
	string name;
	int did;
	int index = 0;
	while (ifs >> id && ifs >> name && ifs >> did)
	{
		Worker* worker = NULL;
		if (did == 1)
		{
			worker = new employee(id, name, did);
		}
		else if (did == 2)
		{
			worker = new manager(id, name, did);
		}
		else
		{
			worker = new boss(id, name, did);
		}
		this->m_Emparray[index] = worker;
		index++;
	}
	ifs.close();
}

void WorkerManager::show_emp()
{
	if (this->m_FileIsEmpty)
	{
		cout << "没有职工信息!" << endl;
	}
	else
	{
		for (int i = 0; i < m_Empnum; i++)
		{
			this->m_Emparray[i]->showInfo();
		}
	}
	system("pause");
	system("cls");
}

int WorkerManager::isExeit(int id)
{
	int index = -1;
	for (int i = 0; i < this->m_Empnum; i++)
	{
		if (this->m_Emparray[i]->m_id == id)
		{
			index = i;
			break;
		}
	}
	return index;
}



void WorkerManager::del_emp()
{
	if (this->m_FileIsEmpty)
	{
		cout << "文件不存在或记录为空!" << endl;
	}
	else
	{
		int find = 0;
		cout << "请输入你要删除的员工的编号:";
		cin >> find;
		int index = this->isExeit(find);
		if (index != -1)
		{
			for (int i = index; i < this->m_Empnum; i++)
			{
				this->m_Emparray[i] = this->m_Emparray[i + 1];
			}
			this->m_Empnum--;
			this->save();
			cout << "删除成功!" << endl;
		}
		else
		{
			cout << ",删除失败,该职工不存在!" << endl;
		}
	}
	system("pause");
	system("cls");
}

void WorkerManager::mod_emp()
{
	if (this->m_FileIsEmpty)
	{
		cout << "文件不存在或记录为空!" << endl;
	}
	else
	{
		int find = 0;
		cout << "请输入你要修改的员工的编号:";
		cin >> find;
		int index = this->isExeit(find);
		if (index != -1)
		{
			delete this->m_Emparray[index];
			int newid = 0;
			string newname = "";
			int newdid = 0;
			cout << "查到: " << find << "号职工,请输入新职工号: ";
			cin >> newid;
			cout << "请输入新名字:" << endl;
			cin >> newname;
			cout << "请输入岗位:" << endl;
			cout << "1、普通岗位" << endl;
			cout << "2、经理" << endl;
			cout << "3、老板" << endl;
			cin >> newdid;
			Worker* worker = NULL;
			switch (newdid)
			{
			case 1:
				worker = new employee(newid, newname, newdid);
				break;
			case 2:
				worker = new manager(newid, newname, newdid);
				break;
			case 3:
				worker = new boss(newid, newname, newdid);
				break;
			default:
				break;
			}
			this->m_Emparray[find] = worker;
			cout << "修改成功!" << endl;
			this->save();
		}
		else
		{
			cout << "修改失败,没有该职工的信息!" << endl;
		}
	}
	system("pause");
	system("cls");
}

void WorkerManager::find_emp()
{
	if (this->m_FileIsEmpty)
	{
		cout << "文件不存在或记录为空!" << endl;
	}
	else
	{
		cout << "请输入查找方式:" << endl;
		cout << "1、按职工编号查找" << endl;
		cout << "2、按职工姓名查找" << endl;
		int select = 0;
		cin >> select;
		if (select == 1)
		{
			int id;
			cout << "请输入查找的职工编号:" << endl;
			cin >> id;
			int ret = isExeit(id);
			if (ret != -1)
			{
				cout << "查找成功!该职工信息如下:" << endl;
				this->m_Emparray[ret]->showInfo();
			}
			else
			{
				cout << "查找失败,查无此人!" << endl;
			}
		}
		else if (select == 2)
		{
			string name;
			bool isfind = false;
			cout << "请输入查找的姓名:" << endl;
			cin >> name;
			for (int i = 0; i < this->m_Empnum; i++)
			{
				if (this->m_Emparray[i]->m_name == name)
				{
					cout << "查找成功,职工编号为:" << this->m_Emparray[i]->m_id << "号职工信息如下" << endl;
					this->m_Emparray[i]->showInfo();
					isfind = true;
				}
			}
			if (isfind == false)
			{
				cout << "查无此人!" << endl;
			}
		}
		else
		{
			cout << "输入选项有误!" << endl;
		}
	}
	system("pause");
	system("cls");
}


void WorkerManager::sort_emp()
{
	if (this->m_FileIsEmpty)
	{
		cout << "文件不存在或记录为空!" << endl;
		system("pause");
		system("cls");
	}
	else
	{
		cout << "请选择排序方式:" << endl;
		cout << "1、按职工号进行升序" << endl;
		cout << "2、按职工号进行降序" << endl;
		int select = 0;
		cin >> select;
		for (int i = 0; i < this->m_Empnum; i++)
		{
			int minOrmax = i;
			for (int j = i + 1; j < this->m_Empnum; j++)
			{
				if (select == 1)
				{
					if (this->m_Emparray[minOrmax]->m_id > this->m_Emparray[j]->m_id)
					{
						minOrmax = j;
					}
				}
				else 
				{
					if (this->m_Emparray[minOrmax]->m_id < this->m_Emparray[j]->m_id)
					{
						minOrmax = j;
					}
				}
			}
			if (i != minOrmax)
			{
				Worker* temp = this->m_Emparray[i];
				this->m_Emparray[i] = this->m_Emparray[minOrmax];
				this->m_Emparray[minOrmax] = temp;
			}
		}
	}
	cout << "排序成功!排序后的结果为:" << endl;
	this->save();
	this->show_emp();
}

void WorkerManager::clean_emp()
{
	cout << "确定清空?" << endl;
	cout << "1、确定" << endl;
	cout << "2、返回" << endl;
	int select = 0;
	cin >> select;
	if (select == 1)
	{
		ofstream ofs("empfile.txt", ios::trunc);
		ofs.close();
		if (this->m_Emparray != NULL)
		{
			for (int i = 0; i < this->m_Empnum; i++)
			{
				delete this->m_Emparray[i];
				this->m_Emparray[i] = NULL;
			}
			delete[] this->m_Emparray;
			this->m_Emparray = NULL;
			this->m_Empnum = 0;
			this->m_FileIsEmpty = true;
		}
		cout << "清空成功!" << endl;
	}
	system("pause");
	system("cls");
}

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值