-
Notifications
You must be signed in to change notification settings - Fork 88
/
polynomial.cpp
379 lines (327 loc) · 9.01 KB
/
polynomial.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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
/* mbinary
#########################################################################
# File : polynomial.cpp
# Author: mbinary
# Mail: zhuheqin1@gmail.com
# Blog: https://mbinary.xyz
# Github: https://github.com/mbinary
# Created Time: 2018-05-19 23:07
# Description:
#########################################################################
*/
#include<cstdlib>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<malloc.h>
#include<map>
using namespace std;
#if defined(__linux__)
#define LINUX true
#elif defined(_WIN32)
#define LINUX false
#endif
bool isZero(double a)
{
if ((a < 0.00001) && -a < 0.00001)
return true;
return false;
}
class node
{
friend class polynomial;
double coefficient;
double index;
};
class polynomial
{
int SIZE;
int n;
node* p;
public:
polynomial(int sz = 50);
polynomial(const polynomial &);
~polynomial();
double cal(double);
void getData();
void display();
polynomial operator=(const polynomial &);
polynomial operator+(const polynomial &);
polynomial operator-(const polynomial &);
polynomial operator*(const polynomial &);
};
polynomial::polynomial(int sz): n(0), SIZE(sz)
{
p = (node*) new node[SIZE];
memset(p, 0, sizeof(p));
}
polynomial::~polynomial()
{
delete p;
}
double polynomial::cal(double x)
{
double rst = 0;
for (int i = 0; i < n; ++i) {
rst += pow(x, p[i].index) * p[i].coefficient;
}
return rst;
}
polynomial::polynomial(const polynomial &a)
{
p = (node*) new node[50];
memset(p, 0, sizeof(p));
n = a.n;
for (int i = 0; i < a.n; ++i) {
p[i].index = a.p[i].index;
p[i].coefficient = a.p[i].coefficient;
}
}
polynomial polynomial::operator=(const polynomial& a)
{
n = a.n;
for (int i = 0; i < a.n; ++i) {
p[i].index = a.p[i].index;
p[i].coefficient = a.p[i].coefficient;
}
return *this;
}
void polynomial::display()
{
node * tmp = p;
if (n == 0) {
printf("0\n");
return;
}
// char *fmt = ("x"); printf(fmt,...);
for (int i = n - 1; i >= 0; --i) {
double t = tmp[i].coefficient;
double idx = tmp[i].index;
if (isZero(idx)) {
printf("%+g", t);
continue;
}
if (isZero(t - 1)) printf("+");
else if (isZero(t + 1))printf("-");
else printf("%+g", t);
printf("x");
if (!isZero(idx - 1)) printf("^%g", idx);
}
printf("\n");
}
void polynomial::getData()
{
printf("Please input data . \n");
printf("For every item,Coefficient first .Use space to separate,EOF to end\n");
map<double, double> mp;
double idx;
double coef;
while (scanf("%lf%lf", &coef, &idx) != EOF) {
if (isZero(coef)) continue;
if (mp.count(idx) == 0) {
mp[idx] = coef;
} else {
mp[idx] += coef;
if (isZero(mp[idx])) {
mp.erase(idx);
}
}
}
if (mp.size() > SIZE) {
SIZE *= 2;
p = (node*)realloc(p, sizeof(node) * SIZE) ;
}
for (map<double, double>::iterator it = mp.begin(); it != mp.end(); ++it) {
p[n].index = it->first;
p[n++].coefficient = it->second;
}
}
polynomial polynomial::operator+(const polynomial & a)
{
polynomial rst ;
int p1 = 0, p2 = 0, p3 = 0;
double exp1 = p[p1].index;
double exp2 = a.p[p2].index;
while (p1 < n && p2 < a.n) {
while (p1 < n && exp1 < exp2) {
rst.p[p3].index = exp1;
rst.p[p3].coefficient = p[p1].coefficient;
++p1, ++p3;
exp1 = p[p1].index;;
}
while (p2 < a.n && exp1 > exp2) {
rst.p[p3].index = exp2;
rst.p[p3].coefficient = a.p[p2].coefficient;
++p2, ++p3;
exp2 = a.p[p2].index;;
}
if (isZero(exp1 - exp2)) {
double tmp = p[p1].coefficient + a.p[p2].coefficient;
if (isZero(tmp)) {
++p1, ++p2;
} else {
rst.p[p3].index = p[p1].index;
rst.p[p3].coefficient = tmp;
++p1, ++p2, ++p3;
}
}
}
if (p1 == n) {
while (p2 < a.n) {
rst.p[p3].index = a.p[p2].index;
rst.p[p3].coefficient = a.p[p2].coefficient;
++p2, ++p3;
}
} else {
while (p1 < n) {
rst.p[p3].index = p[p1].index;
rst.p[p3].coefficient = p[p1].coefficient;
++p1, ++p3;
}
}
rst.n = p3;
return rst;
}
polynomial polynomial::operator-(const polynomial & a)
{
polynomial rst(a) ;
int i = 0;
while (i < rst.n) {
rst.p[i].coefficient = -rst.p[i].coefficient;
++i;
}
return (*this + rst);
}
polynomial polynomial::operator*(const polynomial & a)
{
map<double, double> mp;
for (int i = 0; i < n; ++i) {
double idx = p[i].index;
double coef = p[i].coefficient;
for (int j = 0; j < a.n; ++j) {
double index = idx + a.p[j].index;
if (mp.count(index) == 0) {
mp[index] = coef * a.p[j].coefficient;
} else {
mp[index] += coef * a.p[j].coefficient;
if (isZero(mp[index])) {
mp.erase(index);
}
}
}
}
int sz = 50;
while (mp.size() > sz) {
sz *= 2;
}
polynomial rst(sz);
for (map<double, double>::iterator it = mp.begin(); it != mp.end(); ++it) {
rst.p[rst.n].index = it->first;
rst.p[rst.n++].coefficient = it->second;
}
return rst;
}
int num = 0;
polynomial pl[30];
void menu()
{
printf("**********OPERATIONS***********\n");
printf("*****0. create *****\n");
printf("*****1. add + *****\n");
printf("*****2. sub - *****\n");
printf("*****3. mul * *****\n");
printf("*****4. display *****\n");
printf("*****5. menu *****\n");
printf("*****6. clear screen *****\n");
printf("*****7. exit *****\n");
printf("*****8. copy *****\n");
printf("*****9. display all *****\n");
printf("*****10. cal val *****\n");
printf("**********OPERATIONS***********\n");
}
void loop()
{
int op;
while (scanf("%d", &op) != EOF) {
if (op == 0) {
pl[num].getData();
++num;
printf("You've created polynomial %d:\n", num);
pl[num - 1].display();
} else if (op == 1 || op == 2 || op == 3) {
if (num < 2) {
printf("Oops! you've got less two polynomial\nPlease choose another operation\n");
continue;
}
printf("input two nums of the two polynomial to be operated.eg: 1 2\n");
int t1 = 100, t2 = 100;
while (1) {
scanf("%d%d", &t1, &t2);
if (t1 > num || t2 > num || t1 < 0 || t2 < 0) {
printf("wrong num ,please input again\n");
} else break;
}
printf("the rst is:\n");
t1 -= 1, t2 -= 1;
if (op == 1) {
(pl[t1] + pl[t2]).display();
} else if (op == 2) {
(pl[t1] - pl[t2]).display();
} else (pl[t1]*pl[t2]).display();
} else if (op == 4) {
printf("input a polynomial's num to display it\n");
int tmp;
scanf("%d", &tmp);
if (tmp > num) {
printf("wrong num");
} else {
printf("info of polynomial %d\n", tmp);
pl[tmp - 1].display();
}
} else if (op == 9) {
for (int i = 0; i < num; ++i) {
printf("polynomial %d : ", i + 1);
pl[i].display();
}
} else if (op == 5) {
menu();
} else if (op == 6) {
if (LINUX) system("clear");
else system("cls");
menu();
} else if (op == 10) {
double x;
int t;
printf("choose a polynomial\n");
scanf("%d", &t);
if (t > num || t < 0) {
printf("wrong num\n");
} else {
printf("input a value\n");
scanf("%lf", &x);
pl[t - 1].display();
printf("%g\n", pl[t - 1].cal(x));
}
} else if (op == 8) {
if (num == 0) {
printf("you have'nt any polynomial tp copy\n");
continue;
}
int n = num + 1;
while (n > num) {
printf("input the number of an existing polynomial you want to copy\n");
scanf("%d", &n);
}
(pl[num] = pl[n - 1]);
printf("You've copyed this polynomial:\n");
pl[num++].display();
} else exit(0);
printf("select an operation\n");
}
}
int main(void)
{
menu();
loop();
return 0;
}