C language structure to achieve sorted by date

Title Description

There are some date, the date format is "MM / DD / YYYY". Programming to size sorted by date.

 

Entry

Enter each date as MM / DD / YYYY


 

Export

Results output arrangement


 

Sample input

12/31/2005
10/21/2003
02/12/2004
10/12/1999
10/22/2003
11/30/2005

 

Sample Output

10/12/1999
10/21/2003
10/22/2003
02/12/2004
11/30/2005
12/31/2005

#include <stdio.h>
#include <string.h>
struct data_st
{
int year ;
int month ;
int data ;
}DATA[100];
void bubble_sort(struct data_st * p,int num) ;
int main()
{
int i=0,num=0;
memset(DATA,0,sizeof(DATA));
while(scanf("%d/%d/%d",&DATA[i].month,&DATA[i].data,&DATA[i].year)!=EOF)
{
i++ ;
}
num=i ;
bubble_sort(DATA,num);
for(i=0;i<num;i++)
{
printf("%02d/%02d/%04d\n",DATA[i].month,DATA[i].data,DATA[i].year);
}
}
void bubble_sort(struct data_st * p,int num)
{
int i,j ;
struct data_st temp;
for(i=0;i<num-1;i++)
{
for(j=0;j<num-1-i;j++)
{
if(p[j].year>p[j+1].year)
{
temp =p[j];
p[j] =p[j+1];
p[j+1] =temp;
}
else if(p[j].year == p[j+1].year)
{
if(p[j].month >p[j+1].month)
{
temp =p[j];
p[j]=p[j+1];
p[j+1]=temp;
}
else if(p[j].month == p[j+1].month)
{
if(p[j].data > p[j+1].data)
{
temp =p[j];
p[j]=p[j+1];
p[j+1]=temp;
}
}
}
}
}
}



Guess you like

Origin www.cnblogs.com/cocobear9/p/12577762.html