forked from laviii123/Btecky
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Nupur (ADD POLYNOMIAL)
110 lines (105 loc) · 1.6 KB
/
Nupur (ADD POLYNOMIAL)
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
#include<stdio.h>
#include<stdlib.h>
typedef struct node
{
int coef,pow;
struct node *next;
} node;
node *getnode()
{
node *p;
p=(node*)malloc(sizeof(node));
return p;
}
void CreatePoly(node **list)
{
int coef,pow;
node *p,*q;
do
{
printf("\nEnter the coef and pow(pow=0 to end)\n");
scanf("%d%d",&coef,&pow);
p=getnode();
p->coef=coef;
p->pow=pow;
p->next=NULL;
if(*list==NULL)
{
*list=p;
q=p;
}
else
{
q->next=p;
q=p;
}
} while(pow!=0);
}
void display(node *poly)
{
node *p;
for(p=poly;p!=NULL;p=p->next)
{
printf("%dx^%d ",p->coef,p->pow);
if(p->pow!=0)
printf("+ ");
}
}
node *AddPoly(node *poly1, node *poly2)
{
node *p, *q, *poly3;
poly3=NULL;
while(poly1!=NULL && poly2!=NULL)
{
p=getnode();
p->next=NULL;
if(poly1->pow > poly2->pow)
{
p->coef=poly1->coef;
p->pow=poly1->pow;
poly1=poly1->next;
}
else if(poly2->pow > poly1->pow)
{
p->coef=poly2->coef;
p->pow=poly2->pow;
poly2=poly2->next;
}
else
{
p->coef=poly1->coef + poly2->coef;
p->pow=poly1->pow;
poly1=poly1->next;
poly2=poly2->next;
}
if(poly3==NULL)
{
poly3=p;
q=p;
}
else
{
q->next=p;
q=p;
}
}
if(poly1!=NULL)
q->next=poly1;
if(poly2!=NULL)
q->next=poly2;
return poly3;
}
void main()
{
node *poly1, *poly2, *poly3;
poly1=poly2=poly3=NULL;
CreatePoly(&poly1);
printf("Polynomial 1:\n");
display(poly1);
CreatePoly(&poly2);
printf("Polynomial 2:\n");
display(poly2);
poly3=AddPoly(poly1,poly2);
printf("\nPolynomial 3:\n");
display(poly3);
}