Current location - Plastic Surgery and Aesthetics Network - Plastic surgery and medical aesthetics - C language date (year, month, day) sort
C language date (year, month, day) sort
# contains "stdio.h"

# contains "string.h"

//Date structure

Structure date

{

int y; //year

char m[4]; //month

int d; //day

};

//Comparison year

int cmpY(int a,int b)

{

//The year is between 90- 10, representing 1990 to 20 10.

If (a< 100. & ampa & gt 10)

a+= 1900;

other

a+= 2000;

if(b & lt; 100。 & ampb & gt 10)

b+ = 1900;

other

b+ = 2000;

If (a>b)

Returns1;

else if (a == b)

Returns 0;

other

return- 1;

}

//Comparison month

int cmpM(char *a,char *b)

{

//Define a string array, which means 12 months in English.

Char m[ 12][4] = {"January", "February", "March", "April", "May", "June", "July", "August", "October" and "November".

int i,j;

for(I = 0; I< 12; i++)

if (strcmp(a,m[i]) == 0)

Break;

for(j = 0; j & lt 12; j++)

if (strcmp(a,m[j]) == 0)

Break;

If (I<j)

Returns1;

else if (i == j)

Returns 0;

other

return- 1;

}

//year, month and day comprehensive comparison

Int cmpYMD (structure date a, structure date b)

{

if (cmpY(a.y,b.y) == - 1)

return- 1;

Otherwise if (cmpY(a.y, b.y) == 1)

Returns1;

other

{

if (cmpM(a.m,b.m) == - 1)

return- 1;

else if (cmpM(a.m,b.m) == 1)

Returns1;

other

{

If (A.D. & gtb.d)

Returns1;

else if(a.d == b.d)

Returns 0;

other

return- 1;

}

}

}

void main()

{

//Define the structure array

Structure date dt[3]={

{0, "January", 1 1},

{February 22nd, 1999},

{ 8,“Mar”,2 } };

Structure date dtTemp

int i,j;

//sort

for(I = 0; I & lt3- 1; i++)

for(j = I+ 1; j & lt3; j++)

if (cmpYMD(dt[i],dt[j]) == 1)

{

dt temp = dt[I];

dt[I]= dt[j];

dt[j]= dt temp;

}

//Output the sorted date.

for(I = 0; I<3; i++)

{

printf("%d %s %d\n ",dt[i]。 y & amp; dt[i]。 m,dt[i]。 d);

}

}