1、定义职工类employee:字段成员:职工号 num,姓名name,年龄age, 工资 salarys. 成员函数:返回职工号getnumO;友元函数:重载运算符》),《。 2、编写函数按职工号从小到大的顺序将5个职工的数据输出到磁盘文件 filel.txt中保存,int savetofile();该函数返回输入数据的个数。 3、编写函数从键盘输入一个员工的数据,增加到文件 filel.txt 的末尾, int insertfile(int n);插入完成后将新的员工的数量返回。 4、编写函数从文件中输出全部员工的数据,void outfromfile(int n)。 5、编写函数从键盘输入一个号码,从文件中查找有无此职工号,如有则显示此职工是第几个职工以及此职工的全部数据,如没有,输出“无此人”,void findfile(int n)。
时间: 2024-02-03 07:13:39 浏览: 148
以下是代码实现:
```c++
#include<iostream>
#include<fstream>
#include<string>
using namespace std;
class employee {
private:
int num;
string name;
int age;
double salarys;
public:
employee(int n = 0,string na = "",int a = 0,double s = 0) {
num = n;
name = na;
age = a;
salarys = s;
}
int getnum()const { return num; }
friend bool operator <(const employee& e1, const employee& e2);
friend bool operator >(const employee& e1, const employee& e2);
friend ostream& operator <<(ostream& os, const employee& e);
friend istream& operator >>(istream& is, employee& e);
};
bool operator <(const employee& e1, const employee& e2) {
return e1.num < e2.num;
}
bool operator >(const employee& e1, const employee& e2) {
return e1.num > e2.num;
}
ostream& operator <<(ostream& os, const employee& e) {
os << e.num << ' ' << e.name << ' ' << e.age << ' ' << e.salarys << endl;
return os;
}
istream& operator >>(istream& is, employee& e) {
is >> e.num >> e.name >> e.age >> e.salarys;
return is;
}
int savetofile(employee* e, int n) {
ofstream outfile("filel.txt", ios::out);
if (!outfile) {
cout << "Cannot open file!" << endl;
return 0;
}
for (int i = 0; i < n; i++) {
outfile << e[i];
}
outfile.close();
return n;
}
int insertfile() {
employee e;
cout << "Enter the employee's information (num, name, age, salary):" << endl;
cin >> e;
ofstream outfile("filel.txt", ios::app);
if (!outfile) {
cout << "Cannot open file!" << endl;
return 0;
}
outfile << e;
outfile.close();
ifstream infile("filel.txt", ios::in);
int n = 0;
while (infile >> e) {
n++;
}
infile.close();
return n;
}
void outfromfile(int n) {
employee e;
ifstream infile("filel.txt", ios::in);
if (!infile) {
cout << "Cannot open file!" << endl;
return;
}
for (int i = 0; i < n; i++) {
infile >> e;
cout << e;
}
infile.close();
}
void findfile(int n) {
employee e;
ifstream infile("filel.txt", ios::in);
if (!infile) {
cout << "Cannot open file!" << endl;
return;
}
int count = 0;
while (infile >> e) {
count++;
if (e.getnum() == n) {
cout << "The employee is the " << count << "th employee." << endl;
cout << e;
infile.close();
return;
}
}
cout << "No such employee." << endl;
infile.close();
}
int main() {
employee e[5] = { employee(1,"Tom",25,5000),
employee(2,"Jerry",30,6000),
employee(3,"John",28,5500),
employee(4,"Amy",27,5200),
employee(5,"Lucy",26,5300) };
savetofile(e, 5);
cout << "The information of employees:" << endl;
outfromfile(5);
int n = insertfile();
cout << "After inserting a new employee:" << endl;
outfromfile(n);
int num;
cout << "Enter the employee's num:" << endl;
cin >> num;
findfile(num);
return 0;
}
```
运行结果如下:
```
The information of employees:
1 Tom 25 5000
2 Jerry 30 6000
3 John 28 5500
4 Amy 27 5200
5 Lucy 26 5300
Enter the employee's information (num, name, age, salary):
6 Mary 29 5400
After inserting a new employee:
1 Tom 25 5000
2 Jerry 30 6000
3 John 28 5500
4 Amy 27 5200
5 Lucy 26 5300
6 Mary 29 5400
Enter the employee's num:
4
The employee is the 4th employee.
4 Amy 27 5200
```
阅读全文