c++小项目: 基于多态的职工管理系统

c++小项目: 基于多态的职工管理系统

一. 项目目的

运用c++实现一个基于多态的职工管理系统

该系统可以存入、显示、寻找、排序、修改和删除职工等,模拟一个公司职工情况

该系统具有员工、经理和总裁三个职务,各司其职

我们将在系统中存入一个个职工,并赋予其各自的职工编号、名字和职务

二. 项目实现

1. 创建项目雏形

创建一个源文件"职工管理系统.cpp"

        int input;
	while (1)
	{
		cout << "请输入你的选择:>  ";
		cin >> input;
		switch (input)
		{
		case 0:          //退出职工管理系统
			break;
		case 1:         //添加职工信息
			break;
		case 2:         //显示职工信息
			break;
		case 3:         //删除职工信息
			break;
		case 4:         //修改职工信息
			break;
		case 5:         //查找职工信息 
			break;
		case 6:         //排序职工
			break;
                case 7:         //清空所有文档(破产跑路
			break;
		default:
			break;
		}
	}

建立这么一个雏形之后,我们将根据各种功能编写代码

2. 建立菜单

首先,我们需创建一个菜单选项供人选择功能

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;
}

我们可以创建一个头文件"workermanage.h"

并在里面创建一个类"workermanage", 将菜单放进去

#pragma once         //该代码作用是防止头文件重复包含
using namespace std;
class Workermanager
{
public:
void Show_menu(); //展示菜单
~Workermanager();
}

然后创建一个源文件"workermanage.cpp"

#include"workermanager.h"
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;
}

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

这样第一步就完成了

3. 实现退出功能

此时我们可以先在头文件中“workermanage.h"中声明一个退出函数

class Workermanager
{
public:
void Show_menu();
void Exit();     //退出系统
~Workermanager();
}

然后在源文件"workermanage.cpp"中定义该函数

void Workermanager::Exit()
{
	cout << "已退出职工管理系统,欢迎下次使用" << endl;
	system("pause");    //该代码起暂停程序的作用,然后按任意键继续
	exit(0);
}

这样就完成了第二步

4. 定义职务

首先我们可以创建一个头文件"worker.h"

在该头文件中创建一个类"worker",在类中我们可以实现显示职工信息、获取岗位信息以及对职工编号、名字和岗位信息的声明

#pragma once
#include<iostream>
#include<string>
using namespace std;
class worker
{
public:
	virtual void Showinfo() = 0;      //显示个人信息
	virtual string GetDeptName() = 0; //获取岗位信息
	int Id;                           //职工编号
	string Name;                      //职工名字
	int Deptid;                       //岗位信息
};

以员工为例,我们可先创建一个头文件"employee.h",在头文件里面创建一个类"employee"

#pragma once
#include"worker.h"
using namespace std;
class employee :public worker
{
public:
	employee(int id,string name,int deptid);//函数名需要与类名一致,或者容易出错
	void Showinfo();                       
        //记得删掉virtual,否则worlermanager.cpp会出错
	string GetDeptName();
};

然后创建一个源文件"employee.cpp",在里面定义employee(int id,string name,int deptid)、void Showinfo()和string GetDeptName()

#include"employee.h"
employee::employee(int id, string name, int deptid)
{
	this->Id = id;
	this->Name = name;
	this->Deptid = deptid;
}

void employee::Showinfo()
{
	cout << "职工编号: " << this->Id
	     << "\t职工姓名: " << this->Name
	     << "\t岗位: " << this->GetDeptName()
	     << "\t岗位职责: 完成公司派发的任务" << endl;
}

string employee::GetDeptName()
{
	return string("员工");
}

经理和总裁同理

经理

“manage.h”

#pragma once
#include"worker.h"
using namespace std;
class manage :public worker
{
public:
	manage(int id, string name, int deptid);
	void Showinfo();
	string GetDeptName();
};

“manage.cpp”

#include"manage.h"

manage::manage(int id, string name, int deptid)
{
	this->Id = id;
	this->Name = name;
	this->Deptid = deptid;
}
void manage::Showinfo()
{
	cout << "职工编号: " << this->Id
	     << "\t职工姓名: " << this->Name
	     << "\t岗位: " << this->GetDeptName()
	     << "\t岗位职责: 下放任务给员工,并听从总裁安排" << endl;
}
string manage::GetDeptName()
{
	return string("经理");
}

总裁
“boss.h”

#pragma once
using namespace std;
#include"worker.h"
class boss :public worker
{
public:
	boss(int id, string name, int deptid);
	void Showinfo();
	string GetDeptName();
};

“boss.cpp”

#include"boss.h"
boss::boss(int id, string name, int deptid)
{
	this->Id = id;
	this->Name = name;
	this->Deptid = deptid;
}
void boss::Showinfo()
{
	cout << "职工编号: " << this->Id
		<< "\t职工姓名: " << this->Name
		<< "\t岗位: " << this->GetDeptName()
		<< "\t岗位职责: 管理公司各项事务" << endl;
}
string boss::GetDeptName()
{
	return string("总裁");
}

5. 添加职工

首先我们需要创建一个文本来存在你所添加的职工信息

并声明一个函数来判断文件是否存在

我们可以在头文件"workermanage.h"中定义一个名词来指代该文本

同时声明职工人数和职工数组指针以及添加职工的函数

最后我们需要保存文件

#pragma once //防止头文件重复包含
#include"worker.h"
#include"employee.h"
#include"boss.h"
#include"manage.h"
#include<fstream>
#define FILENAME "...txt"   //文本名字可自己任取
using namespace std;

class Workermanager
{
public:
	Workermanager();    //判断文本是否存在
	int Empnum;         //职工人数
	worker **EmpArray;  //职工数组指针
	void Add_Emp();     //添加职工
        bool FileIsEmpty;   //判断文本是否为空
        void init_Emp();    //初始化职工文本
	int GetEmpnum();    //统计人数
        void save();        //保存文件
        ~Workermanager()};

在源文件"workermanage.cpp"中定义

void Workermanager::init_Emp()
{
	ifstream ifs;
	ifs.open(FILENAME, ios::in);
	int id;
	string name;
	int deptid;
	int index = 0;
	while (ifs >> id&&ifs >> name&&ifs >> deptid)
	{
		worker *worker = NULL;
		if (deptid == 1)
		{
			worker = new employee(id, name, deptid);
		}
		else if (deptid == 2)
		{
			worker = new manage(id, name, deptid);
		}
		else
		{
			worker = new boss(id, name, deptid);
		}
		this->EmpArray[index] = worker;
		index++;
	}
	ifs.close();
}


int Workermanager::GetEmpnum()
{
	ifstream ifs;
	ifs.open(FILENAME, ios::in);  //读取文本里的职工信息
	int id;
	string name;
	int deptid;
	int num=0;
	while (ifs >> id&&ifs >> name&&ifs >> deptid)
	{
		num++;
	}
	return num;
}

Workermanager::Workermanager()
{
	ifstream ifs;
	ifs.open(FILENAME, ios::in);
	//判断文件是否存在
	if (!ifs.is_open())
	{
		cout << "文件不存在" << endl;
		this->Empnum = 0;        //初始化职工人数为0
		this->EmpArray = NULL;   //初始化职工数组指针
		this->FileIsEmpty = true;
		ifs.close();
		return;
	}
	// 判断文件是否为空
	char ch;
	ifs >> ch;  //从文本第一个字符开始读取,若第一个字符不为0,则该文本不为空
	if (ifs.eof())
	{
		cout << "文件为空" << endl;
		this->Empnum = 0; 
		this->EmpArray = NULL;
		this->FileIsEmpty = true;
		ifs.close();
		return;
	}
	int num = this->GetEmpnum();
	cout << "职工人数为: " << num << endl;
	this->Empnum = num;
	this->EmpArray = new worker*[this->Empnum];  //开辟一个空间
	this->init_Emp();                            //将文件数据存到数组中
	for (int i = 0; i < this->Empnum; i++)
	{
		cout << "职工编号: " << this->EmpArray[i]->Id
		     << "  职工名字: " << this->EmpArray[i]->Name
		     << "  职工部门编号: " << this->EmpArray[i]->Deptid << endl;
	}
}

void Workermanager::save()
{
	ofstream ofs;
	ofs.open(FILENAME, ios::out);
	for (int i = 0; i < this->Empnum; i++)
	{
           //将数据保存进文件中
           ofs << this->EmpArray[i]->Id << " "
	       << this->EmpArray[i]->Name << " "
	       << this->EmpArray[i]->Deptid << endl;
		ofs.close();
	}
}

void Workermanager::Add_Emp()
{
	cout << "请输入添加职工数量: " << endl;
	int addNum = 0;
	cin >> addNum;
	if (addNum > 0)
	{
		int newSize = this->Empnum + addNum; //新空间人数=原来的+新的
		worker**newSpace=new worker*[newSize];
                //创建一个新空间,并确定该空间人数
		if (this->EmpArray != NULL)
		{
			for (int i = 0; i < this->Empnum; i++) //将原数据并入新空间
			{
				newSpace[i] = this->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 manage(id, name, 2);
				break;
			case 3:
				worker = new boss(id, name, 3);
				break;
			default:
				break;
			}
			newSpace[this->Empnum + i] = worker; //将数据存入新数组中
		}
		delete[] this->EmpArray; //释放原有空间
		this->EmpArray = newSpace; //更改新空间的指向
		this->Empnum = newSize; //更新新的职工人数
		this->FileIsEmpty = false; //更新职工不为空标志
		cout << "成功添加" << addNum << "名新职工" << endl;
		this->save(); //保存数据到文件中
	}
	else
	{
		cout << "输入数据有误" << endl;
	}
	system("pause");
	system("cls"); //进行清屏操作
}

6. 显示职工信息

在定义职务中我们已经将显示各自职工信息的函数定义完毕

我们此时只需在头文件"workermanage.h"中声明一个显示职工信息函数

class Workermanager
{
public:
      void show_Emp();  //显示职工
      ~Workermanager();
}

在源文件"workermanage.cpp"中定义

void Workermanager::show_Emp()
{
	if (this->FileIsEmpty)
	{
		cout << "文件不存在或为空" << endl;
		system("pause");
		system("cls");
	}
	else
	{
		for (int i = 0; i < Empnum; i++)
		{
		    this->EmpArray[i]->Showinfo();   //利用多态调用程序接口
		}
	}
	system("pause");
	system("cls");
}

此时我们的源文件"职工管理系统.cpp"已经可以填充小一半

#include"workermanager.h"
#include"boss.h"
#include"employee.h"
#include"manage.h"
#include"worker.h"
using namespace std;

int main()
{
	Workermanager WM;
	int input;
	while (1)
	{
		WM.Show_menu();
		cout << "请输入你的选择:>  ";
		cin >> input;
		switch (input)
		{
		case 0:
			WM.Exit(); //退出程序
			break;
		case 1:
			WM.Add_Emp(); //添加职工
			break;
		case 2:
			WM.show_Emp(); //显示职工信息
			break;
		case 3:			
                        break;
		case 4:
			break;
		case 5:
			break;
		case 6:
			break;
		case 7:
			break;
		default:
			system("cls"); //清屏
			break;
		}
	}
	system("pause");
	return 0;

}

7. 删除职工

在删除职工时,我们需要判断该职工是否存在

我们可以在头文件"workermanage.h"中声明一个判断职工是否存在的函数和一个实现删除职工功能的函数

class Workermanager
{
public:
      int Isexit(int id); //输入职工编号来判断职工是否存在
      void del_Emp();    //删除职工
      ~Workermanager();
}

在源文件"workermanage.cpp"中定义

int Workermanager::Isexit(int id)
{
	int index = -1;
        //在空间中寻找职工
	for (int i = 0; i < this->Empnum; i++)  
	{
		index = i; 
		break;
	}
	return index;
}

void Workermanager::del_Emp()
{
	if (this->FileIsEmpty)
	{
		cout << "文件不存在或为空" << endl;
	}
	else
	{
		cout << "要删除的职工编号: " << endl;
		int id;
		cin >> id;
		int index = this->Isexit(id);
		if (index != -1)   //寻找到职工
		{
			for (int i = index; i < this->Empnum - 1; i++)
			{
				this->EmpArray[i] = this->EmpArray[i + 1]; 
                                //将数据前移,因为删除一个职工后,我们需要后面的职工来填补空位
			}
			this->Empnum--;  //更新数组中职工人数
			this->save();   //将数据更新同步到文件中
			cout << "删除成功" << endl;
		}
		else
		{
			cout << "删除失败,未寻找到该职工" << endl;
		}
	}
	system("pause");
	system("cls");
}

8. 修改职工信息

我们可以在头文件"workermanage.h"中声明一个实现修改职工信息的函数

class Workermanager
{
public:
      void mod_Emp();   //修改职工
      ~Workermanager();
}

在源文件"workermanage.cpp"中定义

void Workermanager::mod_Emp()
{
	if (this->FileIsEmpty)
	{
		cout << "文件不存在或为空" << endl;
	}
	else
	{
		cout << "请输入修改职工的编号:" << endl;
		int id;
		cin >> id;
		int ret = this->Isexit(id);
		if (ret != -1) 
		{
			delete this->EmpArray[ret]; //重置该职工信息
			int newId;
			string newName;
			int dSelect;
			cout << "查到: " << id << "号职工,请输入新职工号: " << endl;
			cin >> newId;
			cout << "请输入新姓名: " << endl;
			cin >> newName;
			cout << "请选择该员工岗位: " << endl;
			cout << "1.员工" << endl;
			cout << "2.经理" << endl;
			cout << "3.总裁" << endl;
			cin >> dSelect;
			worker *worker = NULL;  
			switch (dSelect)
			{
			case 1:
				worker = new employee(newId, newName, dSelect);
				break;
			case 2:
				worker = new manage(newId, newName, dSelect);
				break;
			case 3:
				worker = new boss(newId, newName, dSelect);
				break;
			default:
				break;
			}
			this->EmpArray[ret] = worker; //更新数据到数组中
			cout << "修改成功" << endl;
			this->save();
		}
		else
		{
			cout << "修改失败,查无此人" << endl;
		}
	}
	system("pause");
	system("cls");
}

9. 寻找职工

我们可以在头文件"workermanage.h"中声明一个通过职工编号或名字实现查找职工信息的函数

class Workermanager
{
public:
      void fine_Emp(); //查找职工
      ~Workermanager();
}

在源文件"workermanage.cpp"中定义

void Workermanager::fine_Emp()
{
	if (this->FileIsEmpty)
	{
		cout << "文件不存在或为空" << endl;
	}
	else
	{
		cout << "请输入查找的方式: " << endl;
		cout << "1.按职工编号查找" << endl;
		cout << "2.按职工姓名查找" << endl;
		int input = 0;
		cin >> input;
		switch (input)
		{
		case 1:
		{
				  int id;
				  cout << "请输入查找的职工编号: " << endl;
				  cin >> id;
				  int ret = Isexit(id);
				  if (ret != -1)
				  {
					  cout << "查找成功,该职工信息如下: " << endl;
					  this->EmpArray[ret]->Showinfo();
				  }
				  else
				  {
					  cout << "查无此人" << endl;
				  }
				  break;
		} 
                //c/c++中switch语句出现定义需用大括号括起来
		case 2:
		{
				  string name;
				  cout << "请输入查找的职工姓名: " << endl;
				  cin >> name;
				  bool flag = false;  //判断是否查到
				  for (int i = 0; i < Empnum; i++)
				  {
					  if (this->EmpArray[i]->Name == name)
					  {
						  cout << "查找成功,职工编号为:" << this->EmpArray[i]->Id
						       << "号职工信息如下: " << endl;
						  flag = true;
						  this->EmpArray[i]->Showinfo();
					  }
				  }
				  if (flag == false)
				  {
					  cout << "查无此人" << endl;
				  }
				  break;
		}
		default:
			cout << "输入选项有误" << endl;
			break;
		}
	}
	system("pause");
	system("cls");
}

10. 排序职工

我们可以在头文件"workermanage.h"中声明一个通过职工编号对职工今昔降序或升序排序的函数

class Workermanager
{
public:
      void sort_Emp();   //按编号排序职工
      ~Workermanager();
}

在源文件"workermanage.cpp"中定义

void Workermanager::sort_Emp()
{
	if (this->FileIsEmpty)
	{
		cout << "文件不存在或为空" << endl;
		system("pause");
		system("cls");
	}
	else
	{
		cout << "请选择排序方式:" << endl;
		cout << "1.按职工编号进行升序" << endl;
		cout << "2.按职工编号进行降序" << endl;
		int input;
		cin >> input;
		for (int i = 0; i < Empnum; i++)
		{
			int minORmax = i;   //声明最大值或最小值下标
			for (int j = i + 1; j < this->Empnum; j++)
			{
				switch (input)
				{
				case 1:
					if (this->EmpArray[minORmax]->Id>this->EmpArray[j]->Id)
						minORmax = j;
					break;
				case 2:
					if (this->EmpArray[minORmax]->Id<this->EmpArray[j]->Id)
						minORmax = j;
					break;
				default:
					cout << "输入选项有误" << endl;
					break;
				}
			}
			if (i != minORmax)
			{
				worker *temp = this->EmpArray[i];
				this->EmpArray[i] = this->EmpArray[minORmax];
				this->EmpArray[minORmax] = temp;
			}
		}
	}
	cout << "排序成功,排序后结果为:" << endl;
	this->save();
	this->show_Emp(); //已包含清屏操作
}

11.清空文本

我们可以在头文件"workermanage.h"中声明一个实现删除文本的函数

class Workermanager
{
public:
      void clean_File();  //清空文件(破产跑路
      ~Workermanager();
}

在源文件"workermanage.cpp"中定义

void Workermanager::clean_File()
{
	cout << "确定清空?" << endl;
	cout << "1.确定" << endl;
	cout << "0.返回" << endl;
	int input;
	cin >> input;
	switch (input)
	{
	case 1:
	{
			  ofstream ofs(FILENAME, ios::trunc);  //删除文本后重新创建文本
			  ofs.close();
			  if (this->EmpArray != NULL)
			  {
				  for (int i = 0; i < this->Empnum; i++) //删除堆区里的职工
				  {
					  delete this->EmpArray[i];
					  this->EmpArray[i] = NULL;
				  }
				  delete[] this->EmpArray;              //删除堆区里的数组指针
				  this->EmpArray = NULL;
				  this->Empnum = 0;
				  this->FileIsEmpty = true;
			  }
			  cout << "删除成功" << endl;
			  break;
	}
		case 0:
			break;
		default:
			cout << "输入选项有误" << endl;
			break;
	}
	system("pause");
	system("cls");
}

12.职工管理系统创建完毕

最后我们的源文件"职工管理系统.cpp"已经创建完毕了

#include"workermanager.h"
#include"boss.h"
#include"employee.h"
#include"manage.h"
#include"worker.h"
using namespace std;
int main()
{
	Workermanager WM;
	int input;
	while (1)
	{
		WM.Show_menu();
		cout << "请输入你的选择:>  ";
		cin >> input;
		switch (input)
		{
		case 0:
			WM.Exit();  //退出程序
			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.fine_Emp();//寻找职工
			break;
		case 6:
			WM.sort_Emp();//排序职工
			break;
		case 7:
			WM.clean_File();//清空文本
			break;
		default:
			system("cls"); //清屏
			break;
		}
	}
	system("pause");
	return 0;

}

三.源代码显示

1.头文件

“boss.h”

#pragma once
using namespace std;
#include"worker.h"
class boss :public worker
{
public:
	boss(int id, string name, int deptid);
	void Showinfo();
	string GetDeptName();
};

“employee.h”

#pragma once
#include"worker.h"
using namespace std;
class employee :public worker
{
public:
	employee(int id,string name,int deptid);
	void Showinfo(); 
	string GetDeptName();
};

“manage.h”

#pragma once
#include"worker.h"
using namespace std;
class manage :public worker
{
public:
	manage(int id, string name, int deptid);
	void Showinfo();
	string GetDeptName();
};

“worker.h”

#pragma once
#include<iostream>
#include<string>
using namespace std;
class worker
{
public:
	virtual void Showinfo() = 0;
	virtual string GetDeptName() = 0; 

	int Id;
	string Name;
	int Deptid;
};

“workermanage.h”

#include"boss.h"
#include"manage.h"
#include<fstream>
#define FILENAME "empFile.text"
using namespace std;

class Workermanager
{
public:
	Workermanager();
	void Show_menu();
	void Exit();
	int Empnum; 
	worker **EmpArray;
	void Add_Emp();
	void save(); 
	bool FileIsEmpty; 
	int GetEmpnum(); 
	void init_Emp(); 
	void show_Emp(); 
	void del_Emp(); 
	int Isexit(int id); 
	void mod_Emp(); 
	void fine_Emp(); 
	void sort_Emp(); 
	void clean_File(); 
	~Workermanager();
};

2.源文件

“boss.cpp”

#include"boss.h"
boss::boss(int id, string name, int deptid)
{
	this->Id = id;
	this->Name = name;
	this->Deptid = deptid;
}

void boss::Showinfo()
{
	cout << "职工编号: " << this->Id
	     << "\t职工姓名: " << this->Name
             << "\t岗位: " << this->GetDeptName()
	     << "\t岗位职责: 管理公司各项事务" << endl;
}

string boss::GetDeptName()
{
	return string("总裁");
}

“employee.cpp”

#include"employee.h"
employee::employee(int id, string name, int deptid)
{
	this->Id = id;
	this->Name = name;
	this->Deptid = deptid;
}

void employee::Showinfo()
{
	cout << "职工编号: " << this->Id
	     << "\t职工姓名: " << this->Name
	     << "\t岗位: " << this->GetDeptName()
	     << "\t岗位职责: 完成公司派发的任务" << endl;
}

string employee::GetDeptName()
{
	return string("员工");
}

“manage.cpp”

include"manage.h"
manage::manage(int id, string name, int deptid)
{
	this->Id = id;
	this->Name = name;
	this->Deptid = deptid;
}

void manage::Showinfo()
{
	cout << "职工编号: " << this->Id
	     << "\t职工姓名: " << this->Name
	     << "\t岗位: " << this->GetDeptName()
	     << "\t岗位职责: 下放任务给员工,并听从总裁安排" << endl;
}

string manage::GetDeptName()
{
	return string("经理");
}

“workermanage.cpp”

#include"workermanager.h"

Workermanager::Workermanager()
{
	ifstream ifs;
	ifs.open(FILENAME, ios::in);
	if (!ifs.is_open())
	{
		cout << "文件不存在" << endl;
		this->Empnum = 0; 
		this->EmpArray = NULL;
		this->FileIsEmpty = true;
		ifs.close();
		return;
	}
	char ch;
	ifs >> ch;
	if (ifs.eof())
	{
		cout << "文件为空" << endl;
		this->Empnum = 0; 
		this->EmpArray = NULL;
		this->FileIsEmpty = true;
		ifs.close();
		return;
	}
	int num = this->GetEmpnum();
	cout << "职工人数为: " << num << endl;
	this->Empnum = num;
	this->EmpArray = new worker*[this->Empnum]; 
	this->init_Emp();
	for (int i = 0; i < this->Empnum; i++)
	{
		cout << "职工编号: " << this->EmpArray[i]->Id
		     << "  职工名字: " << this->EmpArray[i]->Name
		     << "  职工部门编号: " << this->EmpArray[i]->Deptid << endl;
	}
}


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::Exit()
{
	cout << "已退出职工管理系统,欢迎下次使用" << endl;
	system("pause");
	exit(0);
}

void Workermanager::Add_Emp()
{
	cout << "请输入添加职工数量: " << endl;
	int addNum = 0;
	cin >> addNum;
	if (addNum > 0)
	{
		int newSize = this->Empnum + addNum; 
		worker**newSpace=new worker*[newSize];
		if (this->EmpArray != NULL)
		{
			for (int i = 0; i < this->Empnum; i++) 
			{
				newSpace[i] = this->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 manage(id, name, 2);
				break;
			case 3:
				worker = new boss(id, name, 3);
				break;
			default:
				break;
			}
			newSpace[this->Empnum + i] = worker;
		}
		delete[] this->EmpArray; 
		this->EmpArray = newSpace; 
		this->Empnum = newSize; 
		this->FileIsEmpty = false; 
		cout << "成功添加" << addNum << "名新职工" << endl;
		this->save(); //保存数据到文件中
	}
	else
	{
		cout << "输入数据有误" << endl;
	}
	system("pause");
	system("cls");
}

void Workermanager::save()
{
	ofstream ofs;
	ofs.open(FILENAME, ios::out);
	for (int i = 0; i < this->Empnum; i++)
	{
		ofs << this->EmpArray[i]->Id << " "
		    << this->EmpArray[i]->Name << " "
		    << this->EmpArray[i]->Deptid << endl;
		ofs.close();
	}
}

int Workermanager::GetEmpnum()
{
	ifstream ifs;
	ifs.open(FILENAME, ios::in);
	int id;
	string name;
	int deptid;
	int num=0;
	while (ifs >> id&&ifs >> name&&ifs >> deptid)
	{
		num++;
	}
	return num;
}

void Workermanager::init_Emp()
{
	ifstream ifs;
	ifs.open(FILENAME, ios::in);
	int id;
	string name;
	int deptid;
	int index = 0;
	while (ifs >> id&&ifs >> name&&ifs >> deptid)
	{
		worker *worker = NULL;
		if (deptid == 1)
		{
			worker = new employee(id, name, deptid);
		}
		else if (deptid == 2)
		{
			worker = new manage(id, name, deptid);
		}
		else
		{
			worker = new boss(id, name, deptid);
		}
		this->EmpArray[index] = worker;
		index++;
	}
	ifs.close();
}

void Workermanager::show_Emp()
{
	if (this->FileIsEmpty)
	{
		cout << "文件不存在或为空" << endl;
		system("pause");
		system("cls");
	}
	else
	{
		for (int i = 0; i < Empnum; i++)
		{
			this->EmpArray[i]->Showinfo(); 
		}
	}
	system("pause");
	system("cls");
}

void Workermanager::del_Emp()
{
	if (this->FileIsEmpty)
	{
		cout << "文件不存在或为空" << endl;
	}
	else
	{
		cout << "要删除的职工编号: " << endl;
		int id;
		cin >> id;
		int index = this->Isexit(id);
		if (index != -1) 
		{
			for (int i = index; i < this->Empnum - 1; i++)
			{
				this->EmpArray[i] = this->EmpArray[i + 1]; 
			}
			this->Empnum--; 
			this->save(); 
			cout << "删除成功" << endl;
		}
		else
		{
			cout << "删除失败,未寻找到该职工" << endl;
		}
	}
	system("pause");
	system("cls");
}

int Workermanager::Isexit(int id)
{
	int index = -1;
	for (int i = 0; i < this->Empnum; i++)
	{
		index = i; 
		break;
	}
	return index;
}

void Workermanager::mod_Emp()
{
	if (this->FileIsEmpty)
	{
		cout << "文件不存在或为空" << endl;
	}
	else
	{
		cout << "请输入修改职工的编号:" << endl;
		int id;
		cin >> id;
		int ret = this->Isexit(id);
		if (ret != -1) 
		{
			delete this->EmpArray[ret]; 
			int newId;
			string newName;
			int dSelect;
			cout << "查到: " << id << "号职工,请输入新职工号: " << endl;
			cin >> newId;
			cout << "请输入新姓名: " << endl;
			cin >> newName;
			cout << "请选择该员工岗位: " << endl;
			cout << "1.员工" << endl;
			cout << "2.经理" << endl;
			cout << "3.总裁" << endl;
			cin >> dSelect;
			worker *worker = NULL;
			switch (dSelect)
			{
			case 1:
				worker = new employee(newId, newName, dSelect);
				break;
			case 2:
				worker = new manage(newId, newName, dSelect);
				break;
			case 3:
				worker = new boss(newId, newName, dSelect);
				break;
			default:
				break;
			}
			this->EmpArray[ret] = worker; 
			cout << "修改成功" << endl;
			this->save();
		}
		else
		{
			cout << "修改失败,查无此人" << endl;
		}
	}
	system("pause");
	system("cls");
}

void Workermanager::fine_Emp()
{
	if (this->FileIsEmpty)
	{
		cout << "文件不存在或为空" << endl;
	}
	else
	{
		cout << "请输入查找的方式: " << endl;
		cout << "1.按职工编号查找" << endl;
		cout << "2.按职工姓名查找" << endl;
		int input = 0;
		cin >> input;
		switch (input)
		{
		case 1:
		{
				  int id;
				  cout << "请输入查找的职工编号: " << endl;
				  cin >> id;
				  int ret = Isexit(id);
				  if (ret != -1)
				  {
					  cout << "查找成功,该职工信息如下: " << endl;
					  this->EmpArray[ret]->Showinfo();
				  }
				  else
				  {
					  cout << "查无此人" << endl;
				  }
				  break;
		} //c/c++中switch语句出现定义需用大括号括起来
		case 2:
		{
				  string name;
				  cout << "请输入查找的职工姓名: " << endl;
				  cin >> name;
				  bool flag = false;  
				  for (int i = 0; i < Empnum; i++)
				  {
					  if (this->EmpArray[i]->Name == name)
					  {
						  cout << "查找成功,职工编号为:" << this->EmpArray[i]->Id
						       << "号职工信息如下: " << endl;
						  flag = true;
						  this->EmpArray[i]->Showinfo();
					  }
				  }
				  if (flag == false)
				  {
					  cout << "查无此人" << endl;
				  }
				  break;
		}
		default:
			cout << "输入选项有误" << endl;
			break;
		}
	}
	system("pause");
	system("cls");
}


void Workermanager::sort_Emp()
{
	if (this->FileIsEmpty)
	{
		cout << "文件不存在或为空" << endl;
		system("pause");
		system("cls");
	}
	else
	{
		cout << "请选择排序方式:" << endl;
		cout << "1.按职工编号进行升序" << endl;
		cout << "2.按职工编号进行降序" << endl;
		int input;
		cin >> input;
		for (int i = 0; i < Empnum; i++)
		{
			int minORmax = i;
			for (int j = i + 1; j < this->Empnum; j++)
			{
				switch (input)
				{
				case 1:
					if (this->EmpArray[minORmax]->Id>this->EmpArray[j]->Id)
						minORmax = j;
					break;
				case 2:
					if (this->EmpArray[minORmax]->Id<this->EmpArray[j]->Id)
						minORmax = j;
					break;
				default:
					cout << "输入选项有误" << endl;
					break;
				}
			}
			if (i != minORmax)
			{
				worker *temp = this->EmpArray[i];
				this->EmpArray[i] = this->EmpArray[minORmax];
				this->EmpArray[minORmax] = temp;
			}
		}
	}
	cout << "排序成功,排序后结果为:" << endl;
	this->save();
	this->show_Emp();
}

void Workermanager::clean_File()
{
	cout << "确定清空?" << endl;
	cout << "1.确定" << endl;
	cout << "0.返回" << endl;
	int input;
	cin >> input;
	switch (input)
	{
	case 1:
	{
			  ofstream ofs(FILENAME, ios::trunc); 
			  ofs.close();
			  if (this->EmpArray != NULL)
			  {
				  for (int i = 0; i < this->Empnum; i++) 
				  {
					  delete this->EmpArray[i];
					  this->EmpArray[i] = NULL;
				  }
				  delete[] this->EmpArray; 
				  this->EmpArray = NULL;
				  this->Empnum = 0;
				  this->FileIsEmpty = true;
			  }
			  cout << "删除成功" << endl;
			  break;
	}
		case 0:
			break;
		default:
			cout << "输入选项有误" << endl;
			break;
	}
	system("pause");
	system("cls");
}

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

“职工管理系统.cpp”

#include"workermanager.h"
#include"boss.h"
#include"employee.h"
#include"manage.h"
#include"worker.h"
using namespace std;

int main()
{
	Workermanager WM;
	int input;
	while (1)
	{
		WM.Show_menu();
		cout << "请输入你的选择:>  ";
		cin >> input;
		switch (input)
		{
		case 0:
			WM.Exit();
			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.fine_Emp();
			break;
		case 6:
			WM.sort_Emp();
			break;
		case 7:
			WM.clean_File();
			break;
		default:
			system("cls"); 
			break;
		}
	}
	system("pause");
	return 0;
}

你可能感兴趣的:(c++小项目,c++)