您好,登錄后才能下訂單哦!
這篇文章主要介紹了基于C++如何實現(xiàn)職工管理系統(tǒng)的相關(guān)知識,內(nèi)容詳細易懂,操作簡單快捷,具有一定借鑒價值,相信大家閱讀完這篇基于C++如何實現(xiàn)職工管理系統(tǒng)文章都會有所收獲,下面我們一起來看看吧。
職工管理系統(tǒng)可以用來管理公司內(nèi)所有員工的信息
利用C++來實現(xiàn)一個基于多態(tài)的職工管理系統(tǒng)
公司中職工分為三類:普通員工、經(jīng)理、老板,顯示信息時,需要顯示職工編號、職工姓名、職工崗位、以及職責
普通員工職責:完成經(jīng)理交給的任務(wù)
經(jīng)理職責:完成老板交給的任務(wù),并下發(fā)任務(wù)給員工
老板職責:管理公司所有事務(wù)
管理系統(tǒng)中需要實現(xiàn)的功能如下:
退出管理程序:退出當前管理系統(tǒng)
增加職工信息:實現(xiàn)批量添加職工功能,將信息錄入到文件中,職工信息為:職工編號、姓名、部門編號
顯示職工信息:顯示公司內(nèi)部所有職工的信息
刪除離職職工:按照編號刪除指定的職工
修改職工信息:按照編號修改職工個人信息
查找職工信息:按照職工的編號或者職工的姓名進行查找相關(guān)的人員信息
按照編號排序:按照職工編號,進行排序,排序規(guī)則由用戶指定
清空所有文檔:清空文件中記錄的所有職工信息 (清空前需要再次確認,防止誤刪)
我們創(chuàng)建以下多個頭文件和與其對應(yīng)的 .cpp文件:
boss.h頭文件
#pragma once #include <iostream> using namespace std; #include <string> #include "Worker.h" //老板類 class Boss :public Worker { public: Boss(int id, string name, int post); //顯示個人信息 virtual void ShowInfo(); //獲取崗位名稱 virtual string getPost(); };
manager.h 頭文件
class Manager :public Worker { public: Manager(int id, string name, int post); //顯示個人信息 virtual void ShowInfo(); //獲取崗位名稱 virtual string getPost(); };
employee.h頭文件
class Employee:public Worker { public: Employee(int id,string name,int post); //顯示個人信息 virtual void ShowInfo(); //獲取崗位名稱 virtual string getPost(); };
worker.h頭文件
//職工抽象類 class Worker { public: //顯示個人信息 virtual void ShowInfo() = 0; //獲取崗位名稱 virtual string getPost() = 0; //職員編號 int m_Id; //職工姓名 string m_Name; //部門編號 int m_Post; };
worker_Manager.h頭文件
#pragma once #include <iostream> using namespace std; #include "boss.h" #include "manager.h" #include "Worker.h" #include "employee.h" #include <fstream> #define TXT "empfine.txt" //各程序執(zhí)行的類 class Worker_Manager { public: Worker_Manager(); //顯示菜單 void ShowMenu(); //記錄職工人數(shù) int m_EmpNum; //職工指針數(shù)組 Worker** m_EmpArray; //添加職工 void Add_Emp(); //保存文件 void save(); //判斷文件是否為空 bool m_File; //統(tǒng)計文件中的人數(shù) int get_EmpNum(); //初始化員工 void In_Emp(); //顯示員工 void Show_Emp(); //判斷職工是否存在 int Ison_Emp(int id); //刪除員工 void Del_Emp(); //修改員工 void Mod_Emp(); //查找員工 void Find_Emp(); //排序 void Sort_Emp(); //清空文件 void Clean_File(); ~Worker_Manager(); };
boss.cpp文件
#include "boss.h" Boss::Boss(int id, string name, int post) { this->m_Id = id; this->m_Name = name; this->m_Post = post; } //顯示個人信息 void Boss::ShowInfo() { cout << "職工編號:" << this->m_Id << "\t職工姓名:" << this->m_Name << "\t職工部門:" << this->getPost() << "\t崗位職責:管理公司所有事務(wù)" << endl; } //獲取崗位名稱 string Boss::getPost() { return string("老板"); }
manager.cpp文件
#include "manager.h" Manager::Manager(int id, string name, int post) { this->m_Id = id; this->m_Name = name; this->m_Post = post; } //顯示個人信息 void Manager::ShowInfo() { cout << "職工編號:" << this->m_Id << "\t職工姓名:" << this->m_Name << "\t職工部門:" << this->getPost() << "\t崗位職責:完成老板布置的任務(wù),并下發(fā)任務(wù)給員工" << endl; } //獲取崗位名稱 string Manager::getPost() { return string("經(jīng)理"); }
employee.cpp文件
#include "employee.h" Employee::Employee(int id, string name, int post) { this->m_Id = id; this->m_Name = name; this->m_Post = post; } //顯示個人信息 void Employee::ShowInfo() { cout << "職工編號:" << this->m_Id << "\t職工姓名:" << this->m_Name << "\t職工部門:" << this->getPost() << "\t崗位職責:完成經(jīng)理布置的任務(wù)" << endl; } //獲取崗位名稱 string Employee::getPost() { return string("員工"); }
worker_Manager.cpp文件
#include "worker_Manager.h" Worker_Manager::Worker_Manager() { //1、文件不存在 ifstream ifs; ifs.open(TXT, ios::in); if (!ifs.is_open()) { //cout << "文件不存在" << endl; //初始化屬性 //初始化記錄人數(shù) this->m_EmpNum = 0; //初始化指針數(shù)組 this->m_EmpArray = NULL; //初始化文件是否為空 this->m_File = true; ifs.close(); return; } //2、文件存在,但為空 char ch; ifs >> ch; if (ifs.eof()) { //cout << "文件為空" << endl; //初始化屬性 //初始化記錄人數(shù) this->m_EmpNum = 0; //初始化指針數(shù)組 this->m_EmpArray = NULL; //初始化文件是否為空 this->m_File = true; ifs.close(); return; } //3.文件存在且數(shù)據(jù)不為空 int num = this->get_EmpNum(); this->m_EmpNum = num; //開辟空間 this->m_EmpArray = new Worker * [this->m_EmpNum]; //將文件中的數(shù)據(jù),存到數(shù)組中 this->In_Emp(); } void Worker_Manager::ShowMenu() { cout << "****************************************" << endl; cout << "******* 歡迎來到職工管理系統(tǒng)!******" << 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; } void Worker_Manager::Add_Emp() { cout << "請輸入要添加職工數(shù)量:" << endl; //保存用戶的輸入數(shù)量 int addNum = 0; cin >> addNum; if (addNum > 0) { //計算添加新空間的大小 int newSize = this->m_EmpNum + addNum;//新空間人數(shù)=原來的人數(shù)+新增加的人數(shù) //開辟新空間 Worker** newSpace= new Worker * [newSize]; //將原來的數(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; string name; int post; cout << "請輸入第 " << i + 1 << "個新職工的編號: " << endl; cin >> id; cout << "請輸入第 " << i + 1 << "個新職工的姓名: " << endl; cin >> name; cout << "請選擇職工崗位: " << endl; cout << "1、普通員工" << endl; cout << "2、經(jīng)理" << endl; cout << "3、老板" << endl; cin >> post; Worker* worker = NULL; switch (post) { case 1: worker = new Employee(id, name, post); break; case 2: worker = new Manager(id, name, post); break; case 3: worker = new Boss(id, name, post); break; default: cout << "錯誤輸入" << endl; break; } //將創(chuàng)建的職工指針,保存到數(shù)組中 newSpace[this->m_EmpNum + i] = worker; } //釋放原指針 delete[] this->m_EmpArray; //更改新空間的指向 this->m_EmpArray = newSpace; //更新職工人數(shù) this->m_EmpNum = newSize; cout << "成功添加" << addNum << "名新職工" << endl; this->m_File = false; //保存數(shù)據(jù)到文件中 this->save(); } else { cout << "輸入有誤" << endl; } system("pause"); system("cls"); } void Worker_Manager::save() { ofstream ofs; ofs.open(TXT, 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_Post<<" " << endl; } ofs.close(); } int Worker_Manager::get_EmpNum() { ifstream ifs; ifs.open(TXT, ios::in); int id; string name; int post; int num = 0; while (ifs >> id && ifs >> name && ifs >> post) { num++; } return num; } void Worker_Manager::In_Emp() { ifstream ifs; ifs.open(TXT, ios::in); int id; string name; int post; int index = 0; while (ifs>>id && ifs>>name && ifs>>post) { Worker* worker = NULL; if (post == 1) { worker = new Employee(id, name, post);//普通員工 } else if (post == 2) { worker = new Manager(id, name, post);//經(jīng)理 } else { worker = new Boss(id, name, post);//老板 } this->m_EmpArray[index] = worker; index++; } ifs.close(); } void Worker_Manager::Show_Emp() { if (this->m_File) { cout << "該文件不存在或為空" << endl; } else { for (int i = 0; i < this->m_EmpNum; i++) { //利用多態(tài)調(diào)用 this->m_EmpArray[i]->ShowInfo(); } } //清屏 system("pause"); system("cls"); } int Worker_Manager::Ison_Emp(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 Worker_Manager::Del_Emp() { if (this->m_File) { cout << "該文件不存在或為空" << endl; } else { cout << "請輸入你要刪除員工的編號:" << endl; int id; cin >> id; int ret = this->Ison_Emp(id); //職工存在,且在index位置上 if (ret != -1) { for (int i = ret; i < this->m_EmpNum - 1; i++) { //數(shù)據(jù)前移 this->m_EmpArray[i] = this->m_EmpArray[i + 1]; } //更新人數(shù) this->m_EmpNum--; cout << "刪除成功" << endl; //同步到文件中 this->save(); } else { cout << "刪除失敗" << endl; } } system("pause"); system("cls"); } void Worker_Manager::Mod_Emp() { if (this->m_File) { cout << "該文件不存在或為空" << endl; } else { int id = 0; cout << "輸入你要修改的員工編號:" << endl; cin >> id; int ret = this->Ison_Emp(id); if (ret != -1) { delete this->m_EmpArray[ret]; int Newid = 0; string name=""; int post = 0; cout << "查到: " << id << "號員工,請輸入新的員工編號: " << endl; cin >> Newid; cout << "查到: " << id << "號員工,請輸入新的員工姓名: " << endl; cin >> name; cout << "請輸入新崗位" << endl; cout << "1、普通員工" << endl; cout << "2、經(jīng)理" << endl; cout << "3、老板" << endl; cin >> post; Worker* worker = NULL; switch (post) { case 1: worker = new Employee(Newid, name, post); break; case 2: worker = new Manager(Newid, name, post); break; case 3: worker = new Boss(Newid, name, post); break; default: break; } //將修改的數(shù)據(jù)放回原來位置 this->m_EmpArray[ret] = worker; cout << "修改成功" << endl; this->save(); } else { cout << "修改失敗" << endl; } } system("pause"); system("cls"); } void Worker_Manager::Find_Emp() { if (this->m_File) { cout << "該文件不存在或為空" << endl; } else { cout << "請選擇查找的方式" << endl; cout << "1、按編號查找" << endl; cout << "2、按姓名查找" << endl; int input = 0; cin >> input; if (input == 1) { //按編號查找 int id = 0; cout << "請輸入你要查找的編號為:" << endl; cin >> id; int ret = this->Ison_Emp(id); if (ret != -1) { cout << "查找到此人信息 如下:" << endl; this->m_EmpArray[ret]->ShowInfo(); } else { cout << "查無此人" << endl; } } else if(input==2) { //按姓名查找 string name; cout << "請輸入你要查找人的姓名: " << endl; cin >> name; bool flag = false; for (int i = 0; i < this->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 Worker_Manager::Sort_Emp() { if (this->m_File) { cout << "該文件不存在或為空" << endl; system("pause"); system("cls"); } else { cout << "請選擇排序方式" << endl; cout << "1、按職工號升序" << endl; cout << "2、按職工號降序" << endl; int input = 0; cin >> input; for (int i = 0; i < this->m_EmpNum; i++) { int minOrmax = i; for (int j = i + 1; j < this->m_EmpNum; j++) { if (input == 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 (minOrmax != i) { Worker* temp = this->m_EmpArray[i]; this->m_EmpArray[i] = this->m_EmpArray[minOrmax]; this->m_EmpArray[minOrmax] = temp; } } cout << "排序成功,排序結(jié)果為:" << endl; this->save(); this->Show_Emp(); } } void Worker_Manager::Clean_File() { cout << "確定清空?" << endl; cout << "1、確定" << endl; cout << "2、取消" << endl; int input = 0; cout << "請輸入: " << endl; cin >> input; if (input == 1) { ofstream ofs; ofs.open(TXT, ios::trunc); ofs.close(); if (this->m_EmpArray != NULL) { //刪除堆區(qū)所有職工對象 for (int i = 0; i < this->m_EmpNum; i++) { delete this->m_EmpArray[i]; this->m_EmpArray[i] = NULL; } //刪除堆區(qū)指針 delete[] this->m_EmpArray; this->m_EmpArray = NULL; this->m_File = true; this->m_EmpNum = 0; } cout << "清空成功" << endl; } system("pause"); system("cls"); } Worker_Manager::~Worker_Manager() { 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]; } } delete[]this->m_EmpArray; this->m_EmpArray = NULL; } }
職工管理系統(tǒng).cpp文件
#include <iostream> using namespace std; #include <string> #include "worker_Manager.h" #include "Worker.h" #include "employee.h" #include "boss.h" #include "manager.h" int main() { //測試代碼 //Worker* worker = NULL; //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; //實例化對象 Worker_Manager wm; int input = 0; do { wm.ShowMenu(); cout << "請輸入你的選擇:" << endl; cin >> input; switch (input) { case 0: cout << "歡迎下次使用!" << endl; 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; } } while (input); system("pause"); return 0; }
關(guān)于“基于C++如何實現(xiàn)職工管理系統(tǒng)”這篇文章的內(nèi)容就介紹到這里,感謝各位的閱讀!相信大家對“基于C++如何實現(xiàn)職工管理系統(tǒng)”知識都有一定的了解,大家如果還想學(xué)習(xí)更多知識,歡迎關(guān)注億速云行業(yè)資訊頻道。
免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。