/home/toolbox/public_html/solutions/6/619/c.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 if (('a' <= buff[i]) && ('z' >= buff[i]))
188 {
189 /* ignore invalid characters */
190 t = buff[i] - 'a' + 1;
191 addDigit(t, BASE);
192 } /* ignore invalid characters */
193 } /* process each character */
194 } /* FUNCTION processWord */
195
196 void processNumber()
197 {
198 /* FUNCTION processNumber */
199 int i;
200 int t;
201
202 /* translate input into num format */
203 for (i=0; i<strlen(buff); i++)
204 {
205 /* for */
206 t = buff[i] - '0';
207 addDigit(t, 10);
208 } /* for */
209 /* convert num format to array in base 26 */
210 baseIt();
211 } /* FUNCTION processNumber */
212
213 void process()
214 {
215 /* FUNCTION process */
216 if (isdigit(buff[0]))
217 {
218 /* read in a number */
219 processNumber();
220 } /* read in a number */
221 else
222 {
223 /* read in a word */
224 processWord();
225 } /* read in a word */
226 printf("%-20s ", buff);
227 dumpNum();
228 printf("\n");
229 } /* FUNCTION process */
230
231 int main ()
232 {
233 /* main */
234 int moreToDo;
235
236 moreToDo = getInput();
237 while (moreToDo)
238 {
239 /* while */
240 init();
241 process();
242 moreToDo = getInput();
243 } /* while */
244
245 return EXIT_SUCCESS;
246 } /* main */
247
248