C++ 學習筆記4

來源:互聯網
上載者:User

 

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 <iostream>
#include <fstream>
#include <iomanip>
#include <string>
#include <stdlib.h>
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<<left<<"name:"<<firstname<<" "<<lastname<<"  "<<"address:"<<address
  <<"  "<<"birthday:"<<birthday<<"  "<<"gender:"<<gender;
}

 

//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:"<<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:"<<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:"<<companyname<<"  "<<"college:"<<college;
}

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."<<endl;
 for(k=0;k<num1;k++)
 { 
  min=0;
  for(i=0;i<num1;i++)
  { 
             if(strcmp(per[i].lastname,per[min].lastname)<0)
    { min=i;
     
    }
    continue;
  }
  per[min].show();
  strcpy(per[min].lastname,"{");
  cout<<endl;
 }

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

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

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

   
  
  
  return 0;
}                           

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.