C++ 学习笔记4

 

Define a base class Person that contains universal information, including name, address, birth day, and gender. Derived from this class the following classes:

 


class student: virtual public person {

 


//……relevant additional state and behavior

 


};

 


class worker: virtual public person {

 


//……relevant additional state and behavior

 


};

 


class student_worker: public student, public worker {

 


……

 


};

 


Write a program that reads a file of information and creates a list of persons. Process the list to create, in sorted order by last name, a list of all people, a list of people who are students, a list of people who are employees, and a list of people who are student-employees.


大学里有这样几类人员:学生、教师、职员和在职读书的教师。定义基类Person(类中包括姓名、地址、出生日期、性别等信息),并从该类派生出上述类。请编写程序,首先从文件中读取信息,并建立一个 人员列表,然后再处理该列表并建立如下新表:按姓氏排序的全部人员列表,学生列表,教师列表,职员列表,在职读书的教师列表。


注意虚基类的使用。


 

name.txt

person: green li newyork 0214 male|
person: mark twin london 0314 male| 
student: bush jim newjersy 0401 male havard|
student: wash kate beriln 0501 female yule|
worker: tom kate beijing 0123 male microsoft|
worker: han ruchn miami 0612 female oracle|
student_worker: fan zhao beijing 0826 male ibm bit|
student_worker: ma ting beijing 0725 male ms bit|
0

 

#include
#include
#include
#include
#include
using namespace std;

//define the person class
class person
{
public:
  
  person() {};
  ~person(){};
  void show();
 
public:
  char firstname[16];
  char lastname[16];
  char address[32];
  char birthday[8];
  char gender[6];
};


void person::show()
{      
 cout<   <<"  "<<"birthday:"< }

 


//define the student class
class student: virtual public person
{
public:
   student() {};
   ~student() {};
     void show(); 
 

public:
 char college[20];
    
};


void student::show()
{
 person::show();
    cout<<"  "<<"college:"< }

//define the worker class
class worker: virtual public person
{
public:
  worker() {};
  ~worker(){};
  void show();
  

public:
 char companyname[20];
      
};

void worker::show()
{
 person::show();
 cout<<"  "<<"companyname:"<

}

//define the student_worker class
class student_worker: public student, public worker
{
public :
  student_worker() {};
  ~student_worker() {};
  void show();
  

};


void student_worker::show()
{
 person::show();

 cout<<"  "<<"companyname:"< }


int main()
{
 
   person  per[100];
   student stu[100];
   worker  wor[100];
   student_worker  stw[100];  
   char  a[100];
   char temp[200];
   int sum=1,k=0,num1=0,num2=0,num3=0,num4=0;
   int i,j,min;
   memset(a,'\0',sizeof(a));
 
   ifstream info;
  
   info.open("name.txt",ios::in);
   while(info.getline(temp,199))
   {
    if(temp[0]=='0')
     break;
    if (temp[0]=='p')
    { 
     k=0;
     for(i=8;temp[i]!=' ';i++)
      a[k++]=temp[i];
     a[k]='\0';

     strcpy(per[num1].firstname,a);
     k=0;
     for( j=i+1;temp[j]!=' ';j++)
      a[k++]=temp[j];
     a[k]='\0';

     strcpy(per[num1].lastname,a);

     k=0;
     for( i=j+1;temp[i]!=' ';i++)
      a[k++]=temp[i];
     a[k]='\0';

     strcpy(per[num1].address,a);

     k=0;
     for( j=i+1;temp[j]!=' ';j++)
      a[k++]=temp[j];
     a[k]='\0';
    
     strcpy(per[num1].birthday,a);
          
     k=0;
     for( i=j+1;temp[i]!='|';i++)
      a[k++]=temp[i];
     a[k]='\0';
    
     strcpy(per[num1].gender,a);

     num1++;


    }
    if(temp[0]=='s'&&temp[7]==':')
    {
     k=0;
     for( i=9;temp[i]!=' ';i++)
      a[k++]=temp[i];
     a[k]='\0';
    
     strcpy(stu[num2].firstname,a);
     k=0;
     for( j=i+1;temp[j]!=' ';j++)
      a[k++]=temp[j];
     a[k]='\0';
    
     strcpy(stu[num2].lastname,a);
    
     k=0;
     for( i=j+1;temp[i]!=' ';i++)
      a[k++]=temp[i];
     a[k]='\0';
    
     strcpy(stu[num2].address,a);
    
     k=0;
     for( j=i+1;temp[j]!=' ';j++)
      a[k++]=temp[j];
     a[k]='\0';
    
     strcpy(stu[num2].birthday,a);
          
     k=0;
     for( i=j+1;temp[i]!=' ';i++)
      a[k++]=temp[i];
     a[k]='\0';
    
     strcpy(stu[num2].gender,a);

     k=0;
     for(j=i+1; temp[j]!='|';j++)
      a[k++]=temp[j];
     a[k]='\0';

     strcpy(stu[num2].college,a);
            
     num2++;


    }
    if (temp[0]=='w')
    {
     k=0;
     for( i=8;temp[i]!=' ';i++)
      a[k++]=temp[i];
     a[k]='\0';
    
     strcpy(wor[num3].firstname,a);
     k=0;
     for( j=i+1;temp[j]!=' ';j++)
      a[k++]=temp[j];
     a[k]='\0';
    
     strcpy(wor[num3].lastname,a);
    
     k=0;
     for( i=j+1;temp[i]!=' ';i++)
      a[k++]=temp[i];
     a[k]='\0';
    
     strcpy(wor[num3].address,a);
    
     k=0;
     for( j=i+1;temp[j]!=' ';j++)
      a[k++]=temp[j];
     a[k]='\0';
    
     strcpy(wor[num3].birthday,a);
          
     k=0;
     for( i=j+1;temp[i]!=' ';i++)
      a[k++]=temp[i];
     a[k]='\0';
    
     strcpy(wor[num3].gender,a);

    
     k=0;
     for(j=i+1; temp[j]!='|';j++)
      a[k++]=temp[j];
     a[k]='\0';
    
     strcpy(wor[num3].companyname,a);
    
     num3++;
    

    }
    if (temp[0]=='s'&&temp[7]=='_')
    {
     k=0;
     for( i=16;temp[i]!=' ';i++)
      a[k++]=temp[i];
     a[k]='\0';
    
     strcpy(stw[num4].firstname,a);
     k=0;
     for( j=i+1;temp[j]!=' ';j++)
      a[k++]=temp[j];
     a[k]='\0';
    
     strcpy(stw[num4].lastname,a);
    
     k=0;
     for( i=j+1;temp[i]!=' ';i++)
      a[k++]=temp[i];
     a[k]='\0';
    
     strcpy(stw[num4].address,a);
    
     k=0;
     for( j=i+1;temp[j]!=' ';j++)
      a[k++]=temp[j];
     a[k]='\0';
    
     strcpy(stw[num4].birthday,a);
          
     k=0;
     for( i=j+1;temp[i]!=' ';i++)
      a[k++]=temp[i];
     a[k]='\0';
    
     strcpy(stw[num4].gender,a);
    
     k=0;
     for(j=i+1; temp[j]!=' ';j++)
      a[k++]=temp[j];
     a[k]='\0';
    
     strcpy(stw[num4].college,a);

     k=0;
     for(i=j+1;temp[i]!='|';i++)
      a[k++]=temp[i];
     a[k]='\0';

     strcpy(stw[num4].companyname,a);

      num4++;
    }
   }

info.close;

 


cout<<"this is a list of persons."<  for(k=0;k  { 
  min=0;
  for(i=0;i   { 
             if(strcmp(per[i].lastname,per[min].lastname)<0)
    { min=i;
     
    }
    continue;
  }
  per[min].show();
  strcpy(per[min].lastname,"{");
  cout<  }

 cout<<"this is a list of students."<  for ( k=0; k  {  
  min=0;
  for(i=0;i   {
    if(strcmp(stu[i].lastname,stu[min].lastname)<0)
    {
     min=i;
    
    }
    continue;
  }
     stu[min].show();
  strcpy(stu[min].lastname,"{");
         cout<  }

 cout<<"this is a list of workers."<  for(k=0;k  { 
  min=0;
  for(i=0;i   {
    if(strcmp(wor[i].lastname,wor[min].lastname)<0)
    {
     min=i;
    
    }
    continue;
  }
    wor[min].show();
  strcpy(wor[min].lastname,"{");
 
     cout<  }

  cout<<"this is a list of student_workers."<  for(k=0;k  {
    min=0;
  for(i=0;i   {
    if(strcmp(stw[i].lastname,stw[min].lastname)<0)
    {
     min=i;
    
    }
    continue;
  }
    stw[min].show();
  strcpy(stw[min].lastname,"{");
  cout<  }   

   
  
  
  return 0;
}                           

 

你可能感兴趣的:(Reading,notes)