forked from rylp/DSA
-
Notifications
You must be signed in to change notification settings - Fork 0
/
3.poly
166 lines (126 loc) · 2.23 KB
/
3.poly
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
#include <iostream>
#include<cmath>
using namespace std;
class poly
{
public:
int* a;
int size;
public:
int eval(int val);
void add(poly *p1,poly *p2,int m,int n);
friend ostream& operator<<(ostream& out, poly& p);
friend istream& operator>>(istream& in, poly& p);
void mul(poly* p1, poly* p2, int m, int n);
};
int poly::eval(int val)
{
int result=0;
for (int i = size; i >= 0; i--)
{
result += a[i] * pow(val, i);
}
return result;
}
istream& operator>>(istream& in, poly& p)
{
for (int i = p.size; i >= 0; i--)
{
cout << "Enter coeff for deg " << i << endl;
in >> p.a[i];
}
return in;
}
ostream& operator<<(ostream& out, poly& p)
{
cout << "Display..." << endl;
for (int i = p.size; i >= 0; i--)
{
if (i == 0)
cout <<"("<<p.a[i]<<")" ;
else
cout <<"("<< p.a[i] << "x^" << i << ") + ";
}
return out;
}
void poly::add(poly *p1,poly *p2,int m,int n)
{
int i;
for (i = 0 ; i <= m && i<=n; i++)
{
a[i] = p1->a[i] + p2->a[i];
}
if (i == n + 1)
{
for (int j = n + 1; j <= m; j++)
{
a[j] = p1->a[j];
}
}
else
{
for (int j = m + 1; j <= n; j++)
{
a[j] = p2->a[j];
}
}
}
void poly::mul(poly* p1, poly* p2, int m, int n)
{
for (int i = 0; i <= size; i++)
{
a[i] = 0;
}
for (int i = 0; i <= m; i++)
{
for (int j = 0; j <= n; j++)
{
a[i+j] += (p1->a[i] * p2->a[j]);
}
}
}
int main()
{
poly p1;
poly p2;
poly p3;
poly p4;
int val;
int m;
cout << "Enter highest degree of poly" << endl;
cin >> m;
p1.size = m;
p1.a = new int[m];
cout << "Enter elements of Polynomial" << endl;
cin >> p1;
cout << "Polynomial:" << endl;
cout << p1<<endl;
cout << "Enter value" << endl;
cin >> val;
int ans=p1.eval(val);
cout << "Result: " << ans << endl;
cout << "Addition" << endl;
int n;
cout << "Enter highest degree of poly" << endl;
cin >> n;
p2.size = n;
p2.a = new int[n];
cout << "Enter elements of Polynomial" << endl;
cin >> p2;
cout << "Polynomial:" << endl;
cout << p2<<endl;
cout << "Addition: " << endl;
if (m > n)
p3.size = m;
else
p3.size = n;
p3.a = new int[p3.size];
p3.add(&p1,&p2,m,n);
cout << p3<<endl;
cout << "Multiplication" << endl;
p4.size = m + n;
p4.a = new int[m + n];
p4.mul(&p1,&p2,m,n);
cout << p4;
return 0;
}