/home/toolbox/public_html/solutions/6/619/b.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) - 1;
100 carry = based[i] % base + 1;
101 based[i] = based[i] / base;
102 } /* for each bucket */
103
104 return (carry + 'a' - 1);
105 } /* FUNCTION divideBased */
106
107 void baseIt()
108 {
109 /* FUNCTION baseIt */
110 int i;
111 char tmp;
112 int sl;
113
114 /* copy num to based -- because we will need num to print out */
115 for (i=BUCKETS; 0<=i; i--)
116 {
117 /* for each bucket */
118 based[i] = num[i];
119 } /* for each bucket */
120 /* convert the number to a string */
121 i = 0;
122 while (notZero())
123 {
124 /* more to convert */
125 buff[i++] = divideBased(BASE);
126 } /* more to convert */
127 buff[i] = '\0';
128 /* reverse the string -- came out backwards above */
129 sl = strlen(buff) - 1;
130 for (i=0; ((strlen(buff)+1)/2) > i; i++, sl--)
131 {
132 /* for */
133 tmp = buff[i];
134 buff[i] = buff[sl];
135 buff[sl] = tmp;
136 } /* for */
137 } /* FUNCTION baseIt */
138
139 void dumpNum()
140 {
141 /* FUNCTION dumpNum */
142 int i;
143 int comma = FALSE;
144
145 for (i=(BUCKETS-1); 0<=i; i--)
146 {
147 /* for */
148 if (comma)
149 {
150 /* print regardless of value */
151 printf(",%03d", num[i]);
152 } /* print regardless of value */
153 else if (0 < num[i])
154 {
155 /* time to start printing */
156 printf("%d", num[i]);
157 comma = TRUE;
158 } /* time to start printing */
159 } /* for */
160 } /* FUNCTION dumpNum */
161
162 void dump()
163 {
164 /* FUNCTION dump */
165 } /* FUNCTION dump */
166
167 int getInput()
168 {
169 /* FUNCTION getInput */
170 int dataReadFlag;
171
172 fgets(buff, MAX_BUFFER_LENGTH, stdin);
173 dataReadFlag = ('*' != buff[0]);
174 buff[strlen(buff)-1] = '\0'; /* take care of newline */
175 return (dataReadFlag);
176 } /* FUNCTION getInput */
177
178 void processWord()
179 {
180 /* FUNCTION processWord */
181 int i;
182 int t;
183
184 for (i=0; i<strlen(buff); i++)
185 {
186 /* process each character */
187 t = buff[i] - 'a' + 1;
188 addDigit(t, BASE);
189 } /* process each character */
190 } /* FUNCTION processWord */
191
192 void processNumber()
193 {
194 /* FUNCTION processNumber */
195 int i;
196 int t;
197
198 /* translate input into num format */
199 for (i=0; i<strlen(buff); i++)
200 {
201 /* for */
202 t = buff[i] - '0';
203 addDigit(t, 10);
204 } /* for */
205 /* convert num format to array in base 26 */
206 baseIt();
207 } /* FUNCTION processNumber */
208
209 void process()
210 {
211 /* FUNCTION process */
212 if (isdigit(buff[0]))
213 {
214 /* read in a number */
215 processNumber();
216 } /* read in a number */
217 else
218 {
219 /* read in a word */
220 processWord();
221 } /* read in a word */
222 printf("%-20s ", buff);
223 dumpNum();
224 printf("\n");
225 } /* FUNCTION process */
226
227 int main ()
228 {
229 /* main */
230 int moreToDo;
231
232 moreToDo = getInput();
233 while (moreToDo)
234 {
235 /* while */
236 init();
237 process();
238 moreToDo = getInput();
239 } /* while */
240
241 return EXIT_SUCCESS;
242 } /* main */
243
244