本帖最后由 jhq999 于 2022-12-9 20:43 编辑
楼主有没有样例?#include "stdio.h"
struct Date
{ int year;
int month;
int day;
};
struct Mate
{
char name[10];
struct Date birthday;//////////////
char tel[12];
};
#define N 10
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;
for(j=i+1;j<n;j+=1)///////////////////////
{
if(Datecomp(classmate[j].birthday,classmate[k].birthday)==1)k=j;
}
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)
{
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;
}
|