您好,登錄后才能下訂單哦!
主模塊(職工管理系統.cpp)
#include
using namespace std;
#include "workerManger.h"
#include "worker.h"
#include "employee.h"
#include "Boss.h"
#include "Manager.h"
int main()
{
//實例化一個管理者對象智匯代理申請
WorkerManger wm;
int choice = 0;
while (true)
{
//調用成員函數顯示菜單
wm.Show_Menu();
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;
}
workerManger.h的文件:
#pragma once //防止頭文件重復包含
#include
#include
#include "worker.h"
#include "employee.h"
#include "Boss.h"
#include "Manager.h"
#define FILENAME "empFile.txt"
using namespace std;
class WorkerManger
{
public:
//構造函數
WorkerManger();
//展示菜單
void Show_Menu();
//退出系統
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();
//析構函數
~WorkerManger();
};
workerManger.cpp文件:
#include "workerManger.h"
WorkerManger::WorkerManger()
{
//1、文件不存在
ifstream ifs;
ifs.open(FILENAME, ios::in);
if (!ifs.is_open())
{
this->m_EmpNum = 0; //初始化記錄人數
this->m_EmpArray = NULL; //初始化數組指針
this->m_FileIsEmpty = true; //初始化文件是否為空
ifs.close();
return;
}
//文件存在,數據為空
char ch;
ifs >> ch;
if (ifs.eof())
{
this->m_EmpNum = 0; //初始化記錄人數
this->m_EmpArray = NULL; //初始化數組指針
this->m_FileIsEmpty = true; //初始化文件是否為空
ifs.close();
return;
}
//當文件存在并且記錄有數據
int num = this->get_EmpNum();
this->m_EmpNum = num;
this->m_EmpArray = new Worker*[this->m_EmpNum];
this->init_Emp();
}
//展示菜單
void WorkerManger::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 WorkerManger::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");
}
//統計文件中的人數
int WorkerManger::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 WorkerManger::ExitSystem()
{
cout << "歡迎下次使用" << endl;
system("pause");
exit(0);//退出程序
}
//保存文件
void WorkerManger::save()
{
ofstream ofs;
ofs.open(FILENAME, ios::out); //用輸出的方式打開文件--寫文件
//將每個人的數據寫到文件中
for (int i = 0; i < this->m_EmpNum; i++)
{
ofs << this->m_EmpArray[i]->m_ID << " "
<< this->m_EmpArray[i]->m_Name << " "
<< this->m_EmpArray[i]->m_DeptID << endl;
}
//關閉文件
ofs.close();
}
//初始化職工
void WorkerManger::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 WorkerManger::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 WorkerManger::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 WorkerManger::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 WorkerManger::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 WorkerManger::Find_Emp()
{
if (this->m_FileIsEmpty)
{
cout << "文件不存愛或者記錄為空!" << endl;
}
else
{
cout << "請輸入查找方式:" << endl;
cout << "1、按職工編號查找" << endl;
cout << "2、按職工姓名查找" << endl;
int select;
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]->showInfo();
}
else
{
cout << "查找失敗!查無此人!" << endl;
}
}
else if(select == 2)
{
//按姓名
string name;
cout << "請輸入查找的姓名:";
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;
this->m_EmpArray[i]->showInfo();
flag = true;
}
}
if (flag == false)
{
cout << "查找失敗!查無此人!" << endl;
}
}
else
{
cout << "輸入的選項有誤!" << endl;
}
}
system("pause");
system("cls");
}
//排序
void WorkerManger::Sort_Emp()
{
if (this->m_FileIsEmpty)
{
cout << "文件不存在或記錄為空!" << endl;
system("pause");
system("cls");
}
else
{
cout << "請選擇排序方式:" << endl;
cout << "1、按照職工編號升序排列" << endl;
cout << "2、按照職工編號降序排列" << endl;
int select;
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]->m_ID > this->m_EmpArray[j]->m_ID)
{
MinOrMax = j;
}
}
else
{
if (this->m_EmpArray[MinOrMax]->m_ID < this->m_EmpArray[j]->m_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 WorkerManger::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");
}
WorkerManger::~WorkerManger()
{
if (this->m_EmpArray != NULL)
{
delete[] this->m_EmpArray;
this->m_EmpArray = NULL;
}
}
worker.h文件:
#pragma once
#include
using namespace std;
#include
//職工抽象類
class Worker
{
public:
//顯示個人信息
virtual void showInfo() = 0;
//獲取崗位名稱
virtual string getDeptName() = 0;
int m_ID; //職工編號
string m_Name; //職工姓名
int m_DeptID; //部門編號
};
employee.h文件:
//普通員工文件
#pragma once
#include
#include
using namespace std;
#include "worker.h"
class Employee : public Worker
{
public:
//構造函數
Employee(int id, string name, int dID);
//顯示個人信息
void showInfo();
//獲取崗位名稱
string getDeptName();
};
employee.cpp文件:
#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("員工");
}
Manager.h文件:
#pragma once
#include
#include "worker.h"
using namespace std;
//經理類
class Manager : public Worker
{
public:
//構造函數
Manager(int id, string name, int dId);
//顯示個人信息
virtual void showInfo();
//獲取崗位名稱
virtual string getDeptName();
};
Manager.cpp文件:
#include "Manager.h"
//構造函數
Manager::Manager(int id, string name, int dId)
{
this->m_ID = id;
this->m_Name = name;
this->m_DeptID = dId;
}
//顯示個人信息
void Manager::showInfo()
{
cout << "職工編號: " << this->m_ID
<< "\t職工姓名: " << this->m_Name
<< "\t崗位: " << this->getDeptName()
<< "\t崗位職責: 完成老板交給的任務并且下發任務給普通員工" << endl;
}
//獲取崗位名稱
string Manager::getDeptName()
{
return string("經理");
}
Boss.h文件:
#pragma once
#include
#include "worker.h"
using namespace std;
//經理類
class Boss : public Worker
{
public:
//構造函數
Boss(int id, string name, int dId);
//顯示個人信息
virtual void showInfo();
//獲取崗位名稱
virtual string getDeptName();
};
Boss.cpp文件:
#include "Boss.h"
//構造函數
Boss::Boss(int id, string name, int dId)
{
this->m_ID = id;
this->m_Name = name;
this->m_DeptID = dId;
}
//顯示個人信息
void Boss::showInfo()
{
cout << "職工編號: " << this->m_ID
<< "\t職工姓名: " << this->m_Name
<< "\t崗位: " << this->getDeptName()
<< "\t崗位職責: 管理公司所有事物" << endl;
}
//獲取崗位名稱
string Boss::getDeptName()
{
return string("老板");
}
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。