Computer Programming Contest Preparation

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



/home/toolbox/public_html/solutions/6/619/f.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 60
   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     int d = 0;
  173 
  174     /* fgets(buff, MAX_BUFFER_LENGTH, stdin); */
  175     while (1 != scanf("%s", buff));
  176     dataReadFlag = ('*' != buff[0]);
  177     if ('\n' == buff[strlen(buff)-1])
  178         {
  179             buff[strlen(buff)-1] = '\0';    /* take care of newline */
  180         }
  181     return (dataReadFlag);
  182 } /* FUNCTION getInput */
  183 
  184 void processWord()
  185 {
  186     /* FUNCTION processWord */
  187     int i;
  188     int t;
  189 
  190     for (i=0; i<strlen(buff); i++)
  191         {
  192             /* process each character */
  193             if (('a' <= buff[i]) && ('z' >= buff[i]))
  194                 {
  195                     /* ignore invalid characters */
  196                     t = buff[i] - 'a' + 1;
  197                     addDigit(t, BASE);
  198                 } /* ignore invalid characters */
  199         } /* process each character */
  200 } /* FUNCTION processWord */
  201 
  202 void processNumber()
  203 {
  204     /* FUNCTION processNumber */
  205     int i;
  206     int t;
  207 
  208     /* translate input into num format */
  209     for (i=0; i<strlen(buff); i++)
  210         {
  211             /* for */
  212             cuff[i] = buff[i];
  213             t = buff[i] - '0';
  214             addDigit(t, 10);
  215         } /* for */
  216     cuff[i]='\0';
  217     /* convert num format to array in base 26 */
  218     baseIt();
  219 } /* FUNCTION processNumber */
  220 
  221 void process()
  222 {
  223     /* FUNCTION process */
  224     int i, k;
  225     if (isdigit(buff[0]))
  226         {
  227             /* read in a number */
  228             processNumber();
  229             printf("%-20s  ", buff);
  230             if (TRUE)
  231                 {
  232                     /* leading zero input */
  233                     i = ((strlen(cuff) - 1)  % 3) + 1;
  234                     for (k=0; k<i; k++)
  235                         {
  236                             printf("%c", cuff[k]);
  237                         }
  238                     if (3 < strlen(cuff))
  239                         {
  240                             /* more to do */
  241                             for (; k<strlen(cuff); k=k+3)
  242                                 {
  243                                     /* dump 3 digits */
  244                                     printf(",%c%c%c", cuff[k], cuff[k+1], cuff[k+2]);
  245                                 } /* dump 3 digits */
  246                         } /* more to do */
  247                 } /* leading zero input */
  248             else
  249                 {
  250                     /* normal print */
  251                     dumpNum();
  252                 } /* normal print */
  253         } /* read in a number */
  254     else
  255         {
  256             /* read in a word */
  257             processWord();
  258             printf("%-20s  ", buff);
  259             dumpNum();
  260         } /* read in a word */
  261     printf("\n");
  262 } /* FUNCTION process */
  263 
  264 int main ()
  265 {
  266     /* main */
  267     int moreToDo;
  268 
  269     moreToDo = getInput();
  270     while (moreToDo)
  271         {
  272             /* while */
  273             init();
  274             process();
  275             moreToDo = getInput();
  276         } /* while */
  277 
  278     return EXIT_SUCCESS;
  279 } /* main */
  280 
  281