Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
391 views
in Technique[技术] by (71.8m points)

arrays - How Do I Sort The Employee Data In This Program From Oldest Person To Youngest Person? C++

Here i have a program that reads a file of employee data into an array of structs. I have a function that searches the array and finds the oldest person and prints out that person's data. But now i need to modify a function that sorts through the array and moves the oldest persons data to the 1st position in the array, the 2nd oldest to the 2nd position and so on and does the same for all 1000 employees so that it will sort then print all employee data from oldest to youngest. How could i do this?

Here is the first few lines of data to give an idea of the layout(date of birth is laid out YYYYMMDD

114680858 19670607 Matilda Vincent MI

114930037 19471024 Desdemona Hanover ID

115550206 19790110 Xanadu Perlman ND

116520629 19630921 Alexander Hall SD



struct employees // employee data
{
int ss_number;//social security
int dob;//date of birth YYYY/MM/DD Ex.) 19870314=1987/03/14
string f_name;
string l_name;
string state; //state of residence 
};


void read_file()//read file into array of 1000 structs
{
ifstream data("/home/www/class/een118/labs/database1.txt");
employees array[1000]
if(!data.fail())
{
int i;
for(int i=0;i<1000;i++)
{
data>>array[i].ss_number
>>array[i].dob
>>array[i].f_name
>>array[i].l_name
>>array[i].state;
}
for(int i=0;i<1000;i++)
{
cout<<array[i].ss_number>>" "<<array[i].dob>>" "<<array[i].f_name>>" "<<
array[i].l_name>>" "<<array[i].state;
}}}


void print_person(employees e)
{
cout<<e.ss_number>>" "<<e.dob>>" "<<e.f_name>>" "<<e.l_name>>" "<<e.state;
}


void find_oldest(employees array[])// oldest person = smallest dob
{
int i;
int index=0
int oldest=1000000000;//dummy variable
for(i=1;i<1000;i++)//1000 is array length
{
if(array[i].dob<oldest)
{
index=i;
oldest=array[i].dob;
}
}
print_person(array[i]);
}


int main()
{
employees array[1000];
read_file(array);
find_oldest(array);
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

It is not clear why the function is called as find_oldest.

Nevertheless you could use standard algorithm std::sort with a compare function that can be a lambda expression. For example

#include <iostream>

void find_oldest( employees array[], size_t n )
{
   std::sort( array, array + n, 
              []( const employees &e1, const employees &e2 )
              {
                 return ( e1.dob > e2.dob );
              } );  

   for ( size_t i = 0; i < n; i++ ) print_person( array[i] );
}

The other way instead of using the lambda expression is either declare operator > for structure employees or a functional object. In my opinion it is better to define a functional object. In this case for any kind of sorting for example by first names or by last name you could use a separate functional object. For example

struct employees // employee data
{
   int ss_number;//social security
   int dob;//date of birth YYYY/MM/DD Ex.) 19870314=1987/03/14
   string f_name;
   string l_name;
   string state; //state of residence 
   struct sort_by_dob
   {
      bool operator ()( const employees &e1, const employees &e2 ) const
      {
         return ( e1.dob > e2.dob );
      }
   };
};



void find_oldest( employees array[], size_t n )
{
   std::sort( array, array + n, employees::sort_by_dob() );  

   for ( size_t i = 0; i < n; i++ ) print_person( array[i] );
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...