#pragma once
#include<iostream>
using std::cout, std::endl, std::cin, std::string;
#include<string>
class Worker
{
public:
//显示个人信息
virtual void showInfo() = 0;
//获取岗位名称
virtual string getDeptName() = 0;
int m_ID;
string m_Name;
int m_DeptID;
};
#pragma once
#include<iostream>
#include"worker.h"
using std::cout, std::endl, std::cin, std::string;
#include"employee.h"
#include"manager.h"
#include"boss.h"
#include<fstream>
#define FILENAME "empFile.txt"
class WorkerManager
{
public:
WorkerManager();
void showMenu();
void ExitSystem();
//记录职工人数
int m_EmpNum;
//职工数组指针
Worker ** m_EmpArray;
//添加职工
void Add_Emp();
//保存文件
void save();
//判断文件是否为空
bool m_FileIsEmpty;
//统计文件中人数
int get_EmpNUm();
//初始化员工
void init_Emp();
//显示职工
void Show_Emp();
//判断职工是否存在 如果存在返回数组中位置,不存在返回-1
int IsExist(int id);
//删除职工
void Del_Emp();
//修改职工
void Mod_Emp();
//查找职工
void Find_Emp();
//按照职工编号进行排序
void Sort_Emp();
//快速排序
void quick_Sort(Worker * array[],int left,int right);
~WorkerManager();
};
#pragma once
#include<iostream>
using std::cout, std::endl, std::cin, std::string;
#include"worker.h"
class Manager:public Worker
{
public:
Manager(int id, string name, int dID);
//显示个人信息
virtual void showInfo();
//获取岗位名称
virtual string getDeptName();
};
#pragma once
#include<iostream>
using std::cout, std::endl, std::cin, std::string;
#include"worker.h"
class Employee :public Worker
{
public:
Employee(int id ,string name ,int dID);
//显示个人信息
virtual void showInfo();
//获取岗位名称
virtual string getDeptName();
};
#pragma once
#include<iostream>
using std::cout, std::endl, std::cin, std::string;
#include"worker.h"
class Boss :public Worker
{
public:
Boss(int id, string name, int dID);
//显示个人信息
virtual void showInfo();
//获取岗位名称
virtual string getDeptName();
};
//下面是源文件
#include<iostream>
#include<string>
#include"workerManager.h"
using std::cout, std::endl, std::cin, std::string;
int main()
{
WorkerManager wm;
int choice = 0;
while (true) {
wm.showMenu();
cout << "请输入您的选择" << endl;
cin >> choice;
switch (choice)
{
case 0:wm.ExitSystem();//退出系统
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.Find_Emp();//查找职工
break;
case 6:wm.Sort_Emp();//排序职工
break;
case 7://清空文档
break;
default:
system("cls");
break;
}
};
system("pause");
return 0;
}
#include<iostream>
#include<string>
#include<fstream>
#include"workerManager.h"
using std::cout, std :: endl,std::cin, std::string,std::ofstream,std::ifstream,std::ios;
WorkerManager::WorkerManager()
{
//文件不存在
ifstream ifs;
ifs.open(FILENAME, 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();
}
void WorkerManager::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;
}
void WorkerManager::ExitSystem()
{
cout << "欢迎下次使用" << endl;
system("pause");
exit(0);//退出程序
}
void WorkerManager::Add_Emp()
{
cout << "请输入添加职工的数量" << endl;
int addNum = 0;
cin >> addNum;
if (addNum>0)
{
//计算添加空间大小
int newSize = this->m_EmpNum + addNum;//新空间大小=原来的记录人数+新增人数
//开辟新空间
Worker** newSpace = new Worker * [newSize];
//将原来数据拷贝到新空间
if (this->m_EmpNum!=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;
while (1)
{
if (IsExist(id)==-1)
{
break;
}
cout << "职工编号已存在,请重新输入!" << endl;
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;
//更新后职工不为空
this->m_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->m_EmpNum; i++)
{
ofs << this->m_EmpArray[i]->m_ID << "\t"
<< this->m_EmpArray[i]->m_Name << "\t"
<< this->m_EmpArray[i]->m_DeptID << endl;
}
ofs.close();
}
int WorkerManager::get_EmpNUm()
{
ifstream ifs;
ifs.open(FILENAME, 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(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);
}
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");
}
void WorkerManager::Del_Emp()
{
if (this->m_FileIsEmpty)
{
cout << "文件不存在或者数据为空" << endl;
}
else
{
cout << "请输入想要删除职工编号:" << endl;
int id = 0;
cin >> id;
int index = this->IsExist(id);
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");
}
int WorkerManager::IsExist(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::Mod_Emp()
{
if (this->m_FileIsEmpty)
{
cout << "文件不存在或者记录为空" << endl;
}
else
{
cout << "请输入修改职工编号:" << endl;
int id;
cin >> id;
int ret = this->IsExist(id);
if (ret != -1)
{
delete this->m_EmpArray[ret];
int newId = 0;
string newName = "";
int dSelect = 0;
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 Manager(newId, newName, dSelect);
break;
case 3:
worker = new Boss(newId, newName, dSelect);
break;
default:
break;
}
//更新数据到数组中
this->m_EmpArray[ret] = 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=IsExist(id);
if (ret!=-1)
{
//找到职工
cout << "查找成功!该职工信息如下:" << endl;
this->m_EmpArray[ret]->showInfo();
}
else
{
cout << "查找失败,查无此人" << endl;
}
}
else if (select==2)
{
//按照姓名查
string name;
cout << "请输入查找姓名" << endl;
cin >> name;
bool flag = false;
for (int i = 0; i < m_EmpNum; i++)
{
if (this->m_EmpArray[i]->m_Name == name)
{
cout << "查找成功,职工编号为"
<< this->m_EmpArray[i]->m_ID
<< "号职工信息如下:" << endl;
flag = true;
this->m_EmpArray[i]->showInfo();
}
}
if (!flag)
{
cout << "查找失败,查无此人!" << endl;
}
}
else
{
cout << "输入有误!" << endl;
}
}
system("pause");
system("cls");
}
void WorkerManager::Sort_Emp()
{
if (this->m_FileIsEmpty)
{
cout << "文件不存在或者记录为空!" << endl;
}
else
{
cout << "请选择排序方式:" << endl;
cout << "1、按照职工号升序排列" << endl;
cout << "2、按照职工号降序排列" << endl;
int select = 0;
cin >> select;
if (select==1)
{
quick_Sort(m_EmpArray, 0, m_EmpNum);
save();
}
}
}
void WorkerManager::quick_Sort(Worker* m_EmpArray[], int left, int right)
{
int pivot;
int i = left;
int j = right;
pivot = i;
while( i <= j)
{
while (m_EmpArray[i]->m_ID < m_EmpArray[pivot]->m_ID)
{
i++;
}
while (m_EmpArray[j]->m_ID > m_EmpArray[pivot]->m_ID)
{
j--;
}
if (i <= j)
{
Worker* temp = m_EmpArray[i];
m_EmpArray[i] = m_EmpArray[j];
m_EmpArray[j] = temp;
i++;
j--;
}
}
if (left<j)
{
quick_Sort(m_EmpArray, left, j);
}
if (i<left)
{
quick_Sort(m_EmpArray, i, left);
}
}
WorkerManager::~WorkerManager()
{
if (this->m_EmpArray != NULL)
{
delete[] this->m_EmpArray;
this->m_EmpArray = NULL;
}
}
#include"manager.h"
Manager::Manager(int id, string name, int dID)
{
this->m_ID = id;
this->m_DeptID = dID;
this->m_Name = name;
}
//显示个人信息
void Manager ::showInfo()
{
cout << "职工编号" << this->m_ID
<< "\t职工姓名" << this->m_Name
<< "\t岗位" << this->getDeptName()
<< "\t岗位职责:完成老板任务,下发任务给员工" << endl;
}
//获取岗位名称
string Manager ::getDeptName()
{
return string("经理");
}
#include"employee.h"
Employee::Employee(int id, string name, int dID)
{
this->m_ID = id;
this->m_Name = name;
this->m_DeptID=dID;
}
//显示个人信息
void Employee:: showInfo()
{
cout << "职工编号" << this->m_ID
<< "\t职工姓名" << this->m_Name
<< "\t岗位" << this->getDeptName()
<< "\t岗位职责:完成经理交给的任务" << endl;
}
//获取岗位名称
string Employee::getDeptName()
{
return string("员工");
}
#include"boss.h"
Boss::Boss(int id, string name, int dID)
{
this->m_ID = id;
this->m_DeptID = dID;
this->m_Name = name;
}
//显示个人信息
void Boss::showInfo()
{
cout << "职工编号" << this->m_ID
<< "\t职工姓名" << this->m_Name
<< "\t岗位" << this->getDeptName()
<< "\t岗位职责:管理公司所有任务" << endl;
}
//获取岗位名称
string Boss::getDeptName()
{
return string("老板");
}