C++(九)——职工信息管理系统
生活随笔
收集整理的這篇文章主要介紹了
C++(九)——职工信息管理系统
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
簡單的項(xiàng)目構(gòu)成
?頭文件
職工抽象類
#pragma once #include<iostream> #include<string> using namespace std; class Worker { public://顯示個人信息virtual void showInfo() = 0;//顯示崗位名稱virtual string getDName() = 0;int m_id;string m_name;int m_dId; };Boss類(繼承自職工抽象類)
#pragma once #include<iostream> #include<string> #include"Worker.h" using namespace std; class Boss :public Worker { public:Boss(int id, string name, int dId);void showInfo();string getDName(); };經(jīng)理類(繼承自職工抽象類)
#pragma once #include<iostream> #include<string> #include"Worker.h" using namespace std; class Manger :public Worker { public:Manger(int id, string name, int dId);void showInfo();string getDName(); };普通員工類(繼承自職工抽象類)
#pragma once #include<iostream> #include<string> #include"Worker.h" using namespace std; class Employee :public Worker { public:Employee(int id, string name, int dId);void showInfo();string getDName(); };管理系統(tǒng)類(實(shí)現(xiàn)信息的增刪查改,排序等等)
#pragma once //防止頭文件重復(fù)包含 #include<iostream> #include"Worker.h" using namespace std; //只做聲明, 不做實(shí)現(xiàn) class WorkerManger { public:WorkerManger();//展示菜單的函數(shù)void showMenu();//退出系統(tǒng)函數(shù)void exitSystem();//添加新員工void addEmp();//保存到txt文本中void save();//統(tǒng)計(jì)人數(shù)int getEmNum();//初始化員工void initEmp();//顯示員工信息void showWorker();//刪除職工void delEmp();//判斷編號員工是否存在int isExist(int num);//修改員工信息void modEmp();//查找員工void findEmp();//排序void sortEmp();//清空文件void cleanFile();~WorkerManger();//判斷文件是否為空的標(biāo)志,后面刪除時需要用到bool m_isEmpty;int m_emNum;Worker** m_emArray;};源文件
職工抽象類
#include"Worker.h"void Worker::showInfo() {}string Worker::getDName() {return string("None"); }Boss類(繼承自職工抽象類)
#include"Boss.h"Boss::Boss(int id, string name, int dId) {m_id = id;m_name = name;m_dId = dId; } void Boss::showInfo() {cout << "職工編號:" << m_id;cout << "\n職工姓名:" << m_name;cout << "\n崗位:" << getDName();cout << "\n崗位職責(zé):啥也不用干,誒!就是玩兒" << endl;cout << endl;} string Boss::getDName() {return string("公司老板"); }經(jīng)理類(繼承自職工抽象類)
#include"Manger.h" Manger::Manger(int id, string name, int dId) {m_id = id;m_name = name;m_dId = dId; } void Manger::showInfo() {cout << "職工編號:" << m_id;cout << "\n職工姓名:" << m_name;cout << "\n崗位:" << getDName();cout << "\n崗位職責(zé):壓榨底層打工人,幫老板端茶倒水" << endl;cout << endl; } string Manger::getDName() {return string("部門經(jīng)理"); }普通員工類(繼承自職工抽象類)
#include"Employee.h"Employee::Employee(int id, string name, int dId ) {m_id = id;m_name = name;m_dId = dId; } void Employee::showInfo() {cout << "職工編號:" << m_id;cout << "\n職工姓名:" << m_name;cout << "\n崗位:" << getDName();cout << "\n崗位職責(zé):幫經(jīng)理端茶倒水" << endl;cout << endl;} string Employee::getDName() {return string("底層打工人"); }管理系統(tǒng)類(實(shí)現(xiàn)信息的增刪查改,排序等等)
#include<fstream> #include"WorkManger.h" #include"Worker.h" #include"Employee.h" #include"Manger.h" #include"Boss.h"#define FILENAME "empFile.txt" WorkerManger::WorkerManger() {ifstream ifs;ifs.open(FILENAME, ios::in);//如果文件不存在則初始化各個參數(shù)(第一次使用系統(tǒng))if (!ifs.is_open()) {cout << "文件不存在" << endl;this->m_emNum = 0;this->m_emArray = NULL;this->m_isEmpty = true;ifs.close();return;}//如果文件存在,但是數(shù)據(jù)被清理過char ch;ifs >> ch;if (ifs.eof()) {cout << "數(shù)據(jù)被清理了" << endl;this->m_emNum = 0;this->m_emArray = NULL;this->m_isEmpty = true;ifs.close();return;}//獲取文件中的員工數(shù)量getEmNum();this->m_emArray = new Worker * [this->m_emNum];initEmp();this->m_isEmpty = false;} WorkerManger::~WorkerManger() {if (m_emArray != NULL) {delete[] m_emArray;m_emArray = NULL;}}void WorkerManger::save() {ofstream ofs;ofs.open(FILENAME, ios::out);for (int i = 0; i < m_emNum; ++i) {ofs << m_emArray[i]->m_id << " "<< m_emArray[i]->m_name << " "<< m_emArray[i]->m_dId << " ";} }int WorkerManger::getEmNum() {ifstream ifs;ifs.open(FILENAME, ios::in);int id;string name;int dId;//統(tǒng)計(jì)分?jǐn)?shù)int num = 0;//逐行讀取while (ifs >> id && ifs >> name && ifs >> dId) {num++;}ifs.close();this->m_emNum = num;return num; }//如果文件存在且有數(shù)據(jù)的初始化函數(shù) void WorkerManger::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, dId);else if (dId == 2)worker = new Manger(id, name, dId);elseworker = new Boss(id, name, dId);this->m_emArray[index] = worker;index++;}} //顯示員工信息 void WorkerManger::showWorker() {//如果不為空system("cls");if (!m_isEmpty) {string dName;cout << "員工信息如下:" << endl;for (int i = 0; i < this->m_emNum; ++i) {this->m_emArray[i]->showInfo();}}else {cout << "還未錄入員工信息!!!" << endl;}system("pause");}int WorkerManger::isExist(int num) {for (int i = 0; i < this->m_emNum; ++i) {//找到了員工if (this->m_emArray[i]->m_id == num)return i;}//否則返回-1return -1; }//刪除員工 void WorkerManger::delEmp() {int index = -1;int id;if (getEmNum() == 0) {cout << "還未錄入員工信息!!!" << endl;system("pause");return;}cout << "請輸入要刪除員工的編號:";cin >> id;system("cls");index = isExist(id);if (index == -1) {cout << "未找到該員工!!!" << endl;}else {for (int i = index; i < this->m_emNum - 1; ++i) {this->m_emArray[i] = this->m_emArray[i + 1];}this->m_emNum--;this->save();cout << "刪除成功!!!" << endl;}system("pause"); } void WorkerManger::modEmp() {if (getEmNum() == 0){cout << "還未錄入員工信息!!" << endl;system("pause");return;}else {int index = -1;int id;cout << "請輸入要修改信息的員工的編號:" << endl;cin >> id;index = isExist(id);if (index == -1) {cout << "未找到該員工,請輸入有效編號" << endl;}else {int newId;string newName;int newDid;Worker* newEm = NULL;cout << "請輸入員工的新編號:" << endl;cin >> newId;cout << "請輸入員工的新姓名:" << endl;cin >> newName;cout << "1、底層打工人 2、部門經(jīng)理 3、老板" << endl;cout << "請輸入員工的新職位(輸入編號):" << endl;cin >> newDid;delete this->m_emArray[index];if (newDid == 1) {newEm = new Employee(newId, newName, newDid);}else if (newDid == 2) {newEm = new Manger(newId, newName, newDid);}else {newEm = new Boss(newId, newName, newDid);}this->m_emArray[index] = newEm;this->save();cout << "員工信息修改完成!!!" << endl;}system("pause");;} }void WorkerManger::findEmp() {if (getEmNum() == 0){cout << "還未錄入員工信息!!!" << endl;system("pause");return;}int id;int doId;int index = -1;string name;cout << "請輸入查找方式(1、編號 2、姓名):" << endl;cin >> doId;if (doId == 1) {cout << "請輸入員工編號:" << endl;cin >> id;index = this->isExist(id);if (index == -1) {cout << "輸入編號有誤" << endl;}else{this->m_emArray[index]->showInfo();}}else if (doId == 2) {cout << "請輸入員工姓名:" << endl;cin >> name;int flag = 1;for (int i = 0; i < m_emNum; ++i) {if (name == m_emArray[i]->m_name) {flag = 0;this->m_emArray[i]->showInfo();cout << endl;}}if (flag)cout << "未找到該員工!!!!" << endl;}else {cout << "輸入有誤!!!!" << endl;return;}system("pause"); }//按編號排序 void WorkerManger::sortEmp() {if (getEmNum() == 0) {cout << "還未錄入員工信息" << endl;system("pause");return;}int opt = 1;cout << "請輸入排序方式(1、升序 2、降序):";cin >> opt;if (opt == 1) {for (int i = 0; i < this->m_emNum - 1; ++i) {for (int j = i + 1; j < this->m_emNum - 1; ++j) {if (m_emArray[i]->m_id > m_emArray[j]->m_id) {Worker* t = m_emArray[i];m_emArray[i] = m_emArray[j];m_emArray[j] = t;}}}}else {for (int i = 0; i < this->m_emNum - 1; ++i) {for (int j = i + 1; j < this->m_emNum; ++j) {if (m_emArray[i]->m_id < m_emArray[j]->m_id) {Worker* t = m_emArray[i];m_emArray[i] = m_emArray[j];m_emArray[j] = t;}}}}this->save();cout << "排序完畢" << endl;system("pause"); }void WorkerManger::cleanFile() {int opt = 2;cout << "確認(rèn)清空文件!!!!" << endl;cout << "1、確認(rèn)" << endl;cout << "2、返回" << endl;cin >> opt;if (opt == 2)return;//ios::trunc如果文件存在,則刪除重新創(chuàng)建ofstream ofs(FILENAME, ios::trunc);ofs.close();if (this->m_emArray != NULL) {for (int i = 0; i < this->m_emNum; ++i) {if (this->m_emArray[i] != NULL)delete this->m_emArray[i];}this->m_emNum = 0;delete[] this->m_emArray;this->m_emArray = NULL;}cout << "清除成功!!!" << endl;system("pause"); }void WorkerManger::showMenu() {cout << "****************************************************" << endl;cout << "****************************************************" << endl;cout << "************* 歡迎使用職工管理系統(tǒng) ***********" << endl;cout << "************* 0.退出管理系統(tǒng) ***********" << 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;cout << endl; }void WorkerManger::exitSystem() {cout << "歡迎您下次使用!!!" << endl;system("pause");exit(0); }//添加新員工 void WorkerManger::addEmp() {int addNum = 0;cout << "請輸入要添加的員工的數(shù)量:" << endl;cin >> addNum;if(addNum > 0){int newSize = addNum + m_emNum;Worker** newArray = new Worker * [newSize];if (m_emNum != 0) {//先將原本的數(shù)據(jù)存入newArray里for (int j = 0; j < m_emNum; ++j) {newArray[j] = m_emArray[j];}//釋放之前的空間// delete[] this->m_emArray;}//輸入員工信息for (int i = 0; i < addNum; ++i) {int t_id;string t_name;int t_dId;Worker* t_emp = NULL;cout << "請輸入第" << i + 1 << "位員工的信息!!" << endl;cout << "請輸入編號:";cin >> t_id;cout << "請輸入姓名:";cin >> t_name;cout << "1、底層打工人 2、經(jīng)理 3、老板" << endl;cout << "請選擇崗位(輸入數(shù)字):";cin >> t_dId;if (isExist(t_id) != -1) {cout << "該員工編號重復(fù),請重新錄入" << endl;system("pause");--i;continue;}//根據(jù)崗位new不同的worker類,if (t_dId == 1)t_emp = new Employee(t_id, t_name, t_dId);else if (t_dId == 2)t_emp = new Manger(t_id, t_name, t_dId);else if (t_dId == 3)t_emp = new Boss(t_id, t_name, t_dId);else {cout << "非法輸入!!!" << endl;break;}//暫時存入newArray里newArray[i + this->m_emNum] = t_emp;}delete[] this->m_emArray;//得到新的指向m_emArray = newArray;//更新員工數(shù)量m_emNum = newSize;m_isEmpty = false;cout << "員工信息錄入完成!!!" << endl;save();system("pause");}else {cout << "輸入有誤!!!" << endl;system("pause");} }主程序
#include<iostream> #include"WorkManger.h"#include"Worker.h" #include"Manger.h" #include"Employee.h" #include"Boss.h"using namespace std;int main() {//實(shí)例化管理者對象WorkerManger wm;int choice = 0;while (true) {//調(diào)用展示菜單的成員函數(shù)system("cls");wm.showMenu();cout << "請輸入您的選擇:" << endl;cin >> choice;switch (choice){case 0: //退出系統(tǒng)wm.exitSystem();break;case 1: //增加職工wm.addEmp();break;case 2: //顯示職工wm.showWorker();break;case 3: //刪除職工wm.delEmp();break;case 4: //修改職工wm.modEmp();break;case 5: //查找職工wm.findEmp();break;case 6: //編號排序wm.sortEmp();break;case 7: //清空數(shù)據(jù)wm.cleanFile();break;default:system("cls");break;}}system("pause");return 0; } 《新程序員》:云原生和全面數(shù)字化實(shí)踐50位技術(shù)專家共同創(chuàng)作,文字、視頻、音頻交互閱讀總結(jié)
以上是生活随笔為你收集整理的C++(九)——职工信息管理系统的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: C++(八)——文件操作
- 下一篇: C++(十)——模板(上)