编程序实现以下功能: (1) 按职工号由小到大的顺序将5个员工的数据(包括号码、姓名、年龄、工资)输出到磁盘文件中保存。 (2) 从键盘输入两个员工的数据(职工号大于已有的职工号),增加到文件的末尾。 (3) 输出文件中全部职工的数据。 (4) 从键盘输入一个号码,从文件中查找有无此职工号,如有则显示此职工是第几个职工,以及此职工的全部数据。如没有,就输出“无此人”。可以反复多次查询,如果输入查找的职工号为0,就结束查询。
时间: 2024-02-28 13:57:34 浏览: 74
下面是一个实现以上功能的C++程序:
```c++
#include <iostream>
#include <fstream>
#include <string>
#include <algorithm>
using namespace std;
struct Employee {
int id;
string name;
int age;
double salary;
};
bool cmp(Employee a, Employee b) {
return a.id < b.id;
}
void saveToFile(Employee employees[], int n, string filename) {
ofstream ofs(filename, ios::out);
if (!ofs) {
cerr << "Failed to open file for writing!" << endl;
return;
}
sort(employees, employees + n, cmp);
for (int i = 0; i < n; i++) {
ofs << employees[i].id << " "
<< employees[i].name << " "
<< employees[i].age << " "
<< employees[i].salary << endl;
}
ofs.close();
}
Employee readFromKeyboard() {
Employee employee;
cout << "Input id, name, age, salary: ";
cin >> employee.id >> employee.name >> employee.age >> employee.salary;
return employee;
}
void appendToFile(Employee employee, string filename) {
ofstream ofs(filename, ios::app);
if (!ofs) {
cerr << "Failed to open file for appending!" << endl;
return;
}
ofs << employee.id << " "
<< employee.name << " "
<< employee.age << " "
<< employee.salary << endl;
ofs.close();
}
void displayAll(string filename) {
ifstream ifs(filename, ios::in);
if (!ifs) {
cerr << "Failed to open file for reading!" << endl;
return;
}
int count = 0;
Employee employee;
while (ifs >> employee.id >> employee.name >> employee.age >> employee.salary) {
cout << employee.id << " "
<< employee.name << " "
<< employee.age << " "
<< employee.salary << endl;
count++;
}
ifs.close();
if (count == 0) {
cout << "No employees found!" << endl;
}
}
void searchById(string filename) {
ifstream ifs(filename, ios::in);
if (!ifs) {
cerr << "Failed to open file for reading!" << endl;
return;
}
int id;
bool found = false;
Employee employee;
cout << "Input id to search (0 to exit): ";
cin >> id;
while (id != 0) {
int count = 0;
found = false;
ifs.seekg(0, ios::beg);
while (ifs >> employee.id >> employee.name >> employee.age >> employee.salary) {
count++;
if (employee.id == id) {
found = true;
cout << "Employee #" << count << ": " << endl
<< "id: " << employee.id << " "
<< "name: " << employee.name << " "
<< "age: " << employee.age << " "
<< "salary: " << employee.salary << endl;
break;
}
}
if (!found) {
cout << "No employee with id " << id << " found!" << endl;
}
cout << "Input id to search (0 to exit): ";
cin >> id;
}
ifs.close();
}
int main() {
const int MAX_EMPLOYEES = 5;
string filename = "employees.txt";
Employee employees[MAX_EMPLOYEES] = {
{103, "Tom", 30, 5000.0},
{101, "Jerry", 28, 4500.0},
{104, "Alice", 35, 8000.0},
{105, "Bob", 32, 6000.0},
{102, "Marry", 26, 4000.0}
};
int n = MAX_EMPLOYEES;
saveToFile(employees, n, filename);
cout << "Saved " << n << " employees to file " << filename << endl;
Employee employee1 = readFromKeyboard();
appendToFile(employee1, filename);
cout << "Appended 1 employee to file " << filename << endl;
Employee employee2 = readFromKeyboard();
appendToFile(employee2, filename);
cout << "Appended 1 employee to file " << filename << endl;
displayAll(filename);
searchById(filename);
return 0;
}
```
在这个程序中,我们定义了一个结构体Employee,包含职工号码、姓名、年龄和工资等信息。程序中使用了STL库中的ofstream、ifstream、sort等函数来实现文件的读写和数据的排序等操作。
程序中首先将5个员工的数据保存到文件中,然后从键盘输入两个员工的数据,并将它们追加到文件末尾。接着输出文件中全部职工的数据,最后实现了按职工号从文件中查找职工的功能。
注意,在处理文件时,我们需要对文件是否成功打开进行检查,避免文件操作失败导致程序崩溃。此外,我们在读写文件时使用了文件流的seekg函数来实现文件的随机读取,这样可以快速定位到文件中指定位置的数据。
阅读全文