C++职工工资管理 基本要求:定义职工(employee)类,其中至少包括姓名、性别、工号、电话、所在科室和 工资。功能要求:1、设计菜单实现功能选择;2、输入功能:输入职工信息,并保存到文件中;查询功能:1)能够根据工号精确查询职工信息;2)能够根据姓名
时间: 2023-07-10 12:14:30 浏览: 185
来模糊查询职工信息,并将查询结果输出;3、能够统计并输出所有职工的工资总和、平均工资和最高工资者的信息;4、能够按照工资从高到低排序并输出职工信息。
以下是一个实现职工工资管理系统的示例代码:
```c++
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
// 定义职工类
class Employee {
public:
string name; // 姓名
string gender; // 性别
string id; // 工号
string phone; // 电话
string dept; // 所在科室
double salary; // 工资
};
// 定义职工管理类
class EmployeeManager {
public:
vector<Employee> employees; // 所有职工信息
// 从文件中读取职工信息
void loadFromFile(string filename) {
ifstream ifs(filename);
if (!ifs) {
cout << "Error: cannot open file " << filename << endl;
return;
}
Employee emp;
while (ifs >> emp.name >> emp.gender >> emp.id >> emp.phone >> emp.dept >> emp.salary) {
employees.push_back(emp);
}
ifs.close();
cout << "Load " << employees.size() << " employees from file " << filename << endl;
}
// 将职工信息保存到文件中
void saveToFile(string filename) {
ofstream ofs(filename);
if (!ofs) {
cout << "Error: cannot open file " << filename << endl;
return;
}
for (auto &emp : employees) {
ofs << emp.name << ' ' << emp.gender << ' ' << emp.id << ' ' << emp.phone << ' ' << emp.dept << ' ' << emp.salary << endl;
}
ofs.close();
cout << "Save " << employees.size() << " employees to file " << filename << endl;
}
// 添加职工信息
void addEmployee() {
Employee emp;
cout << "Enter employee's name: ";
getline(cin, emp.name);
cout << "Enter employee's gender (male or female): ";
getline(cin, emp.gender);
cout << "Enter employee's id: ";
getline(cin, emp.id);
cout << "Enter employee's phone: ";
getline(cin, emp.phone);
cout << "Enter employee's department: ";
getline(cin, emp.dept);
cout << "Enter employee's salary: ";
cin >> emp.salary;
cin.ignore(); // 忽略换行符
employees.push_back(emp);
cout << "Add employee successfully!" << endl;
}
// 根据工号查询职工信息
void searchById() {
string id;
cout << "Enter employee's id to search: ";
getline(cin, id);
for (auto &emp : employees) {
if (emp.id == id) {
cout << "Name: " << emp.name << endl;
cout << "Gender: " << emp.gender << endl;
cout << "Id: " << emp.id << endl;
cout << "Phone: " << emp.phone << endl;
cout << "Department: " << emp.dept << endl;
cout << "Salary: " << emp.salary << endl;
return;
}
}
cout << "Cannot find employee with id " << id << endl;
}
// 根据姓名模糊查询职工信息
void searchByName() {
string name;
cout << "Enter employee's name to search: ";
getline(cin, name);
vector<Employee> result;
for (auto &emp : employees) {
if (emp.name.find(name) != string::npos) {
result.push_back(emp);
}
}
if (result.empty()) {
cout << "Cannot find employee with name " << name << endl;
} else {
for (auto &emp : result) {
cout << "Name: " << emp.name << endl;
cout << "Gender: " << emp.gender << endl;
cout << "Id: " << emp.id << endl;
cout << "Phone: " << emp.phone << endl;
cout << "Department: " << emp.dept << endl;
cout << "Salary: " << emp.salary << endl;
}
}
}
// 统计职工工资总和、平均工资和最高工资者的信息
void getStatistics() {
double sum = 0, maxSalary = 0;
for (auto &emp : employees) {
sum += emp.salary;
maxSalary = max(maxSalary, emp.salary);
}
double avgSalary = employees.empty() ? 0 : sum / employees.size();
cout << "Total salary: " << sum << endl;
cout << "Average salary: " << avgSalary << endl;
cout << "Employee(s) with highest salary: " << endl;
for (auto &emp : employees) {
if (emp.salary == maxSalary) {
cout << "Name: " << emp.name << endl;
cout << "Gender: " << emp.gender << endl;
cout << "Id: " << emp.id << endl;
cout << "Phone: " << emp.phone << endl;
cout << "Department: " << emp.dept << endl;
cout << "Salary: " << emp.salary << endl;
}
}
}
// 按照工资从高到低排序并输出职工信息
void sortBySalary() {
sort(employees.begin(), employees.end(), [](const Employee &a, const Employee &b) {
return a.salary > b.salary;
});
for (auto &emp : employees) {
cout << "Name: " << emp.name << endl;
cout << "Gender: " << emp.gender << endl;
cout << "Id: " << emp.id << endl;
cout << "Phone: " << emp.phone << endl;
cout << "Department: " << emp.dept << endl;
cout << "Salary: " << emp.salary << endl;
}
}
};
int main() {
EmployeeManager manager;
manager.loadFromFile("employees.txt");
while (true) {
cout << "Menu:" << endl;
cout << "1. Add employee" << endl;
cout << "2. Search by id" << endl;
cout << "3. Search by name" << endl;
cout << "4. Get statistics" << endl;
cout << "5. Sort by salary" << endl;
cout << "6. Save to file" << endl;
cout << "0. Exit" << endl;
int choice;
cout << "Enter your choice: ";
cin >> choice;
cin.ignore(); // 忽略换行符
switch (choice) {
case 0:
return 0;
case 1:
manager.addEmployee();
break;
case 2:
manager.searchById();
break;
case 3:
manager.searchByName();
break;
case 4:
manager.getStatistics();
break;
case 5:
manager.sortBySalary();
break;
case 6:
manager.saveToFile("employees.txt");
break;
default:
cout << "Invalid choice, please try again!" << endl;
}
}
return 0;
}
```
这个程序使用了类和文件读写等 C++ 特性,实现了简单的职工工资管理系统。
阅读全文