#include <stdio.h>
#include <stdlib.h>
int isLeap(int);
int CheckForm(int, int, int, int);
int countInterval(int, int, int, int, int, int);
int main()
{
int is_quit=0;
while(is_quit != 1)
{
int yy_1, mm_1, dd_1,
yy_2, mm_2, dd_2;
int is_leap=0; // is_leap=0: not Leap Year is_leap=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
is_leap = isLeap(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(is_leap, 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
is_leap = isLeap(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(is_leap, 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;
}
int days;
printf("---------------------------------\n");
days = countInterval(s_yy, s_mm, s_dd, e_yy, e_mm, e_dd);
printf("through: %d days\n\n", days);
printf("enter [0] to countinue or enter [1] to quit: ");
scanf("%d", &is_quit);
}
system("pause");
return 0;
}
//////////////////////////////////////
// Check Leap Year
//////////////////////////////////////
int isLeap(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 is_leap, 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 && is_leap==1)
if (dd<1 || dd>29)
{
result_1 = 0; // wrong formation
break; // leave while loop
}
if (mm==2 && is_leap==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;
}
/////////////////////////////////////////////
// count the interval
// input: Date1(yy, mm, dd), Date2(YY, MM, DD)
// output: interval by days
/////////////////////////////////////////////
int countInterval(int s_yy, int s_mm, int s_dd, int e_yy, int e_mm, int e_dd)
{
// month 1 2 3 4 5 6 7 8 9 10 11 12
int Leap[]={31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
nLeap[]={31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
// index [0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11]
///////////////////////////////////////
// 1. in the same year
// 2. two succesive years
// 3. through >= 1year
//////////////////////////////////////
int interval, s_isleap, e_isleap, t_isleap;
int i, part_1, part_2, part_3, temp;
////////////////////////
// 1. in the same year
if (s_yy == e_yy)
{
s_isleap = isLeap(s_yy);
interval = 0;
temp = 0;
// in Leap Year
if (s_isleap == 1)
{
if (s_mm==e_mm && s_dd==e_dd)
interval = 0;
else if (s_mm == e_mm)
interval = e_dd - s_dd;
else
{
for(i=s_mm ; i < e_mm-1 ; i++)
{
temp += Leap[i];
}
interval = Leap[s_mm-1] - s_dd + temp + e_dd;
}
}
// not in Leap year
else
{
if (s_mm==e_mm && s_dd==e_dd)
interval = 0;
else if (s_mm == e_mm)
interval = e_dd - s_dd;
else
{
for(i=s_mm ; i < e_mm-1 ; i++)
{
temp += nLeap[i];
}
interval = nLeap[s_mm-1] - s_dd + temp + e_dd;
}
}
}
///////////////////////////////
// 2. two succesive years
else if (e_yy - s_yy == 1)
{
s_isleap = isLeap(s_yy);
e_isleap = isLeap(e_yy);
interval = 0;
// part_1: from (yy/mm/dd) to (yy/12/31)
temp = 0;
part_1 = 0;
if (s_isleap ==1)
{
for(i=s_mm ; i <=12 ; i++)
temp += Leap[i];
part_1 = Leap[s_mm-1] - s_dd + temp;
}
else
{
for(i=s_mm ; i < 12 ; i++)
temp += nLeap[i];
part_1 = nLeap[s_mm-1] - s_dd + temp;
}
// part_2: from (yy/12/31) to (yy+1/MM/DD)
temp = 0;
part_2 = 0;
if (e_isleap ==1)
{
for(i=0 ; i < e_mm-1 ; i++)
temp += Leap[i]; // temp contain (12/31 to 1/1)
part_2 = temp + e_dd;
}
else
{
for(i=0 ; i < e_mm-1 ; i++)
temp += nLeap[i]; // temp contain (12/31 to 1/1)
part_2 = temp + e_dd;
}
// interval
interval = part_1 + part_2;
}
///////////////////////////////
// 3. through >= 1year
else
{
s_isleap = isLeap(s_yy);
e_isleap = isLeap(e_yy);
interval = 0;
// part_1: from (yy/mm/dd) to (yy/12/31)
temp = 0;
part_1 = 0;
if (s_isleap ==1)
{
for(i=s_mm ; i <=12 ; i++)
temp += Leap[i];
part_1 = Leap[s_mm-1] - s_dd + temp;
}
else
{
for(i=s_mm ; i < 12 ; i++)
temp += nLeap[i];
part_1 = nLeap[s_mm-1] - s_dd + temp;
}
// part_2: from (yy+1/1/1) to (yy+1/MM/DD)
temp = 0;
part_2 = 0;
if (e_isleap ==1)
{
for(i=0 ; i < e_mm-1 ; i++)
temp += Leap[i];
part_2 = temp + e_dd;
}
else
{
for(i=0 ; i < e_mm-1 ; i++)
temp += nLeap[i];
part_2 = temp + e_dd;
}
// part_3: from (s_yy+1) to (e_YY-1)
temp = 0;
part_3 = 0;
for (i=s_yy+1 ; i < e_yy ; i++)
{
t_isleap = isLeap(i);
if (t_isleap == 1)
temp += 366;
else
temp += 365;
}
part_3 = temp;
interval = part_1 + part_2 + part_3;
}
return interval;
}