职工管理系统
抽象類:
頭文件tou.h
管理類頭文件chouxiang.h
#pragma once #include<iostream> using namespace std; #include"chouxiang.h" class manager :public worker { public:void showin();manager(string na, int iid, int nn);string getdepname(); };員工類,經理類,老板類頭文件 采用繼承方式(全是一樣的,改名字即可)
employee.h
tou.h中的功能實現:包括增刪員工,保存到文件中(代碼最多)。
#include"tou.h" #include"chouxiang.h" #include"employee.h" #include"boss.h" #include"manager.h" workmanager::workmanager() {//文件一共三種情況//文件不存在的情況下的初始化ifstream ifs;ifs.open("eme.txt", ios::in);//讀文件if (!ifs.is_open()){cout << "文件不存在!" << endl;empnum = 0;emarray = NULL;//初始化人數和數組指針isempty = true;//初始化文件為空標志ifs.close();return;}//文件存在且數據為空 判斷一個文件存在且沒有內容char ch;//文件為空時相當于這個文件只有一個文件尾標志ifs >> ch;//相當于用ch讀走文件結尾標if (ifs.eof())//當文件為空eof()函數返回為真{cout << "文件為空!\n";empnum = 0;emarray = NULL;//初始化人數和數組指針isempty = true;//初始化文件為空標志 當文件為空則trueifs.close();return;}//當文件存在且已經有數據int num = getnum();cout << "人數為:" << num << endl;empnum = num;//更改職工人數emarray = new worker*[num];//開辟空間chushihua();//將文件中的數據存到數組中for (int i = 0; i < num; i++){cout << "員工編號" << emarray[i]->id << endl;cout << "name" << emarray[i]->nname << endl;} } void workmanager::show_menu() {cout << "退出" << endl;cout << "顯示" << endl;cout << "刪除" << endl;cout << "修改" << endl;cout << "查找" << endl;} void workmanager::choice(int n) {switch (n){case 1:break;case 2:addemp(); break;case 3:{del();break;}default:break;} } void workmanager::addemp() {cout << "input 新增職工數量" << endl;int addm;cin >> addm;if (addm > 0){int newsize = empnum + addm;//計算新空間大小worker** newspace = new worker * [newsize];//開辟新空間if(emarray != NULL)//如果原來數據不為空for (int i = 0; i < empnum; i++){newspace[i] = emarray[i];}//將原空間下內容存放到新空間下//輸入新數據for (int i = 0; i < addm; i++){int id1;string name1;int bumen;cout << "1普通職工\n2經理\n3老板\n";cout << "請輸入第"<<i+1<<"個員工編號:" << endl;cin >> id1;cout << "請輸入第"<<i+1<<"個姓名:" << endl;cin >> name1;cout << "部門:" << endl;cin >> bumen;worker* wworker = NULL;switch (bumen){case 1:wworker = new employee(name1, id1, 1); break;case 2:wworker = new manager(name1, id1, 2); break;case 3:wworker = new boss(name1, id1, 1); break;default:cout << "無效!\n";break;}newspace[empnum + i] = wworker;}delete[]emarray;//釋放原有空間emarray = newspace;//更改新空間的指向empnum = newsize;//更新新的員工個數isempty = false;//更新職工情況為不為空cout << "成功添加!" << endl;}elsecout << "輸入錯誤!\n";system("pause");system("cls"); } void workmanager::save() {ofstream ofs;ofs.open("eme.txt", ios::out);for (int i = 0; i < empnum; i++){ofs << emarray[i]->id<<" ";ofs << emarray[i]->nname<<" ";ofs << emarray[i]->bumenbianhao<<" "<<endl;}ofs.close(); } int workmanager::getnum() {ifstream ifs;ifs.open("eme.txt", ios::in);//以讀的方式打開文件int id;string name;int did;//部門編號int number = 0;while (ifs >> id && ifs >>name && ifs >> did)//將一行的數據全都讀,讀完一行數量加一number++;return number;} void workmanager::chushihua() {ifstream ifs;ifs.open("eme.txt", ios::in);int index = 0;int id;string name;int did;worker* wworker = NULL;while (ifs >> id && ifs >> name && ifs >> did){if (did == 1)//普通員工wworker = new employee(name, id, did);if (did == 2)wworker = new manager(name, id, did);if(did == 3)//did為3時說明是老板類wworker = new boss(name, id, did);emarray[index] = wworker;index++;}ifs.close(); } void workmanager::showfunction()//利用多態調用程序接口 顯示職工功能 {ifstream ifs;if (isempty)cout << "文件不存在或記錄為空" << endl;else{for (int i = 0; i < empnum; i++)emarray[i]->showin();} } int workmanager::isexist(int id) {int index = -1;for (int i = 0; i < empnum; i++){if (emarray[i]->id == id){index = i;break;}}return index; } void workmanager::del()//刪除功能的實現 {if (isempty)cout << "無數據或無文件!\n";else {cout << "你要刪除的職工編號:\n";int nn;cin >> nn;int ret = isexist(nn);if (ret == -1)cout << "no exist\n";else{cout << "the number:" << ret << endl;for (int i = ret; i < empnum - 1; i++)//說明ret位置上的元素需要刪除,元素依次移動{emarray[i] = emarray[i + 1];}empnum--;save();//刪除后數據同步到文件中cout << "刪除成功!\n";}} }employee.cpp 功能實現函數
#include"employee.h" employee::employee(string nn1,int iid, int nn) {nname = nn1;id = iid;bumenbianhao = nn; } void employee::showin() {getdepname();cout <<"id number:"<< id <<"姓名:"<<nname<< "部門:" << bumenbianhao<<" " <<endl; }string employee::getdepname() {return "員工"; }總結