编程实现以下功能: 1、定义职工类employee:字段成员:职工号num,姓名name,年龄age,工资salary; 成员函数:返回职工号getnum();友元函数:重载运算符>>,<<。 2、编写函数按职工号从小到大的顺序将 5 个职工的数据输出到磁盘文件 file1.txt中保存,int savetofile();该函数返回输入数据的个数。 3、编写函数从键盘输入一个员工的数据,增加到文件file1.txt 的末尾,int insertfile(int n);插入完成后将新的员工的数量返回。 4、编写函数从文件中输出全部员工的数据,void outfromfile(int n)。 5、编写函数从键盘输入一个号码,从文件中查找有无此职工号,如有则显示此职工是第几个职工以及此职工的全部数据,如没有,输出“无此人”,void findfile(int n)。
时间: 2024-03-10 10:45:59 浏览: 44
下面是实现以上功能的代码:
```c++
#include<iostream>
#include<fstream>
#include<string>
#include<algorithm>
using namespace std;
// 职工类
class employee {
private:
string num; // 职工号
string name; // 姓名
int age; // 年龄
double salary; // 工资
public:
employee(string n="", string na="", int a=0, double s=0): num(n), name(na), age(a), salary(s) {}
string getnum() const { return num; } // 返回职工号
friend istream& operator>>(istream& is, employee& e);
friend ostream& operator<<(ostream& os, const employee& e);
};
// 重载运算符>>,用于输入职工信息
istream& operator>>(istream& is, employee& e) {
is >> e.num >> e.name >> e.age >> e.salary;
return is;
}
// 重载运算符<<,用于输出职工信息
ostream& operator<<(ostream& os, const employee& e) {
os << e.num << ' ' << e.name << ' ' << e.age << ' ' << e.salary;
return os;
}
// 按职工号从小到大排序
bool cmp(const employee& e1, const employee& e2) {
return e1.getnum() < e2.getnum();
}
// 将所有职工数据按职工号从小到大的顺序输出到文件
int savetofile(employee emps[], int n, string filename) {
sort(emps, emps+n, cmp); // 按职工号从小到大排序
ofstream ofs(filename, ios::out); // 打开文件
if (!ofs) {
cerr << "打开文件失败" << endl;
return 0;
}
for (int i=0; i<n; ++i) {
ofs << emps[i] << endl; // 输出职工信息
}
ofs.close(); // 关闭文件
return n;
}
// 从键盘输入一个员工的数据,增加到文件末尾
int insertfile(string filename) {
employee emp;
cout << "请输入职工信息(职工号 姓名 年龄 工资):" << endl;
cin >> emp;
ofstream ofs(filename, ios::app); // 以追加方式打开文件
if (!ofs) {
cerr << "打开文件失败" << endl;
return 0;
}
ofs << emp << endl; // 将新的员工信息追加到文件末尾
ofs.close(); // 关闭文件
return 1;
}
// 从文件中输出全部员工的数据
void outfromfile(string filename) {
ifstream ifs(filename, ios::in); // 打开文件
if (!ifs) {
cerr << "打开文件失败" << endl;
return;
}
employee emp;
int i = 0;
while (ifs >> emp) {
++i;
cout << "第" << i << "个职工信息:" << emp << endl; // 输出职工信息
}
ifs.close(); // 关闭文件
}
// 从键盘输入一个职工号,从文件中查找是否有此职工号
void findfile(string filename) {
string num;
cout << "请输入职工号:" << endl;
cin >> num;
ifstream ifs(filename, ios::in); // 打开文件
if (!ifs) {
cerr << "打开文件失败" << endl;
return;
}
employee emp;
int i = 0;
bool found = false;
while (ifs >> emp) {
++i;
if (emp.getnum() == num) {
cout << "第" << i << "个职工信息:" << emp << endl; // 输出职工信息
found = true;
break;
}
}
if (!found) {
cout << "无此人" << endl;
}
ifs.close(); // 关闭文件
}
int main() {
employee emps[5] = {
{"00001", "张三", 25, 3000.0},
{"00002", "李四", 30, 4000.0},
{"00003", "王五", 35, 5000.0},
{"00004", "赵六", 40, 6000.0},
{"00005", "钱七", 45, 7000.0}
};
int n = sizeof(emps) / sizeof(emps[0]);
string filename = "file1.txt";
savetofile(emps, n, filename); // 将职工信息保存到文件
outfromfile(filename); // 输出文件中的职工信息
insertfile(filename); // 在文件末尾插入新的职工信息
outfromfile(filename); // 再次输出文件中的职工信息
findfile(filename); // 查找职工信息
return 0;
}
```
注意事项:
1. 文件名需要包含路径,例如:`string filename = "C:\\data\\file1.txt";`
2. 代码中的文件操作都需要进行错误检查,避免出现打开文件失败的情况。
3. 在文件末尾插入新的职工信息时,需要以追加方式打开文件,即:`ofstream ofs(filename, ios::app);`
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)