/home/toolbox/public_html/solutions/6/619/m.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
97 while(scanf("%s", buf)==1)
98 {
99 int i;
100 if (!strcmp(buf, "*"))
101 {
102 break;
103 }
104
105 if (buf[0]>='0' && buf[0]<='9')
106 {
107 number.len = strlen(buf);
108 for(i=0; i<number.len; i++)
109 {
110 number.num[i] = buf[number.len-i-1]-'0';
111 }
112 num2letters();
113 }
114 else
115 {
116 letter.len = strlen(buf);
117 for(i=0; i<letter.len; i++)
118 {
119 letter.num[i] = buf[letter.len-i-1]-'a';
120 }
121 letters2num();
122 }
123 for(i=letter.len-1; i>=0; i--)
124 {
125 printf("%c", 'a'+letter.num[i]);
126 }
127 printf("%.*s", 22-letter.len,
128 " ");
129 for(i=number.len-1; i>=0; i--)
130 {
131 printf("%c", '0'+number.num[i]);
132 if (i && (i%3==0))
133 {
134 printf(",");
135 }
136 }
137 printf("\n");
138 }
139
140 exit(0);
141 }
142