-#头文件
1.woker
#pragma once
#include
using namespace std;
#include
class Worker {
public:
//个人信息
virtual void getinfo() = 0;
//岗位名
virtual string depName() = 0;
//职工id
int w_id;
//职工姓名
string w_name;
//部门编号
int depid;
};
2.boss
#pragma once
#include
using namespace std;
#include "woker.h"
class Boss :public Worker {
public:
//构造函数
Boss(int id, string name, int did);
//个人信息
virtual void getinfo();
//岗位名
virtual string depName();
};
3.employee
#include "woker.h"
class Employee :public Worker {
public:
Employee(int id, string name, int did);
virtual void getinfo();
//岗位名
virtual string depName();
};
4.manager
#pragma once
#include
using namespace std;
#include "woker.h"
class Manager :public Worker {
public:
//构造函数
Manager(int id, string name, int did);
//个人信息
virtual void getinfo();
//岗位名
virtual string depName();
};
5.workerManager
#define _CRT_SECURE_NO_WARNINGS 1
#pragma once
#include
using namespace std;
#include "woker.h"
class WorkerManager {
public:
WorkerManager();
//析构函数
~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();
//删除职工
void Del_Emp();
//判断职工是否存在,存在返回位置
int IsExist(int id);
//修改职工
void Mod_Emp();
//查找职工
void Find_Emp();
//排序职工
void Sort_Emp();
//清空文件
void Clean_File();
};
-#源文件
1.boss
#include "boss.h"
//构造函数
Boss::Boss(int id, string name, int did) {
this->w_id = id;
this->w_name = name;
this->depid = did;
}
//个人信息
void Boss::getinfo() {
cout << "职工编号:" << this->w_id
<< "\t职工姓名:" << this->w_name
<< "\t岗位:" << this->depName()
<< "\t岗位职责:管理公司所有事务" << endl;
}
//岗位名
string Boss::depName() {
return string("老板");
}
2.employee
#include "employee.h"
Employee::Employee(int id, string name, int did){
this->w_id = id;
this->w_name = name;
this->depid = did;
}
void Employee::getinfo(){
cout << "职工编号:" << this->w_id
<< "\t职工姓名:" << this->w_name
<< "\t岗位:" << this->depName()
<<"\t岗位职责:完成经理交给的任务"<< endl;
}
string Employee::depName(){
return string("员工");
}
3.manager
#include "manager.h"
//构造函数
Manager::Manager(int id, string name, int did) {
this->w_id = id;
this->w_name = name;
this->depid = did;
}
//个人信息
void Manager::getinfo() {
cout << "职工编号:" << this->w_id
<< "\t职工姓名:" << this->w_name
<< "\t岗位:" << this->depName()
<< "\t岗位职责:完成老板交给的任务,并且给员工下发任务" << endl;
}
//岗位名
string Manager::depName() {
return string("经理");
}
4.workmanager
#include "workmanager.h"
#include "employee.h"
#include "boss.h"
#include "manager.h"
#include
#define FILENAME "empFile.txt"
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]->depid << endl;
}*/
}
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 << "请选择第" << i + 1 << "个职工岗位:" << endl;
cout << "1.普通职工" << endl;
cout << "2.经理" << endl;
cout << "3.老板" << endl;
cin >> dSelect;
Worker* worker = NULL;
switch (dSelect)
{
case 1:
worker = new Employee(id, name, dSelect);
break;
case2:
worker = new Manager(id, name, dSelect);
break;
case 3:
worker = new Boss(id, name, dSelect);
break;
default:
break;
}
newSpace[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:: showMenu() {
cout << "********************************" <m_EmpArray != NULL) {
for (int i = 0; i < this->m_EmpNum; i++)
{
if (this->m_EmpArray[i]!=NULL)
{
delete this->m_EmpArray[i];
this->m_EmpArray[i] = NULL;
}
}
delete[] this-> m_EmpArray;
this->m_EmpArray = NULL;
}
}
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]->depid << endl;
}
}
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 < this->m_EmpNum; i++)
{
this->m_EmpArray[i]->getinfo();
}
}
//按任意键后清屏
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]->w_id == id) {
index = i;
break;
}
}
return index;
}
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");
}
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;
}
//更新数据到数组
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 = this->IsExist(id);
if (ret != -1) {
cout << "查找成功!该职工信息如下:" << endl;
this->m_EmpArray[ret]->getinfo();
}
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]->getinfo();
}
}
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;
cout << "1、按照职工号进行升序" << endl;
cout << "2、按照职工号进行降序" << endl;
int select = 0;
cin >> select;
for (int i = 0; i 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++)
{
if (this->m_EmpArray[i] != NULL)
{
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");
}