/home/toolbox/public_html/solutions/6/619/aa.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 9
29 #define NUM_DIGITS 3
30 #define MAX_BUFFER_LENGTH 40
31
32 typedef struct
33 {
34 /* struct BIGNUM */
35 int len;
36 int digit[MAX_BUFFER_LENGTH];
37 } /* struct BIGNUM */ BIGNUM;
38
39 /* global variables */
40 char buff[MAX_BUFFER_LENGTH];
41 BIGNUM num;
42
43 /* start code */
44 void init()
45 {
46 /* FUNCTION init */
47 int i;
48
49 num.len = 1;
50 for (i=0; i<MAX_BUFFER_LENGTH; i++)
51 {
52 /* for each location */
53 num.digit[i] = 0;
54 } /* for each location */
55 } /* FUNCTION init */
56
57 void addDigit(int digit, int base)
58 {
59 /* FUNCTION addDigit */
60 int i;
61 int carry;
62
63 for (i=0; i<num.len; i++)
64 {
65 /* for each bucket */
66 num.digit[i] = num.digit[i] * base;
67 } /* for each bucket */
68 num.digit[0] = num.digit[0] + digit;
69 for (i=0; i<num.len; i++)
70 {
71 /* for each bucket */
72 if (MAX_VALUE < num.digit[i])
73 {
74 /* we have a carry */
75 carry = num.digit[i] / (MAX_VALUE + 1);
76 num.digit[i] = num.digit[i] % (MAX_VALUE + 1);
77 num.digit[i+1] = num.digit[i+1] + carry;
78 if ((i+1) == num.len)
79 {
80 num.len++;
81 }
82 } /* we have a carry */
83 } /* for each bucket */
84 DEBUG printf("len=%d\n", num.len);
85 DEBUG for (i=0; i<num.len; i++)
86 {
87 printf(" [%d] = %d\n", i, num.digit[i]);
88 }
89 } /* FUNCTION addDigit */
90
91 void convertNumToWord()
92 {
93 /* FUNCTION convertNumToWord */
94 int i;
95 char tmp;
96 int tot = 0;
97 int b = 0;
98
99 buff[b]='\0';
100 printf("len=%d\n", num.len);
101 for (i=0; i<num.len; i++)
102 {
103 printf(" [%d] = %d\n", i, num.digit[i]);
104 }
105 /* copy num to based -- because we will need num to print out */
106 for (i=0; i<num.len; i++)
107 {
108 /* for each bucket */
109 tot = tot * 10 + num.digit[i];
110 printf("tot = %d digit[%d] =%di buff=[%s]\n", tot, i, num.digit[i], buff);
111 if (BASE < tot)
112 {
113 /* pull off one letter and keep going */
114 buff[b++] = (tot / BASE) + 'a' - 1;
115 buff[b]='\0';
116 tot = tot % BASE;
117 } /* pull off one letter and keep going */
118 } /* for each bucket */
119 if (0 < tot)
120 {
121 /* one more left */
122 buff[b++] = tot + 'a' - 1;
123 } /* one more left */
124 buff[b] = '\0';
125 printf("b=%d buff=[%s]\n", b, buff);
126 } /* FUNCTION convertNumToWord */
127
128 void dumpNum()
129 {
130 /* FUNCTION dumpNum */
131 int i;
132
133 for (i=(num.len-1); 0<=i; i--)
134 {
135 /* for */
136 DEBUG printf("[%d] = %d\n", i, num.digit[i]);
137 printf("%1d", num.digit[i]);
138 if ((0 != i) && (0 == (i % 3)))
139 {
140 printf(",");
141 }
142 } /* for */
143 } /* FUNCTION dumpNum */
144
145 void dump()
146 {
147 /* FUNCTION dump */
148 } /* FUNCTION dump */
149
150 int getInput()
151 {
152 /* FUNCTION getInput */
153 int dataReadFlag;
154
155 fgets(buff, MAX_BUFFER_LENGTH, stdin);
156 if ('\n' == buff[strlen(buff)-1])
157 {
158 buff[strlen(buff)-1] = '\0'; /* take care of newline */
159 }
160 DEBUG printf("input=[%s]\n", buff);
161 dataReadFlag = ('*' != buff[0]);
162 return (dataReadFlag);
163 } /* FUNCTION getInput */
164
165 void processWord()
166 {
167 /* FUNCTION processWord */
168 int i;
169 int t;
170
171 for (i=0; i<strlen(buff); i++)
172 {
173 /* process each character */
174 t = buff[i] - 'a' + 1;
175 addDigit(t, BASE);
176 } /* process each character */
177 } /* FUNCTION processWord */
178
179 void processNumber()
180 {
181 /* FUNCTION processNumber */
182 int i;
183 int t;
184
185 /* translate input into num format */
186 for (i=0; i<strlen(buff); i++)
187 {
188 /* for */
189 t = buff[i] - '0';
190 addDigit(t, 10);
191 } /* for */
192 /* convert num format to array in base 26 */
193 convertNumToWord();
194 } /* FUNCTION processNumber */
195
196 void process()
197 {
198 /* FUNCTION process */
199 if (isdigit(buff[0]))
200 {
201 /* read in a number */
202 processNumber();
203 } /* read in a number */
204 else
205 {
206 /* read in a word */
207 processWord();
208 } /* read in a word */
209 printf("%-20s ", buff);
210 dumpNum();
211 printf("\n");
212 } /* FUNCTION process */
213
214 int main ()
215 {
216 /* main */
217 int moreToDo;
218
219 moreToDo = getInput();
220 while (moreToDo)
221 {
222 /* while */
223 init();
224 process();
225 moreToDo = getInput();
226 } /* while */
227
228 return EXIT_SUCCESS;
229 } /* main */
230
231