Computer Programming Contest Preparation

ToolBox - Source for: 5/518/a.c



/home/toolbox/public_html/solutions/5/518/a.c
    1 #include <stdio.h>
    2 #include <string.h>
    3 #include <sys/types.h>
    4 #include <sys/stat.h>
    5 #include <fcntl.h>
    6 #include <stdint.h>
    7 #include <math.h>
    8 #include <stdlib.h>
    9 
   10 #define TRUE  (1 == 1)
   11 #define FALSE (1 != 1)
   12 
   13 #define DEBUG if (FALSE)
   14 
   15 #define YEAR 0
   16 #define MONTH 1
   17 #define DAY 2
   18 #define HOUR 3
   19 #define MINUTE 4
   20 #define SECOND 5
   21 #define TOTAL 6
   22 
   23 /*
   24  *  Author:
   25  *    Date:
   26  * Purpose:
   27  * Problem: 518
   28  */
   29 
   30 /*
   31  * This template reads data until a terminating value is reached.
   32  */
   33 
   34 int start[7];
   35 int stop[7];
   36 int interval[2];
   37 int months[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
   38 
   39 void init()
   40 {
   41     /* FUNCTION init */
   42 } /* FUNCTION init */
   43 
   44 void dump()
   45 {
   46     /* FUNCTION dump */
   47 } /* FUNCTION dump */
   48 
   49 void calcSeconds(int ary[7])
   50 {
   51     /* FUNCTION calcSeconds */
   52     int i;
   53     int leapflag;
   54     int end;
   55     int carry = 0;
   56 
   57     leapflag = ( 0 == (3 & ary[YEAR]));
   58     ary[TOTAL] = 0;
   59     /* deal with previous years */
   60     for (i=1970; i<ary[YEAR]; i++)
   61         {
   62             /* for year */
   63             ary[TOTAL] = ary[TOTAL] + 365 * 24 * 60 * 60;
   64             if (0 == (3 & i))
   65                 {
   66                     /* leap year */
   67                     ary[TOTAL] = ary[TOTAL] + 24 * 60 * 60;
   68                 } /* leap year */
   69         } /* for year */
   70 
   71     /* deal with previous months */
   72     for (i = 1; i<ary[MONTH]; i++)
   73         {
   74             /* each month */
   75             ary[TOTAL] = ary[TOTAL] + months[i] * 24 * 60 * 60;
   76             if ((2 == i) && (0 == (3 & i)))
   77                 {
   78                     ary[TOTAL] = ary[TOTAL] + 24 * 60 * 60;
   79                 }
   80         } /* each month */
   81 
   82     /* deal with days this month */
   83     ary[TOTAL] = ary[TOTAL] + (ary[DAY]-1) * 24 * 60 * 60;
   84 
   85     /* deal with completed hours */
   86     ary[TOTAL] = ary[TOTAL] + (ary[HOUR]-1) * 60 * 60;
   87 
   88     /* deal with completed minutes */
   89     ary[TOTAL] = ary[TOTAL] + (ary[MINUTE]-1) * 60;
   90 
   91     ary[TOTAL] = ary[TOTAL] + ary[SECOND] ;
   92 
   93 } /* FUNCTION calcSeconds */
   94 
   95 int getInput()
   96 {
   97     /* FUNCTION getInput */
   98     int dataReadFlag;
   99     char word[80];
  100 
  101     dataReadFlag = 0 <scanf(" %d %d %d %d %d %d ", &start[YEAR], &start[MONTH], &start[DAY], &start[HOUR], &start[MINUTE], &start[SECOND]);
  102     if (dataReadFlag)
  103         {
  104             /* get stop and interval */
  105             scanf(" %d %d %d %d %d %d ", &stop[YEAR], &stop[MONTH], &stop[DAY], &stop[HOUR], &stop[MINUTE], &stop[SECOND]);
  106             scanf(" %d ", &interval[0]);
  107             scanf(" %s ", word);
  108             /* possible intervals:
  109              - year   6
  110              - month  5
  111              - day    4
  112              - hour   3
  113              - minute 2
  114              - second 1
  115              */
  116             switch (word[0])
  117                 {
  118                 /* case */
  119                 case 'y' :
  120                     interval[1] = 6;
  121                     break;
  122                 case 'm' :
  123                     if ('o' == word[1])
  124                         {
  125                             interval[1] = 5;
  126                         }
  127                     else
  128                         {
  129                             interval[1] = 2;
  130                         };
  131                     break;
  132                 case 'd' :
  133                     interval[1] = 4;
  134                     break;
  135                 case 'h' :
  136                     interval[1] = 3;
  137                     break;
  138                 case 's' :
  139                     interval[1] = 1;
  140                     break;
  141                 } /* end case */
  142 
  143             calcSeconds(start);
  144             calcSeconds(stop);
  145             printf("start=%d\n", start[TOTAL]);
  146             printf("stop=%d\n", stop[TOTAL]);
  147         } /* get stop and interval */
  148     return (dataReadFlag);
  149 } /* FUNCTION getInput */
  150 
  151 void process()
  152 {
  153     /* FUNCTION process */
  154     int periods;
  155 
  156     periods = stop[TOTAL] - start[TOTAL];
  157     printf("Initial period=%d\n", periods);
  158     switch (interval[1])
  159         {
  160         /* switch */
  161         case 6:
  162             periods = periods / (365 * 24 * 60 * 60);
  163             break;
  164         case 5:
  165             periods = periods / (31 * 24 * 60 * 60);
  166             break;
  167         case 4:
  168             periods = periods / (24 * 60 * 60);
  169             break;
  170         case 3:
  171             periods = periods / (60 * 60);
  172             break;
  173         case 2:
  174             periods = periods / (60);
  175             break;
  176         } /* switch */
  177     printf("Second period=%d\n", periods);
  178     periods = periods / interval[0];
  179     printf("%d\n", periods);
  180 } /* FUNCTION process */
  181 
  182 int main ()
  183 {
  184     /* main */
  185     int moreToDo;
  186 
  187     init();
  188     moreToDo = getInput();
  189     while (moreToDo)
  190         {
  191             /* while */
  192             process();
  193             moreToDo = getInput();
  194         } /* while */
  195 
  196     return EXIT_SUCCESS;
  197 } /* main */
  198