Computer Programming Contest Preparation

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



/home/toolbox/public_html/solutions/6/619/m1.c
    1 /* Numerically Speaking */
    2 #include <stdio.h>
    3 
    4 typedef struct
    5 {
    6     int len;
    7     int num[50];
    8 } big;
    9 
   10 big number;
   11 big letter;
   12 
   13 void
   14 sum(big *a, int n, int base)
   15 {
   16     int i;
   17 
   18     for(i=0; i<a->len; i++)
   19         {
   20             a->num[i] += n;
   21             n = (a->num[i] / base);
   22             a->num[i] %= base;
   23         }
   24     while (n)
   25         {
   26             a->num[a->len] = (base==26 ? n-1 : n);
   27             n /= base;
   28             a->num[a->len] %= base;
   29             a->len++;
   30         }
   31 }
   32 
   33 void
   34 mul(big *a, int n, int base)
   35 {
   36     int i;
   37 
   38     for(i=0; i<a->len; i++)
   39         {
   40             if (base==26)
   41                 {
   42                     a->num[i] = (a->num[i]+1) * n - 1;
   43                 }
   44             else
   45                 {
   46                     a->num[i] *= n;
   47                 }
   48         }
   49     n=0;
   50     for(i=0; i<a->len; i++)
   51         {
   52             a->num[i] += n;
   53             n = (a->num[i] / base);
   54             a->num[i] %= base;
   55         }
   56     while (n)
   57         {
   58             a->num[a->len] = (base==26 ? n-1 : n);
   59             n /= base;
   60             a->num[a->len] %= base;
   61             a->len++;
   62         }
   63 }
   64 
   65 void
   66 num2letters()
   67 {
   68     int i;
   69 
   70     letter.len = 0;
   71     for(i=number.len-1; i>=0; i--)
   72         {
   73             mul(&letter, 10, 26);
   74             sum(&letter, number.num[i], 26);
   75         }
   76 }
   77 
   78 void
   79 letters2num()
   80 {
   81     int i;
   82 
   83     number.len = 0;
   84     for(i=letter.len-1; i>=0; i--)
   85         {
   86             mul(&number, 26, 10);
   87             sum(&number, letter.num[i], 10);
   88             sum(&number, 1, 10);
   89         }
   90 }
   91 
   92 int
   93 main(void)
   94 {
   95     char buf[1024];
   96     int cnt;
   97 
   98     while(scanf("%s", buf)==1)
   99         {
  100             int i;
  101             if (!strcmp(buf, "*"))
  102                 {
  103                     break;
  104                 }
  105 
  106             if (buf[0]>='0' && buf[0]<='9')
  107                 {
  108                     cnt = 0;
  109                     while ((strlen(buf)>cnt) && ('0'==buf[cnt]))
  110                         {
  111                             cnt++;
  112                         }
  113                     if (0 < cnt) /* leading zeroes found */
  114                         {
  115                             /* if */
  116                             for (i=0; i<strlen(buf); i++)
  117                                 {
  118                                     buf[i] = buf[i+cnt];
  119                                 }
  120                         } /* if */
  121 
  122                     number.len = strlen(buf);
  123                     for(i=0; i<number.len; i++)
  124                         {
  125                             number.num[i] = buf[number.len-i-1]-'0';
  126                         }
  127                     num2letters();
  128                 }
  129             else
  130                 {
  131                     letter.len = strlen(buf);
  132                     for(i=0; i<letter.len; i++)
  133                         {
  134                             letter.num[i] = buf[letter.len-i-1]-'a';
  135                         }
  136                     letters2num();
  137                 }
  138             for(i=letter.len-1; i>=0; i--)
  139                 {
  140                     printf("%c", 'a'+letter.num[i]);
  141                 }
  142             printf("%.*s", 22-letter.len,
  143                    "                                                       ");
  144             for(i=number.len-1; i>=0; i--)
  145                 {
  146                     printf("%c", '0'+number.num[i]);
  147                     if (i && (i%3==0))
  148                         {
  149                             printf(",");
  150                         }
  151                 }
  152             printf("\n");
  153         }
  154 
  155     exit(0);
  156 }
  157