用C++实现职工管理系统

一、系统功能

系统主要运用类和对象的知识,涉及文件的读取等操作,代码实现参考黑马程序员的课程。

二、职工管理类

在面向对象程序设计中,需要定义类来实现各种功能,首先定义 WorkManger类,在头文件声明各种类的属性,保存在 workManger.h 中。

#pragma once  //防止头文件重复包含
#include <iostream>
using namespace std;  //使用标准命名空间
#include "worker.h"
#include <fstream>

class WorkManger
{
public:
	WorkManger();

	void showmenu();

	void exitsystem();

	void add_information();

	int numEmployee;  //职工数量

	Worker** a_empl;  // 职工数组指针

	void savef();

	bool FileIsEmpty;

	int getfileNum();

	void initEmploy();

	void showEmp();

	void delEmp();

	void modEmp();

	void findEmp();

	void sortEmp();

	void clearData();

	int isExitEmp(int id); // 判断职工是否存在,存在就返回idex,不存在返回-1

	~WorkManger();

};



源文件workManger.cpp进行实现,类内声明,类外实现。

#include "workManager.h"
#include "threework.h"

WorkManger::WorkManger()
{
	//1.文件不存在
	ifstream ifs;
	ifs.open("empfile.txt", ios::in);
	if (!ifs.is_open())
	{
		//cout << "文件不存在"<<endl;
		this->numEmployee = 0;
		this->a_empl = NULL;
		this->FileIsEmpty = true;
		ifs.close();
		return;

	}
	// 2.文件存在但为空
	char c;
	ifs >> c;
	if (ifs.eof())
	{
		//cout << "文件为空"<<endl;
		this->numEmployee = 0;
		this->a_empl = NULL;
		this->FileIsEmpty = true;
		ifs.close();
		return;
	}
	// 3. 文件存在不为空
	int num = this->getfileNum();
	//cout << "文件保存的人数为:" << num << endl;
	this->numEmployee = num;
	this->a_empl = new Worker * [this->numEmployee];
	this->initEmploy();
}

void WorkManger::showmenu()
{
	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;

}

void WorkManger::exitsystem()
{
	cout << "你已退出系统" << endl;
	system("pause");
	exit(0);
}
void WorkManger::add_information()
{
	cout << "输入添加员工的个数:";
	int addnum = 0;
	cin >> addnum;
	if (addnum > 0)
	{
		int newsize = this->numEmployee + addnum;
		Worker** newspace = new Worker * [newsize];
		//将原空间下的数组,拷贝到新空间下
		if (this->a_empl != NULL)
		{
			for (int i = 0; i < this->numEmployee; i++)
			{
				newspace[i] = this->a_empl[i];
			}
		}

		//添加新数据
		for (int i = 0; i < addnum; i++)
		{
			int id = 0;
			string name = "";
			int bumen = 0;
			cout << "请输入第" << i + 1 << "个员工的编号:";
			cin >> id;
			cout << "请输入第" << i + 1 << "个员工的姓名:";
			cin >> name;
			cout << "请输入第" << i + 1 << "个员工的部门:(1.职工 2.经理 3.老板,填数字)";
			cin >> bumen;
			Worker* worker = NULL;
			switch (bumen)
			{
			case 1:
				worker = new Employee(id, bumen, name);
				break;
			case 2:
				worker = new Manager(id, bumen, name);
				break;
			case 3:
				worker = new Boss(id, bumen, name);
				break;
			default:
				break;
			}
			newspace[this->numEmployee + i] = worker;
		}
		delete[] this->a_empl;
		this->a_empl = newspace;
		this->numEmployee = newsize;
		this->FileIsEmpty = false;
		//保存到文件中
		this->savef();

		cout << "成功添加" << addnum<< "个员工" << endl;
	}
	else
	{
		cout << "输入有误!";
	}
	system("pause");
	system("cls");  //清屏,返回上级目录
}

void WorkManger::savef()  //保存文件
{
	ofstream ofs;
	ofs.open("empfile.txt", ios::out);
	for (int i = 0; i < this->numEmployee; i++)
	{
		ofs << this->a_empl[i]->idWork << " "
			<< this->a_empl[i]->nameWorker << " "
			<< this->a_empl[i]->idComp << endl;
	}
	ofs.close();
}

int WorkManger::getfileNum()
{
	ifstream ifs;
	ifs.open("empfile.txt", ios::in);
	int id;
	string name;
	int offer;
	int num = 0;
	while (ifs >> id && ifs >> name && ifs >> offer)
	{
		num++;
	}
	ifs.close();
	return num;
}

void WorkManger::initEmploy()
{
	ifstream ifs;
	ifs.open("empfile.txt", ios::in);
	int id;
	string name;
	int offer;
	int idex = 0;
	while (ifs >> id && ifs >> name && ifs >> offer)
	{
		Worker* worker = NULL;
		if (id == 1) {
			worker = new Employee(id, offer, name);

		}
		else if (id == 2)
		{
			worker = new Manager(id, offer, name);
		}
		else
		{
			worker = new Boss(id, offer, name);
		}
		this->a_empl[idex] = worker;
		idex++;
	}
	ifs.close();
}

void WorkManger::showEmp()
{
	if (this->FileIsEmpty)
	{
		cout << "文件为空,不存在." << endl;
	}
	else
	{
		for (int i = 0; i < this->numEmployee; i++)
		{
			this->a_empl[i]->showWorker();
		}
	}
	system("pause");
	system("cls");  //清屏,返回上级目录
}


void WorkManger::delEmp()
{
	if (this->FileIsEmpty)
	{
		cout << "文件不存在" << endl;
	}
	else
	{
		cout << "要删除的职工编号: ";
		int id = 0;
		cin >> id;
		if (this->isExitEmp(id) != -1)
		{
			for (int i = this->isExitEmp(id); i < this->numEmployee; i++)  // 或者numEmployee-1
			{
				this->a_empl[i] = this->a_empl[i + 1];
				
			}
			this->numEmployee--;
			this->savef();
			cout << "删除成功!" << endl;
		}
		else
		{
			cout << "未找到职工" << endl;
		}
		
	}
	system("pause");
	system("cls");  //清屏,返回上级目录
}

void WorkManger::modEmp()
{
	if (this->FileIsEmpty)
	{
		cout << "文件不存在" << endl;
	}
	else
	{
		cout << "要修改的职工编号: ";
		int id = 0;
		cin >> id;
		int ret = this->isExitEmp(id);
		if (ret != -1)
		{
			delete this->a_empl[ret];
			//this->a_empl[this->isExitEmp(id)] = NULL;
			int newid = 0;
			string name = "";
			int bumen = 0;
			cout << "请输入员工的编号:";
			cin >> newid;
			cout << "请输入员工的姓名:";
			cin >> name;
			cout << "请输入员工的部门:(1.职工 2.经理 3.老板,填数字)";
			cin >> bumen;
			Worker* worker = NULL;
			switch (bumen)
			{
			case 1:
				worker = new Employee(newid, bumen, name);
				this->a_empl[ret] = worker;  //放case里,程序就不崩溃了
				break;
			case 2:
				worker = new Manager(newid, bumen, name);
				this->a_empl[ret] = worker;
				break;
			case 3:
				worker = new Boss(newid, bumen, name);
				this->a_empl[ret] = worker;
				break;
			default:
				break;
			}
			

			this->savef();
			cout << "修改成功!" << endl;
		}
		else
		{
			cout << "失败,未找到职工" << endl;
		}

	}
	system("pause");
	system("cls");  //清屏,返回上级目录

}

int WorkManger::isExitEmp(int id)
{
	int idex = -1;
	for (int i = 0; i < this->numEmployee; i++)
	{
		if (this->a_empl[i]->idWork == id)
		{
			idex = i;
			break;
		}
	}
	return idex;
}

void WorkManger::findEmp()
{
	if (this->FileIsEmpty)
	{
		cout << "文件不存在" << endl;
	}
	else
	{
		cout << "按编号查找还是按姓名查找(1.编号,2.姓名): ";
		int select = 0;
		cin >> select;
		if (select == 1)
		{
			cout << "要查找的职工编号: ";
			int id = 0;
			cin >> id;
			if (this->isExitEmp(id) != -1)
			{
				cout << "查找成功,找到职工信息如下:" << endl;
				this->a_empl[this->isExitEmp(id)]->showWorker();
			}
			else
			{
				cout << "失败,未找到职工" << endl;
			}
		}
		else if (select == 2)
		{
			bool flag = false;
			cout << "要查找的职工姓名: ";
			string name = "";
			cin >> name;
			for (int i = 0; i < this->numEmployee; i++)
			{
				if (this->a_empl[i]->nameWorker == name)
				{
					cout << "查找成功,找到职工信息如下:" << endl;
					this->a_empl[i]->showWorker();
					flag = true;
				}

			}
			if (flag == false)
			{
				cout << "失败,未找到职工" << endl;
			}
		}
		else
		{
			cout << "输入有误" << endl;
		}
	}
	system("pause");
	system("cls");  //清屏,返回上级目录
}

void WorkManger::sortEmp()
{
	if (this->FileIsEmpty)
	{
		cout << "文件不存在" << endl;
		system("pause");
		system("cls");  //清屏,返回上级目录
	}
	else
	{
		cout << "升序还是降序(1.升序,2.降序): ";
		int select = 0;
		cin >> select;
		for (int i = 0; i < this->numEmployee; i++)
		{
			int minORmax = i;
			for (int j = i + 1; j < this->numEmployee; j++)
			{
				if (select == 1)  //升序
				{
					if (this->a_empl[minORmax]->idWork > this->a_empl[j]->idWork)
					{
						minORmax = j;
					}
				}
				else  //降序
				{
					if (this->a_empl[minORmax]->idWork < this->a_empl[j]->idWork)
					{
						minORmax = j;
					}
				}
			}
			//交换
			if (i != minORmax)
			{
				Worker* temp = this->a_empl[i];
				this->a_empl[i] = this->a_empl[minORmax];
				this->a_empl[minORmax] = temp;
			}
		}
		cout << "排序后的结果为:" << endl;
		this->savef();
		this->showEmp();
	}
}

void WorkManger::clearData()
{
	cout << "确认清空数据吗(1.确定,2.我再想想): ";
	int select = 0;
	cin >> select;
	if (select == 1)
	{
		ofstream ofs("empfile.txt", ios::trunc); //删除文件并重新创建
		ofs.close();
		if (this->a_empl != NULL)
		{
			for (int i = 0; i < this->numEmployee; i++)
			{
				delete this->a_empl[i];
				this->a_empl[i] = NULL;

			}
			delete[] this->a_empl;
			this->a_empl = NULL;
			this->numEmployee = 0;
			this->FileIsEmpty = true;
		}
		cout << "清空成功" << endl;

	}
	system("pause");
	system("cls");  //清屏,返回上级目录
}

WorkManger::~WorkManger()
{
	if (this->a_empl != NULL)
	{
		for (int i = 0; i < this->numEmployee; i++)
		{
			delete this->a_empl[i];
			this->a_empl[i] = NULL;
		}
		delete[] this->a_empl;
		this->a_empl = NULL;
	}
}

里面包括系统要求的所有功能的实现。

三、三种职员的类

有三种职员:1.职工 2.经理 3.老板,各司其职。

首先定义一个父类,来实现他们的共有特性,在 worker.h 文件中。

#pragma once
#include <iostream>
#include <string>
using namespace std;  //使用标准命名空间

class Worker
{
public:

	virtual void showWorker() = 0;  //显示个人信息
	virtual string getName() = 0;  //获取岗位名称
	
	int idWork;
	int idComp;
	string nameWorker;



};

之后三种职员继承父类的属性,并在 threework.h 声明。

#pragma once
#include <iostream>
#include <string>
#include "worker.h"
using namespace std;  //使用标准命名空间

class Employee: public Worker
{
public:
	Employee(int idWork, int idComp, string nameWorker);
	string workfunc;
	virtual void showWorker();  //显示个人信息
	virtual string getName();  //获取岗位名称

};

class Manager : public Worker
{
public:
	Manager(int idWork, int idComp, string nameWorker);
	string workfunc;
	virtual void showWorker();  //显示个人信息
	virtual string getName();  //获取岗位名称

};


class Boss : public Worker
{
public:
	Boss(int idWork, int idComp, string nameWorker);
	string workfunc;
	virtual void showWorker();  //显示个人信息
	virtual string getName();  //获取岗位名称

};

在 threework.cpp 源文件中实现,类内声明,类外实现。

#include "threework.h"

Employee::Employee(int idWork, int idComp ,string nameWorker)
{
	this->idWork = idWork;
	this->idComp = idComp;
	this->nameWorker = nameWorker;
	this->workfunc = "完成经理交给的任务";
}

void Employee::showWorker()
{
	cout << "编号: " << this->idWork << "  ";
	cout << "岗位: " << this->getName() << "  ";
	cout << "姓名: " << this->nameWorker << "  ";
	cout << "职责: " << this->workfunc << endl;
}
string Employee::getName()
{
	return "职工";
}

//---------
Manager::Manager(int idWork, int idComp, string nameWorker)
{
	this->idWork = idWork;
	this->idComp = idComp;
	this->nameWorker = nameWorker;
	this->workfunc = "完成老板交给的任务,并下发给职工";
}

void Manager::showWorker()
{
	cout << "编号: " << this->idWork << "  ";
	cout << "岗位: " << this->getName() << "  ";
	cout << "姓名: " << this->nameWorker << "  ";
	cout << "职责: " << this->workfunc << endl;
}
string Manager::getName()
{
	return "经理";
}

//---------
Boss::Boss(int idWork, int idComp, string nameWorker)
{
	this->idWork = idWork;
	this->idComp = idComp;
	this->nameWorker = nameWorker;
	this->workfunc = "管理公司所有事物";
}

void Boss::showWorker()
{
	cout << "编号: " << this->idWork << "  ";
	cout << "岗位: " << this->getName() << "  ";
	cout << "姓名: " << this->nameWorker << "  ";
	cout << "职责: " << this->workfunc << endl;
}
string Boss::getName()
{
	return "总裁";
}

四、主函数调用

最后在主函数中调用,实现系统设计。

#include <iostream>
#include "workManager.h"
using namespace std;  //使用标准命名空间

int main()
{
	
	int choice = 0;
	while (true)
	{
		WorkManger wm;
		wm.showmenu();
		cout << "请选择功能:";
		cin >> choice;

		switch (choice) {
		case 0:  //退出
			wm.exitsystem();
			break;
		case 1://增加
			wm.add_information();
			break;
		case 2:  //显示
			wm.showEmp();
			break;
		case 3:  //删除
			wm.delEmp();
			break;
		case 4:  //修改
			wm.modEmp();  
			break;
		case 5:  //查找
			wm.findEmp();
			break;
		case 6:  //排序
			wm.sortEmp();
			break;
		case 7:  //清空
			wm.clearData();
			break;
		default:  //清屏
			system("cls");
			break;
		}
	}

	system("pause");
	return 0;
}

终端如下,具体操作可自行探索

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

 ☆cwlulu

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值