如何从文件中提取薪酬最高的员工的详细信息

How to extract the details of the highest paid employee in files?

本文关键字:详细信息 文件 提取      更新时间:2023-10-16

我在从empdetails.txt提取薪酬最高的员工,并最终显示给用户的问题。我已经完成了从用户和合并两个文件的详细信息,但显示使用功能的最高报酬,我不知道它。

这是我到现在为止的代码:

#include<iostream>
#include<conio.h>
#include<fstream>
using namespace std;
class emp
{
      int num,age;
      char name[20],dep[5];
      public:
          void getdata()
          {
             cout<<"nn  Name   = ";
             cin>>name;
             cout<<"n Emp Num   = ";
             cin>>num;
             cout<<"n Department= ";
             cin>>dep;
             cout<<"n Age       = ";
             cin>>age;
          }
          void display1()
          {  
            cout<<"n"<<name<<"t"<<num<<"t"<<dep<<"tt"<<age;
          }  
};
class sal
{
      float gs,ns;
       public:
           void getsal()
           {
             cout<<"n Gross sal = ";
             cin>>gs;
             cout<<"n Net sal   = ";
             cin>>ns;
           }
           void display2()
           {
             cout<<"t"<<gs<<"t"<<ns;
           }          
};
void display()
{
   emp e;sal s;
   ifstream fil1;
   fil1.open("empdetails.txt",ios::in);
   cout<<"nn Name t Emp Num t Dep t Age t Gross Sal t Net Sal n";  
  while(!fil1.eof())
  {
    fil1.read((char*)&e,sizeof(e));
    e.display1();
    fil1.read((char*)&s,sizeof(s));
    s.display2();
  }   
}
int main()
{
    int n;
    emp e1;sal s1;
    ofstream fil1,fil2,fil3;
    fil1.open("emp.txt",ios::out);
    fil2.open("sal.txt",ios::out);
    fil3.open("empdetails.txt",ios::out);
    cout<<"n How many employee details do you want to enter = ";
    cin>>n;
    cout<<"n Enter the deatils one by one n";
    for(int i=0;i<n;i++)
    {
        e1.getdata();
        fil1.write((char*)&e1,sizeof(e1));
        s1.getsal();
        fil2.write((char*)&s1,sizeof(s1));
        fil3.write((char*)&e1,sizeof(e1));
        fil3.write((char*)&s1,sizeof(s1));
    }
    fil1.close();
    fil2.close();
    fil3.close();
    cout<<"nntt Merged file contents nntt";
    display();
    getch();
    return 0;
} 

如何创建函数以及使用什么条件?

你不需要一个函数,已经有一个了:std::max_element。它可以计算出您正在处理class emp (std::max_element的前两个参数)。它无法计算出您希望员工按工资排序,因此这是您必须提供的第三个参数:一个接受两个员工的函数,如果第一个员工的收入低于第二个员工,则返回true。(听起来很奇怪,但这允许您对std::min_element使用相同的函数)