這是b站 黑馬程式員C++教程從0到1入門編程教程里面的職工管理系統專案,我學完以后想整理一下分享給大家,希望對碼友們有幫助,(一些變數名我的可能不是完全一樣,但是程式的架構和教程里面的一樣)
視頻教程鏈接 黑馬程式員c++入門教程-職工管理系統
目錄
1.案例分析
2、源代碼
1、workerMnager.h
2.Worker.h
1.employee.h
2.Employee.cpp
3.manager.h
4.Mnager.cpp
5.boss.h
6.Boss.cpp
3.職工管理系統.cpp
3.總結
1.案例分析
公司有三種員工分別為 普通員工、經理和老板
每個員工有 職工編號,姓名和部門編號
- 普通員工職責:完成經理交給的任務
- 經理職責:完成老板交給的任務,并下發任務給員工
- 老板職責:管理公司所有事務
要求實作如下8個功能:
- 退出管理程式
- 增加職工資訊
- 顯示職工資訊
- 洗掉離職職工
- 修改職工資訊
- 查找職工資訊
- 按照編號排序
- 清空所有檔案
這個專案利用了多型的性質,還有通過類外宣告等方式,
檔案架構如下:

首先在頭檔案里宣告類,然后創建cpp檔案實作類外實作,包括建構式,成員函式,在頭檔案只做函式或類的宣告,然后在cpp檔案寫函式體,
2、源代碼
1、workerMnager.h
#pragma once //是為了避免頭檔案命名沖突
#include<iostream> //輸入輸出流
#include "worker.h"
#include<fstream>
#define FILENAME "empFile.txt"
using namespace std;
class WorkerManager //創建類
{
public:
//建構式
WorkerManager();
//展示選單函式
void Show_Menu(); //頭函式里不能開大括號,不然系統會以為已經有了主體
//退出系統函式
void exitSystem();
//添加職工
void Add_Emp();
//記錄職工人數
int EmpNum ;
//職工陣列指標
Worker ** Emp_Arr;
//保存檔案函式
void save();
//判斷檔案是否為空
bool FileIsEmpty;
//統計檔案中的人數
int getEmpNum();
//初始化員工
void initEmp();
//顯示職工
void showEmp();
//洗掉員工
void DelEmp();
//判斷職工是否存在
int IsExit(int id);
//修改職工
void modEmp();
//查找職工
void FindEmp();
//排序職工
void sortEmp();
//清空檔案
void clean_File();
//解構式
~WorkerManager();
};
2、workerMnager.cpp
#include "workerManager.h" //直接呼叫寫好的頭函式
#include "employee.h"
#include "manager.h"
#include "boss.h"
//類外實作建構式
WorkerManager::WorkerManager()
{
//分三種情況初始化
//
//1、檔案不存在
ifstream ifs;
ifs.open(FILENAME, ios::in); //讀檔案
if (!ifs.is_open())
{
//cout << "檔案不存在" << endl;
this->EmpNum = 0;
this->Emp_Arr = NULL;
this->FileIsEmpty = true;
ifs.close();
return;
}
//2、檔案存在,資料為空
char ch;
ifs>>ch;
if (ifs.eof())
{
//檔案為空
//cout << "檔案為空!" << endl;
this->EmpNum = 0;
this->Emp_Arr = NULL;
this->FileIsEmpty = true;
ifs.close();
return;
}
//3、檔案存在且資料不為空
int num = this->getEmpNum();
//cout << "職工人數為:" << num << endl;
this->EmpNum = num;
//開辟空間
this->Emp_Arr = new Worker * [this->EmpNum];
//將檔案中的資料讀到陣列中
this->initEmp();
//測驗代碼
/*for (int i = 0; i < this->EmpNum; i++)
{
cout << "職工編號:" << this->Emp_Arr[i]->id
<< "姓名:" << this->Emp_Arr[i]->name
<< "部門編號:" << this->Emp_Arr[i]->did << endl;
}*/
};
//類外實作解構式
WorkerManager::~WorkerManager()
{
if (this->Emp_Arr != NULL)
{
delete[] this->Emp_Arr;
this->Emp_Arr = NULL;
}
};
//類外實作選單函式
void WorkerManager::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 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->EmpNum + addNum;
//開辟空間
Worker **newSpace=new Worker* [newSize];
//將原來的空間的資料拷貝到新空間上
if (this->Emp_Arr!=NULL)
{
for (int i = 0; i < this->EmpNum; i++)
{
newSpace[i] = this->Emp_Arr[i];
}
}
//開始添加新的資料
for (int i = 0; i < addNum; i++)
{
int id;
string name;
int did;
cout << "請輸入第" << i + 1 << "個新職工編號:" << endl;
cin >> id;
cout << "請輸入該職工姓名:" << endl;
cin >> name;
cout << "請選擇該職工崗位:" << endl;
cout << "1、普通員工" << endl;
cout << "2、經理" << endl;
cout << "3、老板" << endl;
Worker * worker_1 = NULL;
cin >> did;
switch (did)
{
case 1:
worker_1 = new Employee(id,name,did);
case 2:
worker_1 = new Manager(id, name, did);
case 3:
worker_1 = new Boss(id, name, did);
deault:
break;
}
//將創建的職工指標保存到陣列中
newSpace[this->EmpNum + i] = worker_1;
}
//釋放原有空間
delete[] this->Emp_Arr;
//更改新空間的指向
this->Emp_Arr = newSpace;
//更新新空間人數
this->EmpNum = newSize;
//更新一下檔案不為空的情況
this->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->EmpNum; i++)
{
ofs << this->Emp_Arr[i]->id << " "
<< this->Emp_Arr[i]->name << " "
<< this->Emp_Arr[i]->did << " " << endl;
}
//關閉檔案
ofs.close();
}
//統計檔案中的人數
int WorkerManager::getEmpNum()
{
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::initEmp()
{
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, 1);
}
else if (did == 2)
{
worker = new Manager(id, name, 2);
}
else if(did==3)
{
worker = new Boss(id, name, 3);
}
//存放在陣列中
this->Emp_Arr[index] = worker;
index++;
}
}
//顯示職工
void WorkerManager::showEmp()
{
if (FileIsEmpty)
{
cout << "檔案不存在或記錄為空!" << endl;
}
else
{
for (int i = 0; i < this->EmpNum; i++)
{
//利用多型呼叫介面
this->Emp_Arr[i]->showInfo();
}
}
system("pause");
system("cls");//按任意鍵以后回到選單選擇狀態
}
//洗掉員工
void WorkerManager::DelEmp()
{
if (FileIsEmpty)
{
cout << "檔案不存在或記錄為空!" << endl;
}
else
{
//按照職工編號洗掉
cout << "請輸入想要洗掉職工編號:" << endl;
int id = 0;
cin >> id;
int index = this->IsExit(id);
if (index != -1)
{
for (int i = index; i < this->EmpNum-1; i++)
{
this->Emp_Arr[i]=this->Emp_Arr[i+1];
}
this->EmpNum--; //更新一下陣列中記錄的人員個數
//同步更新到檔案
this->save();
cout << "洗掉成功" << endl;
}
else
{
cout << "洗掉失敗,未找到該職工" << endl;
}
}
system("pause");
system("cls");//按任意鍵以后回到選單選擇狀態
}
//判斷職工是否存在
int WorkerManager::IsExit(int id)
{
int index = -1;
for (int i = 0; i < this->EmpNum; i++)
{
if (this->Emp_Arr[i]->id == id)
{
index = i;
break;
}
}
return index;
}
//修改職工
void WorkerManager::modEmp()
{
if (FileIsEmpty)
{
cout << "檔案不存在或記錄為空!" << endl;
}
else
{
//按照職工編號洗掉
cout << "請輸入想要修改職工編號:" << endl;
int id = 0;
cin >> id;
int ret = this->IsExit(id);
if (ret != -1)
{
//查找到該編號的職工
delete this->Emp_Arr[ret];
int newid=0;
string newname="";
int newdid=0;
cout << "查到" << id << "號職工,請重新輸入新職工號:" << endl;
cin >> newid;
cout << "請輸入新姓名:" << endl;
cin >> newname;
cout << "請輸入崗位:" << endl;
cout << "1、普通職工" << endl;
cout << "2、經理" << endl;
cout << "3、老板" << endl;
cin >> newdid;
Worker* worker = NULL;
switch (newdid)
{
case 1:
worker = new Employee(newid, newname, newdid);
case 2:
worker = new Manager(newid, newname, newdid);
case 3:
worker = new Boss(newid, newname, newdid);
deault:
break;
}
//更新資料到陣列中
this->Emp_Arr[ret] = worker;
cout << "修改成功!" << Emp_Arr[ret]->did << endl;
this->save();
cout << "洗掉成功" << endl;
}
else
{
cout << "修改失敗,未找到該職工" << endl;
}
}
system("pause");
system("cls");//按任意鍵以后回到選單選擇狀態
}
//查找職工
void WorkerManager::FindEmp()
{
if (FileIsEmpty)
{
cout << "檔案不存在或記錄為空!" << endl;
}
else
{
//按照職工編號洗掉
cout << "請輸入查找方式:" << endl;
cout << "1、按職工號查找" << endl;
cout << "2、按職工姓名查找" << endl;
int select = 0;
cin >> select;
if (select == 1)
{
cout << "請輸入想要查找職工編號:" << endl;
int id = 0;
cin >> id;
int ret = this->IsExit(id);
if (ret != -1)
{
cout << "查找成功,該職工資訊如下:" << endl;
this->Emp_Arr[ret]->showInfo();
}
else
{
cout << "修改失敗,未找到該職工" << endl;
}
}
else if (select == 2)
{
cout << "請輸入想要查找職工姓名:" << endl;
string name;
cin >> name;
bool flag = false; //查找到標志
for (int i = 0; i < this->EmpNum; i++)
{
if (Emp_Arr[i]->name == name)
{
cout << "查找成功,職工編號為:" << Emp_Arr[i]->id
<< ",職工的資訊如下" << endl;
flag = true;
this->Emp_Arr[i]->showInfo();
}
}
if (flag == false)
{
cout << "修改失敗,未找到該職工" << endl;
}
}
else
{
cout << "輸入項有誤" << endl;
}
}
system("pause");
system("cls");//按任意鍵以后回到選單選擇狀態
}
//排序職工
void WorkerManager::sortEmp()
{
if (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->EmpNum - 1; i++)
{
int minOrmax = i;
for (int j = i+1; j < this->EmpNum ; j++)
{
if (select == 1) //升序
{
if (this->Emp_Arr[minOrmax]->id > Emp_Arr[j]->id)
{
minOrmax = j;
}
}
else //降序
{
if (this->Emp_Arr[minOrmax]->id < Emp_Arr[j]->id)
{
minOrmax = j;
}
}
}
if (i != minOrmax)
{
Worker * temp = Emp_Arr[i];
Emp_Arr[i] = Emp_Arr[minOrmax];
Emp_Arr[minOrmax] = temp;
}
}
cout << "排序成功,排序后的結果為:" << endl;
this->save();
this->showEmp();
}
}
//清空檔案
void WorkerManager::clean_File()
{
cout << "取人清空?" << endl;
cout << "1、確認" << endl;
cout << "2、回傳" << endl;
int select;
cin >> select;
if (select == 1)
{
//打開模式ios::trunc 如果存在洗掉檔案并重新創建
ofstream ofs(FILENAME,ios::trunc);
ofs.close();
if (this->Emp_Arr != NULL)
{
for (int i = 0; i < this->EmpNum; i++)
{
if (this->Emp_Arr[i] != NULL)
{
delete this->Emp_Arr[i];
}
}
this->EmpNum = 0;
delete[] this->Emp_Arr;
this->Emp_Arr = NULL;
this->FileIsEmpty = true;
}
cout << "清空成功!" << endl;
}
system("pause");
system("cls");
}
2.Worker.h
#pragma once
#include<iostream>
#include<string>
using namespace std;
//職工抽象基類
class Worker
{
public:
//顯示個人資訊
virtual void showInfo()=0;
//獲取崗位名稱
virtual string getDeptName() = 0;
int id; // 職工編號
string name; //職工姓名
int did; //職工所在部門編號
};
1.employee.h
#pragma once
#include<iostream>
#include<string>
using namespace std;
#include "worker.h"
//員工類
class Employee:public Worker
{
public:
//建構式
Employee(int id, string name, int did);
//顯示個人資訊
virtual void showInfo();
//獲取職工崗位名稱
virtual string getDeptName();
};
2.Employee.cpp
#include "employee.h"
Employee::Employee(int id, string name, int did)
{
this->id = id;
this->name = name;
this->did = did;
}
void Employee::showInfo()
{
cout << "職工編號: " << this->id
<< "\t職工姓名: " << this->name
<< "\t崗位: " << this->getDeptName()
<< "\t崗位職責:完成經理交給的任務" << endl;
}
string Employee::getDeptName()
{
if (did == 1)
{
return string("普通員工");
}
else if (did == 2)
{
return string("經理");
}
else
{
return string("總裁");
}
}
3.manager.h
#pragma once
#include<iostream>
#include<string>
using namespace std;
#include "worker.h"
class Manager :public Worker
{
public:
//建構式
Manager(int id, string name, int did);
//顯示個人資訊
virtual void showInfo();
//獲取職工崗位名稱
virtual string getDeptName();
};
4.Mnager.cpp
#include "manager.h"
Manager::Manager(int id, string name, int did)
{
this->id = id;
this->name = name;
this->did = did;
}
void Manager::showInfo()
{
cout << "職工編號: " << this->id
<< "\t職工姓名: " << this->name
<< "\t崗位: " << this->getDeptName()
<< "\t崗位職責:完成老板交給的任務,并下發給員工" << endl;
}
string Manager::getDeptName()
{
if (did == 1)
{
return string("普通員工");
}
else if (did == 2)
{
return string("經理");
}
else
{
return string("總裁");
}
}
5.boss.h
#pragma once
#include<iostream>
#include<string>
using namespace std;
#include "worker.h"
class Boss :public Worker
{
public:
//建構式
Boss(int id, string name, int did);
//顯示個人資訊
virtual void showInfo();
//獲取職工崗位名稱
virtual string getDeptName();
};
6.Boss.cpp
#include "boss.h"
Boss::Boss(int id, string name, int did)
{
this->id = id;
this->name = name;
this->did = did;
}
void Boss::showInfo()
{
cout << "職工編號: " << this->id
<< "\t職工姓名: " << this->name
<< "\t崗位: " << this->getDeptName()
<< "\t崗位職責:管理公司所有事務" << endl;
}
string Boss::getDeptName()
{
if (did == 1)
{
return string("普通員工");
}
else if (did == 2)
{
return string("經理");
}
else
{
return string("總裁");
}
}
3.職工管理系統.cpp
#include<iostream>
using namespace std;
#include "workerManager.h"
#include "worker.h"
#include "employee.h"
#include "manager.h"
#include "boss.h"
int main()
{
//測驗代碼
/*Worker* worker;
worker = new Employee(1, "張三", 1);
worker->showInfo();
delete worker;
worker = new Manager(2, "李四", 2);
worker->showInfo();
delete worker;
worker = new Boss(3, "王五", 3);
worker->showInfo();
delete worker;*/
//實體化管理者物件
WorkerManager 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.showEmp();
break;
case 3://洗掉離職職工
wm.DelEmp();
break;
case 4://修改職工資訊
wm.modEmp();
break;
case 5://查找職工資訊
wm.FindEmp();
break;
case 6://按照編號排序
wm.sortEmp();
break;
case 7://清空所有檔案
wm.clean_File();
break;
default:
system("cls");
break;
}
}
system("pause");
return 0;
}
3.總結
難點:陣列,指標,多型
檔案操作可以看視頻教程的前幾個視頻,
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/299481.html
標籤:其他
