|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
【问题描述】教材P233第9章课后习题题三(5):通讯录排序,试完成程序中空缺代码。
【输入形式】输入整数n及n个人的基本信息。
【输出形式】输出按照年龄由大到小的排序结果。
【样例输入】:图片地址:
【样例说明】出生日期要求按照给定日期格式输入
补充下列代码:
#include"stdio.h"
#define N 10
struct Date
{ int year;
int month;
int day;
};
struct Mate
{ char name[10];
此处补充: ________________________________________
char tel[12];
};
struct Mate classmate[N];
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[i].name);
scanf("%d-%d-%d",&classmate[i].birthday.year,&classmate[i].birthday.month,&classmate[i].birthday.day);
scanf("%s",classmate[i].tel);
}
for(i=0;i<n-1;i++) //按照年龄降序排列
{ k=i;
此处补充:______________________________________________
if(k!=i)
{ m=classmate[i];
classmate[i]=classmate[k];
classmate[k]=m;
}
}
for(i=0;i<n;i++)
printf("name:%10s,%d-%d-%d,%12s\n",classmate[i].name,classmate[i].birthday.year,classmate[i].birthday.month,classmate[i].birthday.day,classmate[i].tel);
return 0;
}
int Datecomp(struct Date d1,struct Date d2)
{
此处补充:________________________________________________
}
求助大佬!!!
我又忘了这个是C语言
- #include"stdio.h"
- #define N 10
- struct Date
- { int year;
- int month;
- int day;
- };
- struct Mate
- { char name[10];
- /*此处填充:*/ struct Date birthday;
- char tel[12];
- };
- struct Mate classmate[N];
- 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[i].name);
- scanf("%d-%d-%d",&classmate[i].birthday.year,&classmate[i].birthday.month,&classmate[i].birthday.day);
- scanf("%s",classmate[i].tel);
- }
- for(i=0;i<n-1;i++)
- { /*这里的排序写的有问题,我没见过这么排序的,照这么做应该用冒泡排序*/
- k=i;
- for (; k < n - 1; ++k) {
- if (Datecomp(classmate[k].birthday, classmate[k + 1].birthday)) {
- m = classmate[k];
- classmate[k] = classmate[k + 1];
- classmate[k + 1] = m;
-
- }
- }
-
- }
- for(i=0;i<n;i++)
- printf("name:%10s,%d-%d-%d,%12s\n",classmate[i].name,classmate[i].birthday.year,classmate[i].birthday.month,classmate[i].birthday.day,classmate[i].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;
-
- }
复制代码
|
|