c++职工管理系统

我简单的实现了一个c++的职工管理系统。

有 ofstream,ifstream 的运用,多态的运用,虚函数,纯虚函数的运用 , 堆区的开辟和空间释放的运用和 数组指针的运用

下面是实现的功能。

void WorkManage::showMenu()
{
	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;
}

创建了职工管理系统.hWorkManage.hWorkManage.cpp    worker.h   和employee.h   employee.cpp   manager.h manager.cpp boss.h boss.cpp

worker是父类,子类有员工,经理 ,老板 对应 employee ,manager,,boss。

workmanage.h的代码为

#pragma once
#include<iostream>
using namespace std;
#include"Worker.h"
#include<fstream>
#define filename "empfile.txt"
class WorkManage
{
public:
	WorkManage();
	void showMenu();//展示菜单
	void Exitsystem();//退出系统
	int Empnum;//记录职工数
	Worker** Emparray;//职工数组指针
	void Addemp();//添加职工
	void  save();//保存文件
	bool fileempty;//判断文件是否存在
	//统计文件人数
	int get_EmpNum();
	//初始化员工
	void init_Emp();
	//显示职工
	void show_Emp();
	//判断是否有这个职工
	int isExist(int id);
	//删除职工
	void Del_Emp();
	//修改职工信息
	void Mod_Emp();
	//查找职工
	void Find_Emp();
	//排序职工
	void sort_Emp();
	//清空文件
	void Clean_File();
	~WorkManage();
};

workmanage.cpp的代码为

#pragma once
#include<iostream>
using namespace std;
#include "workmanage.h"
#include"employee.h"
#include"manager.h"
#include"boss.h"
WorkManage::WorkManage()
{
	//文件不存在
	ifstream ifs(filename, ios::in);
	if (!ifs.is_open())
	{
		cout << "文件不存在" << endl;
		Empnum = 0;
		Emparray = NULL;
		fileempty = true;
		ifs.close();
		return;
	}
	//文件存在,数据为空
	char ch;
	ifs >> ch;
	if (ifs.eof())
	{
		cout << "文件为空" << endl;
		Empnum = 0;
		Emparray = NULL;
		fileempty = true;
		ifs.close();
		return;
	}
	//文件存在并且有数据
	int num = get_EmpNum();
	//  cout << "职工人数为:" << num << endl;//测试代码
	Empnum = num;

	//开辟空间
	Emparray = new Worker * [Empnum];
	//把文件的数据存到数组中
	init_Emp();

	
}
WorkManage::~WorkManage()
{
	if (Emparray != NULL)
	{
		for (int i = 0; i < Empnum; i++)
		{
			if (Emparray[i] != NULL)
			{
				delete Emparray[i];
			}
		}
	}
}
//显示菜单
void WorkManage::showMenu()
{
	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 WorkManage::Addemp()
{
	cout << "请输入添加职工数量:";
	int addnum; cin >> addnum;
	if (addnum > 0)
	{
		int newsize = Empnum + addnum;//原来的人数加上新增的人数
		Worker**newspace=new Worker* [newsize];//开辟新空间
		//把原来的数据拷贝到更新的空间
		if (Emparray != NULL)
		{
			for (int i = 0; i < Empnum; i++)
			{
				newspace[i] = Emparray[i];
			}
		}
		for (int i = 0; i < addnum; i++)
		{
			int id; string name; int deptid;
			cout << "请输入第" << i + 1 << "个新员工的编号" << endl;
			cin >> id;
			cout << "请输入第" << i + 1 << "个新员工的姓名" << endl;
			cin >> name;
			cout << "请选择岗位:" << endl;
			cout << "1.普通职工" << endl;
			cout << "2.经理" << endl;
			cout << "3.老板" << endl;
			while (1)
			{

				cin >> deptid;
				if (deptid == 1 || deptid == 2 || deptid == 3)
				{
					break;
				}
				else
				{
					cout << "输入有误,请重新输入" << endl;
				}
					
			}
			Worker* worker = NULL;
			switch (deptid)
			{
				case 1:
					worker = new Employee(id, name, deptid);
					break;
				case 2:
					worker = new Manager(id, name, deptid);
					break;
				case 3:
					worker = new Boss(id, name, deptid);
					break;
				default:
					break;
			}
			int a = Empnum + i;
			if (a <= newsize)
			{
				newspace[a] = worker;
			}
			
		}
		//释放原有空间
		delete[]Emparray;
		//更改新空间指向
		Emparray = newspace;

		Empnum = newsize;
		cout << "成功添加" << addnum << "个人" << endl;
		save();//保存文件
		//更新文件职工不为空
		fileempty = false;
	}
	else
	{
		cout << "数据有误" << endl;
	}
	system("pause");
	system("cls");
}
//保存数据到文件
void WorkManage::save()
{
	ofstream ofs(filename,ios::out);
	for (int i = 0; i < Empnum; i++)
	{
		ofs << Emparray[i]->id << "   "
			<< Emparray[i]->name << "   "
			<< Emparray[i]->deptid << endl;
	}
	ofs.close();
}
//获取文件里的职工人数
int WorkManage::get_EmpNum()
{
	ifstream ifs(filename, ios::out);
	int num = 0; int id; string name; int did;
	while (ifs >> id && ifs >> name && ifs >> did)
	{
		num++;
	}
	return num;
}
//当文件里有职工时,把文件数据初始化到这边
void WorkManage::init_Emp()
{
	ifstream ifs(filename,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);
		}
		Emparray[index] = worker;
		index++;
	}
	ifs.close();
}
//显示职工
void WorkManage:: show_Emp()
{
	if (fileempty)
	{
		cout << "文件不存在" << endl;
	}
	else
	{
		for (int i = 0; i < Empnum; i++)
		{
			 Emparray[i]->showinfo();
		}
	}
	system("pause");
	system("cls");
}

//判断是否存在这个职工
int WorkManage::isExist(int id)
{
	for (int i = 0; i < Empnum; i++)
	{
		if (Emparray[i]->id == id)
		{
			return i;
		}
	}
	return -1;
}
//删除职工
void WorkManage:: Del_Emp()
{
	if (fileempty)
	{
		cout << "文件不存在" << endl;
	}
	else
	{
		cout << "请输入想要删除的编号";
		int id; cin >> id;
		int a = isExist(id);
		if (a != -1)
		{
			for (int i = a; i < Empnum - 1; i++)
			{
				Emparray[i] = Emparray[i + 1];
			}
			Empnum--;
			save();
			cout << "删除成功" << endl;
		}
		else
		{
			cout << "不存在这个人,删除失败" << endl;
		}
		system("pause");
		system("cls");
	}
}

//修改职工信息
void WorkManage::Mod_Emp()
{
	if (fileempty)
	{
		cout << "文件不存在" << endl;
	}
	else
	{
		cout << "输入要修改人的编号";
		int a; cin >> a;
		int index = isExist(a);
		if (index != -1)
		{
			delete Emparray[index];
			cout << "查到此人" << endl;
			int id; string name; int did;
			cout << "输入新编号";
			cin >> id;
			cout << "输入新姓名";
			cin >> name;
			cout << "1.普通职工" << endl;
			cout << "2.经理" << endl;
			cout << "3.老板" << endl;
			cout << "输入新职位";
			while (1)
			{

				cin >> did;
				if (did == 1 || did == 2 || did == 3)
				{
					break;
				}
				else
				{
					cout << "输入有误,请重新输入" << endl;
				}

			}
			Worker* worker = NULL;
			switch (did)
			{
			case 1:
				worker = new Employee(id, name, did);
				break;
			case 2:
				worker = new Manager(id, name, did);
				break;
			case 3:
				worker = new Boss(id, name, did);
				break;
			default:
				break;
			}
			Emparray[index] = worker;
			cout << "修改成功" << endl;
			save();
		}
		else
		{
			cout << "查无此人" << endl;
		}
	}
	system("pause");
	system("cls");
}

//查找职工
void WorkManage:: Find_Emp()
{
	if (fileempty)
	{
		cout << "文件不存在" << endl;
	}
	else
	{
		cout << "输入要查找的方式" << endl;
		cout << "1.职工编号寻找" << endl;
		cout << "2.职工姓名寻找" << endl;
		int a; 
		while (1)
		{
			cin >> a;
			if (a == 1 || a == 2)
			{
				break;
			}
			else
			{
				cout << "输入错误,请重新输入" << endl;
			}
		}
		if (a == 1)
		{
			cout << "输入寻找人的编号";
			int b; cin >> b;
			int ret = isExist(b);
			if (ret != -1)
			{
				cout << "找到了,职工信息为:" << endl;
				Emparray[ret]->showinfo();
			}
			else
			{
				cout << "查无此人" << endl;
			}
		}
		else if (a == 2)
		{
			cout << "输入查找的姓名" << endl;
			string name; cin >> name;
			int i = 0;
			for ( i = 0; i < Empnum; i++)
			{
				if (Emparray[i]->name == name)
				{
					cout << "找到了此人,职工信息为:" << endl;
					Emparray[i]->showinfo();
					break;
				}
			}
			if (i == Empnum)
			{
				cout << "查无此人" << endl;
			}
		}
		else
		{
			cout << "输入有误" << endl;
		}
	}
	system("pause");
	system("cls");
}
//职工排序
void WorkManage::sort_Emp()
{
	if (fileempty)
	{
		cout << "文件不存在或为空" << endl;
		system("pause");
		system("cls");
	}
	//冒泡排序
	cout << "选择排序方法" << endl;
	cout << "1.按职工号升序" << endl;
	cout << "2.按职工号降序" << endl;
	int a; 
	while (1)
	{
		cin >> a;
		if (a == 1 || a == 2)
		{
			break;
		}
		else
		{
			cout << "输入错误,请重新输入" << endl;
		}
	}
	if (a == 1)
	{
		
		for (int i = 0; i < Empnum-1; i++)
		{
			for (int j = 0; j<Empnum - i - 1; j++)
			{
				if (Emparray[j]->id > Emparray[j + 1]->id)
				{
					Worker* worker = Emparray[j+1];
					Emparray[j + 1] = Emparray[j];
					Emparray[j] = worker;
				}
			}
		}
	}
	else if (a == 2)
	{
		for (int i = 0; i < Empnum - 1; i++)
		{
			for (int j = 0; j<Empnum-i-1; j++)
			{
				if (Emparray[j]->id < Emparray[j + 1]->id)
				{
					Worker* worker = Emparray[j + 1];
					Emparray[j + 1] = Emparray[j];
					Emparray[j] = worker;
				}
			}
		}
	}
	cout << "排序成功" << endl;
	save();
	show_Emp();
}
//清空文件
void WorkManage::Clean_File()
{
	cout << "确定退出?" << endl;
	cout << "1.确定" << endl;
	cout << "2.返回" << endl;
	int a;
	while (1)
	{
		cin >> a;
		if (a == 1 || a == 2)
		{
			break;
		}
		else
		{
			cout << "输入错误,请重新输入" << endl;
		}
	}
	if (a == 1)
	{
		//ios::trunc ,如果存在,删除文件并重新创建
		ofstream ofs(filename, ios::trunc);
		ofs.close();
		if (Emparray != NULL)
		{
			//删除堆区的每个职工对象
			for (int i = 0; i < Empnum; i++)
			{
				if (Emparray[i] != NULL)
				{
					delete Emparray[i];
				}
			}
		}
		//删除堆区数组指针
         delete[] Emparray;
		 Empnum = 0;
		 Emparray = NULL;
		 fileempty = true;
		 cout << "清空成功" << endl;
	}
	
	
		system("pause");
		system("cls");

	
}
//退出程序
void WorkManage::Exitsystem()
{
	cout << "欢迎下次使用" << endl;
	system("pause");
	exit(0);
}

职工管理系统.h的代码为

#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
using namespace std;
#include"WorkManage.h"
#include"Worker.h"
#include"employee.h"
#include"manager.h"
#include"boss.h"

//嘿嘿黑,伟大的推力之神,马+奇啊,赐予我力量

int main()
{
	WorkManage wm;
	int choice = 0;//用来存储用户的选项

	//测试
	//Worker* worker = new Employee(1, "张三", 1);
	//worker->showinfo();
	//delete worker;
	//worker = new Manager(2, "李四", 2);
	//worker->showinfo();
	//delete worker;
	//worker = new Boss(3, "王五", 3);
	//worker->showinfo();
	//delete worker;

	while(1)
	{
		wm.showMenu();
		cout << "请选择" << endl;
		cin >> choice;
		switch (choice)
		{
			case 0://退出职工
				wm.Exitsystem();
				break;
			case 1://增加职工
				wm.Addemp();
				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 0;
}

worker.h的代码为

#pragma once
#include<iostream>
using namespace std;
#include<string>
//职工的抽象类
class Worker
{
public:
	//显示个人信息
	virtual void showinfo() = 0;
	//获取岗位名称
	virtual string getDeptname() = 0;//纯虚函数
	int id;
	string name;
	int deptid;
};

employee.h 的代码为

#pragma once
#include<iostream>
using namespace std;
#include"worker.h"
class Employee:public Worker
{
public:

	Employee(int id,string name,int did);
	virtual void showinfo();
	virtual string getDeptname();
};

employee.cpp的代码为

#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
using namespace std;
#include"employee.h"
Employee::Employee(int id,string name,int did)
{
	this->id = id;
	this->name = name;
	this->deptid = did;
}

void Employee::showinfo()
{
	cout << "职工编号:" << id;
	cout << "\t职工姓名:" << name;
	cout << "\t职工岗位:" << getDeptname() << endl;;
}
string Employee::getDeptname()
{
	return string("普通职工");
}

manager.h的代码为

#pragma once
#include<iostream>
using namespace std;
#include"worker.h"
class Manager :public Worker
{
public:
	Manager(int id, string name, int did);
	virtual void showinfo();
	virtual string getDeptname();
};

manager.cpp的代码为

#define _CRT_SECURE_NO_WARNINGS
#include"manager.h"

Manager::Manager(int id, string name, int did)
{
	this->id = id;
	this->name = name;
	this->deptid = did;
}
 void Manager::showinfo()
{
	 cout << "职位编号:" << id;
	 cout << "\t职位姓名:" << name;
	 cout << "\t职工岗位:" << getDeptname() << endl;
 }
string Manager::getDeptname()
{
	return string("经理");
 }

boss.h的代码为

#pragma once
#include<iostream>
using namespace std;
#include"worker.h"
class Boss :public Worker
{
public:
	Boss(int id, string name, int did);
	virtual void showinfo();
	virtual string getDeptname();
};

boss.cpp的代码为

#pragma once
#include"Boss.h"
Boss::Boss(int id, string name, int did)
{
	this->id = id;
	this->name = name;
	this->deptid = did;
}
void Boss::showinfo()
{
	cout << "职位编号:" << id;
	cout << "\t职位姓名:" << name;
	cout << "\t职工岗位:" << getDeptname() << endl;
}
string Boss::getDeptname()
{
	return string("老板");
}
  • 10
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值