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 <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;
}                           

 

题目 “高校人员息管理系统” 1、问题描述 某高校,主要人员有:领导、教师和学生,共有息包括:编号、姓名、性别、年龄。其中领导还应包含的息有:任职部门(学院、各系部)、职称;教师还应包含的息有:职称、任职时间、专业;学生还应包含的息有:所在班级、所在宿舍等。 某高校有四类员工教师、实验员、行政人员教师行政人员共有息包括:编号、姓名、性别、年龄等。其中,教师还包含的息有:所在系部、专业、职称;实验员还包含的息由:所在实验室、职务;行政人员还包含的息有:政治面貌、职称等。 2、功能要求 (1)基本功能 1) 添加功能:程序能够任意添加上述四类人员的记录,可提供选择界面供用户选择所要添加的人员类别,要求员工的编号要唯一,如果添加了重复编号的记录时,则提示数据添加重复并取消添加。 2) 查询功能:可根据编号、姓名等息对已添加的记录进行查询,如果未找到,给出相应的提示息,如果找到,则显示相应的记录息。 3) 显示功能:可显示当前系统中所有记录,每条记录占据一行。 4) 编辑功能:可根据查询结果对相应的记录进行修改,修改时注意编号的唯一性。 5)删除功能:主要实现对已添加的人员记录进行删除。如果当前系统中没有相应的人员记录,则提示“记录为空!”并返回操作;否则,输入要删除的人员的编号或姓名,根据所输入的息删除该人员记录,如果没有找到该人员息,则提示相应的记录不存。 6) 统计功能:能根据多种参数进行人员的统计。例如,统计四类人员数量以及总数, 或者统计男、女员工的数量,或者统计某年龄段人员的数量等。 7)保存功能:可将当前系统中各类人员记录存入文件中,存入方式任意。 8)读取功能:可将保存在文件中的人员息读入到当前系统中,供用户进行使用。 (2)扩展功能 在完成以上基本功能的基础上,可自行进行扩展或完善,例如: 1) 添加人员记录时,考虑各种数据的有效性。 2)组合查询功能的实现。 3)可按人员的某些息进行排序。 4)对文件的读写使用二进制息。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值