#include <stdio.h>
#include <stdlib.h>
int Leap(int);
int CheckForm(int, int, int, int);
int main()
{
int yy_1, mm_1, dd_1,
yy_2, mm_2, dd_2;
int isLeap=0; // isLeap=0: not Leap Year isLeap=1: Leap Year
int form_1;
int s_yy, s_mm, s_dd, // star of the time interval
e_yy, e_mm, e_dd; // end of the time interval
/* input 1st date and check the formation
form_1=0 is worng formation and enter loop again
form_1=1 is right formation to leave loop
*/
/////////////////////////////
// check formation of date1
/////////////////////////////
form_1 = 0;
while (form_1 != 1)
{
system("CLS"); // clean the screen
printf("enter the 1st year(0001-9999):\t");
scanf("%d", &yy_1);
// is the year a leap year? 1 is Leap, 0 not Leap
isLeap = Leap(yy_1);
printf("enter the 1st month(01-12):\t");
scanf("%d", &mm_1);
printf("enter the 1st day(01-31):\t");
scanf("%d", &dd_1);
// check the formation: 1 to leave, 0 loop again
form_1 = CheckForm(isLeap, yy_1, mm_1, dd_1);
if (form_1 == 1)
printf("Date 1: %d / %d / %d\n", yy_1, mm_1, dd_1);
}
/////////////////////////////
// check formation of date2
/////////////////////////////
form_1 = 0;
while (form_1 != 1)
{
system("CLS");
printf("enter the 2nd year(0001-9999):\t");
scanf("%d", &yy_2);
// is the year a leap year? 1 is Leap, 0 not Leap
isLeap = Leap(yy_2);
printf("enter the 2nd month(01-12):\t");
scanf("%d", &mm_2);
printf("enter the 2nd day(01-31):\t");
scanf("%d", &dd_2);
// check the formation: 1 to leave, 0 loop again
form_1 = CheckForm(isLeap, yy_2, mm_2, dd_2);
if (form_1 == 1)
printf("Date 1: %d / %d / %d\n", yy_2, mm_2, dd_2);
}
////////////////////////////////
// Arrange the Date Sequence
////////////////////////////////
system("CLS");
if (yy_1 < yy_2)
{
printf("From: %d / %d / %d\n", yy_1, mm_1, dd_1);
printf(" To : %d / %d / %d\n", yy_2, mm_2, dd_2);
// star
s_yy = yy_1;
s_mm = mm_1;
s_dd = dd_1;
// end
e_yy = yy_2;
e_mm = mm_2;
e_dd = dd_2;
}
else if (yy_1 > yy_2)
{
printf("From: %d / %d / %d\n", yy_2, mm_2, dd_2);
printf(" To : %d / %d / %d\n", yy_1, mm_1, dd_1);
// star
s_yy = yy_2;
s_mm = mm_2;
s_dd = dd_2;
// end
e_yy = yy_1;
e_mm = mm_1;
e_dd = dd_1;
}
// yy_1 == yy_2
else if (mm_1 < mm_2)
{
printf("From: %d / %d / %d\n", yy_1, mm_1, dd_1);
printf(" To : %d / %d / %d\n", yy_2, mm_2, dd_2);
// star
s_yy = yy_1;
s_mm = mm_1;
s_dd = dd_1;
// end
e_yy = yy_2;
e_mm = mm_2;
e_dd = dd_2;
}
else if (mm_1 > mm_2)
{
printf("From: %d / %d / %d\n", yy_2, mm_2, dd_2);
printf(" To : %d / %d / %d\n", yy_1, mm_1, dd_1);
// star
s_yy = yy_2;
s_mm = mm_2;
s_dd = dd_2;
// end
e_yy = yy_1;
e_mm = mm_1;
e_dd = dd_1;
}
// (yy_1 == yy_2) && (mm_1 == mm_2)
else if (dd_1 < dd_2)
{
printf("From: %d / %d / %d\n", yy_1, mm_1, dd_1);
printf(" To : %d / %d / %d\n", yy_2, mm_2, dd_2);
// star
s_yy = yy_1;
s_mm = mm_1;
s_dd = dd_1;
// end
e_yy = yy_2;
e_mm = mm_2;
e_dd = dd_2;
}
else
{
printf("From: %d / %d / %d\n", yy_2, mm_2, dd_2);
printf(" To : %d / %d / %d\n", yy_1, mm_1, dd_1);
// star
s_yy = yy_2;
s_mm = mm_2;
s_dd = dd_2;
// end
e_yy = yy_1;
e_mm = mm_1;
e_dd = dd_1;
}
system("pause");
return 0;
}
//////////////////////////////////////
// Check Leap Year
//////////////////////////////////////
int Leap(int yy)
{
int result_0;
if (yy%4 != 0)
result_0 = 0; // not Leap Year
else if (yy%400 == 0)
result_0 = 0;
else
result_0 = 1; // Leap Year
return result_0; // 1 is Leap, 0 not Leap
}
///////////////////////////////////////
// Check Date Formation
///////////////////////////////////////
int CheckForm(int isLeap, int yy, int mm, int dd)
{
int count=0, result_1=1;
while (count != 1)
{
// check formation of year
if (yy<1 || yy>9999)
{
result_1 = 0; // wrong formation
break;
}
// check formation of month
if (mm<1 || mm>12)
{
result_1 = 0; // wrong formation
break;
}
// check formation of day
if (dd<1 || dd>31)
{
result_1 = 0; // wrong formation
break;
}
// check the relation of month and day
if (mm==1 || mm==3 || mm==5 || mm==7 || mm==8 || mm==10 || mm==12)
if (dd<1 || dd>31)
{
result_1 = 0; // wrong formation
break; // leave while loop
}
else if (mm==4 || mm==6 || mm==9 || mm==11)
if (dd<1 || dd>30)
{
result_1 = 0; // wrong formation
break; // leave while loop
}
else
if (mm==2 && isLeap==1)
if (dd<1 || dd>29)
{
result_1 = 0; // wrong formation
break; // leave while loop
}
if (mm==2 && isLeap==0)
if (dd<1 || dd>28)
{
result_1 = 0; // wrong formation
break; // leave while loop
}
// pass all the check list with right form
result_1 = 1;
count = 1;
}
return result_1;
}