Computer Programming Contest Preparation

ToolBox - Source for: 7/713/a.c



/home/toolbox/public_html/solutions/7/713/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 
   11 #define TRUE  (1 == 1)
   12 #define FALSE (1 != 1)
   13 
   14 #define DEBUG if (TRUE)
   15 #define DEBUG1 if (FALSE)
   16 #define DEBUG2 if (FALSE)
   17 
   18 /* fprintf(stderr, "functionName: message", varslist); */
   19 
   20 /*
   21  *  Author: Isaac Traxler
   22  *    Date: March 1, 2016
   23  * Purpose: fun
   24  * Problem: 713 - Adding Reversed Numbers
   25  */
   26 
   27 /*
   28  * This template reads data a specified number of times.
   29  */
   30 
   31 #define MAX_LENGTH 205
   32 
   33 int numberOfTimes;
   34 char n1[MAX_LENGTH];
   35 char n2[MAX_LENGTH];
   36 char n3[MAX_LENGTH];
   37 
   38 int init()
   39 {
   40     /* FUNCTION init */
   41     scanf("%d ", &numberOfTimes);
   42 } /* FUNCTION init */
   43 
   44 void dump()
   45 {
   46     /* FUNCTION dump */
   47 } /* FUNCTION dump */
   48 
   49 void getInput()
   50 {
   51     /* FUNCTION getInput */
   52     int i;
   53     int ln;
   54 
   55     scanf(" %s %s ", n1, n2);
   56     DEBUG1 printf(" Read in: \n[%s]\n[%s]\n", n1, n2);
   57 
   58     /* strip trailing 0s */
   59     i = strlen(n1) - 1;
   60     while ('0' == n1[i])
   61         {
   62             /* while */
   63             n1[i] = 0;
   64             i--;
   65         } /* while */
   66 
   67     /* strip trailing 0s */
   68     i = strlen(n2) - 1;
   69     while ('0' == n2[i])
   70         {
   71             /* while */
   72             n2[i] = 0;
   73             i--;
   74         } /* while */
   75 
   76 
   77 } /* FUNCTION getInput */
   78 
   79 void addThem()
   80 {
   81     /* FUNCTION addThem */
   82     int i;
   83     int l1;
   84     int l2;
   85     int cmmn;
   86     int carry;
   87     int tmp;
   88 
   89     l1 = strlen(n1);
   90     l2 = strlen(n2);
   91     if (l1 > l2)
   92         {
   93             cmmn = l2;
   94         }
   95     else
   96         {
   97             cmmn = l1;
   98         }
   99     carry = 0;
  100     for (i=0; i<cmmn; i++)
  101         {
  102             /* for */
  103             tmp = carry + n1[i] + n2[i] - '0' - '0';
  104             carry = tmp / 10;  /* should only be 0 or 1 */
  105             tmp = tmp % 10;
  106             n3[i] = tmp + '0';
  107         } /* for */
  108     if (l1 > cmmn)
  109         {
  110             /* more of number 1 to add */
  111             while (i < l1)
  112                 {
  113                     /* while */
  114                     tmp = carry + n1[i] - '0';
  115                     carry = tmp / 10;  /* should only be 0 or 1 */
  116                     n3[i] = (tmp % 10) + '0';
  117                     i++;
  118                 } /* while */
  119         } /* more of number 1 to add */
  120     if (l2 > cmmn)
  121         {
  122             /* more of number 2 to add */
  123             while (i < l2)
  124                 {
  125                     /* while */
  126                     tmp = carry + n2[i] - '0';
  127                     carry = tmp / 10;  /* should only be 0 or 1 */
  128                     n3[i] = (tmp % 10) + '0';
  129                     i++;
  130                 } /* while */
  131         } /* more of number 2 to add */
  132     if (0 != carry)
  133         {
  134             n3[i] = '1';
  135             i++;
  136         }
  137     n3[i] = 0;
  138 } /* FUNCTION addThem */
  139 
  140 void process()
  141 {
  142     /* FUNCTION process */
  143     int i;
  144     int l3;
  145     int flag = TRUE;
  146     int printed = FALSE;
  147 
  148     addThem();
  149     l3 = strlen(n3);
  150     for (i=0; i<l3; i++)
  151         {
  152             /* for */
  153             if (flag)
  154                 {
  155                     flag =  ('0' == n3[i]);
  156                 }
  157             if (! flag )
  158                 {
  159                     printf("%c", n3[i]);
  160                     printed = TRUE;
  161                 }
  162         } /* for */
  163     if (! printed)
  164         {
  165             printf("0");
  166         }
  167     printf("\n");
  168 } /* FUNCTION process */
  169 
  170 int main ()
  171 {
  172     /* main */
  173     int i;
  174 
  175     init();
  176     for (i=0; i<numberOfTimes; i++)
  177         {
  178             /* while */
  179             getInput();
  180             process();
  181         } /* while */
  182 
  183     return EXIT_SUCCESS;
  184 } /* main */
  185