程序名称 - main.cpp C++

//主程序:
#include"WorkerManager.h"
#include"worker.h"
int main()
{
	WorkerManager WM;
	int input;
	do
	{
		WM.show_menu();
		cout << "请选择:>";
		cin >> input;
		switch(input)
		{
		case 0://退出程序
			WM.WM_exit();
			break;
		case 1://添加职工
			WM.WM_Add();
			break;
		case 2://显示职工
			WM.WM_ShowEmp();
			break;
		case 3://删除职工
			WM.WM_Delete();
			break;
		case 4://修改职工
			WM.WM_ModEmp();
			break;
		case 5://查找职工
			WM.WM_FindEmp();
			break;
		case 6://排序职工
			WM.WM_Sort();
			break;
		case 7://清空信息
			WM.WM_Clear();
			break;
		default://输入错误
			system("cls");
			break;
		}
	} while (input);
	system("pause");
	return 0;


//WorkerManager.h:
#pragma once
#include<iostream>
using namespace std;
#include<string>
#include<fstream>
#include"worker.h"

#define FILENAME "empfile.txt"

class WorkerManager
{
public:
	WorkerManager();
	void show_menu();//展示菜单
	void WM_exit();//退出程序
	void WM_Add();//添加职工
	void WM_save();//保存文件
	int WM_GetNum(); //查询职工人数
	void WM_InitEmp();//初始化职工
	void WM_ShowEmp();//展示职工信息
	int WM_IsExist(int id);//根据id查找职工是否存在
	int WM_IsExist(string name);//根据姓名查找职工是否存在
	void WM_Delete();//删除职工,上述功能综合
	void WM_ModEmp();//修改职工
	void WM_FindEmp();//查找并显示职工
	void WM_Sort();//排序
	void WM_Clear();//清空文件
	int m_Number;//记录职工人数
	bool WM_FileIsNULL;//判断文件是否为空
	Worker** m_EmpArray;//记录职工指针
	~WorkerManager();
};


//WorkerManager.cpp:
#include"WorkerManager.h"

WorkerManager::WorkerManager()
{
	ifstream ifs;
	ifs.open(FILENAME, ios::in);
	if (!ifs.is_open())//文件不存在
	{
		cout << "文件不存在" << endl;
		this->m_Number = 0;
		this->m_EmpArray = NULL;
		this->WM_FileIsNULL = true;
		ifs.close();
		return;
	}
	//如果文件为空
	char ch;
	ifs >> ch;
	if (ifs.eof())
	{
		cout << "文件为空" << endl;
		this->m_Number = 0;
		this->m_EmpArray = NULL;
		this->WM_FileIsNULL = true;
		ifs.close();
		return;
	}
	//文件存在且不为空
	this->m_Number = this->WM_GetNum();
	this->m_EmpArray = new Worker * [this->m_Number];
	this->WM_InitEmp();
	cout << "职工人数为: " << this->m_Number << 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::WM_Add()//添加职工
{
	cout << "请输入添加人数:" << endl;
	int AddNum = 0;
	cin >> AddNum;
	if (AddNum > 0)
	{
		system("cls");
		int NewSize = this->m_Number + AddNum;//计算新开辟空间大小
		Worker** NewSpace = new Worker * [NewSize];//开辟新空间
		if (this->m_EmpArray != NULL)
		{
			for (int i = 0; i <this->m_Number; i++)
			{
				NewSpace[i] = this->m_EmpArray[i];
			}
		}
		for (int i = 0; i < AddNum; i++)//批量添加数据
		{
			int id;
			string name;
			int depid;
			cout << "请输入第 " << i + 1 << " 个职工的编号" << endl;
			cin >> id;
			cout << "请输入第 " << i + 1 << " 个职工的姓名" << endl;
			cin >> name;
			cout << "请选择职工的岗位:" << endl;
			cout << "1、普通员工   2、经理    3、总裁" << endl;
			cin >> depid;
			Worker* worker = NULL;
			switch (depid)
			{
			case 1:
				worker = new Employee(id, name, depid);
				break;
			case 2:
				worker = new Manager(id, name, depid);
				break;
			case 3:
				worker = new Boss(id, name, depid);
				break;
			default:
				cout << "岗位添加错误,已取消该条添加" << endl;
				--i;
				continue;
			}
			NewSpace[this->m_Number+i] = worker;//将职工保存在数组中
		}
		delete[] this->m_EmpArray;//释放原有空间
		this->m_EmpArray = NewSpace;//更改新空间指向
		this->m_Number =NewSize;//更新新的职工人数
		this->WM_save();//保存数据到文件
		cout << "成功添加 " <<AddNum<<" 名职工" << endl;
		this->WM_FileIsNULL = false;//更新文件不为空
	}
	else
	{
		cout << "输入有误" << endl;
	}
	system("pause");
	system("cls");
	return;
}
void WorkerManager::WM_exit()//退出程序
{
	cout << "欢迎下次使用!" << endl;
	system("pause");
	exit(0);
}
void WorkerManager::WM_save()//保存到文件
{
	ofstream ofs;
	ofs.open(FILENAME, ios::out);
	for (int i = 0; i < this->m_Number; 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::WM_GetNum()//获取文件中职工人数
{
	ifstream ifs;
	ifs.open(FILENAME, ios::in);
	string st;
	int num=0;
	while (getline(ifs, st))
	{
		num++;
	}
	ifs.close();
	return num;
}
void WorkerManager::WM_InitEmp()//职工信息初始化
{
	ifstream ifs;
	ifs.open(FILENAME, ios::in);
	int id;
	string name;
	int did;
	int num=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 if(did == 3)//总裁
		{
			worker = new Boss(id, name, did);
		}
		this->m_EmpArray[num] = worker;
		num++;
	}
	ifs.close();
}
void WorkerManager::WM_ShowEmp()//显示所有职工信息
{
	if (this->WM_FileIsNULL)
	{
		cout << "文件不存在或为空" << endl;
	}
	else
	{
		for (int i = 0; i < this->m_Number; i++)
		{
			this->m_EmpArray[i]->W_show();
		}
	}
	system("pause");
	system("cls");
}
int WorkerManager::WM_IsExist(int id)//查找职工,有则返回下标
{
	for (int i = 0; i < this->m_Number; i++)
	{
		if (this->m_EmpArray[i]->m_ID == id)
		{
			return i;
		}
	}
	return -1;
}
int WorkerManager::WM_IsExist(string name)//重载
{
	for (int i = 0; i < this->m_Number; i++)
	{
		if (this->m_EmpArray[i]->m_Name == name)
		{
			return i;
		}
	}
	return -1;
}
void WorkerManager::WM_Delete()//删除职工
{
	if (this->WM_FileIsNULL)//判断是否有数据
	{
		cout << "文件不存在或为空" << endl;
		system("pause");
		system("cls");
		return;
	}
	int input;
	cout << "请选择删除方式:" << endl;
	cout << "1、按ID  2、按姓名" << endl;
	cin >> input;
	if (input == 1)
	{
		int id;
		cout << "请输入ID:" << endl;
		cin >> id;
		int p = this->WM_IsExist(id);
		if (p != -1)
		{
			if (p == this->m_Number - 1)//此人为最后一位
			{
				this->m_Number--;
			}
			else//此人在其他位置
			{
				for (int i = p; i < this->m_Number-1; i++)
				{
					this->m_EmpArray[i] = this->m_EmpArray[i + 1];
				}
				this->m_Number--;
			}
		}
		else
		{
			cout << "查无此人" << endl;
			system("pause");
			system("cls");
			return;
		}
	}
	else if (input == 2)
	{
		string name;
		cout << "请输入姓名:" << endl;
		cin >> name;
		int p = this->WM_IsExist(name);
		if (p != -1)
		{
			if (p == this->m_Number - 1)//此人为最后一位
			{
				this->m_Number--;
			}
			else//此人在其他位置
			{
				for (int i = p; i < this->m_Number - 1; i++)
				{
					this->m_EmpArray[i] = this->m_EmpArray[i + 1];
				}
				this->m_Number--;
			}
		}
		else
		{
			cout << "查无此人" << endl;
			system("pause");
			system("cls");
			return;
		}
	}
	else
	{
		cout << "输入错误!" << endl;
		system("pause");
		system("cls");
		return;
	}
	this->WM_save();
	cout << "删除成功" << endl;
	system("pause");
	system("cls");
}
void WorkerManager::WM_ModEmp()//修改职工信息
{
	if (this->WM_FileIsNULL)//判断是否有数据
	{
		cout << "文件不存在或为空" << endl;
		system("pause");
		system("cls");
		return;
	}
	int input;
	cout << "请选择修改方式:" << endl;
	cout << "1、按ID  2、按姓名" << endl;
	cin >> input;
	switch (input)
	{
	case 1:
	{
		int id;
		cout << "请输入ID:" << endl;
		cin >> id;
		int p = this->WM_IsExist(id);
		if (p != -1)
		{
			cout << "原信息为:";
			this->m_EmpArray[p]->W_show();
			cout << "请重新输入编号" << endl;
			cin >> this->m_EmpArray[p]->m_ID;
			cout << "请重新输入姓名" << endl;
			cin >> this->m_EmpArray[p]->m_Name;
			cout << "请重新输入岗位" << endl;
			cout << "1、员工  2、经理  3、总裁" << endl;
			cin >> this->m_EmpArray[p]->m_DeptID;
		}
		else
		{
			cout << "查无此人" << endl;
			system("pause");
			system("cls");
			return;
		}
	}
		break;
	case 2:
	{
		string name;
		cout << "请输入姓名:" << endl;
		cin >> name;
		int p = this->WM_IsExist(name);
		if (p != -1)
		{
			cout << "原信息为:";
			this->m_EmpArray[p]->W_show();
			cout << "请重新输入编号" << endl;
			cin >> this->m_EmpArray[p]->m_ID;
			cout << "请重新输入姓名" << endl;
			cin >> this->m_EmpArray[p]->m_Name;
			cout << "请重新输入岗位" << endl;
			cout << "1、员工  2、经理  3、总裁" << endl;
			cin >> this->m_EmpArray[p]->m_DeptID;
		}
		else
		{
			cout << "查无此人" << endl;
			system("pause");
			system("cls");
			return;
		}
	}
		break;
	default:
		cout << "输入错误!" << endl;
		system("pause");
		system("cls");
		return;
		break;
	}
	this->WM_save();
	cout << "修改成功" << endl;
	system("pause");
	system("cls");
}
void WorkerManager::WM_FindEmp()
{
	if (this->WM_FileIsNULL)//判断是否有数据
	{
		cout << "文件不存在或为空" << endl;
		system("pause");
		system("cls");
		return;
	}
	int input;
	cout << "请选择修改方式:" << endl;
	cout << "1、按ID  2、按姓名" << endl;
	cin >> input;
	switch (input)
	{
	case 1:
	{
		int id;
		cout << "请输入ID:" << endl;
		cin >> id;
		int p = this->WM_IsExist(id);
		if (p != -1)
		{
			this->m_EmpArray[p]->W_show();
		}
		else
		{
			cout << "查无此人" << endl;
			system("pause");
			system("cls");
			return;
		}	
	}
		break;
	case 2:
	{
		string name;
		cout << "请输入姓名:" << endl;
		cin >> name;
		int p = this->WM_IsExist(name);
		if (p != -1)
		{
			this->m_EmpArray[p]->W_show();
		}
		else
		{
			cout << "查无此人" << endl;
			system("pause");
			system("cls");
			return;
		}
	}
		break;
	default:
	{
		cout << "输入错误!" << endl;
		system("pause");
		system("cls");
		return;
	}
		break;
	}
	system("pause");
	system("cls");
}
void WorkerManager::WM_Sort()//冒泡排序
{
	if (this->WM_FileIsNULL)//判断是否有数据
	{
		cout << "文件不存在或为空" << endl;
		system("pause");
		system("cls");
		return;
	}
	int select;
	cout << "请选择排序方式:" << endl;
	cout << "1.按ID进行升序排序" << endl;
	cout << "2.按ID进行降序排序" << endl;
	cin >> select;
	if (select == 2)//降序排序
	{
		for (int i = 0; i < this->m_Number - 1; i++)
		{
			for (int j = 0; j < this->m_Number - 1 - i; j++)
			{
				if (this->m_EmpArray[j]->m_ID < this->m_EmpArray[j + 1]->m_ID)
				{
					int temp = this->m_EmpArray[j]->m_ID;
					this->m_EmpArray[j]->m_ID = this->m_EmpArray[j + 1]->m_ID;
					this->m_EmpArray[j + 1]->m_ID = temp;
				}
			}
		}
		cout << "已完成降序排序" << endl;
		system("pause");
		system("cls");
	}
	else // 升序排序(默认)
	{
		for (int i = 0; i < this->m_Number - 1; i++)
		{
			for (int j = 0; j < this->m_Number-1 - i; j++)
			{
				if (this->m_EmpArray[j]->m_ID > this->m_EmpArray[j + 1]->m_ID)
				{
					int temp = this->m_EmpArray[j]->m_ID;
					this->m_EmpArray[j]->m_ID = this->m_EmpArray[j + 1]->m_ID;
					this->m_EmpArray[j + 1]->m_ID = temp;
				}
			}
		}
		cout << "已完成升序排序" << endl;
		system("pause");
		system("cls");
	}
}
void WorkerManager::WM_Clear()//清空文件
{
	int select;
	cout << "是否确定清空文件?" << endl;
	cout << "1、是  2、否" << endl;
	cin >> select;
	if (select == 1)
	{
		ofstream ofs;
		ofs.open(FILENAME, ios::trunc);
		ofs.close();
		if (this->m_EmpArray != NULL)
		{
			delete[] this->m_EmpArray;
			this->m_EmpArray = NULL;
		}
		this->m_Number = 0;
		this->WM_FileIsNULL=true;
		cout << "已清空文件" << endl;
	}
	else
	{
		cout << "取消成功" << endl;
	}	
		system("pause");
		system("cls");
}
WorkerManager::~WorkerManager()
{
	if (this->m_EmpArray != NULL)
	{
		delete[] this->m_EmpArray;
		this->m_EmpArray = NULL;
	}
}


//worker.h:
#pragma once
#include<iostream>
using namespace std;
#include<string>
#include<fstream>
class Worker//总类,抽象类
{
public:

	virtual void W_show() = 0;//显示所有信息
	static string W_GetDep(int DeptID);//获取岗位名称
	static string W_GetWork(int DeptID);//获取岗位职责

	int m_ID;//员工编号
	string m_Name;//员工姓名
	int m_DeptID;//部门编号
};
class Employee :public Worker//普通员工
{
public:
	Employee(int ID,string name,int Dep);
	virtual void W_show();//显示所有信息
	~Employee();
};
class Manager :public Worker
{
public:
	Manager(int ID, string name, int Dep);
	virtual void W_show();//显示所有信息
	~Manager();
};
class Boss :public Worker
{
public:
	Boss(int ID, string name, int Dep);
	virtual void W_show();//显示所有信息
	~Boss();
};


//work.cpp:
#include"worker.h"

string Worker::W_GetDep(int DeptID)//获取岗位名称
{
	string Dep;
	switch (DeptID)
	{
	case 1:
		Dep = "员工";
		break;
	case 2:
		Dep = "经理";
		break;
	case 3:
		Dep = "总裁";
		break;
	}
	return Dep;
}
string Worker::W_GetWork(int DeptID)//获取岗位职责
{
	string Dep;
	switch (DeptID)
	{
	case 1:
		Dep = "完成经理下达的任务";
		break;
	case 2:
		Dep = "根据总裁的决定制定并分发任务";
		break;
	case 3:
		Dep = "管理整个公司";
		break;
	}
	return Dep;
}
Employee::Employee(int ID, string name, int Dep)//构造函数
{
	this->m_ID = ID;
	this->m_Name = name;
	this->m_DeptID = Dep;
}
void Employee::W_show()//显示所有信息
{
	cout << "职工编号: " << this->m_ID
		<< "    职工姓名: " << this->m_Name
		<< "    岗位: " << Worker::W_GetDep(this->m_DeptID)
		<< "    岗位职责: " << Worker::W_GetWork(this->m_DeptID)
		<<endl;
}
Manager::Manager(int ID, string name, int Dep)
{
	this->m_ID = ID;
	this->m_Name = name;
	this->m_DeptID = Dep;
}
void Manager::W_show()//显示所有信息
{
	cout << "职工编号: " << this->m_ID
		<< "    职工姓名: " << this->m_Name
		<< "    岗位: " << Worker::W_GetDep(this->m_DeptID)
		<< "    岗位职责: " << Worker::W_GetWork(this->m_DeptID)
		<< endl;
}
Boss::Boss(int ID, string name, int Dep)
{
	this->m_ID = ID;
	this->m_Name = name;
	this->m_DeptID = Dep;
}
void Boss::W_show()//显示所有信息
{
	cout << "职工编号: " << this->m_ID
		<< "    职工姓名: " << this->m_Name
		<< "    岗位: " << Worker::W_GetDep(this->m_DeptID)
		<< "    岗位职责: " << Worker::W_GetWork(this->m_DeptID)
		<< endl;
}