-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathPolynomialFunctionsusingoperatoroverload.cpp
201 lines (184 loc) · 4.23 KB
/
PolynomialFunctionsusingoperatoroverload.cpp
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
#include<bits/stdc++.h>
using namespace std;
class polynomial
{
int sizeterms;
int *coeff;
int *exp;
public:
polynomial(int sizeterms)
{
this->sizeterms = sizeterms;
coeff= new int[sizeterms];
exp =new int[sizeterms];
}
polynomial()
{
cout<<"\nEnter number of terms";
cin>>sizeterms;
coeff= new int[sizeterms];
exp =new int[sizeterms];
}
int getsize()
{
return sizeterms;
}
friend istream &operator >> (istream &IN, polynomial &T)
{
int j;
for(j=0;j<T.sizeterms;j++)
{
cout<<"\nEnter "<<j+1<<"th term exponent";
IN>>T.exp[j];
cout<<"\nEnter "<<j+1<<"th term coefficient";
IN>>T.coeff[j];
}
return IN;
}
friend ostream &operator << (ostream &OUT, polynomial &t)
{
int i;
cout<<"\nPOLYNOMIAL :";
int j;
for(j=0;j<t.sizeterms;j++)
{
if(t.coeff[j]!=0)
{(t.coeff[j]>0&&j!=0)?cout<<"+":cout<<"";cout<<t.coeff[j]<<"x^"<<t.exp[j];}
}
return OUT;
}
polynomial operator +(polynomial T)
{
int maxv= sizeterms + T.getsize();
/*if(sizeterms>=T.getsize())
{
maxv = sizeterms;
}
else
{
maxv = T.getsize();
}*/
polynomial R(maxv);
int p = 0, q = 0, k = 0;
while(p<sizeterms&&q<T.getsize())
{
if(exp[p]>T.exp[q])
{
R.coeff[k] = coeff[p];
R.exp[k] = exp[p];
p++;
k++;
}
else if(exp[p]<T.exp[q])
{
R.coeff[k] = T.coeff[q];
R.exp[k] = T.exp[q];
q++;
k++;
}
else if(exp[p]==T.exp[q])
{
R.exp[k] = exp[p];
R.coeff[k] = coeff[p] + T.coeff[q];
q++;
p++;
k++;
}
}
if(p==sizeterms)
{
for(int j = q;j<T.getsize();j++)
{
R.exp[k] = T.exp[j];
R.coeff[k] = T.coeff[j];
k++;
}
}
else if(q==T.getsize())
{
for(int j = p;j<sizeterms;j++)
{
R.exp[k] = exp[j];
R.coeff[k] = coeff[j];
k++;
}
}
return R;
}
polynomial operator *(polynomial T)
{
polynomial R(sizeterms * T.sizeterms);
int i, j;
static int k = 0;
for(i=0;i<sizeterms;i++)
{
for(j=0;j<T.sizeterms;j++)
{
R.coeff[k] = coeff[i]*T.coeff[j];
R.exp[k] = exp[i]+T.exp[j];
k++;
}
}
for(i=0;i<R.getsize();i++)
{
for(j=i+1;j<R.getsize();j++)
{
if(R.exp[i]==R.exp[j]&&R.coeff[i]!=0)
{
R.coeff[i] += R.coeff[j];
R.coeff[j] = 0;
}
}
}
return R;
}
void evaluate()
{
int x;
int sum=0; //MADE BY: ARJUN DASHRATH, 3210
cout<<"\nEnter the value of x";
cin>>x;
int j;
for(j=0;j<sizeterms;j++)
{
sum += coeff[j]*(pow(x,exp[j]));
}
for(j=0;j<sizeterms;j++)
{
(coeff[j]>0&&j!=0)?cout<<"+":cout<<"";cout<<coeff[j]<<"x^"<<exp[j];
}
cout<<" for x = "<<x<<" is "<<sum;
}
};
int main()
{
polynomial obj , obj1;
char ch = 'y';
while(ch == 'y'){
cout<<"\n############################# MENU ##############################################\n";
int choice;
cout<<"1. CREATE/INPUT POLYNOMIALS\n";
cout<<"2. DISPLAY POLYNOMIALS\n";
cout<<"3. ADD POLYNOMIALS\n";
cout<<"4. MULTIPLY POLYNOMIALS\n";
cout<<"5. EVALUATE POLYNOMIALS\n";
cout<<"\nEnter choice:";
cin>>choice;
switch(choice)
{
case 1: {cin>>obj>>obj1;}
break;
case 2: {cout<<obj<<"\n"<<obj1;}
break;
case 3: {polynomial res = obj + obj1;cout<<res;}
break;
case 4: {polynomial mulres = obj * obj1;cout<<mulres;}
break;
case 5: {obj.evaluate();cout<<"\n";obj1.evaluate();}
break;
default: {cout<<"Invalid option";}
}
cout<<"\n Enter again?(y/n)";
cin>>ch;
}
}