
求个 Visual c++高手 帮我看看下面一个题目 Please!!
填空题填写横线的空白使程序输出如下要求结果(5)要求输出结果按姓名排序排序前:张三男出生日期:1978年4月20日王五女出生日期:1965年8月3日杨六女出生日期:196...
填空题 填写横线的空白 使程序输出如下要求结果
(5)要求输出结果
按姓名排序
排序前:
张三 男 出生日期: 1978年4月20日
王五 女 出生日期: 1965年8月3日
杨六 女 出生日期: 1965年9月5日
李四 男 出生日期: 1973年5月30日
排序后:
李四 男 出生日期: 1973年5月30日
王五 女 出生日期: 1965年8月3日
杨六 女 出生日期: 1965年9月5日
张三 男 出生日期: 1978年4月20日
#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, Date birth_date)
//**********found**********
:_____________________________________________________
{
strcpy(this->name, name);
}
const char *getName()const{ return name; }
bool isMale()const{ return is_male; }
Date getBirthdate()const{ return birth_date; }
//利用strcmp()函数比较姓名,返回一个正数、0 或负数,分别表示大于、等于、小于
int compareName(const Person &p)const{
//**********found**********
_______________________________________
}
void show(){
cout<<endl;
cout<<name<<' '<<(is_male? "男" : "女")<<' '<<"出生日期:"
<<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[i];
ps[i]=p;
}
}
}
int main(){
Person staff[]={
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[i].show();
sortByName(staff,size);
cout<<endl<<endl<<"排序后:";
for(i=0; i<size; i++) staff[i].show();
cout<<endl;
return 0;
} 展开
(5)要求输出结果
按姓名排序
排序前:
张三 男 出生日期: 1978年4月20日
王五 女 出生日期: 1965年8月3日
杨六 女 出生日期: 1965年9月5日
李四 男 出生日期: 1973年5月30日
排序后:
李四 男 出生日期: 1973年5月30日
王五 女 出生日期: 1965年8月3日
杨六 女 出生日期: 1965年9月5日
张三 男 出生日期: 1978年4月20日
#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, Date birth_date)
//**********found**********
:_____________________________________________________
{
strcpy(this->name, name);
}
const char *getName()const{ return name; }
bool isMale()const{ return is_male; }
Date getBirthdate()const{ return birth_date; }
//利用strcmp()函数比较姓名,返回一个正数、0 或负数,分别表示大于、等于、小于
int compareName(const Person &p)const{
//**********found**********
_______________________________________
}
void show(){
cout<<endl;
cout<<name<<' '<<(is_male? "男" : "女")<<' '<<"出生日期:"
<<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[i];
ps[i]=p;
}
}
}
int main(){
Person staff[]={
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[i].show();
sortByName(staff,size);
cout<<endl<<endl<<"排序后:";
for(i=0; i<size; i++) staff[i].show();
cout<<endl;
return 0;
} 展开
2个回答
展开全部
////结果为
#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, Date birth_date)
//**********found**********
: is_male(is_male),birth_date(birth_date) //////
{
strcpy(this->name, name);
}
const char *getName()const{ return name; }
bool isMale()const{ return is_male; }
Date getBirthdate()const{ return birth_date; }
//利用strcmp()函数比较姓名,返回一个正数、0 或负数,分别表示大于、等于、小于
int compareName(const Person &p)const{
//**********found**********
return strcmp(name,p.name);///////
}
void show(){
cout<<endl;
cout<<name<<' '<<(is_male? "男" : "女")<<' '<<"出生日期:"
<<birth_date.getYear()<<"年" //显示出生年
//**********found**********
<<birth_date.getMonth()<<"月"//显示出生月 /////
<<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[i];
ps[i]=p;
}
}
}
int main(){
Person staff[]={
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[i].show();
sortByName(staff,size);
cout<<endl<<endl<<"排序后:";
for(i=0; i<size; i++) staff[i].show();
cout<<endl;
return 0;
}
#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, Date birth_date)
//**********found**********
: is_male(is_male),birth_date(birth_date) //////
{
strcpy(this->name, name);
}
const char *getName()const{ return name; }
bool isMale()const{ return is_male; }
Date getBirthdate()const{ return birth_date; }
//利用strcmp()函数比较姓名,返回一个正数、0 或负数,分别表示大于、等于、小于
int compareName(const Person &p)const{
//**********found**********
return strcmp(name,p.name);///////
}
void show(){
cout<<endl;
cout<<name<<' '<<(is_male? "男" : "女")<<' '<<"出生日期:"
<<birth_date.getYear()<<"年" //显示出生年
//**********found**********
<<birth_date.getMonth()<<"月"//显示出生月 /////
<<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[i];
ps[i]=p;
}
}
}
int main(){
Person staff[]={
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[i].show();
sortByName(staff,size);
cout<<endl<<endl<<"排序后:";
for(i=0; i<size; i++) staff[i].show();
cout<<endl;
return 0;
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询