forked from harshitbansal373/hacktoberfest2020
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpolymuladd.cpp
More file actions
170 lines (132 loc) · 2.6 KB
/
polymuladd.cpp
File metadata and controls
170 lines (132 loc) · 2.6 KB
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
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
typedef struct term{
int coeff;
int expo;
};
typedef struct poly{
int no_of_terms;
term t[10];
};
poly * accept(poly* p){
printf("Enter the number of terms in the polynomial\n");
scanf("%d",&(p->no_of_terms));
printf("Start entering the polynomial \n");
for (int i=0;i<(p->no_of_terms);i++)
{
scanf("%d%d",&(p->t[i].coeff),&(p->t[i].expo));
}
return p;
}
void display(poly * p){
for (int i=0;i<(p->no_of_terms);i++)
{
printf("%dx^%d ",(p->t[i].coeff),(p->t[i].expo));
}
printf("\n");
}
poly * add (poly * p1 ,poly *p2)
{
int i=0, j=0, k=0;
poly *p3;
p3 = (poly*)malloc(sizeof(poly));
while(i<(p1->no_of_terms) && j<(p2->no_of_terms))
{
if(p1->t[i].expo==p2->t[j].expo)
{
p3->t[k].expo=p1->t[i].expo;
p3->t[k].coeff=p1->t[i].coeff + p2->t[j].coeff;
i++;
j++;
k++;
}
else if(p1->t[i].expo > p2->t[j].expo)
{
p3->t[k].expo=p1->t[i].expo;
p3->t[k].coeff=p1->t[i].coeff;
i++;
k++;
}
else if(p1->t[i].expo <p2->t[j].expo)
{
p3->t[k].expo=p2->t[i].expo;
p3->t[k].coeff=p2->t[i].coeff;
j++;
k++;
}
}
while(i<(p1->no_of_terms))
{
p3->t[k].expo=p1->t[i].expo;
p3->t[k].coeff=p1->t[i].coeff;
i++;
k++;
}
while(j<(p2->no_of_terms))
{
p3->t[k].expo=p2->t[j].expo;
p3->t[k].coeff=p2->t[j].coeff;
j++;
k++;
}
//printf("k=%d\n",k);
p3->no_of_terms=k;
//k=0;
return p3;
}
poly *multiply(poly * p1, poly *p2 )
{
poly * p3;
poly * p4;
poly * p5;
p3=(poly*)malloc(sizeof(poly));
p4=(poly*)malloc(sizeof(poly));
p5=(poly*)malloc(sizeof(poly));
p3->no_of_terms = 0;
p4->no_of_terms = 0;
p5->no_of_terms = 0;
for(int i=0;i<(p1->no_of_terms);i++)
{
int k=0;
for(int j=0;j<(p2->no_of_terms);j++)
{
p3->t[k].coeff=p1->t[i].coeff*p2->t[j].coeff;
p3->t[k].expo=p1->t[i].expo + p2->t[j].expo;
k++;
}
p3->no_of_terms=k;
p4=add(p3,p4);
}
return p4;
}
float evaluate(poly*p,float x)
{
float value=0;
for (int i=0;i<p->no_of_terms;i++)
{
value=value+(float(p->t[i].coeff)*pow(x,float(p->t[i].expo)));
}
return value;
}
int main()
{
poly *p1;
poly *AddResult;
poly *MulResult;
p1 = (poly*)malloc(sizeof(poly));
poly *p2;
p2 = (poly*)malloc(sizeof(poly));
printf("Enter the first polynomial\n");
accept(p1);
printf("Enter the second polynomial\n");
accept(p2);
display(p1);
display(p2);
AddResult=add(p1,p2);
display(AddResult);
MulResult=multiply(p1,p2);
display(MulResult);
float ans=evaluate(p1,2);
printf("%f\n",ans);
}