Computer Programming Contest Preparation

ToolBox - Source for: 6/619/d.c



/home/toolbox/public_html/solutions/6/619/d.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: 2013-01-25
   18  * Purpose: fun
   19  * Problem: 619 - Numerically Speaking
   20  */
   21 
   22 /*
   23  * This template reads data until a terminating value is reached.
   24  */
   25 
   26 #define BASE 26
   27 #define BUCKETS 40
   28 #define MAX_VALUE 999
   29 #define NUM_DIGITS 3
   30 #define MAX_BUFFER_LENGTH 40
   31 
   32 /* global variables */
   33 int num[BUCKETS+1];
   34 char buff[MAX_BUFFER_LENGTH];
   35 char cuff[MAX_BUFFER_LENGTH];
   36 int based[BUCKETS];
   37 
   38 /* start code */
   39 void init()
   40 {
   41     /* FUNCTION init */
   42     int i;
   43 
   44     for (i=0; i<=BUCKETS; i++)
   45         {
   46             /* for each bucket */
   47             num[i] = 0;
   48             based[i] = 0;
   49         } /* for each bucket */
   50 } /* FUNCTION init */
   51 
   52 int notZero()
   53 {
   54     /* FUNCTION notZero */
   55     int zero = TRUE;
   56     int i = 0;
   57 
   58     while ((zero) && (BUCKETS>i))
   59         {
   60             /* while */
   61             zero = (0 == based[i++]);
   62         } /* while */
   63     return (! zero);
   64 } /* FUNCTION notZero */
   65 
   66 void addDigit(int digit, int base)
   67 {
   68     /* FUNCTION addDigit */
   69     int i;
   70     int carry;
   71 
   72     for (i=0; i<BUCKETS; i++)
   73         {
   74             /* for each bucket */
   75             num[i] = num[i] * base;
   76         } /* for each bucket */
   77     num[0] = num[0] + digit;
   78     for (i=0; i<BUCKETS; i++)
   79         {
   80             /* for each bucket */
   81             if (MAX_VALUE < num[i])
   82                 {
   83                     /* we have a carry */
   84                     carry = num[i] / (MAX_VALUE + 1);
   85                     num[i] = num[i] % (MAX_VALUE + 1);
   86                     num[i+1] = num[i+1] + carry;
   87                 } /* we have a carry */
   88         } /* for each bucket */
   89 } /* FUNCTION addDigit */
   90 
   91 char divideBased(int base)
   92 {
   93     /* FUNCTION divideBased */
   94     int i;
   95     int carry = 0;
   96 
   97     for (i=BUCKETS; 0<=i; i--)
   98         {
   99             /* for each bucket */
  100             based[i] = based[i] + carry * (MAX_VALUE + 1) - 1;
  101             carry = based[i] % base + 1;
  102             based[i] = based[i] / base;
  103         } /* for each bucket */
  104 
  105     return (carry + 'a' - 1);
  106 } /* FUNCTION divideBased */
  107 
  108 void baseIt()
  109 {
  110     /* FUNCTION baseIt */
  111     int i;
  112     char tmp;
  113     int sl;
  114 
  115     /* copy num to based -- because we will need num to print out */
  116     for (i=BUCKETS; 0<=i; i--)
  117         {
  118             /* for each bucket */
  119             based[i] = num[i];
  120         } /* for each bucket */
  121     /* convert the number to a string */
  122     i = 0;
  123     while (notZero())
  124         {
  125             /* more to convert */
  126             buff[i++] = divideBased(BASE);
  127         } /* more to convert */
  128     buff[i] = '\0';
  129     /* reverse the string -- came out backwards above */
  130     sl = strlen(buff) - 1;
  131     for (i=0; ((strlen(buff)+1)/2) > i; i++, sl--)
  132         {
  133             /* for */
  134             tmp = buff[i];
  135             buff[i] = buff[sl];
  136             buff[sl] = tmp;
  137         } /* for */
  138 } /* FUNCTION baseIt */
  139 
  140 void dumpNum()
  141 {
  142     /* FUNCTION dumpNum */
  143     int i;
  144     int comma = FALSE;
  145 
  146     for (i=(BUCKETS-1); 0<=i; i--)
  147         {
  148             /* for */
  149             if (comma)
  150                 {
  151                     /* print regardless of value */
  152                     printf(",%03d", num[i]);
  153                 } /* print regardless of value */
  154             else if (0 < num[i])
  155                 {
  156                     /* time to start printing */
  157                     printf("%d", num[i]);
  158                     comma = TRUE;
  159                 } /* time to start printing */
  160         } /* for */
  161 } /* FUNCTION dumpNum */
  162 
  163 void dump()
  164 {
  165     /* FUNCTION dump */
  166 } /* FUNCTION dump */
  167 
  168 int getInput()
  169 {
  170     /* FUNCTION getInput */
  171     int dataReadFlag;
  172 
  173     fgets(buff, MAX_BUFFER_LENGTH, stdin);
  174     dataReadFlag = ('*' != buff[0]);
  175     buff[strlen(buff)-1] = '\0';   /* take care of newline */
  176     return (dataReadFlag);
  177 } /* FUNCTION getInput */
  178 
  179 void processWord()
  180 {
  181     /* FUNCTION processWord */
  182     int i;
  183     int t;
  184 
  185     for (i=0; i<strlen(buff); i++)
  186         {
  187             /* process each character */
  188             if (('a' <= buff[i]) && ('z' >= buff[i]))
  189                 {
  190                     /* ignore invalid characters */
  191                     t = buff[i] - 'a' + 1;
  192                     addDigit(t, BASE);
  193                 } /* ignore invalid characters */
  194         } /* process each character */
  195 } /* FUNCTION processWord */
  196 
  197 void processNumber()
  198 {
  199     /* FUNCTION processNumber */
  200     int i;
  201     int t;
  202 
  203     /* translate input into num format */
  204     for (i=0; i<strlen(buff); i++)
  205         {
  206             /* for */
  207             cuff[i] = buff[i];
  208             t = buff[i] - '0';
  209             addDigit(t, 10);
  210         } /* for */
  211     cuff[i]='\0';
  212     /* convert num format to array in base 26 */
  213     baseIt();
  214 } /* FUNCTION processNumber */
  215 
  216 void process()
  217 {
  218     /* FUNCTION process */
  219     int i, k;
  220     if (isdigit(buff[0]))
  221         {
  222             /* read in a number */
  223             processNumber();
  224             printf("%-20s  ", buff);
  225             if ('0' == cuff[0])
  226                 {
  227                     /* leading zero input */
  228                     i = ((strlen(cuff) - 1)  % 3) + 1;
  229                     for (k=0; k<i; k++)
  230                         {
  231                             printf("%c", cuff[k]);
  232                         }
  233                     if (3 < strlen(cuff))
  234                         {
  235                             /* more to do */
  236                             for (; k<strlen(cuff); k=k+3)
  237                                 {
  238                                     /* dump 3 digits */
  239                                     printf(",%c%c%c", cuff[k], cuff[k+1], cuff[k+2]);
  240                                 } /* dump 3 digits */
  241                         } /* more to do */
  242                 } /* leading zero input */
  243             else
  244                 {
  245                     /* normal print */
  246                     dumpNum();
  247                 } /* normal print */
  248         } /* read in a number */
  249     else
  250         {
  251             /* read in a word */
  252             processWord();
  253             printf("%-20s  ", buff);
  254             dumpNum();
  255         } /* read in a word */
  256     printf("\n");
  257 } /* FUNCTION process */
  258 
  259 int main ()
  260 {
  261     /* main */
  262     int moreToDo;
  263 
  264     moreToDo = getInput();
  265     while (moreToDo)
  266         {
  267             /* while */
  268             init();
  269             process();
  270             moreToDo = getInput();
  271         } /* while */
  272 
  273     return EXIT_SUCCESS;
  274 } /* main */
  275 
  276