-
Notifications
You must be signed in to change notification settings - Fork 0
/
uva-4211.cpp
303 lines (240 loc) · 6.25 KB
/
uva-4211.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
#include <iostream>
#include <vector>
#include <cmath>
#include <cstdio>
#include <cstring>
#define EXP_LEN 50
#define MAX_GRAU 20
#define MAX(a,b) ((a) > (b) ? (a) : (b))
#define MIN(a,b) ((a) < (b) ? (a) : (b))
#define C2D(c) ((c) - '0')
#define INF 0x3f3f3f3
using namespace std;
int max_number;
void imprimePolinomio(int *polinomio, int grau)
{
int i;
printf ("[%d] ", grau);
for (i = 0; i <= grau; i++)
printf ("%d ", polinomio[i]);
printf ("\n");
}
int somaPolinomios (int *p1, int g1, int *p2, int g2, int *p3)
{
int i;
int grau_result = MAX(g1, g2);
int min = MIN(g1, g2);
memset (p3, 0, MAX_GRAU * sizeof(int));
for (i = 0; i <= min; i++) p3[i] = p1[i] + p2[i];
if (g1 > g2) for (; i <= g1; i++) p3[i] = p1[i];
else for (; i <= g2; i++) p3[i] = p2[i];
#ifdef DEBUG
printf (">>> Somando polinomios\n");
printf ("==========================================\n");
imprimePolinomio(p1, g1);
imprimePolinomio(p2, g2);
printf ("------------------------------------------\n");
imprimePolinomio(p3, grau_result);
printf ("==========================================\n");
#endif
return grau_result;
}
int subtraiPolinomios (int *p1, int g1, int *p2, int g2, int *p3)
{
int i;
int grau_result = MAX(g1, g2);
int min = MIN(g1, g2);
memset (p3, 0, MAX_GRAU * sizeof(int));
for (i = 0; i <= min; i++) p3[i] = p1[i] - p2[i];
if (g1 > g2) for (; i <= g1; i++) p3[i] = p1[i];
else for (; i <= g2; i++) p3[i] = -p2[i];
while ((p3[grau_result] == 0) && grau_result) grau_result--;
#ifdef DEBUG
printf (">>> Subtraindo polinomios\n");
printf ("==========================================\n");
imprimePolinomio(p1, g1);
imprimePolinomio(p2, g2);
printf ("------------------------------------------\n");
imprimePolinomio(p3, grau_result);
printf ("==========================================\n");
#endif
return grau_result;
}
int multiplicaPolinomios (int *p1, int g1, int *p2, int g2, int *p3)
{
int i, j;
int grau_result = g1 + g2;
memset (p3, 0, MAX_GRAU * sizeof(int));
for (i = 0; i <= g1; i++)
for (j = 0; j <= g2; j++)
p3[i+j] += p1[i]*p2[j];
#ifdef DEBUG
printf (">>> multiplicando polinomios\n");
printf ("==========================================\n");
imprimePolinomio(p1, g1);
imprimePolinomio(p2, g2);
printf ("------------------------------------------\n");
imprimePolinomio(p3, grau_result);
printf ("==========================================\n");
#endif
return grau_result;
}
int adicionaPolinomioBase(int *polinomio, int n)
{
char s[EXP_LEN];
int i, grau = 0;
memset (polinomio, 0, MAX_GRAU * sizeof(int));
sprintf (s, "%d", n);
grau = strlen(s) - 1;
for (i = 0; i <= grau; i++)
{
polinomio[grau - i] += C2D(s[i]);
max_number = MAX(polinomio[grau - i], max_number);
}
#ifdef DEBUG
printf ("Adiciona [%d] ao polinomio em grau %d\n", n, grau);
imprimePolinomio(polinomio, grau);
#endif
return grau;
}
int reduzPolinomio(int *polinomio, int grau)
{
int i, reducao = 0;
for (i = 0; (i <= grau) && (polinomio[i] == 0); i++);
if (i)
{
reducao = i;
while (i <= grau)
{
polinomio[i - reducao] = polinomio[i];
i++;
}
}
return grau-reducao;
}
int criaPolinomio (char *expression, int *polinomio)
{
int number, len;
char oper, *ptr;
int poli_tmp1[MAX_GRAU], poli_tmp2[MAX_GRAU], poli_tmp3[MAX_GRAU];
int grau1, grau2, grau3;
memset (poli_tmp1, 0, MAX_GRAU * sizeof(int));
memset (poli_tmp2, 0, MAX_GRAU * sizeof(int));
memset (polinomio, 0, MAX_GRAU * sizeof(int));
#ifdef DEBUG
printf ("Criando polinomio de [%s]\n", expression);
#endif
ptr = expression;
oper = '\0';
if ((sscanf(ptr, "%d%c%n", &number, &oper, &len) != -1) && oper)
{
#ifdef DEBUG
printf ("%s -> [%d] [%c] \n", ptr, number, oper);
#endif
grau1 = adicionaPolinomioBase(poli_tmp1, number);
ptr += len;
nunca_pensei_que_fosse_utilizar_isso_1:
if (oper == '+')
{
#ifdef DEBUG
printf ("\nSOMA\n");
#endif
grau2 = criaPolinomio (ptr, poli_tmp2);
grau3 = somaPolinomios(poli_tmp1, grau1, poli_tmp2, grau2, polinomio);
return grau3;
}
else if (oper == '=')
{
#ifdef DEBUG
printf ("\nSUBTRACAO\n");
#endif
grau2 = criaPolinomio (ptr, poli_tmp2);
grau3 = subtraiPolinomios(poli_tmp1, grau1, poli_tmp2, grau2, polinomio);
return grau3;
}
else if (oper == '*')
{
#ifdef DEBUG
printf ("\nMULTIPLICACAO\n");
#endif
do
{
#ifdef DEBUG
printf ("Expressao = [%s]\n", ptr);
#endif
oper = '\0';
sscanf(ptr, "%d%c%n", &number, &oper, &len);
if (!oper)
{
sscanf(ptr, "%d%n", &number, &len);
grau2 = adicionaPolinomioBase (poli_tmp2, number);
return multiplicaPolinomios(poli_tmp1, grau1, poli_tmp2, grau2, polinomio);
}
#ifdef DEBUG
printf ("*** %s -> [%d] [%d] \n", ptr, number, oper);
#endif
grau2 = adicionaPolinomioBase(poli_tmp2, number);
grau3 = multiplicaPolinomios(poli_tmp1, grau1, poli_tmp2, grau2, poli_tmp3);
memcpy(poli_tmp1, poli_tmp3, sizeof(int)*(grau3+1));
grau1 = grau3;
#ifdef DEBUG
imprimePolinomio(poli_tmp1, grau1);
imprimePolinomio(poli_tmp3, grau3);
#endif
ptr += len;
} while (oper == '*');
#ifdef DEBUG
printf ("diferente de '*'\n");
#endif
goto nunca_pensei_que_fosse_utilizar_isso_1;
}
}
nunca_pensei_que_fosse_utilizar_isso_2:
if (sscanf(ptr, "%d%n", &number, &len) != -1)
{
#ifdef DEBUG
printf ("** so tem um numero [%d]\n", number);
#endif
memset (polinomio, 0, MAX_GRAU * sizeof(int));
return adicionaPolinomioBase (polinomio, number);
}
}
double resolvePolinomio(int *polinomio, int grau, double x)
{
int i;
double result = 0;
for (i = 0; i <= grau; i++)
{
result += polinomio[i]*pow(x, i);
}
return result;
}
int main(void)
{
char expression[EXP_LEN];
int polinomio[MAX_GRAU], tmp, grau = 0;
int i;
while (1)
{
scanf ("%s", expression);
if (!strcmp(expression, "=")) break;
printf ("expression = [%s]\n", expression);
memset(polinomio, 0, MAX_GRAU * sizeof(int));
max_number = 0;
grau = criaPolinomio (expression, polinomio);
imprimePolinomio(polinomio, grau);
if (grau == 0)
{
if (polinomio[0])
printf ("*\n");
else
printf ("%d+\n", max_number ? max_number+1 : 2);
}
else
{
}
for (i = 2; i <= 15; i++)
printf ("%d -> %llf\n", i, resolvePolinomio(polinomio, grau, (double)i));
printf ("\n\n\n");
}
}