根据以下要求用C++编写代码,企业工资管理系统的主要任务是用计算机对各种工资信息进行日常的管理,如查询、修改、增加、删除以及存储等,迅速准确地完成各种工资信息的统计计算和汇总工作,快速打印出工资报表,针对系统服务对象的具体要求,设计了企业工资管理系统。企业工资管理系统主要有以下几大功能: (1)对单位人员的变动进行处理。一个单位的职工不会是一成不变的,总是在不断的变化:有调出、有调入、也有职工在本单位内部调动。因此,设计系统是应考虑到这些情况。 (2)对职工的工资进行计算、修改。可以对职工的工资档案进行个别、部分和批量修改,同时,能对各职工的工资进行计算,即计算应发金额、应扣金额及实发金额等。 (3)查询统计功能。要求即可以单项查询,比如查看某个职工的工资情况等;也可以多项查询,比如某部门工资数在某一范围的职工的工资情况等。 (4)报表打印功能。每月发放工资时,要求能够打印本月的工资表、随工资发给每个职工的工资条以及工资统计表。
时间: 2024-02-23 15:57:18 浏览: 21
以下是一个简单的企业工资管理系统的 C++ 代码示例,包括人员变动处理、工资计算、修改、查询统计和报表打印等功能。代码中使用了结构体来存储职工信息,使用文件来实现数据的存储和读取。
```c++
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
struct Employee{
string name; // 姓名
string id; // 工号
string dep; // 部门
double base_salary; // 基本工资
double bonus; // 奖金
double allowance; // 津贴
double tax; // 税金
double salary; // 实发工资
};
// 添加新员工
void add_employee(){
Employee new_emp;
cout << "请输入新员工的姓名、工号、部门、基本工资、奖金和津贴:" << endl;
cin >> new_emp.name >> new_emp.id >> new_emp.dep >> new_emp.base_salary >> new_emp.bonus >> new_emp.allowance;
new_emp.tax = (new_emp.base_salary + new_emp.bonus + new_emp.allowance) * 0.1;
new_emp.salary = new_emp.base_salary + new_emp.bonus + new_emp.allowance - new_emp.tax;
ofstream outfile("employees.dat", ios::app | ios::binary);
outfile.write((char*)&new_emp, sizeof(new_emp));
outfile.close();
cout << "新员工添加成功!" << endl;
}
// 删除员工
void delete_employee(){
string id;
cout << "请输入要删除的员工工号:" << endl;
cin >> id;
ifstream infile("employees.dat", ios::binary);
ofstream outfile("temp.dat", ios::binary);
Employee emp;
bool found = false;
while (infile.read((char*)&emp, sizeof(emp))){
if (emp.id != id){ // 将非删除员工写入临时文件
outfile.write((char*)&emp, sizeof(emp));
} else {
found = true;
}
}
infile.close();
outfile.close();
remove("employees.dat"); // 删除原文件
rename("temp.dat", "employees.dat"); // 重命名临时文件
if (found){
cout << "员工删除成功!" << endl;
} else {
cout << "未找到该员工,请检查输入工号是否正确。" << endl;
}
}
// 修改员工信息
void modify_employee(){
string id;
cout << "请输入要修改的员工工号:" << endl;
cin >> id;
fstream file("employees.dat", ios::in | ios::out | ios::binary);
Employee emp;
bool found = false;
while (file.read((char*)&emp, sizeof(emp)) && !found){
if (emp.id == id){
found = true;
cout << "请输入修改后的员工姓名、工号、部门、基本工资、奖金和津贴:" << endl;
cin >> emp.name >> emp.id >> emp.dep >> emp.base_salary >> emp.bonus >> emp.allowance;
emp.tax = (emp.base_salary + emp.bonus + emp.allowance) * 0.1;
emp.salary = emp.base_salary + emp.bonus + emp.allowance - emp.tax;
file.seekp(-sizeof(emp), ios::cur); // 移动文件指针到当前记录的开始位置
file.write((char*)&emp, sizeof(emp)); // 写入修改后的记录
cout << "员工信息修改成功!" << endl;
}
}
file.close();
if (!found){
cout << "未找到该员工,请检查输入工号是否正确。" << endl;
}
}
// 统计部门工资总额
void total_salary(){
string dep;
double total = 0;
cout << "请输入要统计工资总额的部门名称:" << endl;
cin >> dep;
ifstream infile("employees.dat", ios::binary);
Employee emp;
while (infile.read((char*)&emp, sizeof(emp))){
if (emp.dep == dep){
total += emp.salary;
}
}
infile.close();
cout << dep << "部门本月工资总额为:" << total << "元。" << endl;
}
// 打印工资表
void print_salary_table(){
string date;
cout << "请输入工资表的日期(如2022年1月):" << endl;
cin >> date;
ofstream outfile("salary_table.txt");
outfile << "工资表 " << date << endl;
outfile << "-------------------------------------------------------------------------------------" << endl;
outfile << "姓名\t工号\t部门\t基本工资\t奖金\t津贴\t税金\t实发工资" << endl;
ifstream infile("employees.dat", ios::binary);
Employee emp;
while (infile.read((char*)&emp, sizeof(emp))){
outfile << emp.name << "\t" << emp.id << "\t" << emp.dep << "\t"
<< emp.base_salary << "\t" << emp.bonus << "\t" << emp.allowance << "\t"
<< emp.tax << "\t" << emp.salary << endl;
}
infile.close();
outfile.close();
cout << "工资表已成功打印到 salary_table.txt 文件中。" << endl;
}
int main(){
int choice;
while (true){
cout << "请选择要执行的操作:" << endl;
cout << "1. 添加新员工" << endl;
cout << "2. 删除员工" << endl;
cout << "3. 修改员工信息" << endl;
cout << "4. 统计部门工资总额" << endl;
cout << "5. 打印工资表" << endl;
cout << "0. 退出" << endl;
cin >> choice;
switch (choice){
case 1:
add_employee();
break;
case 2:
delete_employee();
break;
case 3:
modify_employee();
break;
case 4:
total_salary();
break;
case 5:
print_salary_table();
break;
case 0:
return 0;
default:
cout << "无效的选择,请重新输入。" << endl;
}
}
}
```