struct
tagTime{
int year;
int month;
int day;
};
void Set( int y, int m, int d, struct tagTime * t) /* 设置日期 */
{
t -> year = y;
t -> month = m;
t -> day = d;
}
int IsLeapYear( int year) /* 是否闰年 */
{
return ((year % 4 == 0 ) && (year % 100 != 0 ) || year % 400 == 0 );
}
int GetLastDay( struct tagTime date) /* 得到date.month的最大天数 */
{
int num;
switch (date.month)
{
case 1 :
case 3 :
case 5 :
case 7 :
case 8 :
case 10 :
case 12 :
num = 31 ;
break ;
case 2 :
num = 28 + IsLeapYear(date.year);
break ;
default :
num = 30 ;
}
return num;
}
int IsDateValid( struct tagTime date) /* 日期是否合法 */
{
if (date.year < 0 || date.month < 1 || date.month > 12 )
return 0 ;
if (date.day < 1 || date.day > GetLastDay(date)) return 0 ;
return 1 ;
}
void AddDay( struct tagTime * date) /* date+1 */
{
date -> day ++ ;
if (date -> day > GetLastDay( * date))
{
date -> day = 1 ;date -> month ++ ;
if (date -> month > 12 ){
date -> month = 1 ;
date -> year ++ ;
}
}
}
int Compare( struct tagTime t1, struct tagTime t2) /* t1比t2小返回值为1,否则为0 */
{
if (t1.year < t2.year) return 1 ;
if (t1.year <= t2.year && t1.month < t2.month) return 1 ;
if (t1.year <= t2.year && t1.month <= t2.month && t1.day < t2.day) return 1 ;
return 0 ;
}
long CountDate( struct tagTime t1, struct tagTime t2)
{
long delta = 0 ; struct tagTime t3;
if ( ! Compare(t1,t2)) /* 若t1> t2,交换t1,t2 */
{
t3 = t2;t2 = t1;t1 = t3;
}
while (Compare(t1,t2)) /* 本方法是笨方法,但很直观,t1比t2少时,t1日期加1 */
{
AddDay( & t1);
delta ++ ;
}
return delta;
}
void main()
{
struct tagTime t1,t2,t3;
int y,m,d;clrscr();
while ( 1 )
{
puts( " \nInput t1\t " );
scanf( " %d,%d,%d " , & y, & m, & d);
Set(y,m,d, & t1);
if ( ! IsDateValid(t1))
{
puts( " T1 is invalid " );
exit( 1 );
}
puts( " \nInput t2\t " );
scanf( " %d,%d,%d " , & y, & m, & d);
Set(y,m,d, & t2);
if ( ! IsDateValid(t2))
{
puts( " T2 is invalid " );
exit( 1 );
}
printf( " \n[%ld] " ,CountDate(t1,t2));
}
}
int year;
int month;
int day;
};
void Set( int y, int m, int d, struct tagTime * t) /* 设置日期 */
{
t -> year = y;
t -> month = m;
t -> day = d;
}
int IsLeapYear( int year) /* 是否闰年 */
{
return ((year % 4 == 0 ) && (year % 100 != 0 ) || year % 400 == 0 );
}
int GetLastDay( struct tagTime date) /* 得到date.month的最大天数 */
{
int num;
switch (date.month)
{
case 1 :
case 3 :
case 5 :
case 7 :
case 8 :
case 10 :
case 12 :
num = 31 ;
break ;
case 2 :
num = 28 + IsLeapYear(date.year);
break ;
default :
num = 30 ;
}
return num;
}
int IsDateValid( struct tagTime date) /* 日期是否合法 */
{
if (date.year < 0 || date.month < 1 || date.month > 12 )
return 0 ;
if (date.day < 1 || date.day > GetLastDay(date)) return 0 ;
return 1 ;
}
void AddDay( struct tagTime * date) /* date+1 */
{
date -> day ++ ;
if (date -> day > GetLastDay( * date))
{
date -> day = 1 ;date -> month ++ ;
if (date -> month > 12 ){
date -> month = 1 ;
date -> year ++ ;
}
}
}
int Compare( struct tagTime t1, struct tagTime t2) /* t1比t2小返回值为1,否则为0 */
{
if (t1.year < t2.year) return 1 ;
if (t1.year <= t2.year && t1.month < t2.month) return 1 ;
if (t1.year <= t2.year && t1.month <= t2.month && t1.day < t2.day) return 1 ;
return 0 ;
}
long CountDate( struct tagTime t1, struct tagTime t2)
{
long delta = 0 ; struct tagTime t3;
if ( ! Compare(t1,t2)) /* 若t1> t2,交换t1,t2 */
{
t3 = t2;t2 = t1;t1 = t3;
}
while (Compare(t1,t2)) /* 本方法是笨方法,但很直观,t1比t2少时,t1日期加1 */
{
AddDay( & t1);
delta ++ ;
}
return delta;
}
void main()
{
struct tagTime t1,t2,t3;
int y,m,d;clrscr();
while ( 1 )
{
puts( " \nInput t1\t " );
scanf( " %d,%d,%d " , & y, & m, & d);
Set(y,m,d, & t1);
if ( ! IsDateValid(t1))
{
puts( " T1 is invalid " );
exit( 1 );
}
puts( " \nInput t2\t " );
scanf( " %d,%d,%d " , & y, & m, & d);
Set(y,m,d, & t2);
if ( ! IsDateValid(t2))
{
puts( " T2 is invalid " );
exit( 1 );
}
printf( " \n[%ld] " ,CountDate(t1,t2));
}
}