Computer Programming Contest Preparation

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



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