workermanager.h
#pragma once //防止头文件重复包含
#include<iostream> //包含输入输出流的头文件
#include"worker.h"
#include<fstream>
#define FILENAME "empfise.txt"
using namespace std; //标准命名空间
class workermanager
{
public:
workermanager(); //构造函数
void showmenu(); //展示菜单
void exitsystem(); //退出系统
int m_empnum = 0; //记录职工人数
worker ** m_emparray; //职工数组指针
void add_emp(); //添加职工
void save(); //保存文件
bool m_fileisempty; //判断是否为空
int get_empnum();//统计文件中人数
void init_emp();//初始化员工
void show_emp();//显示职工
void del_emp();//删除职工
int isexist(int id);//判断职工是否存在,如果存在返回职工在数组中的位置,不存在返回-1
void mod_emp();//修改职工
void find_emp();//查找职工
void sort_emp();//排序职工
void clean_file();//清空文件
~workermanager(); //析构函数
};
worker.h
#pragma once
#include<iostream>
using namespace std;
#include<string>
class worker
{
public:
virtual void showif() = 0; //显示个人信息
virtual string showdepart() = 0; //获取岗位名称
virtual string showrespons() = 0; //获取职责
string w_name;
int w_id;
int w_did;
};
manager.h
#pragma once
#include"worker.h"
class manager :public worker
{
public:
manager(int id, string name, int did); //构造函数
virtual void showif(); //显示个人信息
virtual string showdepart(); //获取岗位名称
virtual string showrespons(); //获取职责
};
employee.h
#pragma once
#include<iostream>
#include"worker.h"
using namespace std;
class employee :public worker
{
public:
employee(int id,string name,int did); //构造函数
virtual void showif(); //显示个人信息
virtual string showdepart(); //获取岗位名称
virtual string showrespons(); //获取职责
};
boss.h
#pragma once
#include"worker.h"
class boss :public worker
{
public:
boss(int id, string name, int did); //构造函数
virtual void showif(); //显示个人信息
virtual string showdepart(); //获取岗位名称
virtual string showrespons(); //获取职责
};
manager.cpp
#include"manager.h"
manager::manager(int id, string name, int did)
{
this->w_name = name;
this->w_id = id;
this->w_did = did;
}
void manager::showif()
{
cout << "职工编号:" << this->w_id
<< "\t职工姓名: " << this->w_name
<< "\t职工岗位: " << this->showdepart()
<< "\t职工职责:" << this->showrespons()
<< endl;
}
string manager::showdepart()
{
return string("经理");
}
string manager::showrespons()
{
return string("完成老板交给的任务,并且下发任务");
}
employee.cpp
#include"employee.h"
employee::employee(int id, string name, int did)
{
this->w_id = id;
this->w_name = name;
this->w_did = did;
}
void employee::showif()
{
cout << "职工编号:" << this->w_id
<< "\t职工姓名: " << this->w_name
<< "\t职工岗位: " << this->showdepart()
<< "\t职工职责:" <<this->showrespons()
<< endl;
}
string employee::showdepart()
{
return string("员工");
}
string employee::showrespons()
{
return string( "完成经理交给的任务");
}
boss.cpp
#include"boss.h"
boss::boss(int id, string name, int did)
{
this->w_name = name;
this->w_id = id;
this->w_did = did;
}
void boss::showif()
{
cout << "职工编号:" << this->w_id
<< "\t职工姓名: " << this->w_name
<< "\t职工岗位: " << this->showdepart()
<< "\t职工职责:" << this->showrespons()
<< endl;
}
string boss::showdepart()
{
return string("老板");
}
string boss::showrespons()
{
return string("管理公司所有事务");
}
workermanager.cpp
#include"workermanager.h"
#include<string>
#include"boss.h"
#include"employee.h"
#include"manager.h"
workermanager::workermanager()
{
//情况1:文件不存在
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;
}
//情况2.文件存在但为空
char ch;
ifs >> ch;
if (ifs.eof())
{
//文件为空
cout << "文件为空!" << endl;
//初始化属性
this->m_empnum = 0; //初始化人数
this->m_emparray = NULL; //初始化数组指针
this->m_fileisempty = true; //初始化文件是否为空
ifs.close();
return;
}
//情况3.当文件存在并且也记录数据
int num = this->get_empnum();
//cout << "职工人数为:" << num<<endl;
this->m_empnum = num;
//开辟空间
this->m_emparray = new worker*[this->m_empnum];
//将文件中的数据,存到数组中
this->init_emp();
//测试
/*for (int i = 0; i < this->m_empnum; i++)
{
cout << "职工编号:" << this->m_emparray[i]->w_id << " 姓名:" << this->m_emparray[i]->w_name<< " 部门:" << this->m_emparray[i]->w_did<<endl;
}*/
}
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;
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_emparray != 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;
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]->w_id << " "
<< this->m_emparray[i]->w_name << " "
<< this->m_emparray[i]->w_did << 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]->showif();
}
}
//按任意键清屏
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) //说明职工存在,并且要删除掉index位置上的职工
{
for (int i=index;i<this->m_empnum-1;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)//判断职工是否存在,如果存在返回职工在数组中的位置,不存在返回-1
{
int index = -1;
for (int i = 0; i < m_empnum; i++)
{
if (this->m_emparray[i]->w_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 << "2.经理" << endl << "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]->showif();
}
else
{
cout << "查无此人" << endl;
}
}
else if (select == 2) //按姓名查
{
string name;
cout << "请输入查找的职工姓名:" << endl;
cin >> name;
bool flag = false; //加入判断标志,默认没找到
for (int i = 0; i < this->m_empnum; i++)
{
if (this->m_emparray[i]->w_name == name)
{
cout << "查找成功,职工的编号为: " << this->m_emparray[i]->w_id << " 号职工的信息如下:" << endl;
flag = true;
this->m_emparray[i]->showif();
}
}
if (flag == false)
{
cout << "查找失败,查无此人!" << endl;
}
}
else
{
cout << "输入有误!" << endl;
}
//按任意键清屏
system("pause");
system("cls");
}
}
void workermanager::sort_emp()
{
if (this->m_fileisempty)
{
cout << "文件不存在或记录为空!" << endl;
system("pause");
system("cls");
}
else
{
cout << "请选择排序方式:" <<endl<< "1.按职工号升序" << endl << "2.按职工号降序" << endl;
}
int select = 0;
cin >> select;
for (int i = 0; i < this->m_empnum; i++)
{
int minormax = i; //最小值或最大值下标
for (int j=i+1;j<this->m_empnum;j++)
{
if (select == 1)//升序
{
if (this->m_emparray[minormax]->w_id > this->m_emparray[j]->w_id)
{
minormax = j;
}
}
else //降序
{
if (this->m_emparray[minormax]->w_id < this->m_emparray[j]->w_id)
{
minormax = j;
}
}
}
//判断一开始认定的最小值或最大值是不是计算的最小值或最大值,如果不是,交换数据
if (i != minormax)
{
worker* temp = this->m_emparray[i];
this->m_emparray[i] = this->m_emparray[minormax];
this->m_emparray[minormax] = temp;
}
}
cout << "排序成功!排序后的结果为: " << endl;
this->save();
this->show_emp();
}
void workermanager::clean_file()
{
cout << "确定清空?" << endl;
cout << "1.确认" << endl;
cout << "2.返回" << endl;
int select = 0;
cin >> select;
if (select == 1)
{
//清空文件
ofstream ofs(FILENAME,ios::trunc); //删除文件后重新创建
ofs.close();
if (this->m_emparray != NULL)
{
//删除堆区的每个职工对象
for (int i = 0; i < this->m_empnum; i++)
{
delete this->m_emparray[i];
this->m_emparray[i] = NULL;
}
//删除堆区数组指针
delete[] this->m_emparray;
this->m_emparray = NULL;
this->m_empnum = 0;
this->m_fileisempty = true;
}
cout << "清空成功" << endl;
}
system("pause");
system("cls");
}
workermanager::~workermanager()
{
}
职工管理系统.cpp
#include<iostream>
#include"workermanager.h"
#include"worker.h"
#include"employee.h"
#include"manager.h"
#include"boss.h"
using namespace std;
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: //清空文档
wm.clean_file();
break;
default:
system("cls"); //清屏
break;
}
}
system("pause");
return 0;
}