Computer Programming Contest Preparation

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



/home/toolbox/public_html/solutions/4/424/c.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 tmp[MAX_LINE];
   31 
   32 void init()
   33 {
   34     /* FUNCTION init */
   35     int i;
   36 
   37     for (i=0; MAX_LINE>i; i++)
   38         {
   39             /* empty arrays */
   40             tot[i] = 0;
   41         } /* empty arrays */
   42 } /* FUNCTION init */
   43 
   44 void initTmp()
   45 {
   46     /* FUNCTION initTmp */
   47     int i;
   48 
   49     for (i=0; MAX_LINE>i; i++)
   50         {
   51             /* empty arrays */
   52             tmp[i] = 0;
   53         } /* empty arrays */
   54 } /* FUNCTION initTmp */
   55 
   56 void showAnswer()
   57 {
   58     /* FUNCTION showAnswer */
   59     int i;
   60     char digits[10] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9'};
   61     int zeroFlag = TRUE;
   62 
   63     for (i=MAX_LINE-1; -1<i; i--)
   64         {
   65             /* for */
   66             zeroFlag = zeroFlag && (0 == tot[i]);
   67             if (! zeroFlag)
   68                 {
   69                     printf("%c", digits[tot[i]]);
   70                 }
   71         } /* for */
   72     if (zeroFlag)
   73         {
   74             printf("0");
   75         }
   76     printf("\n");
   77 } /* FUNCTION showAnswer */
   78 
   79 int getInput()
   80 {
   81     /* FUNCTION getInput */
   82     int dataReadFlag;
   83     int i;
   84     int top;
   85 
   86     scanf(" %s ", buffer);
   87     if (0 == strcmp("0", buffer))
   88         {
   89             /* end of file reached */
   90             dataReadFlag = FALSE;
   91         } /* end of file reached */
   92     else
   93         {
   94             /* read in something */
   95             initTmp();
   96             top = strlen(buffer);
   97             for (i=0; i<top; i++)
   98                 {
   99                     /* for each char */
  100                     tmp[i] = buffer[top-(i+1)] - '0';
  101                 } /* for each char */
  102         } /* read in something */
  103     return (dataReadFlag);
  104 } /* FUNCTION getInput */
  105 
  106 void process()
  107 {
  108     /* FUNCTION process */
  109     int i;
  110 
  111     for (i=0; MAX_LINE > i; i++)
  112         {
  113             /* for each digit */
  114             tot[i] = tot[i] + tmp[i];
  115             if (9 < tot[i])
  116                 {
  117                     /* we have a carry */
  118                     tot[i+1] = tot[i+1] + 1;
  119                     tot[i] = tot[i] - 10;
  120                 } /* we have a carry */
  121         } /* for each digit */
  122 } /* FUNCTION process */
  123 
  124 int main()
  125 {
  126     /* main */
  127     int moreToDo;
  128     int gotInput = FALSE;
  129 
  130     init();
  131     moreToDo = getInput();
  132     while (moreToDo)
  133         {
  134             /* while */
  135             gotInput = TRUE;
  136             process();
  137             moreToDo = getInput();
  138         } /* while */
  139     if (gotInput)
  140         {
  141             showAnswer();
  142         }
  143 
  144     return EXIT_SUCCESS;
  145 } /* main */
  146