日期排序

在这里插入代码片
Description
有一些日期,日期格式为“MM/DD/YYYY”。编程将其按日期大小排列。 
Input
Output
Sample Input
12/12/1999
10/21/2003
10/22/2003
02/12/2004
11/30/2005
12/31/2005
Sample Output
12/12/1999
10/21/2003
10/22/2003
02/12/2004
11/30/2005
12/31/2005

#include<stdio.h>
#include<stdlib.h>
struct riqi{
 int month,day,year;
}data[1000];
int comp(const void *p1,const void *p2)
{
 struct riqi *c = (struct riqi *)p1;
 struct riqi *d = (struct riqi *)p2;
 if(c->year != d->year)
 {
  return d->year < c->year?1:-1;
 }
 else if(c->month != d->month&&c->year == d->year)
 {
  return d->month < c->month?1:-1;
 }
 else
 {
  return d->day < c->day?1:-1;
 }
}
int main(void)
{
 int i = 0;
 while(scanf("%d/%d/%d",&data[i].month,&data[i].day,&data[i].year) != EOF)
 {
  i++;
 }
 qsort(data,i,sizeof(data[0]),comp);
 for(int n = 0;n < i;n++)
 {
  printf("%02d/%02d/%02d\n",data[n].month,data[n].day,data[n].year);
 }
}

发布了146 篇原创文章 · 获赞 44 · 访问量 4127

猜你喜欢

转载自blog.csdn.net/weixin_45949073/article/details/104510598