Computer Programming Contest Preparation

ToolBox - Source for: 4/424/a.c



/home/toolbox/public_html/solutions/4/424/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 /*
   16  *  Author: Isaac Traxler
   17  *    Date: 2015 02 11
   18  * Purpose:
   19  * Problem: 424
   20  */
   21 
   22 /*
   23  * This template reads data until a terminating value is reached.
   24  */
   25 
   26 #define MAX_LINE 151
   27 
   28 char buffer[MAX_LINE];
   29 int tot[MAX_LINE];
   30 int totTop;
   31 int tmp[MAX_LINE];
   32 int tmpTop;
   33 
   34 void init()
   35 {
   36     /* FUNCTION init */
   37     int i;
   38 
   39     for (i=0; MAX_LINE>i; i++)
   40         {
   41             /* empty arrays */
   42             tot[i] = 0;
   43         } /* empty arrays */
   44     totTop = 0;
   45 } /* FUNCTION init */
   46 
   47 void initTmp()
   48 {
   49     /* FUNCTION initTmp */
   50     int i;
   51 
   52     for (i=0; MAX_LINE>i; i++)
   53         {
   54             /* empty arrays */
   55             tmp[i] = 0;
   56         } /* empty arrays */
   57     tmpTop = 0;
   58 } /* FUNCTION initTmp */
   59 
   60 void dump()
   61 {
   62     /* FUNCTION dump */
   63     int i;
   64     char digits[10] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9'};
   65 
   66     DEBUG printf("Enter dump\n");
   67     printf("dump: ");
   68     for (i=tmpTop-1; -1<i; i--)
   69         {
   70             /* for */
   71             DEBUG printf("%d %d\n", i, tmp[i]);
   72             printf("%c", digits[tmp[i]]);
   73         } /* for */
   74     printf("\n");
   75     DEBUG printf("Leave dump\n");
   76 } /* FUNCTION dump */
   77 
   78 void showAnswer()
   79 {
   80     /* FUNCTION showAnswer */
   81     int i;
   82     char digits[10] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9'};
   83     int zeroFlag = TRUE;
   84 
   85     DEBUG printf("enter showAnswer\n");
   86     for (i=MAX_LINE-1; -1<i; i--)
   87         {
   88             /* for */
   89             zeroFlag = zeroFlag && (0 == tot[i]);
   90             DEBUG printf("%d %d\n", i, tot[i]);
   91             if (! zeroFlag)
   92                 {
   93                     printf("%c", digits[tot[i]]);
   94                 }
   95         } /* for */
   96     if (zeroFlag)
   97         {
   98             printf("0");
   99         }
  100     printf("\n");
  101     DEBUG printf("Leave showAnswer\n");
  102 } /* FUNCTION showAnswer */
  103 
  104 void add()
  105 {
  106     /* FUNCTION add */
  107     int i;
  108 
  109     if (tmpTop > totTop)
  110         {
  111             totTop = tmpTop;
  112         }
  113     for (i=0; totTop > i; i++)
  114         {
  115             /* for each digit */
  116             DEBUG printf("adding: tot[%d] (%d) tmp{%d] (%d) = ", i, tot[i], i, tmp[i]);
  117             tot[i] = tot[i] + tmp[i];
  118             DEBUG printf("%d\n", tot[i]);
  119             if (9 < tot[i])
  120                 {
  121                     /* we have a carry */
  122                     tot[i+1] = tot[i+1] + tot[i] / 10;
  123                     tot[i] = tot[i] % 10;
  124                 } /* we have a carry */
  125         } /* for each digit */
  126     if (0 < tot[totTop])
  127         {
  128             /* handle carry into an new digit */
  129             totTop++;
  130         } /* handle carry into an new digit */
  131 
  132 } /* FUNCTION add */
  133 
  134 int getInput()
  135 {
  136     /* FUNCTION getInput */
  137     int dataReadFlag;
  138     int i;
  139 
  140     scanf("%s ", buffer);
  141     if (0 == strcmp("0", buffer))
  142         {
  143             /* end of file reached */
  144             dataReadFlag = FALSE;
  145         } /* end of file reached */
  146     else
  147         {
  148             /* read in something */
  149             initTmp();
  150             tmpTop = strlen(buffer);
  151             DEBUG printf("[%s]\n", buffer);
  152             for (i=0; i<tmpTop; i++)
  153                 {
  154                     /* for each char */
  155                     tmp[i] = buffer[tmpTop-(i+1)] - '0';
  156                 } /* for each char */
  157         } /* read in something */
  158     return (dataReadFlag);
  159 } /* FUNCTION getInput */
  160 
  161 void process()
  162 {
  163     /* FUNCTION process */
  164     DEBUG dump();
  165     DEBUG showAnswer();
  166     add();
  167     DEBUG dump();
  168     DEBUG showAnswer();
  169 } /* FUNCTION process */
  170 
  171 int main()
  172 {
  173     /* main */
  174     int moreToDo;
  175 
  176     init();
  177     DEBUG dump();
  178     DEBUG showAnswer();
  179     moreToDo = getInput();
  180     DEBUG dump();
  181     while (moreToDo)
  182         {
  183             /* while */
  184             process();
  185             moreToDo = getInput();
  186         } /* while */
  187     showAnswer();
  188 
  189     return EXIT_SUCCESS;
  190 } /* main */
  191