在计算机中建立相应的数据结构,利用程序实现工资的输入、查询、增、删、改、统计、输出等操作。 1、设计一个能够记录公司员工工资的数据结构(比如包括:员工号,姓名,基本工资,补贴金额,奖励金额,扣除金额、实发工资等)、文件(文件作为工资数据的保存介质)。注:,其中实发工资=基本工资+补贴金额+奖励金额-扣除金额 2、应用文件保存工资管理数据。 3、新增:能将所有输入的员工工资数据保存到文件中,并可选择继续输入或退出。 4、查找:输入员工的员工号,并在文件中查找 若找到,则显示该员工的工资详情 否则提示未找
时间: 2023-07-10 20:07:10 浏览: 91
ACM中使用java(完整版)
3星 · 编辑精心推荐
到该员工的工资信息。
以下是一个简单的C++程序,可以实现上述功能:
```c++
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
using namespace std;
// 定义员工工资结构体
struct EmployeeSalary {
int id;
string name;
double baseSalary;
double allowance;
double bonus;
double deduction;
double netSalary;
};
// 保存员工工资数据到文件中
void saveEmployeeSalaryToFile(vector<EmployeeSalary>& employeeSalaries, string filename) {
ofstream fout(filename);
if (!fout) {
cout << "无法打开文件:" << filename << endl;
return;
}
for (auto& employeeSalary : employeeSalaries) {
fout << employeeSalary.id << " " << employeeSalary.name << " "
<< employeeSalary.baseSalary << " " << employeeSalary.allowance << " "
<< employeeSalary.bonus << " " << employeeSalary.deduction << " "
<< employeeSalary.netSalary << endl;
}
fout.close();
}
// 从文件中读取员工工资数据
void readEmployeeSalaryFromFile(vector<EmployeeSalary>& employeeSalaries, string filename) {
ifstream fin(filename);
if (!fin) {
cout << "无法打开文件:" << filename << endl;
return;
}
EmployeeSalary employeeSalary;
while (fin >> employeeSalary.id >> employeeSalary.name >> employeeSalary.baseSalary
>> employeeSalary.allowance >> employeeSalary.bonus >> employeeSalary.deduction
>> employeeSalary.netSalary) {
employeeSalaries.push_back(employeeSalary);
}
fin.close();
}
// 输入员工工资数据
void inputEmployeeSalary(vector<EmployeeSalary>& employeeSalaries) {
EmployeeSalary employeeSalary;
cout << "请输入员工编号:";
cin >> employeeSalary.id;
cout << "请输入员工姓名:";
cin >> employeeSalary.name;
cout << "请输入员工基本工资:";
cin >> employeeSalary.baseSalary;
cout << "请输入员工补贴金额:";
cin >> employeeSalary.allowance;
cout << "请输入员工奖励金额:";
cin >> employeeSalary.bonus;
cout << "请输入员工扣除金额:";
cin >> employeeSalary.deduction;
employeeSalary.netSalary = employeeSalary.baseSalary + employeeSalary.allowance
+ employeeSalary.bonus - employeeSalary.deduction;
employeeSalaries.push_back(employeeSalary);
}
// 查询员工工资数据
void queryEmployeeSalary(vector<EmployeeSalary>& employeeSalaries) {
int id;
cout << "请输入要查询的员工编号:";
cin >> id;
for (auto& employeeSalary : employeeSalaries) {
if (employeeSalary.id == id) {
cout << "员工编号:" << employeeSalary.id << endl;
cout << "员工姓名:" << employeeSalary.name << endl;
cout << "员工基本工资:" << employeeSalary.baseSalary << endl;
cout << "员工补贴金额:" << employeeSalary.allowance << endl;
cout << "员工奖励金额:" << employeeSalary.bonus << endl;
cout << "员工扣除金额:" << employeeSalary.deduction << endl;
cout << "员工实发工资:" << employeeSalary.netSalary << endl;
return;
}
}
cout << "未找到该员工的工资信息。" << endl;
}
// 删除员工工资数据
void deleteEmployeeSalary(vector<EmployeeSalary>& employeeSalaries) {
int id;
cout << "请输入要删除的员工编号:";
cin >> id;
for (auto it = employeeSalaries.begin(); it != employeeSalaries.end(); ++it) {
if (it->id == id) {
employeeSalaries.erase(it);
cout << "已删除该员工的工资信息。" << endl;
return;
}
}
cout << "未找到该员工的工资信息,无法删除。" << endl;
}
// 修改员工工资数据
void updateEmployeeSalary(vector<EmployeeSalary>& employeeSalaries) {
int id;
cout << "请输入要修改的员工编号:";
cin >> id;
for (auto& employeeSalary : employeeSalaries) {
if (employeeSalary.id == id) {
cout << "请输入修改后的员工基本工资:";
cin >> employeeSalary.baseSalary;
cout << "请输入修改后的员工补贴金额:";
cin >> employeeSalary.allowance;
cout << "请输入修改后的员工奖励金额:";
cin >> employeeSalary.bonus;
cout << "请输入修改后的员工扣除金额:";
cin >> employeeSalary.deduction;
employeeSalary.netSalary = employeeSalary.baseSalary + employeeSalary.allowance
+ employeeSalary.bonus - employeeSalary.deduction;
cout << "已修改该员工的工资信息。" << endl;
return;
}
}
cout << "未找到该员工的工资信息,无法修改。" << endl;
}
// 统计所有员工的工资总额和平均工资
void statisticsEmployeeSalary(vector<EmployeeSalary>& employeeSalaries) {
double totalSalary = 0;
for (auto& employeeSalary : employeeSalaries) {
totalSalary += employeeSalary.netSalary;
}
double averageSalary = employeeSalaries.empty() ? 0 : totalSalary / employeeSalaries.size();
cout << "所有员工的工资总额为:" << totalSalary << endl;
cout << "所有员工的平均工资为:" << averageSalary << endl;
}
int main() {
vector<EmployeeSalary> employeeSalaries;
string filename = "employee_salary.txt";
// 从文件中读取员工工资数据
readEmployeeSalaryFromFile(employeeSalaries, filename);
while (true) {
int choice;
cout << "请选择要进行的操作:" << endl;
cout << "1. 输入员工工资数据" << endl;
cout << "2. 查找员工工资数据" << endl;
cout << "3. 删除员工工资数据" << endl;
cout << "4. 修改员工工资数据" << endl;
cout << "5. 统计所有员工的工资总额和平均工资" << endl;
cout << "6. 保存员工工资数据到文件中并退出" << endl;
cin >> choice;
switch (choice) {
case 1:
inputEmployeeSalary(employeeSalaries);
break;
case 2:
queryEmployeeSalary(employeeSalaries);
break;
case 3:
deleteEmployeeSalary(employeeSalaries);
break;
case 4:
updateEmployeeSalary(employeeSalaries);
break;
case 5:
statisticsEmployeeSalary(employeeSalaries);
break;
case 6:
saveEmployeeSalaryToFile(employeeSalaries, filename);
return 0;
default:
cout << "输入的选项不合法,请重新输入。" << endl;
}
}
return 0;
}
```
该程序使用了一个结构体 `EmployeeSalary` 来表示员工工资信息,同时使用了一个动态数组 `employeeSalaries` 来保存所有员工的工资数据。程序提供了以下操作:
1. 输入员工工资数据
2. 查找员工工资数据
3. 删除员工工资数据
4. 修改员工工资数据
5. 统计所有员工的工资总额和平均工资
6. 保存员工工资数据到文件中并退出
在程序中,我们通过 `ofstream` 和 `ifstream` 分别实现了将员工工资数据保存到文件中和从文件中读取员工工资数据的功能。在输入员工工资数据时,我们计算出了员工的实发工资,并将所有工资数据保存到 `employeeSalaries` 中。在查找、删除和修改员工工资数据时,我们都需要遍历 `employeeSalaries` 数组来查找对应的员工。在统计所有员工的工资总额和平均工资时,我们遍历 `employeeSalaries` 数组并累加所有员工的实发工资,然后根据员工数量计算出平均工资。最后,在用户选择退出程序时,我们将所有员工工资数据保存到文件中,并退出程序。
阅读全文