網站首頁 編程語言 正文
本文實例為大家分享了C++實現公司人事管理系統的具體代碼,供大家參考,具體內容如下
一.問題描述
一個小公司包含四類人員:經理,技術人員,銷售人員和銷售經理,各類人員的工資計算方法如下:經理:固定月薪(8000);技術人員:月薪按技術等級(1~8)(1600+等級*300);銷售人員:按提成(4%*銷售額);銷售經理:底薪+提成(1500+0.2%*總銷售額);設計一個管理程序,實現對各類人員的信息輸入,修改和顯示。
二 .基本要求
(1)使用面向對象編程思想編寫開發過程中需要用到的類,比如:設計Person類:編號,姓名,崗位,工資,成員函數可設一個計算月薪的純虛函數;另外再設計四個針對四類人員的類均繼承 Person;添加相應的派生類數據成員和函數,經理和銷售經理可以沒有新的數據成員,計算月薪即可; 技術人員添加技術等級數據成員,銷售人員添加數據成員:銷售額。還需設計一個Manage 類來完成各種操作。人員數組 vector,數據類型為基類指針。
(2)需要使用菜單功能顯示添加人員(輸入),修改信息,瀏覽信息,按姓名查找,月薪排序。
(3)為了設計簡潔,假定經理和銷售經理都只能有一個;用文本編輯器編輯一個文本文件(總數 20 人以上)包含各類人員的信息;并且在程序中能修改保存。
基本流程圖
#include#include #include #include #include #include #include #include #define filename "student.txt" using namespace std;? class Person { public: ?? ?Person(string, string, int = 0);//構造函數? ?? ?double virtual pay_salary() = 0; //借用虛函數進行工資初始化? ?? ?void ?virtual show(); ?? ??? ?//顯示信息? ?? ?bool operator<(const Person*&) const;?? ?//重載<比較薪水大小用于排序? ?? ?static int num; //定義靜態變量,自動賦予員工編號? ?? ?int Number; ? //編號? ?? ?double Salary;//工資? ?? ?string Name;//姓名? ?? ?string Department;//部門? ?? ?int c;//技術級? }; bool Person::operator<(const Person*& obj) const//函數重載<,用于比較薪水? { ?? ?return this->Salary > obj->Salary; } Person::Person(string name1, string work1, int c1) //構造函數的實現? { ?? ?c = c1; ?? ?Number = num++; ?? ?Name = name1; ?? ?Department = work1; } int Person::num = 1;//編號從1開始? void ?Person::show() { ?? ?cout<<"-----------------------------------"< Ma;//vector數組,存放Person類的對象指針? ?? ?void add(Person*);//添加人員信息? ?? ?void alter(string);//刪除人員信息? ?? ?void addtofile();//寫入文件? ?? ?void show();//顯示所有信息? ?? ?void show1();//按月薪降序? ?? ?Person* find(string&);//查找人員信息? }; Person* Manage::find(string& name1) { //查找 ?? ?for (vector ::iterator iter = Ma.begin(); iter != Ma.end(); iter++) { ?? ??? ?if ((*iter)->Name == name1) { ?? ??? ??? ?return *iter; ?? ??? ?} ?? ?} ?? ?return NULL; } void Manage::alter(string name1) { //刪除 ?? ?for (vector ::iterator iter = Ma.begin(); iter != Ma.end(); iter++) { ?? ??? ?if ((*iter)->Name == name1) { ?? ??? ??? ?Ma.erase(iter); ?? ??? ??? ?return; ?? ??? ?} ?? ?} ?? ?cout << "查無此人" << endl; } void Manage::add(Person* people) //添加? { ?? ?if (people->Department == "銷售人員") { ?? ??? ?salevolume += ((Salesman*)people)->salevolume; ?? ?} ?? ?Ma.push_back(people); } void Manage::addtofile()//寫入文件? ?{ ?? ?ofstream outfile(filename);//打開文件寫入? ?? ?for (vector ::iterator iter = Ma.begin(); iter != Ma.end(); iter++) { ?? ??? ?outfile << (*iter)->Department << " " << (*iter)->Name << " "; ?? ??? ?if ((*iter)->c == 0) outfile << endl; ?? ??? ?else outfile << (*iter)->c << endl; ?? ?} ?? ?outfile.close();//關閉? } bool cmp(Person* x, Person* y) { //比較薪水 ?? ?return x->Salary > y->Salary; } void Manage::show() { ?? ?for (vector ::iterator iter = Ma.begin(); iter != Ma.end(); iter++) { ?? ??? ?if ((*iter)->Department == "銷售經理") { ?? ??? ??? ?(*iter)->Salary = salevolume * 0.002 +1500; ?? ??? ??? ?break; ?? ??? ?} ?? ?} ?? ?sort(Ma.begin(), Ma.end(), cmp);//薪水大小排序 ?? ?for (vector ::iterator iter = Ma.begin(); iter != Ma.end(); iter++) { ?? ??? ?(*iter)->show(); ?? ??? ?cout << endl; ?? ?} } void readfile(Manage& obj)//讀取文件? ?{ ?? ?FILE* fp; ?? ?fp = fopen(filename, "r");//打開文件,只讀? ?? ?if (fp == NULL) { ?? ??? ?cout << "未找到人員名單" << endl; ?? ??? ?return; ?? ?} ?? ?while (!feof(fp)) { ?? ??? ?char post[20]; ?? ??? ?char Name[20]; ?? ??? ?int c; ? //銷售額或技術等級 ?? ??? ?fscanf(fp, "%s%s%d", post, Name,&c); ?? ??? ?if (!strcmp(post, "經理")) { //文件中為經理的人的信息先填入 ?? ??? ??? ?Person* peo = new Manager(Name, post, 0); ?? ??? ??? ?obj.add(peo); ?? ??? ?} ?? ??? ?else if (!strcmp(post, "技術人員")) { ?? ??? ??? ?Person* peo = new Technician(Name, post, c); ?? ??? ??? ?obj.add(peo); ?? ??? ?} ?? ??? ?else if (!strcmp(post, "銷售人員")) { ?? ??? ??? ?Person* peo = new Salesman(Name, post, c); ?? ??? ??? ?obj.add(peo); ?? ??? ?} ?? ??? ?else if (!strcmp(post, "銷售經理")) { ?? ??? ??? ?Person* peo = new SaleManager(Name, post, 0); ?? ??? ??? ?obj.add(peo); ?? ??? ?} ?? ?} ?? ?fclose(fp);//關閉文件? } void Manage::show1()//對vector數組進行讀取? { ?? ?for (vector ::iterator iter = Ma.begin(); iter != Ma.end(); iter++) { ?? ??? ?(*iter)->show(); ?? ??? ?cout << endl; ?? ?} } int main(){ ?? ?int x; ?? ?Manage T; ?? ?readfile(T); ?? ?while(1){ ? ? ? ? cout<< " ? ?———————————————————————————————" << endl ?? ??? ??? ?<< " ? ?| ? ? ? 公司人事管理系統 ? ? ? ?|" << endl ?? ??? ??? ?<< " ? ?———————————————————————————————" << endl ?? ??? ??? ?<< " ? ?| ? ? ? ? 1.添加員工 ? ? ? ? ? |" << endl ?? ??? ??? ?<< " ? ?| ? ? ? ? 2.修改信息 ? ? ? ? ? |" << endl ?? ??? ??? ?<< " ? ?| ? ? ? ? 3.按姓名查找 ? ? ? ? |" << endl ?? ??? ??? ?<< " ? ?| ? ? ? ? 4.顯示所有信息 ? ? ? |" << endl ?? ??? ??? ?<< " ? ?| ? ? ? ? 5.按月薪降序排序 ? ? |" << endl ?? ??? ??? ?<< " ? ?| ? ? ? ? 0.保存并退出程序 ? ? |" << endl ?? ??? ??? ?<< " ? ?———————————————————————————————" << endl;?? ??? ? ?? ??? ?cout<< "請選擇->"; ?? ??? ?cin >> x; ?? ??? ?switch (x) { ?? ??? ?case 1: { ?? ??? ??? ?while (1) { ?? ??? ??? ??? ?int n; ?? ??? ??? ??? ?string Name; ?? ??? ??? ??? ?cout << "請輸入姓名:" ; ?? ??? ??? ??? ?cin >> Name; ?? ??? ??? ??? ?cout << "請輸入人員崗位(1.經理 2.技術人員 3. 銷售人員 4.銷售經理):" ;? ?? ??? ??? ??? ?cin >> n; ?? ??? ??? ??? ?if (n == 1) { ?? ??? ??? ??? ??? ?Person* peo = new Manager(Name, "經理", 0); ?? ??? ??? ??? ??? ?T.add(peo); ?? ??? ??? ??? ??? ?cout << "添加成功" << endl << endl << endl; ?? ??? ??? ??? ??? ?break; ?? ??? ??? ??? ?} ?? ??? ??? ??? ?else if (n == 2) { ?? ??? ??? ??? ??? ?while (1) { ?? ??? ??? ??? ??? ??? ?int rank = 0; ?? ??? ??? ??? ??? ??? ?cout << "請輸入技術等級(1~8):" ; ?? ??? ??? ??? ??? ??? ?cin >> rank; ?? ??? ??? ??? ??? ??? ?if (rank > 8 || rank < 1) { ?? ??? ??? ??? ??? ??? ??? ?cout << "輸入錯誤,請在1~8之間輸入:" ; ?? ??? ??? ??? ??? ??? ?} ?? ??? ??? ??? ??? ??? ?else { ?? ??? ??? ??? ??? ??? ??? ?Person* peo = new Technician(Name, "技術人員", rank); ?? ??? ??? ??? ??? ??? ??? ?T.add(peo); ?? ??? ??? ??? ??? ??? ??? ?break; ?? ??? ??? ??? ??? ??? ?} ?? ??? ??? ??? ??? ?} ?? ??? ??? ??? ??? ?cout << "添加成功" << endl << endl << endl; ?? ??? ??? ??? ??? ?break; ?? ??? ??? ??? ?} ?? ??? ??? ??? ?else if (n == 3) { ?? ??? ??? ??? ??? ?int sales = 0; ?? ??? ??? ??? ??? ?cout << "請輸入銷售額:" << endl; ?? ??? ??? ??? ??? ?cin >> sales; ?? ??? ??? ??? ??? ?Person* peo = new Salesman(Name, "銷售人員", sales); ?? ??? ??? ??? ??? ?T.add(peo); ?? ??? ??? ??? ??? ?cout << "添加成功" << endl << endl << endl; ?? ??? ??? ??? ??? ?break; ?? ??? ??? ??? ?} ?? ??? ??? ??? ?else if (n == 4) { ?? ??? ??? ??? ??? ?Person* peo = new SaleManager(Name, "銷售經理", 0); ?? ??? ??? ??? ??? ?T.add(peo); ?? ??? ??? ??? ??? ?cout << "添加成功" << endl << endl << endl; ?? ??? ??? ??? ??? ?break; ?? ??? ??? ??? ?} ?? ??? ??? ??? ?else { ?? ??? ??? ??? ??? ?cout << "輸入錯誤,請重新輸入:" << endl; ?? ??? ??? ??? ?} ?? ??? ??? ?} ?? ??? ? system("pause");} ?? ??? ??? ??? ?break; ?? ??? ?case 2: { ?? ??? ??? ?string Name; ?? ??? ??? ?int n = 0; ?? ??? ??? ?cout << "請輸入姓名:" ; ?? ??? ??? ?cin >> Name; ?? ??? ??? ?Person* peo = T.find(Name); ?? ??? ??? ?if (peo == NULL) { ?? ??? ??? ??? ?cout << "?? ??? ?查無此人" << endl << endl << endl; ?? ??? ??? ??? ?break; ?? ??? ??? ?} ?? ??? ??? ?peo->show(); ?? ??? ??? ?if (peo->Department == "經理") { ?? ??? ??? ??? ?cout << " ? ?經理無法修改" << endl; ?? ??? ??? ?} ?? ??? ??? ?else if (peo->Department == "技術人員") { ?? ??? ??? ??? ?int rank = 0; ?? ??? ??? ??? ?while (1) { ?? ??? ??? ??? ??? ?cout < > rank; ?? ??? ??? ??? ??? ?if (rank > 8 || rank < 1) { ?? ??? ??? ??? ??? ??? ?cout << "等級輸入錯誤,請重新輸入" << endl; ?? ??? ??? ??? ??? ?} ?? ??? ??? ??? ??? ?else break; ?? ??? ??? ??? ?} ?? ??? ??? ??? ?T.alter(Name); ?? ??? ??? ??? ?peo = new Technician(Name, "技術人員", rank); ?? ??? ??? ??? ?T.add(peo); ?? ??? ??? ??? ?cout << "修改成功!" << endl; ?? ??? ??? ??? ? ?? ??? ??? ?} ?? ??? ??? ?else if (peo->Department == "銷售人員") { ?? ??? ??? ??? ?int sales = 0; ?? ??? ??? ??? ?cout < > sales; ?? ??? ??? ??? ?T.alter(Name); ?? ??? ??? ??? ?peo = new Salesman(Name, "銷售人員", sales); ?? ??? ??? ??? ?T.add(peo); ?? ??? ??? ??? ?cout << "?? ?修改成功!" << endl; ?? ??? ??? ??? ? ?? ??? ??? ?} ?? ??? ??? ?else if (peo->Department == "銷售經理") { ?? ??? ??? ??? ?cout << " ? 銷售經理無法修改" << endl; ?? ??? ??? ?} ?? ??? ??? ?else { ?? ??? ??? ??? ?cout << "輸入錯誤" << endl; ?? ??? ??? ?} ?? ??? ?}system("pause"); ?? ??? ??? ??? ?break; ?? ??? ?case 3: { ?? ??? ??? ?string Name; ?? ??? ??? ?int n = 0; ?? ??? ??? ?cout << "請輸入所查找人的姓名:"; ?? ??? ??? ?cin >> Name; ?? ??? ??? ?Person* peo = T.find(Name); ?? ??? ??? ?if (peo == NULL) { ?? ??? ??? ??? ?cout << "查無此人" << endl; ?? ??? ??? ??? ?system("cls"); ?? ??? ??? ??? ?break; ?? ??? ??? ?} ?? ??? ??? ?cout<<"-----------------------------------"< show(); ?? ??? ??? ?cout<
原文鏈接:https://blog.csdn.net/cjl1831050185/article/details/109231213
相關推薦
- 2024-02-28 CSS,文本溢出顯示省略號
- 2022-10-23 python?groupby函數實現分組選取最大值與最小值_python
- 2022-03-14 跨域:Response to preflight request doesn t pass acce
- 2022-09-18 jenkins配置golang?代碼工程自動發布的實現方法_Golang
- 2022-07-03 C#列表List<T>、HashSet和只讀集合介紹_C#教程
- 2022-10-08 C#在新建線程中使用Timer無效問題及解決_C#教程
- 2022-07-26 Springboot 解決跨域問題
- 2023-12-22 MAC電腦添加hosts
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支