请打开考生文件夹下的解决方案文件proj2,该工程中包含一个程序文件main.cpp,其中有日期类Date、人员类Person及排序函数sortByName和主函数main的定义。请在程序中的横线处填写适当的代码并删除横线,以实现上述类定义和函数定义。此程

admin2018-07-06  18

问题 请打开考生文件夹下的解决方案文件proj2,该工程中包含一个程序文件main.cpp,其中有日期类Date、人员类Person及排序函数sortByName和主函数main的定义。请在程序中的横线处填写适当的代码并删除横线,以实现上述类定义和函数定义。此程序的正确输出结果应为:
按姓名排序
排序前
张三  男  出生日期:1978年4月20日
王五  女  出生日期:1965年8月3日
杨六  女  出生日期:1965年9月5日
李四  男  出生日期:1973年5月30日
排序后:
李四  男  出生日期:1973年5月30日
王五  女  出生日期:1965年8月3日
杨六  女  出生日期:1965年9月5日
张三  男  出生日期:1978年4月20日
注意:只能在横线处填写适当的代码,不要改动程序中的其他内容,也不要删除或移动“//****found****”。
#include<iostream>
using namespace std;
class Date{//日期类
int year,month,day;//年、月、日
public:
Date(int year,int month,int day):year(year),month(month),day(day){}
int getYear( )const{return year;}
int getMonth( )const{return month;}
int getDay( )const{return day;}
};
class Person{//人员类
char name[14];//姓名
bool is_male;//性别,为true时表示男性
Date birth_date;//出生日期public:
Person(char*name,bool is_male,Datebirth_date)
//*******found*******
:_______
{
strcpy(this->name,name);
}
const char*getName( )const{return name;}
bool isMale( )const{returnis male;}
Date getBirthdate( )const{return birth date;}
//利用strcmp( )函数比较姓名,返回一个正数、0或负数,分别表示大于、等于、小于
int compareName(const Person&p)const{
//*******found*******
_______)
void show( ){
cout<<endl;
cout<<name<<’ ’<<fiSmale?"男":"女")<<’ ’<<"出生日期:"<<birth date.getYear( )<<"年"//显示出生年
//*******found*******
[ ]//显示出生月
<<birth date.getDay( )<<"日";//显示出生日
}
};
void sortByName(Person ps[ ],int Size){
//将人员数组按姓名排列为升序
for(int i=0;i<size-1;i++){
//采用选择排序算法
int m=i;
for(int j=i+1;j<size;j++)
if(ps[j].compareName(ps
[m])<0)
m=j;
if(m>i){
Person p=ps[m];
ps[m]=ps
Lps[m]=ps
ps=p;
}
}
}

int main( ){
Person staff[ ]=f
Person("张三",true,Date(1978,4,20)),
Person("王五",false,Date(1965,8,3)),
Person("杨六",false,Date(1965,9,5)),
Person("李四",true,Date(1973,5,30))
};
const int size=sizeof(staff)/sizeof(staff[0]);
int i;
cout<<endl<<"按姓名排序";
cout<<endl<<"排序前:";
for(i=0;i<Size;i++)staff.show( );
SortByName(Staff,size);
cout<<endl<<endl<<"排序后:";
for(i=0;i<size;i++)
staff.show( );
cout<<endl;
return0;
}

选项

答案(1)is_male(is_nla]e),birth_date(birth_date) (2)return strcmp(name,p.getNanle( )); (3)<<birth_date.getMonth( )<<"月’

解析 (1)主要考查考生对构造函数的掌握,由函数体内strcpy(this->name,name);可知,要使用成员列表初始化的成员为is_male和birth_date。
(2)主要考查考牛对strcmp( )函数的掌握,先看程序对该函数的功能要求:利用strcmp( )函数比较姓名,返回一个正数、0或负数,分别表示大于、等于、小于。因为strcmp( )函数的功能足比较字符串大小,因此可以直接被return语句调用:return strcmp(name,p.getName( ));。
(3)主要考查考生对成员函数的掌握,程序的注释为:显示出生月,由此可以知道这里要输出出生月份,直接调用函数getMonth( )即可。
转载请注明原文地址:https://jikaoti.com/ti/Btt0FFFM
0

最新回复(0)