Computer Programming Contest Preparation

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



/home/toolbox/public_html/solutions/5/518/b.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[12] = {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 
   56     leapflag = ( 0 == (3 & ary[YEAR]));
   57     ary[TOTAL] = 0;
   58     /* deal with year */
   59     for (i=1970; i< ary[YEAR]; i++)
   60         {
   61             /* for year */
   62             ary[TOTAL] = ary[TOTAL] + 365 * 24 * 60 * 60;
   63             if (leapflag)
   64                 {
   65                     /* leap year */
   66                     ary[TOTAL] = ary[TOTAL] + 24 * 60 * 60;
   67                 } /* leap year */
   68         } /* for year */
   69 
   70     for (i = 1; i<ary[MONTH]; i++)
   71         {
   72             /* each month */
   73             ary[TOTAL] = ary[TOTAL] + months[i] * 24 * 60 * 60;
   74             if ((2 == i) && (leapflag))
   75                 {
   76                     ary[TOTAL] = ary[TOTAL] + 24 * 60 * 60;
   77                 }
   78         } /* each month */
   79 
   80     if (leapflag)
   81         {
   82             end = months[ary[MONTH]] + 1;
   83         }
   84     else
   85         {
   86             end = 1 + months[ary[MONTH]];
   87         }
   88     for (i=0; i < ary[MONTH]; i++)
   89         {
   90             /* do day */
   91             ary[TOTAL] = ary[TOTAL] + 24 * 60 * 60;
   92         } /* do day */
   93 
   94     for (i=1; i < end; i++)
   95         {
   96             /* do day */
   97             ary[TOTAL] = ary[TOTAL] + 24 * 60 * 60;
   98         } /* do day */
   99 
  100     for (i=ary[]+1; i < end; i++)
  101         {
  102             /* do day */
  103             ary[TOTAL] = ary[TOTAL] + 24 * 60 * 60;
  104         } /* do day */
  105 
  106 } /* FUNCTION calcSeconds */
  107 
  108 int getInput()
  109 {
  110     /* FUNCTION getInput */
  111     int dataReadFlag;
  112     char word[80];
  113 
  114     scanf(" %d %d %d %d %d %d ", &start[YEAR], &start[MONTH], &start[DAY], &start[HOUR], &start[MINUTE], &start[SECOND]);
  115     dataReadFlag = (0 != start[YEAR]) || (0 != start[MONTH]) || (0 != start[DAY]) || (0 != start[HOUR]) || (0 != start[MINUTE]) || (0 != start[SECOND]);
  116     if (dataReadFlag)
  117         {
  118             /* get stop and interval */
  119             scanf(" %d %d %d %d %d %d ", &stop[YEAR], &stop[MONTH], &stop[DAY], &stop[HOUR], &stop[MINUTE], &stop[SECOND]);
  120             scanf(" %d ", &interval[0]);
  121             scanf(" %s ", word);
  122         } /* get stop and interval */
  123     /* possible intervals:
  124     - year   6
  125     - month  5
  126     - day    4
  127     - hour   3
  128     - minute 2
  129     - second 1
  130     */
  131 
  132     switch (word[0])
  133         {
  134             /* case */
  135 'y' :
  136             interval[1] = 6;
  137             break;
  138             'm'
  139 :
  140             if ('o' == word[1])
  141                 {
  142                     interval[1] =5;
  143                 }
  144             else
  145                 {
  146                     interval[1] = 2
  147                 };
  148             break;
  149             'd'
  150 :
  151             interval[1] = 4;
  152             break;
  153             'h'
  154 :
  155             interval[1] = 3;
  156             break;
  157             'm'
  158 :
  159             interval[1] = 1;
  160             break;
  161         } /* end case */
  162 
  163     calcSeconds(start);
  164     calcSeconds(stop);
  165 
  166     return (dataReadFlag);
  167 } /* FUNCTION getInput */
  168 
  169 void process()
  170 {
  171     /* FUNCTION process */
  172 } /* FUNCTION process */
  173 
  174 int main ()
  175 {
  176     /* main */
  177     int moreToDo;
  178 
  179     init();
  180     moreToDo = getInput();
  181     while (moreToDo)
  182         {
  183             /* while */
  184             process();
  185             moreToDo = getInput();
  186         } /* while */
  187 
  188     return EXIT_SUCCESS;
  189 } /* main */
  190