-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpoly.c
580 lines (559 loc) · 13.3 KB
/
poly.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
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
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
#include <stdlib.h>
#include "poly.h"
#include<stdio.h>
#include<string.h>
//List of help functions
void init_list(poly *lst)
{
lst->head = lst->tail = NULL;
}
int isEmpty(poly *lst)
{
return lst->head == NULL;
}
poly *build_list()
{
poly *lst = (poly*)malloc(sizeof(poly));
if (!lst){
puts("Error by create new item!!");
system("Pause");
exit(1);
}
lst->head = lst->tail = NULL;
return lst;
}
void insertFirst(poly *lst, list_type value, list_type power )
{
//create a new link
node *link = (node*)malloc(sizeof(node));
link->coeff = value;
link->pow = power;
link->next = NULL;
link->prev = NULL;
if(isEmpty(lst))
{
//make head & tail pointing to link
lst->head = lst->tail = link;
}
else
{
//update first prev link
lst->head->prev = link;
//point it to old first link
link->next = lst->head;
//point first to new first link
lst->head = link;
}
}
void insert_after(poly *lst, list_type value, list_type power, int idx)
{
if (!isEmpty(lst))
{
int i;
//start from the first link
node* current = lst->head;
node* newLink = NULL;
//navigate through list
for (i=0; i<idx && current != NULL ;i++)
current = current->next;
if (current == NULL)//if we got to the null before we got to the right indx
printf("ERROR insert_after function, wrong index");
if (current == lst->tail)
{
insert_last(lst,value,power);
}
//create a link
newLink = (node*)malloc(sizeof(node));
newLink->coeff = value;
newLink->pow = power;
//assign new link pointers
newLink->prev = current;
newLink->next = current->next;
//chain current and current->next to new link
current->next->prev = newLink;
current->next = newLink;
}
}
void insert_last(poly *p3, list_type value, list_type power)
{
//create a new link
node *link = (node*)malloc(sizeof(node));
link->coeff = value;
link->pow=power;
link->next = NULL;
link->prev = NULL;
//link=last;
//we need to know if the list is empty
if(p3->head==NULL)
{
p3->head=p3->tail=link;
}
else
{
link->prev=p3->tail;
//link->next= NULL//already
p3->tail->next=link;
p3->tail=link;
}
}
//main functions
poly *sum_of(poly *p1, poly *p2)
{
poly *p3=build_list();
node *curr1=p1->head;
node *curr2=p2->head;
while((curr1) && (curr2))//until heads not NULL
{
if(curr1->pow==curr2->pow)
{
//if((curr1->coeff + curr2->coeff)!=NULL)
//{
insert_last(p3, curr1->coeff+curr2->coeff, curr1->pow);
curr1=curr1->next;
curr2=curr2->next;
//}
//else {
//curr1=curr1->next;//move on without action
//curr2=curr2->next;
//}
}
else if((curr1->pow) < (curr2->pow))
{
insert_last(p3, curr1->coeff, curr1->pow);
curr1=curr1->next;
}
else{//(curr1->pow > curr2->pow)
insert_last(p3,curr2->coeff, curr2->pow);
curr2->next;
}
}
//if(p3->tail->prev==NULL)//if the list is still empty
while(curr1)
{
insert_last(p3, curr1->coeff, curr1->pow);
curr1=curr1->next;
}
while(curr2)
{
insert_last(p3, curr2->coeff, curr2->pow);
curr2=curr2->next;
}
return p3;
}
poly *diff_of(poly *p1, poly *p2)
{
poly *p3=build_list();
node *curr1=p1->head;
node *curr2=p2->head;
while((curr1) && (curr2))//until heads not NULL
{
if(curr1->pow==curr2->pow)
{
//if((curr1->coeff + curr2->coeff)!=NULL)
//{
insert_last(p3, curr1->coeff-curr2->coeff, curr1->pow);
curr1=curr1->next;
curr2=curr2->next;
//}
//else {
//curr1=curr1->next;//move on without action
//curr2=curr2->next;
//}
}
else if((curr1->pow) < (curr2->pow))
{
insert_last(p3, curr1->coeff, curr1->pow);
curr1=curr1->next;
}
else{//(curr1->pow > curr2->pow)
insert_last(p3,-(curr2->coeff), curr2->pow);
curr2->next;
}
}
//if(p3->tail->prev==NULL)//if the list is still empty
while(curr1)
{
insert_last(p3, curr1->coeff, curr1->pow);
curr1=curr1->next;
}
while(curr2)
{
insert_last(p3, -(curr2->coeff), curr2->pow);
curr2=curr2->next;
}
return p3;
}
poly *multipl(poly *polynom, int z)
{
node *currm=NULL;
currm=polynom->head;
if(isEmpty(polynom))
{
printf("the list is empty, there's nothing to multiply");
return;
}
while(currm!=NULL)
{
currm->coeff*=z;
currm=currm->next;
}
return polynom;
}
void seder_godel(poly *polynom)
{
if(isEmpty)
{
printf("the list is empty");
return;
}
else
printf("\nthe seder godel is % d\n", polynom->tail->pow);
}
poly *new_polynom(list_type value, list_type power)
{
poly *newpol=build_list();
node *newmem;
newmem=(node*)malloc(sizeof(node));
if(!newmem)
{
printf("memory allocation failed");
return;
}
newpol->head=newpol->tail=newmem;
newmem->coeff=value;
newmem->pow=power;
newmem->next=NULL;
newmem->prev=NULL;
return newpol;
}
void order_pol(poly* polynom)
{
node* arr;
node tmp;
int size,i,d,c;
//1. to build an array of structs that will hold coeffs and powers
printf("\nHow many MEMBERS the Polynom will have?\n");
scanf("%d",&size);
printf("The polynom will have %d members\n",size);
arr=(node*)malloc(size*sizeof(node));
for(i=0; i<size; i++)
{
printf("enter the value of a COEFFICIENT no.%d\n",i+1);
scanf("%d",&arr[i].coeff);
printf("enter the value of POWER no.%d\n",i+1);
scanf("%d", &arr[i].pow);
printf("\n");
}
//finished building an array^
//2.sorting the array with insertion sort, goal: power ascending order
for(c=1; c<=size-1; c++)//n is the size
{
d=c;
while(d>0 && (arr[d].pow<arr[d-1].pow))
{
tmp=arr[d];//arr[d] is smaller, arr[d-1] is bigger
arr[d]=arr[d-1];
arr[d-1]=tmp;
d--;
}
}
//filling the linked list
for(i=0;i<size;i++)
insert_last(polynom, arr[i].coeff, arr[i].pow);
}
void order2_pol(poly* polynom)
{
char response;
node* temp;
node* curr=NULL;//search for index
int counter=0;
printf("\nenter the new polynom\n");
do
{
temp=(node*)malloc(sizeof(node));
temp->next=NULL;
temp->prev=NULL;
printf("enter the COEFFICIENT and the POWER of the polynom. \n");
scanf("%d %d", &temp->coeff,&temp->pow);
if(isEmpty(polynom))
{
curr=polynom->head=polynom->tail=temp;//insert the first element
//curr=polynom->head;
}
else{
if(temp->pow > polynom->tail->pow)//if bigger than the last pow
insert_last(polynom, temp->coeff, temp->pow);
else if(temp->pow == polynom->tail->pow)
polynom->tail->coeff+=temp->coeff;//dont insert but sum the coeffs of the same power
else if(temp->pow < polynom->tail->pow)//smaller than the last pow
{
//we need to find the indx of the node that is smaller or equal to the new node
while(curr->pow <= temp->pow)
{
curr=curr->next;
counter++;
}
if(curr->pow==temp->pow)
curr->coeff += temp->coeff;
else
{
if(counter==0)
insertFirst(polynom, temp->coeff, temp->pow);
else
insert_after(polynom, temp->coeff,temp->pow, counter);//else insert at the index
}
}
}
printf("\nDo you want to add another number? Say 'y' to continue, type enter to stop\n");
getchar();
response = getchar();
counter=0;
}
while(response == 'y');
//return;
}
void sortedInsert(poly *lst)
{
int count = 0, index = 0;
int ch = 'y';
list_type coeff, power;
printf("\n\n#Please enter value to build Equation:\n");
while ((ch == 'y') || (ch == 'Y'))
{
printf("Input [coefficient #%d]: ", index + 1);
scanf("%d", &coeff);
printf("Input [Power #%d]: ", index + 1);
scanf("%d", &power);
index++;
if (isEmpty(lst)) // empty list -> insert to first node.
{
insertFirst(lst, coeff, power);
}
else if (lst->head->prev == NULL) //when only one node exist in list.
{
if (power > lst->head->pow)
{
insert_last(lst, coeff, power);
}
else if (power < lst->head->pow)
{
insertFirst(lst, coeff, power);
}
else
{
lst->head->coeff += coeff;
}
}
else if (lst->head->next != NULL)
{
{
while (lst->head != NULL)
{
count++;
if ((power < lst->head->next->pow) && (power > lst->head->pow)) //when there are more then one node.
{
insert_after(lst, coeff, power, count);
break;
}
else if (power == lst->head->pow)
{
lst->head->coeff += coeff;
break;
}
else
lst->head->next;
}
}
}
printf("\nDo you want to Continue? \n");
printf("For Yes Enter ----->[Y]\n");
printf("For No Enter ------>[N]\n");
printf("-------------------> ");
ch = _getch();
printf("\n\n");
}
}
void insert_new(poly *polynom ,list_type coefficient, list_type power)
{
//adding a node in an ascending order
//if the list is empty add_first
//if node->pow > tail->pow; then add_last
//if node->pow== tail->pow; then sum_of them
//if node->pow < tail-> pow; then find the index of the smaller and insert after him. start from tail
}
void print_poly(poly *polynom)
{
//1.--power==0, show only coeff
//2.--no "+" before first element
//3.--if coeff equals zero then DONT print it
//4.--power 1, dont show the power
//5.coeff 1,print without coeff
node* curr2;
curr2=(node*)malloc(sizeof(node));
curr2->next=NULL;
curr2->prev=NULL;
curr2=polynom->head;
//curr2->coeff=polynom->head->coeff;
//curr2->pow=polynom->head->pow;
while(curr2!=NULL)
{
if((curr2->coeff==0) && (curr2->prev==NULL))//first and zero
printf("0");
else if(curr2->coeff==0)
printf("");
else//coeff!=0
{
if(curr2->prev==NULL)//if first node
{
if(curr2->coeff>0)//positive coeff
{
if(curr2->pow==0)
printf("%d",curr2->coeff);
else if(curr2->pow==1)
printf("%d*x",curr2->coeff);
else if(curr2->coeff==1)
printf("x^%d",curr2->pow);
else
printf("%d*x^%d",curr2->coeff, curr2->pow);
}
if(curr2->coeff<0)//negative coeff
{
if(curr2->pow==0)//print only coeff
printf("%d",curr2->coeff);
else if(curr2->pow==1)//print "coeff*x"
if(curr2->coeff==-1)
printf("-x");
else
printf("%d*x",curr2->coeff);
else if(curr2->coeff==-1)//coeff is -1
printf("-x^%d",curr2->pow);
else
printf(" %d*x^%d",curr2->coeff, curr2->pow);
}
}
else//--------------NOT FIRST
{
if(curr2->coeff>0)//pos coeff
{
if(curr2->pow==0)//print only coeff
printf("+ %d",curr2->coeff);
else if(curr2->pow==1)//print "coeff*x"
if(curr2->coeff==1)
printf(" + x ");
else printf(" + %d*x",curr2->coeff);
else
printf("+ %d*x^%d",curr2->coeff, curr2->pow);
}
if(curr2->coeff<0)//neg coeff
{
if(curr2->pow==0)//print only coeff
printf(" %d",curr2->coeff);
else if(curr2->pow==1)//print "coeff*x"
if(curr2->coeff==-1)
printf(" -x ");
else printf(" %d*x",curr2->coeff);
else
printf(" %d*x^%d",curr2->coeff, curr2->pow);
}
}
}
curr2=curr2->next;
}
}//while
void print_rever(poly *polynom)
{
//1.--power==0, show only coeff
//2.--no "+" before first element
//3.--if coeff equals zero then DONT print it
//4.--power 1, dont show the power
//5.coeff 1,print without coeff
node* curr2;
curr2=(node*)malloc(sizeof(node));
curr2->next=NULL;
curr2->prev=NULL;
curr2=polynom->tail;
//curr2->coeff=polynom->head->coeff;
//curr2->pow=polynom->head->pow;
while(curr2!=NULL)
{
if((curr2->coeff==0) && (curr2->next==NULL))//last node and zero
printf("0");
else if(curr2->coeff==0)
printf("");
else//coeff!=0
{
if(curr2->next==NULL)//if last node
{
if(curr2->coeff>0)//positive coeff
{
if(curr2->pow==0)
printf("%d",curr2->coeff);
else if(curr2->pow==1)
printf("%d*x",curr2->coeff);
else if(curr2->coeff==1)
printf("x^%d",curr2->pow);
else
printf("%d*x^%d",curr2->coeff, curr2->pow);
}
if(curr2->coeff<0)//negative coeff
{
if(curr2->pow==0)//print only coeff
printf("%d",curr2->coeff);
else if(curr2->pow==1)//print "coeff*x"
if(curr2->coeff==-1)
printf("-x");
else
printf("%d*x",curr2->coeff);
else if(curr2->coeff==-1)//coeff is -1
printf("-x^%d",curr2->pow);
else
printf(" %d*x^%d",curr2->coeff, curr2->pow);
}
}
else//--------------NOT FIRST
{
if(curr2->coeff>0)//pos coeff
{
if(curr2->pow==0)//print only coeff
printf("+ %d",curr2->coeff);
else if(curr2->pow==1)//print "coeff*x"
if(curr2->coeff==1)
printf(" + x ");
else printf(" + %d*x",curr2->coeff);
else
printf("+ %d*x^%d",curr2->coeff, curr2->pow);
}
if(curr2->coeff<0)//neg coeff
{
if(curr2->pow==0)//print only coeff
printf(" %d",curr2->coeff);
else if(curr2->pow==1)//print "coeff*x"
if(curr2->coeff==-1)
printf(" -x ");
else printf(" %d*x",curr2->coeff);
else
printf(" %d*x^%d",curr2->coeff, curr2->pow);
}
}
}
curr2=curr2->prev;
}
}//while
void main()
{
poly* p3;
poly* p1=build_list();
//poly* p2=build_list();
//poly* p3=build_list();
order2_pol(p1);
//sortedInsert(p1);
print_poly(p1);
printf("\n");
print_rever(p1);
/*order2_pol(p2);
print_poly(p2);
p3=sum_of(p1,p2);*/
/*printf("\nThe Sum of two polynoms is:\n");
print_poly(p3);*/
system("pause");
}