用C++编写程序实现以下功能: (1)按职工号由小到大的顺序将5个员工的数据(包括号码、姓名、年龄、工资)输出到磁盘文件中保存。 (2)从键盘输入两个员工的数据(职工号大于已有的职工号),增加到文件的末尾。 (3)输出文件中全部职工的数据。 (4)从键盘输入一个号码,从文件中查找有无此职工号,如有则显示此职工是第几个只工以及此职工的全部数据。如没有,就输出“无此人”。可以反复多次查询,如果输入找的职工号为0,就结束查询。
时间: 2023-06-25 16:08:32 浏览: 208
C++实现职工工资信息管理系统
以下是使用C++实现的程序,实现以上功能:
```c++
#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>
using namespace std;
// 员工结构体
struct Employee {
int id; // 职工号
string name; // 姓名
int age; // 年龄
float salary; // 工资
};
// 比较函数,用于按职工号排序
bool cmp(const Employee& a, const Employee& b) {
return a.id < b.id;
}
// 将员工数据写入文件
void writeToFile(Employee employees[], int count) {
// 按职工号排序
sort(employees, employees + count, cmp);
// 写入文件
ofstream outFile("employee.txt");
if (outFile.is_open()) {
for (int i = 0; i < count; i++) {
outFile << employees[i].id << " " << employees[i].name << " " << employees[i].age << " " << employees[i].salary << endl;
}
outFile.close();
}
}
// 从文件中读取员工数据
int readFromFile(Employee employees[]) {
int count = 0;
ifstream inFile("employee.txt");
if (inFile.is_open()) {
while (!inFile.eof()) {
inFile >> employees[count].id >> employees[count].name >> employees[count].age >> employees[count].salary;
count++;
}
inFile.close();
}
return count;
}
// 增加员工数据
void addEmployee(Employee employees[], int& count) {
cout << "请输入新员工的职工号:" << endl;
int id;
cin >> id;
// 查找职工号是否已存在
bool found = false;
for (int i = 0; i < count; i++) {
if (employees[i].id == id) {
found = true;
break;
}
}
// 如果职工号已存在,输出错误信息
if (found) {
cout << "职工号已存在!" << endl;
return;
}
// 输入其他信息
cout << "请输入新员工的姓名:" << endl;
string name;
cin >> name;
cout << "请输入新员工的年龄:" << endl;
int age;
cin >> age;
cout << "请输入新员工的工资:" << endl;
float salary;
cin >> salary;
// 增加新员工数据
employees[count].id = id;
employees[count].name = name;
employees[count].age = age;
employees[count].salary = salary;
count++;
// 写入文件
writeToFile(employees, count);
cout << "新员工数据已添加。" << endl;
}
// 显示所有员工数据
void showAllEmployees(Employee employees[], int count) {
cout << "职工号\t姓名\t年龄\t工资" << endl;
for (int i = 0; i < count; i++) {
cout << employees[i].id << "\t" << employees[i].name << "\t" << employees[i].age << "\t" << employees[i].salary << endl;
}
}
// 查找员工数据
void searchEmployee(Employee employees[], int count) {
cout << "请输入要查找的职工号:" << endl;
int id;
cin >> id;
// 查找职工号是否存在
int index = -1;
for (int i = 0; i < count; i++) {
if (employees[i].id == id) {
index = i;
break;
}
}
// 输出查找结果
if (index == -1) {
cout << "无此人。" << endl;
} else {
cout << "第" << index + 1 << "个职工" << endl;
cout << "职工号\t姓名\t年龄\t工资" << endl;
cout << employees[index].id << "\t" << employees[index].name << "\t" << employees[index].age << "\t" << employees[index].salary << endl;
}
}
int main() {
Employee employees[10];
int count = readFromFile(employees);
int choice;
do {
cout << "请选择操作:" << endl;
cout << "1. 输出所有员工数据到文件" << endl;
cout << "2. 增加员工数据" << endl;
cout << "3. 显示所有员工数据" << endl;
cout << "4. 查找员工数据" << endl;
cout << "0. 退出程序" << endl;
cin >> choice;
switch (choice) {
case 1:
writeToFile(employees, count);
break;
case 2:
addEmployee(employees, count);
break;
case 3:
showAllEmployees(employees, count);
break;
case 4:
searchEmployee(employees, count);
break;
case 0:
break;
default:
cout << "无效的选择!" << endl;
break;
}
} while (choice != 0);
return 0;
}
```
注意:以上程序使用了文件读写操作,需要在程序所在的目录下新建一个名为“employee.txt”的文件,用于存储员工数据。
阅读全文