-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpolynomial.c
333 lines (281 loc) · 5.47 KB
/
polynomial.c
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
#include "polynomial.h"
typedef struct parser_ctx_t{
const char* str;
}parser_ctx_t;
polynomial create_poly()
{
polynomial head = (polynomial)malloc(sizeof( poly));
if (head != NULL)
head->next = NULL;
return head;
}
void delete_poly(polynomial p)
{
polynomial temp;
if (p!=NULL)
{
while(p->next != NULL)
{
temp = p;
p = p->next;
free(temp);
}
free(p);
}
p = NULL;
}
void display_polynomial(polynomial p)
{
if(p!=NULL)
p = p->next;
while(p!=NULL){
printf("%0.2f X^ %d||",p->coeff,p->exp);
p = p->next;
}
printf("\n");
}
void accept_space(parser_ctx_t* ctx)
{
while(isspace(*ctx->str))
ctx->str++;
}
int accept_staroperator(parser_ctx_t* ctx)
{
accept_space(ctx);
if(*ctx->str== '*' ){
ctx->str++;
return 1;
}
return 0;
}
int accept_sign(parser_ctx_t* ctx)
{
int sign;
sign =1;
accept_space(ctx);
if(*ctx->str == '-'){
sign = -1;
ctx->str++;
}else if(*ctx->str == '+'){
ctx->str++;
}
return sign;
}
double accept_number(parser_ctx_t* ctx)
{
double num;
char* end;
printf("\n%s\n",ctx->str);
num = strtod(ctx->str,&end);
if(ctx->str == end){
num= 1.0;
}
ctx->str = end;
return num;
}
double accept_coefficient(parser_ctx_t* ctx)
{
double c= 1.0;
int sign;
sign = accept_sign(ctx);
c = accept_number(ctx);
if(accept_staroperator(ctx))
c *= accept_coefficient(ctx);
return c*sign;
}
int accept_exponent(parser_ctx_t* ctx)
{
int e;
e =0;
accept_space(ctx);
accept_staroperator(ctx);
accept_space(ctx);
if(*ctx->str == 'x'){
e =1;
ctx->str++;
if(*ctx->str == '^'){
ctx->str++;
e = accept_number(ctx);
}
}
return e;
}
polynomial new_term(double c,int e)
{
polynomial poly;
poly = (polynomial)malloc(sizeof(poly));
if (poly == NULL)
error(__FILE__,__LINE__,__func__,"Failed to allocate poly.");
poly->coeff =c;
poly->exp =e;
poly->next =NULL;
return poly;
}
polynomial accept_term(parser_ctx_t* ctx)
{
polynomial term;
double c;
int e;
c =accept_coefficient(ctx);
e =accept_exponent(ctx);
if(accept_staroperator(ctx))
c *= accept_coefficient(ctx);
term = new_term(c,e);
return term;
}
int read_poly(polynomial p ,const char * s)
{
polynomial temp,prev,t;
parser_ctx_t ctx;
ctx.str= s;
temp= prev= t= NULL;
while(*ctx.str){
if((p->next) == NULL){//first node
(p->next) = accept_term(&ctx);
}else{
t = accept_term(&ctx);
temp = p->next;
prev = p;
while(temp!=NULL){//find position in the sorted list to insert new node t
if(temp->exp>t->exp){
prev = temp;
temp = temp->next;
}
else if (temp->exp<t->exp){
prev->next = t;
t->next = temp;
break;
}
else{
temp->coeff += t->coeff;
if(temp->coeff==0){//delete node if coeff is 0
prev->next = temp->next;
free(temp);
temp=p;
}
break;
}
}
if(temp==NULL){
prev->next = t;
}
t=NULL;
}
}
return 1;
}
polynomial add_poly(polynomial a ,polynomial b)
{
polynomial a_pos,b_pos,c_pos,c;
c = (polynomial)malloc(sizeof(poly));
if(c!=NULL)
c_pos = c;
else{
printf("\nError allocating m/m in add_poly()!");
return c;
}
if(a!=NULL)
a_pos = a->next;
else
a_pos = NULL;
if(b!=NULL)
b_pos = b->next;
else
b_pos = NULL;
while(a_pos!=NULL && b_pos!=NULL){
if((a_pos->exp)>(b_pos->exp)){
(c_pos->next) = new_term(a_pos->coeff,a_pos->exp);
a_pos = a_pos->next;
}
else if((b_pos->exp)>(a_pos->exp)){
(c_pos->next) = new_term(b_pos->coeff,b_pos->exp);
b_pos = b_pos->next;
}
else{
(c_pos->next) = new_term((a_pos->coeff)+(b_pos->coeff),a_pos->exp);
a_pos = a_pos->next;
b_pos = b_pos->next;
if((c_pos->next)->coeff==0){
free(c_pos->next);
c_pos->next = NULL;
continue;
}
}
c_pos = c_pos->next;
}
if(a_pos!=NULL)
while(a_pos!=NULL){
(c_pos->next) = new_term(a_pos->coeff,a_pos->exp);
a_pos = a_pos->next;
c_pos = c_pos->next;
}
else if (b_pos!=NULL)
while(b_pos!=NULL){
(c_pos->next) = new_term(b_pos->coeff,b_pos->exp);
b_pos = b_pos->next;
c_pos = c_pos->next;
}
return c;
}
polynomial subtract_poly(polynomial a,polynomial b)
{
polynomial b_copy,temp,b_copy_pos ;
b_copy = create_poly();
b_copy_pos = b_copy;
if(b!=NULL)
temp = b->next;
else
temp = NULL;
while(temp!=NULL){//reverse sign of coeffs in b and store in b_copy
b_copy_pos->next = new_term(-1.0*temp->coeff,temp->exp);
b_copy_pos = b_copy_pos->next;
temp = temp->next;
}
return add_poly(a,b_copy);
}
polynomial multiply_poly(polynomial a,polynomial b)
{
polynomial c=NULL,a_pos,b_pos,result=NULL,temp,temp_pos;
if(a==NULL || b==NULL)
return c;
c = create_poly();
temp = create_poly();
if(a->next==NULL || b->next == NULL) //zero polynomial
return c;
a_pos = a->next;
b_pos = b->next;
temp = create_poly();
temp_pos= temp;
while(a_pos!=NULL){
temp_pos->next = new_term(a_pos->coeff,a_pos->exp);
temp_pos = temp_pos->next;
a_pos = a_pos->next;
}
while(b_pos!=NULL){
a_pos = a->next;
temp_pos = temp->next;
while(a_pos!=NULL){
temp_pos->coeff = a_pos->coeff * b_pos->coeff;
temp_pos->exp = a_pos->exp + b_pos->exp;
temp_pos = temp_pos->next;
a_pos = a_pos->next;
}
result = add_poly(temp,result);
delete_poly(c);
c = result;
b_pos = b_pos->next;
}
return result;
}
double evaluate_poly(polynomial a,double val)
{
double result= 0.0;
if(a==NULL || a->next == NULL)
return result;
a = a->next;
while(a!=NULL){
result += a->coeff * pow(val,a->exp);
a = a->next;
}
return result;
}