C++實現職工工資管理系統
本文實例為大傢分享瞭C++實現職工工資管理系統的具體代碼,供大傢參考,具體內容如下
main.cpp
#include <iostream> #include <vector> #include <map> #include <algorithm> #include <string> #include <windows.h> #include "data.h" #include "user.h" #include "fuction1-9.h" #include "fuction10.h" #include "menu.h" using namespace std; int main() { system("color 3f"); if (login() == false) return 0; load_inf(); while (show_menu()); save_inf(); return 0; }
data.h
#pragma once const int maxn = 100; using namespace std; struct department { int code; int number; string department_name; void show_inf() { printf("%50s部門號:%-6d部門名稱:%-12s部門人數:%-6d\n", "", code, department_name.c_str(), number); } }; map<int, department> dep;//部門表的映射 struct employee { int code; string name; string gender; int working_age; int department_code;//部門編碼 void show_inf() { printf("%50s編號:%-6d姓名:%-12s性別:%-9s工齡(年):%-6d部門:%-12s\n", "", code, name.c_str(), gender.c_str(), working_age, dep[department_code].department_name.c_str()); } }; map<int, employee> emp;///存儲數據的容器 struct wage { int code; int base_wage;//基本工資 int overtime;//加班工資 int bonus;//津貼 int gross_pay;//應發工資 int withholding;//代扣款 int payment; //實發工資 void show_inf() { printf("%30s員工號:%-6d員工姓名:%-12s基本工資:%-6d\t加班工資:%-6d\t津貼:%-6d\t應發工資:%-6d\t代扣款:%6d\t實發工資:%6d\n", "", code, emp[code].name.c_str(), base_wage, overtime, bonus, gross_pay, withholding, payment); } void calculate() { gross_pay = base_wage + overtime + bonus, payment = gross_pay - withholding; } }; map<int, wage> wag;
fuciton1-9.h
#pragma once using namespace std; void load_inf() { FILE *p; char s[maxn]; system("cls"); if ((p = fopen("employee.txt", "r")) == NULL) { printf("%70s數據異常,無法訪問!\n", ""); system("pause"); exit(0); } while (fgets(s, 100, p)) { int code; char name[maxn]; char gender[maxn]; int working_age; int department_code; sscanf(s, "%d %s %s %d %d", &code, name, gender, &working_age, &department_code); emp[code] = (employee{ code, name, gender, working_age, department_code }); } printf("%70s", ""); for (int i = 0; i < 5; i++) { cout << "."; Sleep(200); } printf("員工信息導入成功!\n"); cout << endl; fclose(p); if ((p = fopen("department.txt", "r")) == NULL) { printf("%70s數據異常,無法訪問!\n", ""); system("pause"); exit(0); } while (fgets(s, 100, p)) { int code; int number; char department_name[maxn]; sscanf(s, "%d %d %s", &code, &number, department_name); dep[code] = { code, number, department_name }; } printf("%70s", " "); for (int i = 0; i < 5; i++) { cout << "."; Sleep(200); } printf("部門信息導入成功!\n"); cout << endl; fclose(p); if ((p = fopen("wage.txt", "r")) == NULL) { printf("%70s數據異常,無法訪問!", ""); system("pause"); exit(0); } while (fgets(s, 100, p)) { int code;//員工號 int base_wage;//基本工資 int overtime;//加班工資 int bonus;//津貼 int gross_pay; int withholding;//代扣款 int payment; sscanf(s, "%d %d %d %d %d %d %d", &code, &base_wage, &overtime, &bonus, &gross_pay, &withholding, &payment); wag[code] = (wage{ code, base_wage, overtime, bonus, gross_pay, withholding, payment }); } printf("%70s", " "); for (int i = 0; i < 5; i++) { cout << "."; Sleep(200); } printf("工資信息導入成功!\n"); cout << endl; fclose(p); return; } void save_inf() { FILE *p = fopen("employee.txt", "w"); char inf[maxn]; for (auto it = emp.begin(); it != emp.end(); it++) { sprintf(inf, "%d %s %s %d %d\n", it->second.code, it->second.name.c_str(), it->second.gender.c_str(), it->second.working_age, it->second.department_code); fputs(inf, p); } printf("%70s", " "); for (int i = 0; i < 5; i++) { cout << "."; Sleep(200); } printf("員工信息保存成功!\n"); fclose(p); p = fopen("department.txt", "w"); for (auto it = dep.begin(); it != dep.end(); it++) { sprintf(inf, "%d %d %s\n", it->second.code, it->second.number, it->second.department_name.c_str()); fputs(inf, p); } printf("%70s", ""); for (int i = 0; i < 5; i++) { cout << "."; Sleep(200); } printf("部門信息保存成功!\n"); fclose(p); p = fopen("wage.txt", "w"); for (auto it = wag.begin(); it != wag.end(); it++) { sprintf(inf, "%d %d %d %d %d %d %d\n", it->second.code, it->second.base_wage, it->second.overtime, it->second.bonus, it->second.gross_pay, it->second.withholding, it->second.payment); fputs(inf, p); } printf("%70s", ""); for (int i = 0; i < 5; i++) { cout << "."; Sleep(200); } printf("工資信息保存成功!\n"); fclose(p); return; } /以下為部門表的操作 void add_dep() { printf("%70s請輸入要添加的部門號:", ""); int code; cin >> code; string department_name; if (!dep.count(code)) { printf("%70s請輸入部門名稱:", ""); while (1) { int flag = 0; cin >> department_name; for (auto it = dep.begin(); it != dep.end(); it++) if (it->second.department_name == department_name) { printf("%70s該部門已存在, 請重新輸入!\n", ""); flag = 1; } if (!flag) break; } dep[code] = { code, 0, department_name }; printf("%70s部門添加成功!該部門信息如下:\n", ""); dep[code].show_inf(); } else printf("%70s該部門已存在!\n", ""); return; } void change_dep() { printf("%70s請輸入要修改的部門編號:", ""); int code; cin >> code; if (!dep.count(code)) printf("%70s該部門不存在!\n", ""); else { printf("%70s該部門當前信息如下:\n", ""); dep[code].show_inf(); printf("%70s請輸入部門的新名字:", ""); while (1) { string name; cin >> name; int flag = 0; for (auto it = dep.begin(); it != dep.end(); it++) if (it->second.department_name == name) { flag = 1; break; } if (flag) printf("%70s部門名字重復!請重新輸入\n", ""); else { dep[code].department_name = name; printf("%70s修改成功!\n", ""); printf("%70s修改後的部門信息如下:\n", ""); dep[code].show_inf(); break; } } } return; } void delete_dep() { printf("%70s請輸入要刪除的部門編號:", ""); int code; cin >> code; if (!dep.count(code)) printf("%70s該部門不存在,無法刪除!\n", ""); else if (!dep[code].number)//如果該部門沒有人,則可刪除 { for (auto it = dep.begin(); it != dep.end(); it++) if (it->first == code) { printf("%70s您刪除瞭%s\n", "", it->second.department_name.c_str()); dep.erase(it); break; } } else printf("%70s該部門有員工,不可刪除!\n", ""); return; } /以下為員工表的操作 void add_emp() { printf("%70s請輸入要添加的員工號:", ""); int code; cin >> code; if (!emp.count(code)) { emp[code].code = code; printf("%70s請輸入員工的姓名:", "");cin >> emp[code].name; printf("%70s請輸入員工的性別:", "");cin >> emp[code].gender; printf("%70s請輸入員工的工齡:", "");cin >> emp[code].working_age; printf("%70s請輸入員工的部門號:", ""); int department_code; while (cin >> department_code && !dep.count(department_code)) printf("%70s該部門不存在,請重新輸入:", ""); emp[code].department_code = department_code; dep[department_code].number++; printf("%70s員工信息添加成功!員工信息如下:\n", ""); emp[code].show_inf(); } else printf("%70s該員工號已存在\n", ""); return; } void change_emp() { int code; printf("%70s請輸入要修改的員工號:", ""); while (cin >> code && !emp.count(code)) printf("%70s該員工不存在!請重新輸入:", ""); printf("%70s該員工當前信息如下:\n", ""); emp[code].show_inf(); printf("%70s請輸入修改後的工齡:", "");cin >> emp[code].working_age; printf("%70s請輸入修改後的部門編碼:", ""); dep[emp[code].department_code].number--;///原部門人數減一 cin >> emp[code].department_code; dep[emp[code].department_code].number++;//現部門人數加一 printf("%70s修改成功!修改之後的信息如下:\n", ""); emp[code].show_inf(); return; } void delete_emp() { printf("%70s請輸入要刪除的員工號:", ""); int code; cin >> code; if (!emp.count(code)) printf("%70s該員工不存在!無法刪除\n", ""); else { dep[emp[code].department_code].number--;//原部門人數減一 for (auto it = emp.begin(); it != emp.end(); it++) if (it->first == code) { printf("%70s您刪除的員工信息如下:\n", ""); emp[it->second.code].show_inf(); emp.erase(it); break; } for (auto it = wag.begin(); it != wag.end(); it++) if (it->first == code) { wag.erase(it); break; } } return; } void add_wag() { printf("%70s請輸入錄入工資的員工編號:", ""); int code; cin >> code; if (!emp.count(code)) printf("%70s該員工不存在!\n", ""); else if (wag.count(code)) printf("%70s該員工信息已錄入!\n", ""); else { wag[code].code = code; printf("%70s請輸入員工的基本工資:", ""); cin >> wag[code].base_wage; printf("%70s請輸入員工的加班工資:", ""); cin >> wag[code].overtime; printf("%70s請輸入員工的津貼:", "");cin >> wag[code].bonus; printf("%70s請輸入員工的代扣款:", ""); cin >> wag[code].withholding; printf("%70s員工工資信息添加成功!\n", ""); printf("%70s工資信息如下:\n", ""); wag[code].calculate(); wag[code].show_inf(); } return; } void change_wag() { int code; printf("%70s要修改者的編號:", ""); while (cin >> code && !wag.count(code)) printf("%70s該員工不存在!請重新輸入\n", ""); printf("%70s該員工當前的工資信息如下\n", ""); wag[code].show_inf(); printf("%70s請輸入基本工資:", ""); cin >> wag[code].base_wage; printf("%70s請輸入加班工資:", "");cin >> wag[code].overtime; printf("%70s請輸入津貼:", ""); cin >> wag[code].bonus; printf("%70s請輸入代扣款:", ""); cin >> wag[code].withholding; wag[code].calculate(); printf("%70s修改成功!修改之後的工資信息如下\n", ""); wag[code].show_inf(); return; } void delete_wag() { printf("%70s請輸入要刪除工資信息的員工編號:", ""); int code; cin >> code; if (!wag.count(code)) printf("%70s不存在該員工的工資信息\n", ""); else { for (auto it = wag.begin(); it != wag.end(); it++) if (it->first == code) { wag.erase(it); printf("%70s刪除成功!\n", ""); break; } } return; }
fuction10.h
#pragma once bool comp1(const int &a, const int &b)//升序 { return wag[a].payment < wag[b].payment; } bool comp2(const int &a, const int &b) { return wag[a].payment > wag[b].payment; } bool search_inf() { system("cls"); printf("%70s0.返回\n", ""); printf("%70s1.查詢全部員工的工資\n", ""); printf("%70s2.按員工號查詢員工工資\n", ""); printf("%70s3.按部門查詢員工工資\n", ""); printf("%70s4.按指定實發工資區間查詢員工工資\n", ""); printf("%70s5.按員工姓名查詢員工工資\n", ""); printf("%70s6.查詢全部部門信息\n", ""); printf("%70s7.按編號查詢單個員工的基本信息\n", ""); printf("%70s8.按姓名查詢單個員工的基本信息\n", ""); printf("%70s9.顯示所有員工的基本信息\n", ""); printf("%70s請輸入操作編號[0-9]:", ""); int ope; int code; string name; int dir; int flag = 0; long long sum = 0; vector<int> temp; //存儲滿足查詢要求的人員編號 while (cin >> ope && !(ope >= 0 && ope <= 9)) printf("%70s輸入非法,請重新輸入:", ""); if (!ope) return false; switch (ope) { case 1: for (auto it = wag.begin(); it != wag.end(); it++) { temp.push_back(it->first); sum += it->second.payment; } printf("%70s1.按實發工資升序\n", ""); printf("%70s2.按實發工資降序\n", ""); printf("%70s請輸入操作類型:", ""); while (cin >> dir && dir != 1 && dir != 2) printf("%70s輸入非法,請重新輸入!\n", ""); if (dir == 1) sort(temp.begin(), temp.end(), comp1); else sort(temp.begin(), temp.end(), comp2); for (int i = 0; i < (int)temp.size(); i++) wag[temp[i]].show_inf(); printf("%144s合計: %10lld\n", "", sum); break; case 2: printf("%70s請輸入要查詢的員工號:", ""); cin >> code; if (!emp.count(code)) printf("%70s該員工不存在!\n", ""); else if (!wag.count(code)) printf("%70s該員工的工資信息未錄入!\n", ""); else wag[code].show_inf(); break; case 3: printf("%70s請輸入所要查詢員工的部門:", ""); cin >> code; if (!dep.count(code)) printf("%70s該部門不存在!\n", ""); else { for (auto it = wag.begin(); it != wag.end(); it++) if (emp[it->first].department_code == code) { temp.push_back(it->first); sum += it->second.payment; } printf("%70s1.按實發工資升序\n", ""); printf("%70s2.按實發工資降序\n", ""); printf("%70s請輸入操作類型:", ""); while (cin >> dir && dir != 1 && dir != 2) printf("%70s輸入非法,請重新輸入!\n", ""); if (dir == 1) sort(temp.begin(), temp.end(), comp1); else sort(temp.begin(), temp.end(), comp2); for (int i = 0; i < (int)temp.size(); i++) wag[temp[i]].show_inf(); printf("%144s合計: %10lld\n", "", sum); } break; case 4: int up, down; printf("%70s請輸入實發工資區間的上限:", ""); cin >> up; printf("%70s請輸入實發工資區間的下限:", ""); cin >> down; if (up < down) printf("%70s非法的輸入!\n", ""); else { for (auto it = wag.begin(); it != wag.end(); it++) if (it->second.payment >= down && it->second.payment <= up) { temp.push_back(it->first); sum += it->second.payment; } if (!temp.size()) { printf("%70s查詢不到符合條件的員工!\n", ""); cout << "" << endl; break; } printf("%70s1.按實發工資升序\n", ""); printf("%70s2.按實發工資降序\n", ""); printf("%70s請輸入操作類型:", ""); int dir; cin >> dir; if (dir == 1) sort(temp.begin(), temp.end(), comp1); else sort(temp.begin(), temp.end(), comp2); for (int i = 0; i < (int)temp.size(); i++) wag[temp[i]].show_inf(); printf("%144s合計: %10lld\n", "", sum); } break; case 5: printf("%70s請輸入員工姓名:", ""); cin >> name; for (auto it = emp.begin(); it != emp.end(); it++) if (it->second.name == name) { wag[it->first].show_inf(); flag = 1; } if (!flag) printf("%70s查詢不到此人!\n", ""); break;/這個不需要排序,考慮到會有重名 case 6: for (auto it = dep.begin(); it != dep.end(); it++) dep[it->first].show_inf(); break; case 7: printf("%70s請輸入員工編號:", ""); cin >> code; if (!emp.count(code)) printf("%70s該員工不存在!\n", ""); else emp[code].show_inf(); break; case 8: printf("%70s請輸入員工姓名:", ""); cin >> name; for(auto it = emp.begin(); it != emp.end(); it++) if (it->second.name == name) { flag = 1; emp[it->second.code].show_inf(); } if (!flag) printf("%70s查詢不到此人!\n", ""); break; case 9: for (auto it = emp.begin(); it != emp.end(); it++) emp[it->second.code].show_inf(); break; default: return false; } system("pause"); return true; }
menu.h
#pragma once using namespace std; bool show_menu() { int dir; system("cls");//清屏 printf("%70s*************************\n", ""); printf("%70s歡迎進入職工工資管理系統!\n", ""); printf("%70s*************************\n", ""); printf("%70s0. 退出系統\n", ""); printf("%70s1. 部門信息錄入\n", ""); printf("%70s2. 部門信息修改\n", ""); printf("%70s3. 部門信息刪除\n", ""); printf("%70s4. 員工信息錄入\n", ""); printf("%70s5. 員工信息修改\n", ""); printf("%70s6. 員工信息刪除\n", ""); printf("%70s7. 員工工資錄入\n", ""); printf("%70s8. 員工工資修改\n", ""); printf("%70s9. 員工工資刪除\n", ""); printf("%70s10.信息查詢\n", ""); printf("%70s11.修改登錄密碼\n", ""); printf("%70s請輸入操作選項[0-11]:", ""); while (cin >> dir && (dir < 0 || dir > 11)) printf("%70s非法指令,請重新輸入:\n", ""); switch (dir) { case 0: return false; case 1: system("cls"); add_dep(); system("pause"); break; case 2: system("cls"); change_dep(); system("pause"); break; case 3: system("cls"); delete_dep(); system("pause"); break; case 4: system("cls"); add_emp(); system("pause"); break; case 5: system("cls"); change_emp(); system("pause"); break; case 6: system("cls"); delete_emp(); system("pause"); break; case 7: system("cls"); add_wag(); system("pause"); break; case 8: system("cls"); change_wag(); system("pause"); break; case 9: system("cls"); delete_wag(); system("pause"); break; case 10: while (search_inf()); break; case 11: system("cls"); reset_passwd(); system("pause"); break; default: return false; } return true; }
user.h
#pragma once using namespace std; bool login() { int chance = 3; char temp[maxn]; char passwd[maxn]; FILE *p; if ((p = fopen("passwd.txt", "r")) == NULL) { printf("%80s數據異常,無法訪問!\n", ""); fclose(p); exit(0); } fgets(passwd, 100, p); //cout << passwd << endl; fclose(p); printf("%70s********************\n", ""); printf("%70s歡迎使用工資管理系統!\n", ""); printf("%70s********************\n", ""); while (chance--) { printf("%70s請輸入登錄密碼:", ""); scanf("%s", temp); if (!strcmp(temp, passwd)) { printf("%70s密碼正確!\n", ""); return true; } else printf("%70s密碼錯誤!你還剩%d次機會!\n", "", chance); if (!chance) printf("%70s非法用戶!\n", ""); } return false; } void reset_passwd() { char temp1[maxn], temp2[maxn]; char passwd[maxn]; FILE *p; printf("%70s請輸入3-10位不含空格的密碼:", ""); while (scanf("%s", temp1) && (strlen(temp1) < 3 && strlen(temp1) > 10)) printf("%70s密碼非法,請重新輸入\n", ""); printf("%70s請再次輸入修改密碼:", ""); scanf("%s", temp2); if (!strcmp(temp1, temp2)) { p = fopen("passwd.txt", "w"); fgets(passwd, 100, p); strcpy(passwd, temp1); fputs(passwd, p); fclose(p); printf("%70s修改成功!\n", ""); } else printf("%70s修改失敗!\n", ""); return; }
以上就是本文的全部內容,希望對大傢的學習有所幫助,也希望大傢多多支持WalkonNet。