Advertisement
Go-Ice

ZeroJudge [a263: 日期差幾天] by Go-Ice

Apr 18th, 2015
515
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.47 KB | None | 0 0
  1. /*
  2.     Name: ZeroJudge a263: 日期差幾天
  3.     Author: LinChuWen
  4.     Date: 17/04/15
  5.     Description: a program that counts days between two dates
  6. */
  7.  
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10.  
  11. int monthLength();
  12. int testLeapYear();
  13. void arrangeOrder();
  14. void switchDate();
  15.  
  16. int main(){
  17.     int y1, m1, d1;
  18.     int y2, m2, d2;
  19.     int dayAmount;
  20.     int cnt;
  21.  
  22.     while ( scanf("%d %d %d %d %d %d", &y1, &m1, &d1, &y2, &m2, &d2)!=EOF ){
  23.         /* reset variable */
  24.         dayAmount = 0;
  25.  
  26.         arrangeOrder( &y1, &m1, &d1, &y2, &m2, &d2 );
  27.  
  28.         /* count days between Date1 and Date2 */
  29.         if ( y1 > y2 ){
  30.             /* count days between y1 and y2 */
  31.             for ( cnt=y2+1 ; cnt<y1 ; cnt++ )
  32.                 dayAmount += 365;
  33.  
  34.             /* count leap years between y1 and y2 */
  35.             for ( cnt=y2-y2%4+4 ; cnt<y1 ; cnt+=4 )
  36.                 if ( testLeapYear(cnt) == 1 )
  37.                     dayAmount++;
  38.  
  39.             /*  count days from the beginning of the year to Date1 */
  40.             dayAmount += d1;
  41.             for ( cnt=1 ; cnt<m1 ; cnt++ )
  42.                 dayAmount += monthLength( y1, cnt );
  43.  
  44.             /* count days from Date2 to the end of the year */
  45.             dayAmount += monthLength( y2, m2 ) - d2;
  46.             for ( cnt=m2+1 ; cnt<=12 ; cnt++ )
  47.                 dayAmount += monthLength( y2, cnt );
  48.  
  49.         } /* END if (y1>y2) */
  50.  
  51.         else if ( y1 == y2 ){
  52.             if ( m1 > m2 ){
  53.                 /* count days between m1 and m2 */
  54.                 for ( cnt=m2+1 ; cnt<m1 ; cnt++ )
  55.                     dayAmount += monthLength( y1, cnt );
  56.                 dayAmount += monthLength( y2, m2 ) - d2;
  57.                 dayAmount += d1;
  58.             } /* END if (m1>m2) */
  59.  
  60.             else if ( m1 == m2 ){
  61.                 if ( d1 > d2 )
  62.                     dayAmount = d1 - d2;
  63.  
  64.                 else if ( d1 == d2 )
  65.                     dayAmount = 0;
  66.             } /* END else if (m1=m2) */
  67.         } /* END else if (y1=y2) */
  68.  
  69.         /* print out the answer, dayAmount */
  70.         printf("%d\n", dayAmount);
  71.  
  72.     } /* END outer while */
  73.  
  74.     system("PAUSE");
  75.     return 0;
  76. } /* END main()*/
  77.  
  78. int testLeapYear( int year ){
  79.     /* return 1 represented "true" (leap year) */
  80.     /* return 0 represented "false" (common year) */
  81.     if ( year%4 == 0 ){
  82.         if ( year%100 == 0 ){
  83.             if ( year%400 == 0 )
  84.                 return 1;
  85.             else
  86.                 return 0;
  87.         }
  88.         else
  89.             return 1;
  90.     }
  91.     else
  92.         return 0;
  93. } /* END testLeapYear() */
  94.  
  95. int monthLength( int year, int month ){
  96.     int monthDays[12] = {31, 0, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
  97.  
  98.     if ( month == 2 ){
  99.         if ( testLeapYear(year) == 1 )
  100.             return 29;
  101.         else
  102.             return 28;
  103.     }
  104.     else
  105.         return monthDays[--month];
  106.  
  107. } /* END monthLength() */
  108.  
  109. void switchDate( int *y1, int *m1, int *d1, int *y2, int *m2, int *d2 ){
  110.     int reg;
  111.     reg = *y1;  *y1 = *y2;  *y2 = reg;
  112.     reg = *m1;  *m1 = *m2;  *m2 = reg;
  113.     reg = *d1;  *d1 = *d2;  *d2 = reg;
  114. } /* END switchDate() */
  115.  
  116. void arrangeOrder( int *y1, int *m1, int *d1, int *y2, int *m2, int *d2 ){
  117.     if ( *y1 < *y2 )
  118.         switchDate( y1, m1, d1, y2, m2, d2 );
  119.  
  120.     else if ( *y1 == *y2 )
  121.         if ( *m1 < *m2 )
  122.             switchDate( y1, m1, d1, y2, m2, d2 );
  123.  
  124.         else if ( *m1 == *m2 )
  125.             if ( *d1 < *d2 )
  126.                 switchDate( y1, m1, d1, y2, m2, d2 );
  127.  
  128. } /* END arrangeOrder() */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement