教材P233第9章课后习题题三(5):通讯录排序,试完成程序中空缺代码
【问题描述】教材P233第9章课后习题题三(5):通讯录排序,试完成程序中空缺代码。【输入形式】输入整数n及n个人的基本信息。
【输出形式】输出按照年龄由大到小的排序结果。
【样例输入】:图片地址:http://10.81.10.7/userfiles/image/ch9_510.JPG
【样例说明】出生日期要求按照给定日期格式输入
补充下列代码:
#include"stdio.h"
#define N 10
struct Date
{ int year;
int month;
int day;
};
struct Mate
{ char name;
此处补充: ________________________________________
char tel;
};
struct Mate classmate;
int Datecomp(struct Date d1,struct Date d2);//d1年龄比d2大返回1,相等返回0,小则返回-1
int main()
{ int i,j,k,n;
struct Mate m;
scanf("%d",&n);
for(i=0;i<n;i++) //输入n个同学的信息
{ scanf("%s",classmate.name);
scanf("%d-%d-%d",&classmate.birthday.year,&classmate.birthday.month,&classmate.birthday.day);
scanf("%s",classmate.tel);
}
for(i=0;i<n-1;i++) //按照年龄降序排列
{ k=i;
此处补充:______________________________________________
if(k!=i)
{ m=classmate;
classmate=classmate;
classmate=m;
}
}
for(i=0;i<n;i++)
printf("name:%10s,%d-%d-%d,%12s\n",classmate.name,classmate.birthday.year,classmate.birthday.month,classmate.birthday.day,classmate.tel);
return 0;
}
int Datecomp(struct Date d1,struct Date d2)
{
此处补充:________________________________________________
}
求助大佬!!! 求助大佬!!! 用代码格式,我看得吃力的很{:10_291:} #include"stdio.h"
#define N 10
struct Date
{ int year;
int month;
int day;
};
struct Mate
{ char name;
/*此处填充:*/ Date birthday;
char tel;
};
struct Mate classmate;
int Datecomp(struct Date d1,struct Date d2);
int main()
{ int i,j,k,n;
struct Mate m;
scanf("%d",&n);
for(i=0;i<n;i++)
{ scanf("%s",classmate.name);
scanf("%d-%d-%d",&classmate.birthday.year,&classmate.birthday.month,&classmate.birthday.day);
scanf("%s",classmate.tel);
}
for(i=0;i<n-1;i++)
{ /*这里的排序写的有问题,我没见过这么排序的,照这么做应该用冒泡排序*/
k=i;
for (; k < n - 1; ++k) {
if (Datecomp(classmate.birthday, classmate.birthday)) {
m = classmate;
classmate = classmate;
classmate = m;
}
}
}
for(i=0;i<n;i++)
printf("name:%10s,%d-%d-%d,%12s\n",classmate.name,classmate.birthday.year,classmate.birthday.month,classmate.birthday.day,classmate.tel);
return 0;
}
int Datecomp(struct Date d1,struct Date d2)
{ /*此处填充:*/
if (d1.year != d2.year) return d1.year < d2.year;
if (d1.month != d2.month) return d1.month < d2.month;
return d1.day < d2.day;
}
zhangjinxuan 发表于 2022-12-9 18:06
大佬,貌似编译有点问题{:5_100:} 本帖最后由 jhq999 于 2022-12-9 20:43 编辑
楼主有没有样例?
#include "stdio.h"
struct Date
{ int year;
int month;
int day;
};
struct Mate
{
char name;
struct Date birthday;//////////////
char tel;
};
#define N 10
struct Mate classmate;
int Datecomp(struct Date d1,struct Date d2);//d1年龄比d2大返回1,相等返回0,小则返回-1
int main()
{ int i,j,k,n;
struct Mate m;
scanf("%d",&n);
for(i=0;i<n;i++) //输入n个同学的信息
{ scanf("%s",classmate.name);
scanf("%d-%d-%d",&classmate.birthday.year,&classmate.birthday.month,&classmate.birthday.day);
scanf("%s",classmate.tel);
}
for(i=0;i<n-1;i++) //按照年龄降序排列
{ k=i;
for(j=i+1;j<n;j+=1)///////////////////////
{
if(Datecomp(classmate.birthday,classmate.birthday)==1)k=j;
}
if(k!=i)
{m=classmate;
classmate=classmate;
classmate=m;
}
}
for(i=0;i<n;i++)
printf("name:%10s,%d-%d-%d,%12s\n",classmate.name,classmate.birthday.year,classmate.birthday.month,classmate.birthday.day,classmate.tel);
return 0;
}
int Datecomp(struct Date d1,struct Date d2)
{
if(d1.year>d2.year)return 1;/////////
else if(d1.year<d2.year)return -1;
else
{
if(d1.month>d2.month)return 1;
else if(d1.month<d2.month)return -1;
else
{
if(d1.day>d2.day)return 1;
else if(d1.day<d2.day)return -1;
else
return 0;
}
}
}
int Datecomp(struct Date d1,struct Date d2)
{
if(d1.year==d2.year)//同龄的比较多,所以先==
{
if(d1.month>d2.month)return 1;
else if(d1.month<d2.month)return -1;
else
{
if(d1.day>d2.day)return 1;
else if(d1.day<d2.day)return -1;
else
return 0;
}
}
else if(d1.year>d2.year)return 1;
else return -1;
} zzs2963584895 发表于 2022-12-9 20:11
大佬,貌似编译有点问题
我又忘了这个是C语言{:10_285:}
#include"stdio.h"
#define N 10
struct Date
{ int year;
int month;
int day;
};
struct Mate
{ char name;
/*此处填充:*/ struct Date birthday;
char tel;
};
struct Mate classmate;
int Datecomp(struct Date d1,struct Date d2);
int main()
{ int i,j,k,n;
struct Mate m;
scanf("%d",&n);
for(i=0;i<n;i++)
{ scanf("%s",classmate.name);
scanf("%d-%d-%d",&classmate.birthday.year,&classmate.birthday.month,&classmate.birthday.day);
scanf("%s",classmate.tel);
}
for(i=0;i<n-1;i++)
{ /*这里的排序写的有问题,我没见过这么排序的,照这么做应该用冒泡排序*/
k=i;
for (; k < n - 1; ++k) {
if (Datecomp(classmate.birthday, classmate.birthday)) {
m = classmate;
classmate = classmate;
classmate = m;
}
}
}
for(i=0;i<n;i++)
printf("name:%10s,%d-%d-%d,%12s\n",classmate.name,classmate.birthday.year,classmate.birthday.month,classmate.birthday.day,classmate.tel);
return 0;
}
int Datecomp(struct Date d1,struct Date d2)
{ /*此处填充:*/
if (d1.year != d2.year) return d1.year < d2.year;
if (d1.month != d2.month) return d1.month < d2.month;
return d1.day < d2.day;
}
可恶的C++,我终于明白为什么C和C++是两种语言了{:10_250:} 现在问题解决了吗{:10_254:} zhangjinxuan 发表于 2022-12-9 20:44
现在问题解决了吗
解决了 zhangjinxuan 发表于 2022-12-9 20:39
我又忘了这个是C语言
能不能帮我看看最新的一个问题 zzs2963584895 发表于 2022-12-9 20:51
能不能帮我看看最新的一个问题
好的
页:
[1]