C++分步實(shí)現(xiàn)職工管理系統(tǒng)詳解
1.職工管理系統(tǒng)的需求
本教程主要利用C++實(shí)現(xiàn)一個(gè)職工管理系統(tǒng)
公司職工分類:普通員工,經(jīng)理,老板,顯示信息時(shí),需要顯示職工的編號(hào),職工姓名,職工崗位,以及職責(zé)。
普通員工的職責(zé):完成經(jīng)理交給的任務(wù)。
經(jīng)理職責(zé):完成老板交給的任務(wù),并下發(fā)任務(wù)給員工。
老板職責(zé):管理公司所有事物。
| 程序 | 功能 |
|---|---|
| 退出管理程序 | 退出當(dāng)前管理系統(tǒng) |
| 增加職工信息 | 實(shí)現(xiàn)批量添加職工功能,將信息錄入到文件中,職工信息為:職工編號(hào),姓名,部門編號(hào) |
| 顯示職工信息 | 顯示公司內(nèi)部所有職工信息 |
| 刪除離職職工 | 按照編號(hào)刪除指定的職工 |
| 修改職工信息 | 按照職工的編號(hào)或者職工的姓名查找相關(guān)的人員信息 |
| 按照編號(hào)排序 | 按照職工編號(hào),進(jìn)行排序,排序規(guī)則由用戶指定 |
| 清空所有文檔 | 清空文件中記錄的所有職工信息 |
2.功能實(shí)現(xiàn)
2.1創(chuàng)建管理類
class Worker_Manger
{
public:
Worker_Manger();
//展示菜單
void Dispaly() ;
//退出系統(tǒng)
void ExitSystem();
//記錄職工的人數(shù)
int M_EmpNum=0;
//職工數(shù)組指針
Worker ** M_EmpArray=0;
//添加職工
void Add_Emp();
//判斷是否編號(hào)重復(fù),姓名可能相同
bool If_Id_Repeat(int id);
//保存文件
void Save();
//判斷文件是否為空
bool M_FileEmpty=0;
//統(tǒng)計(jì)文件中的人數(shù)
int Get_EmpNum();
//初始化員工
void init_Emp();
//顯示職工
void Show_Emp();
//刪除職工
void Delete_Emp();
//判斷是否存在要查找的職工
int IsExit_Id(int id);//Id查找方式
int IsExit_Name(string name);//Name查找方式
string IsExit_Id_Name_choice(int cho);
//查找聯(lián)系人
void Find_Emp_Name();
void Find_Emp_Id();
void Find_Emp();
//修改職工信息
void Modify();
//編號(hào)排序
void Sort_Emp();
//清空操作
void Clean_File();
//析構(gòu)函數(shù)
~Worker_Manger();
};2.2退出功能
void Worker_Manger::ExitSystem()
{
cout << "歡迎下次使用" << endl;
system("pause");
exit(0);
}
2.3增加聯(lián)系人信息
oid Worker_Manger::Add_Emp()
{
cout << "請(qǐng)輸入添加的職工數(shù)量:" << endl;
int addnum = 0;//保存用戶的輸入數(shù)量
cin >> addnum;
if (addnum > 0)
{
int newSize = this->M_EmpNum + addnum;//新空間人數(shù)=原來的人數(shù)+新增加人數(shù)
//開辟新空間
Worker ** newspace = new Worker * [newSize+4];
//拷貝原來空間的數(shù)據(jù)
if (this->M_EmpArray != NULL)
{
for (int i = 0; i < this->M_EmpNum;i++)
{
newspace[i] = this->M_EmpArray[i];
}
}
//添加新數(shù)據(jù)
for (int i = 0; i < addnum; i++)
{
int id;//職工編號(hào)
string name;//職工的姓名
int dSelect;//部門選擇
cout << "請(qǐng)輸入第" << i + 1 << "個(gè)職工編號(hào):" << endl;
cin >>id;
//If_Id_Repeat(id);
while (If_Id_Repeat(id))
{
int cho = IsExit_Id(id);//Id查找方式
cout << "通訊錄已經(jīng)有相同的編號(hào),請(qǐng)重新輸入" << endl;
this->M_EmpArray[cho]->Showinformation();
cin >> id;
}
cout << "請(qǐng)輸入第" << i + 1 << "個(gè)職工姓名:" << endl;
cin >> name;
cout << "請(qǐng)選擇該職工的崗位:" << endl;
cout << "1.職員" << endl;
cout << "2.經(jīng)理" << 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;
}
//將創(chuàng)建職工職責(zé),保存到數(shù)組中
newspace[this->M_EmpNum + i] = worker;
}
//釋放空間
delete[] this->M_EmpArray;
//更改新空間的指向
this->M_EmpArray = newspace;
//更新新空間的人數(shù)
this->M_EmpNum = newSize;
//更新職工不為空的情況標(biāo)志
this->M_FileEmpty = false;
//提示添加成功
cout << "成功添加" << addnum << "名新職工" << endl;
//保存數(shù)據(jù)
this->Save();
}
else
{
cout << "輸入有誤" << endl;
}
system("pause");
system("cls");
}2.4顯示職工信息
void Worker_Manger::Show_Emp()
{
//判斷文件是否為空
if (this->M_FileEmpty)
{
cout << "文件不存在或文件為空!" << endl;
}
else
{
for (int i = 0; i < M_EmpNum; i++)
{
//利用多態(tài)
this->M_EmpArray[i]->Showinformation();
}
}
//按任意鍵清屏
system("pause");
system("cls");
//Name查找方式
int Worker_Manger::IsExit_Name(string name)
{
int index = -1;
for (int i = 0; i < this->M_EmpNum; i++)
{
if (this->M_EmpArray[i]->M_name == name)
{
//找到
index = i;
break;
}
}
return index;
}
//Id查找方式
int Worker_Manger::IsExit_Id(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;
}
}2.5刪除離職職工
void Worker_Manger::Delete_Emp()
{
if (this->M_FileEmpty)
{
cout << "文件不存在或者為空" << endl;
}
else
{
int choose=0;
cout << "請(qǐng)選擇通過方式幾查找要?jiǎng)h除的聯(lián)系人" << endl;
cout << "1.通過姓名查找要?jiǎng)h除的聯(lián)系人" << endl;
cout << "2.通過編號(hào)查找要?jiǎng)h除的聯(lián)系人" << endl;
cin >> choose;
switch (choose)
{
case 1:
{
cout << "請(qǐng)輸入要?jiǎng)h除職工的姓名" << endl;
string name;
cin >> name;
int index = this->IsExit_Name(name);
if (index != -1)
{
string ch = IsExit_Id_Name_choice(index);
if (ch == "是")
{
for (int i = index; i < this->M_EmpNum - 1; i++)
{
this->M_EmpArray[i] = this->M_EmpArray[i + 1];
}
this->M_EmpNum--;
//數(shù)據(jù)同步更新文件當(dāng)中
this->Save();
cout << "刪除成功!" << endl;
}
else
{
cout << "取消成功" << endl;
break;
}
}
else
{
cout << "未找到此人,刪除失敗!" << endl;
break;
}
}
case 2:
{
cout << "請(qǐng)輸入要?jiǎng)h除職工的編號(hào)" << endl;
int id=0;
cin >>id;
int index=this->IsExit_Id(id);
if (index != -1)
{
string ch= IsExit_Id_Name_choice(index);
if (ch == "是")
{
for (int i = index; i < this->M_EmpNum - 1; i++)
{
this->M_EmpArray[i] = this->M_EmpArray[i + 1];
}
this->M_EmpNum--;
//數(shù)據(jù)同步更新文件當(dāng)中
this->Save();
cout << "刪除成功!" << endl;
}
else
{
break;
}
}
else
{
cout << "未找到此人,刪除失敗!" << endl;
break;
}
}
}
}
//按照任意鍵返回
system("pause");
system("cls");
}2.6修改職工信息
void Worker_Manger::Modify()
{
if (this->M_FileEmpty)
{
cout << "文件不存在或記錄為空" << endl;
}
else
{
int choose = 0;
cout << "請(qǐng)選擇對(duì)應(yīng)方式來查找要修改的聯(lián)系人" << endl;
cout << "1.通過姓名查找要修改的聯(lián)系人" << endl;
cout << "2.通過編號(hào)查找要修改的聯(lián)系人" << endl;
cin >> choose;
switch (choose)
{
case 1:
{
cout << "請(qǐng)輸入修改的職工的姓名:" << endl;
string m_name = " ";
cin >> m_name;
int ret1 = this->IsExit_Name(m_name);
if (ret1 != -1)
{
string str1 = IsExit_Id_Name_choice(ret1);
if (str1 == "是")
{
//查找到編號(hào)的職工
delete this->M_EmpArray[ret1];
int newId = 0;
string newName = " ";
int dSelect = 0;
cout << "輸入新的職工編號(hào)" << endl;
cin >> newId;
If_Id_Repeat(newId);
while (If_Id_Repeat(newId))
{
int cho= IsExit_Id(newId);//Id查找方式
cout << "通訊錄已經(jīng)有相同的編號(hào),請(qǐng)重新輸入" << endl;
this->M_EmpArray[cho]->Showinformation();
//cout << "職工編號(hào):" << this->M_EmpArray[cho]->M_id
// << "\t職工姓名:" << this->M_EmpArray[cho]->M_name
// << "\t崗位名稱:" << this->M_EmpArray[cho]->GetDeptname()
// << "\t崗位職責(zé):" << this->M_EmpArray[cho]->GetDeptduty()
// << endl;
cin >> newId;
}
cout << "輸入新的姓名" << endl;
cin >> newName;
cout << "請(qǐng)輸入新的崗位" << endl;
cout << "1.職員" << endl;
cout << "2.經(jīng)理" << endl;
cout << "3.總裁" << endl;
cin >> dSelect;
Worker* worker = NULL;
switch (dSelect)
{
case 1:
worker = new Employee(newId, newName, 1);
break;
case 2:
worker = new Manager(newId, newName, 2);
break;
case 3:
worker = new Boss(newId, newName, 3);
break;
default:
break;
}
this->M_EmpArray[ret1] = worker;
cout << "更改信息成功" << endl;
//保存到文件
this->Save();
}
else if(str1 == "否")
{
cout << "取消成功!" << endl;
}
}
else
{
cout << "修改失敗,查無此人!" << endl;
}
break;
}
case 2:
{
cout << "請(qǐng)輸入修改的職工的編號(hào):" << endl;
int id;
cin >> id;
int ret2 = this->IsExit_Id(id);
if (ret2 != -1)
{
string str2 = IsExit_Id_Name_choice(ret2);
//查找到編號(hào)的職工
if (str2 == "是")
{
delete this->M_EmpArray[ret2];
int newId = 0;
string newName = " ";
int dSelect = 0;
cout << "輸入新的職工編號(hào)" << endl;
cin >> newId;
If_Id_Repeat(newId);
while (If_Id_Repeat(newId))
{
int cho = IsExit_Id(newId);//Id查找方式
cout << "通訊錄已經(jīng)有相同的編號(hào),請(qǐng)重新輸入" << endl;
this->M_EmpArray[cho]->Showinformation();
//cout << "職工編號(hào):" << this->M_EmpArray[cho]->M_id
// << "\t職工姓名:" << this->M_EmpArray[cho]->M_name
// << "\t崗位名稱:" << this->M_EmpArray[cho]->GetDeptname()
// << "\t崗位職責(zé):" << this->M_EmpArray[cho]->GetDeptduty()
// << endl;
cin >> newId;
}
cout << "輸入新的姓名" << endl;
cin >> newName;
cout << "請(qǐng)輸入新的崗位" << endl;
cout << "1.職員" << endl;
cout << "2.經(jīng)理" << endl;
cout << "3.總裁" << endl;
cin >> dSelect;
Worker* worker = NULL;
switch (dSelect)
{
case 1:
worker = new Employee(newId, newName, 1);
break;
case 2:
worker = new Manager(newId, newName, 2);
break;
case 3:
worker = new Boss(newId, newName, 3);
break;
default:
break;
}
this->M_EmpArray[ret2] = worker;
cout << "更改信息成功" << endl;
//保存到文件
this->Save();
}
else if (str2 == "否")
{
cout << "取消成功!" << endl;
}
}
else
{
cout << "修改失敗,查無此人!" << endl;
}
break;
}
}
system("pause");
system("cls");
}
}2.7查找職工信息
void Worker_Manger::Find_Emp()
{
cout << "請(qǐng)選擇查找方式" << endl;
cout << "1.通過姓名查找聯(lián)系人" << endl;
cout << "2.通過編號(hào)查找聯(lián)系人" << endl;
int choose = 0;
cin >> choose;
switch (choose)
{
case 1:Find_Emp_Name();
break;
case 2:Find_Emp_Id();
break;
default:
break;
}
}2.8按照編號(hào)排序
void Worker_Manger::Sort_Emp()
{
if (this->M_FileEmpty)
{
cout << "文件不存在或記錄為空" << endl;
system("pause");
system("cls");
}
cout << "選擇排序的方式" << endl;
cout << "1.按照編號(hào)升序排列" << endl;
cout << "2.按照編號(hào)進(jìn)行降序排列" << endl;
int choose = 0;
cin >> choose;
switch (choose)
{
case 1:
{
for (int i = 0; i < this->M_EmpNum; i++)
{
int MIN=i;
for (int j = i+1; j < this->M_EmpNum; j++)
{
if (this->M_EmpArray[MIN]->M_id>this->M_EmpArray[j]->M_id)
{
MIN = j;
}
}
if (i != MIN)
{
Worker* temp = M_EmpArray[i];
M_EmpArray[i] = M_EmpArray[MIN];
M_EmpArray[MIN] = temp;
}
}
this->Save();
this->Show_Emp();
break;
}
case 2:
{
for (int i = 0; i < this->M_EmpNum; i++)
{
int MAX = i;
for (int j = i + 1; j <this->M_EmpNum; j++)
{
if (this->M_EmpArray[MAX]->M_id < this->M_EmpArray[j]->M_id)
{
MAX = j;
}
}
if (i != MAX)
{
Worker* temp = M_EmpArray[i];
M_EmpArray[i] = M_EmpArray[MAX];
M_EmpArray[MAX] = temp;
}
}
this->Save();
this->Show_Emp();
break;
}
default:
break;
}
}
2.9清空所有文檔
void Worker_Manger::Clean_File()
{
cout << "確定清空?" << endl;
cout << "1.確定清空" << endl;
cout << "2.取消清空" << endl;
int select = 0;
cin >> select;
if (select == 1)
{
ofstream ofs(FILENAME, ios::trunc);//刪除文件后再重新創(chuàng)建
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_EmpNum = 0;
this->M_EmpArray = NULL;
this->M_FileEmpty = true;
}
cout << "清空成功" << endl;
}
system("pause");
system("cls");
}3.代碼下載
鏈接: https://pan.baidu.com/s/1irv2cVgShCpZILEp5-VWEw?pwd=65qj
提取碼: 65qj
到此這篇關(guān)于C++分步實(shí)現(xiàn)職工管理系統(tǒng)詳解的文章就介紹到這了,更多相關(guān)C++職工管理內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C語言實(shí)現(xiàn)打印楊輝三角的方法詳細(xì)(三種方法)
楊輝三角是中國古代數(shù)學(xué)的杰出研究成果之一,它把二項(xiàng)式系數(shù)圖形化,把組合數(shù)內(nèi)在的一些代數(shù)性質(zhì)直觀地從圖形中體現(xiàn)出來,是一種離散型的數(shù)與形的結(jié)合。本文將介紹三種可以實(shí)現(xiàn)打印楊輝三角的辦法,感興趣的可以試一試2022-01-01
C++調(diào)用matlab引擎實(shí)現(xiàn)三維圖的繪制
這篇文章主要為大家詳細(xì)介紹了C++如何調(diào)用matlab引擎實(shí)現(xiàn)三維圖的繪制,文中的示例代碼講解詳細(xì),對(duì)我們學(xué)習(xí)C++和Matlab有一定的幫助,需要的可以參考一下2022-12-12
C++中結(jié)構(gòu)體和Json字符串互轉(zhuǎn)的問題詳解
這篇文章主要給大家介紹了關(guān)于C++中結(jié)構(gòu)體和Json字符串互轉(zhuǎn)問題的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-03-03
你不知道的C++中namespace和using的用法實(shí)例
在C++語言編寫的程序中,變量和函數(shù)等的作用范圍是有一定限制的,下面這篇文章主要給大家介紹了一些你不知道的C++中namespace和using的用法,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-12-12
select函數(shù)實(shí)現(xiàn)高性能IO多路訪問的關(guān)鍵示例深入解析
這篇文章主要為大家介紹了select函數(shù)實(shí)現(xiàn)高性能IO多路訪問的關(guān)鍵示例深入解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-09-09
用c語言實(shí)現(xiàn)HUP信號(hào)重啟進(jìn)程的方法
本篇文章是對(duì)使用c語言實(shí)現(xiàn)HUP信號(hào)重啟進(jìn)程的方法進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-05-05

