-
Notifications
You must be signed in to change notification settings - Fork 1
/
polynomial.c
133 lines (129 loc) · 3.44 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
#include<stdio.h>
#include<malloc.h>
struct node
{
int coef;
int exp;
struct node *link;
}*new,*A=NULL,*B=NULL,*E=NULL,*temp;
void main()
{
int c;
int m,n,i,k,d;
struct node *t1,*t2,*t3;
printf("enter the nof terms of A \n");
scanf("%d",&m);
for(i=0;i<m;i++)
{
printf("enter the coefficents & exponents \n");
scanf("%d %d",&c,&d);
new=(struct node *)malloc(sizeof(struct node));
new->coef=c;
new->exp=d;
new->link=NULL;
if(A==NULL)
{
A=new;
temp=A;
}
else
{
temp->link=new;
temp=new;
}
}
printf("enter the no:- of terms of B \n");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("enter the coefficents & exponents \n");
scanf("%d %d",&c,&d);
new=(struct node *)malloc(sizeof(struct node));
new->coef=c;
new->exp=d;
new->link=NULL;
if(B==NULL)
{
B=new;
temp=B;
}
else
{
temp->link=new;
temp=new;
}
}
t1=A;
t2=B;
while(t1!=NULL&&t2!=NULL)
{
if(t1->exp==t2->exp)
{
k=t1->coef+t2->coef;
new=(struct node *)malloc(sizeof(struct node));
new->coef=k;
new->exp=t1->exp;;
new->link=NULL;
t1=t1->link;
t2=t2->link;
}
else if(t1->exp>t2->exp)
{
new=(struct node *)malloc(sizeof(struct node));
new->coef=t1->coef;
new->exp=t1->exp;
t1=t1->link;
}
else
{
new=(struct node *)malloc(sizeof(struct node));
new->coef=t2->coef;
t2=t2->link;
}
if(E==NULL)
{
E=new;
temp=E;
}
else
{
temp->link=new;
temp=new;
}
}
while(t1!=NULL)
{
t3->link=t1;
}
while(t2!=NULL)
{
t3->link=t2;
}
printf("Polynomial A \n");
temp=A;
while(temp!=NULL)
{
printf("%d(x^%d)+",temp->coef,temp->exp);
temp = temp->link;
}
printf("0");
printf("\n");
printf("Polynomial B \n");
temp=B;
while(temp!=NULL)
{
printf("%d(x^%d)+",temp->coef,temp->exp);
temp = temp->link;
}
printf("0");
printf("\n");
printf("Polynomial E \n");
temp=E;
while(temp!=NULL)
{
printf("%d(x^%d)+",temp->coef,temp->exp);
temp = temp->link;
}
printf("0");
printf("\n");
}