本文实例为大家分享了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;
}