/home/toolbox/public_html/solutions/4/424/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: 2015 02 11
18 * Purpose:
19 * Problem: 424
20 */
21
22 /*
23 * This template reads data until a terminating value is reached.
24 */
25
26 #define MAX_LINE 151
27
28 char buffer[MAX_LINE];
29 int tot[MAX_LINE];
30 int tmp[MAX_LINE];
31
32 void init()
33 {
34 /* FUNCTION init */
35 int i;
36
37 for (i=0; MAX_LINE>i; i++)
38 {
39 /* empty arrays */
40 tot[i] = 0;
41 } /* empty arrays */
42 } /* FUNCTION init */
43
44 void initTmp()
45 {
46 /* FUNCTION initTmp */
47 int i;
48
49 for (i=0; MAX_LINE>i; i++)
50 {
51 /* empty arrays */
52 tmp[i] = 0;
53 } /* empty arrays */
54 } /* FUNCTION initTmp */
55
56 void dump()
57 {
58 /* FUNCTION dump */
59 int i;
60 char digits[10] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9'};
61
62 DEBUG printf("Enter dump\n");
63 printf("dump: ");
64 for (i=MAX_LINE-1; -1<i; i--)
65 {
66 /* for */
67 DEBUG printf("%d %d\n", i, tmp[i]);
68 printf("%c", digits[tmp[i]]);
69 } /* for */
70 printf("\n");
71 DEBUG printf("Leave dump\n");
72 } /* FUNCTION dump */
73
74 void showAnswer()
75 {
76 /* FUNCTION showAnswer */
77 int i;
78 char digits[10] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9'};
79 int zeroFlag = TRUE;
80
81 DEBUG printf("enter showAnswer\n");
82 for (i=MAX_LINE-1; -1<i; i--)
83 {
84 /* for */
85 zeroFlag = zeroFlag && (0 == tot[i]);
86 DEBUG printf("%d %d\n", i, tot[i]);
87 if (! zeroFlag)
88 {
89 printf("%c", digits[tot[i]]);
90 }
91 } /* for */
92 if (zeroFlag)
93 {
94 printf("0");
95 }
96 printf("\n");
97 DEBUG printf("Leave showAnswer\n");
98 } /* FUNCTION showAnswer */
99
100 void add()
101 {
102 /* FUNCTION add */
103 int i;
104
105 for (i=0; MAX_LINE > i; i++)
106 {
107 /* for each digit */
108 DEBUG printf("adding: tot[%d] (%d) tmp{%d] (%d) = ", i, tot[i], i, tmp[i]);
109 tot[i] = tot[i] + tmp[i];
110 DEBUG printf("%d\n", tot[i]);
111 if (9 < tot[i])
112 {
113 /* we have a carry */
114 tot[i+1] = tot[i+1] + tot[i] / 10;
115 tot[i] = tot[i] % 10;
116 } /* we have a carry */
117 } /* for each digit */
118 } /* FUNCTION add */
119
120 int getInput()
121 {
122 /* FUNCTION getInput */
123 int dataReadFlag;
124 int i;
125 int top;
126
127 scanf(" %s ", buffer);
128 if (0 == strcmp("0", buffer))
129 {
130 /* end of file reached */
131 dataReadFlag = FALSE;
132 } /* end of file reached */
133 else
134 {
135 /* read in something */
136 initTmp();
137 top = strlen(buffer);
138 DEBUG printf("[%s]\n", buffer);
139 for (i=0; i<top; i++)
140 {
141 /* for each char */
142 tmp[i] = buffer[top-(i+1)] - '0';
143 } /* for each char */
144 } /* read in something */
145 return (dataReadFlag);
146 } /* FUNCTION getInput */
147
148 void process()
149 {
150 /* FUNCTION process */
151 DEBUG dump();
152 DEBUG showAnswer();
153 add();
154 DEBUG dump();
155 DEBUG showAnswer();
156 } /* FUNCTION process */
157
158 int main()
159 {
160 /* main */
161 int moreToDo;
162 int gotInput = FALSE;
163
164 init();
165 DEBUG dump();
166 DEBUG showAnswer();
167 moreToDo = getInput();
168 DEBUG dump();
169 while (moreToDo)
170 {
171 /* while */
172 gotInput = TRUE;
173 process();
174 moreToDo = getInput();
175 } /* while */
176 if (gotInput)
177 {
178 showAnswer();
179 }
180
181 return EXIT_SUCCESS;
182 } /* main */
183