编程实现以下功能: 1、定义职工类employee:字段成员:职工号nurn,姓名narne,年龄age,工资salary: 成员函数:返回职工号getnum0:友元函数:重载运算符>>, ≤≤。 2、编写函数按职工号从小到大的顺序将5个职工的数据输出到磁益文件 filel.txt 中保存,int savetofileO:该函数返回输入数据的个数。 3、编写函数从键盘输入一个员工的数据,增加到文件flel.txt的末感,intinserttile(int n):插入完成后将新的员工的数量返回。 4、编写函数从文件中输出全部员工的数据,void outfromfile(int n)。 5、编写函数从键盘输入一个号码,从文件中查找有无此职工号,如有则豆示此职工是第几个职工以及此职工的全部数据,如没有,输出“无此人”,void findfle(int a)。

时间: 2024-02-01 17:17:02 浏览: 31
以下是实现上述功能的代码: ```c++ #include<iostream> #include<fstream> #include<string> #include<algorithm> using namespace std; class employee{ private: int num; string name; int age; int salary; public: employee(){} employee(int n, string nm, int a, int s):num(n), name(nm), age(a), salary(s){} friend istream& operator>>(istream& in, employee& e); friend bool operator<=(employee& e1, employee& e2); int getnum() const {return num;} void print() const {cout<<num<<'\t'<<name<<'\t'<<age<<'\t'<<salary<<endl;} }; istream& operator>>(istream& in, employee& e){ in>>e.num>>e.name>>e.age>>e.salary; return in; } bool operator<=(employee& e1, employee& e2){ return e1.num<=e2.num; } int savetofile(employee* emp, int n){ sort(emp, emp+n, [](employee& e1, employee& e2){return e1<=e2;}); ofstream fout("file1.txt"); if(!fout){ cout<<"Cannot open the file!"<<endl; } else{ for(int i=0;i<n;i++){ fout<<emp[i].getnum()<<'\t'<<emp[i].name<<'\t'<<emp[i].age<<'\t'<<emp[i].salary<<endl; } fout.close(); } return n; } int insertfile(int n){ employee newemp; cout<<"Please input the information of the new employee:"<<endl; cin>>newemp; ofstream fout("file1.txt", ios::app); if(!fout){ cout<<"Cannot open the file!"<<endl; return n; } else{ fout<<newemp.getnum()<<'\t'<<newemp.name<<'\t'<<newemp.age<<'\t'<<newemp.salary<<endl; fout.close(); return n+1; } } void outfromfile(int n){ ifstream fin("file1.txt"); if(!fin){ cout<<"Cannot open the file!"<<endl; } else{ employee emp; for(int i=0;i<n;i++){ fin>>emp; emp.print(); } fin.close(); } } void findfile(int a){ ifstream fin("file1.txt"); if(!fin){ cout<<"Cannot open the file!"<<endl; } else{ employee emp; bool found=false; int count=0; while(fin>>emp){ count++; if(emp.getnum()==a){ found=true; cout<<"The employee is the "<<count<<"th employee:"<<endl; emp.print(); break; } } if(!found){ cout<<"No such employee!"<<endl; } fin.close(); } } int main(){ employee emp[5]={employee(1001,"Tom",25,5000),employee(1002,"Jerry",26,6000), employee(1003,"Bob",30,8000),employee(1004,"Alice",28,7000), employee(1005,"Lucy",23,4500)}; int n=savetofile(emp, 5); cout<<"The number of employees in the file is "<<n<<endl; cout<<"The information of all employees in the file:"<<endl; outfromfile(n); n=insertfile(n); cout<<"After inserting a new employee, the number of employees in the file is "<<n<<endl; cout<<"The information of all employees in the file:"<<endl; outfromfile(n); int num; cout<<"Please input the number of the employee you want to find:"<<endl; cin>>num; findfile(num); return 0; } ``` 这个程序中,职工类`employee`包含了职工的基本信息,包括职工号、姓名、年龄和工资。其中,`getnum`函数返回职工号,而`print`函数输出职工的全部信息。 为了方便操作,我们在类中定义了一个友元函数`operator>>`用来输入职工信息,以及一个重载运算符`<=`用来比较两个职工的职工号大小。 `savetofile`函数用来将职工信息按职工号从小到大的顺序输出到文件中保存。具体操作是,先使用`sort`函数将职工数组按职工号从小到大排序,然后依次将每个职工的信息输出到文件中。 `insertfile`函数用来从键盘输入一个新员工的信息,并将其添加到文件的末尾。具体操作是,先使用`ofstream`打开文件并指定为追加模式,然后将新员工的信息输出到文件中。 `outfromfile`函数用来从文件中输出全部职工的信息。具体操作是,使用`ifstream`打开文件,然后依次读取每个职工的信息并输出。 `findfile`函数用来从文件中查找指定职工号的职工信息。具体操作是,使用`ifstream`打开文件,并依次读取每个职工的信息。如果找到了指定职工号的职工,则输出其信息并退出循环;如果没有找到,则输出提示信息。 在`main`函数中,我们首先创建了5个职工对象,并使用`savetofile`函数将它们的信息输出到文件中。然后,我们使用`outfromfile`函数从文件中输出全部职工信息。接着,我们使用`insertfile`函数向文件中添加一个新职工,并使用`outfromfile`函数输出全部职工信息。最后,我们使用`findfile`函数从文件中查找指定职工号的职工信息。

相关推荐

最新推荐

recommend-type

源代码-ajax即时聊天程序(新手学习推荐).zip

源代码-ajax即时聊天程序(新手学习推荐).zip
recommend-type

python读取excel数据.doc

以下是一个简单的Python代码,用于在控制台上打印一个看起来像爱心的形状。这个代码使用了ASCII字符来创建形状。 python print('\n'.join([''.join([('Love'[(x-y)%4] if ((x*0.05)**2+(y*0.1)**2-1)**3-(x*0.05)**2*(y*0.1)**3 <= 0 else ' ') for x in range(-30, 30)]) for y in range(15, -15, -1)])) 这个代码使用了数学公式来生成心形。但是,由于ASCII字符的限制,这个心形可能看起来不是很完美。 如果你想要一个更详细和定制化的心形,你可能需要使用图形库,如PIL(Python Imaging Library)或matplotlib。但是,这些库通常用于创建图像文件或在图形用户界面上绘制,而不是在控制台上打印。 另外,这里有一个使用turtle模块在图形窗口中绘制爱心的简单示例: python import turtle # 创建一个新的turtle对象 heart = turtle.Turtl
recommend-type

【图像评价】图像去雾质量评价【含Matlab源码 066期】.zip

Matlab领域上传的视频均有对应的完整代码,皆可运行,亲测可用,适合小白; 1、代码压缩包内容 主函数:main.m; 调用函数:其他m文件;无需运行 运行结果效果图; 2、代码运行版本 Matlab 2019b;若运行有误,根据提示修改;若不会,私信博主; 3、运行操作步骤 步骤一:将所有文件放到Matlab的当前文件夹中; 步骤二:双击打开main.m文件; 步骤三:点击运行,等程序运行完得到结果; 4、仿真咨询 如需其他服务,可私信博主或扫描视频QQ名片; 4.1 博客或资源的完整代码提供 4.2 期刊或参考文献复现 4.3 Matlab程序定制 4.4 科研合作
recommend-type

30客户满意度调查表.DOC

30客户满意度调查表.DOC
recommend-type

labelme, 一个用于图像标记的工具

使用python3以上版本
recommend-type

zigbee-cluster-library-specification

最新的zigbee-cluster-library-specification说明文档。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

实现实时数据湖架构:Kafka与Hive集成

![实现实时数据湖架构:Kafka与Hive集成](https://img-blog.csdnimg.cn/img_convert/10eb2e6972b3b6086286fc64c0b3ee41.jpeg) # 1. 实时数据湖架构概述** 实时数据湖是一种现代数据管理架构,它允许企业以低延迟的方式收集、存储和处理大量数据。与传统数据仓库不同,实时数据湖不依赖于预先定义的模式,而是采用灵活的架构,可以处理各种数据类型和格式。这种架构为企业提供了以下优势: - **实时洞察:**实时数据湖允许企业访问最新的数据,从而做出更明智的决策。 - **数据民主化:**实时数据湖使各种利益相关者都可
recommend-type

云原生架构与soa架构区别?

云原生架构和SOA架构是两种不同的架构模式,主要有以下区别: 1. 设计理念不同: 云原生架构的设计理念是“设计为云”,注重应用程序的可移植性、可伸缩性、弹性和高可用性等特点。而SOA架构的设计理念是“面向服务”,注重实现业务逻辑的解耦和复用,提高系统的灵活性和可维护性。 2. 技术实现不同: 云原生架构的实现技术包括Docker、Kubernetes、Service Mesh等,注重容器化、自动化、微服务等技术。而SOA架构的实现技术包括Web Services、消息队列等,注重服务化、异步通信等技术。 3. 应用场景不同: 云原生架构适用于云计算环境下的应用场景,如容器化部署、微服务
recommend-type

JSBSim Reference Manual

JSBSim参考手册,其中包含JSBSim简介,JSBSim配置文件xml的编写语法,编程手册以及一些应用实例等。其中有部分内容还没有写完,估计有生之年很难看到完整版了,但是内容还是很有参考价值的。