Hey guys, I'm mostly done with this but need a little help with multiplication and the output. (Just outputting +'s)
#include<iostream>
using namespace std;
class Polynomial
{
friend ostream &operator<<(ostream&, Polynomial&);
private:
int degree;
int coefficient[10];
public:
Polynomial(int, int[]);
Polynomial operator+(Polynomial&);
Polynomial operator-(Polynomial&);
Polynomial operator*(Polynomial);
};
Polynomial::Polynomial(int d, int c[])
{
for(int x=0;x<10;x++)
{
coefficient[x]=0;
}
degree = d;
for(int x=d;x>=0;x--)
{
coefficient[x]=c[d-x];
}
}
Polynomial Polynomial::operator+(Polynomial &right)
{
int newdegree = right.degree;
int sum[10]={0};
for(int x= 0;x<=newdegree;x++)
{
sum[newdegree-x] = coefficient[x] + right.coefficient[x];
}
return Polynomial(newdegree, sum);
}
Polynomial Polynomial::operator-(Polynomial &right)
{
int newdegree = right.degree;
int sum[10]={0};
for(int x=0; x<=newdegree;x++)
{
sum[newdegree-x] = right.coefficient[x] - coefficient[x];
}
return Polynomial(newdegree, sum);
}
Polynomial Polynomial::operator*(Polynomial right)
{
int newdegree = right.degree;
int sum[10]={0};
for(int x= 0;x<=newdegree;x++)
{
sum[newdegree-x] = coefficient[x] * right.coefficient[x];
}
return Polynomial(newdegree,sum);
}
ostream &operator<<(ostream &output, Polynomial &p)
{
for(int n=p.degree;n>=0;n--)
{
if(p.coefficient[n]>0)
{
output << p.coefficient[n];
if(n > 0 && n != 1)
{
output << "x^" << n;
}
else if(n==1)
{
output << "x";
}
if(p.coefficient[n-1] > 0 && n != 0)
{
if(n > 0)
{
output << "+";
}
}
}
if(p.coefficient[n]<0)
{
output << p.coefficient[n];
if(n > 0 && n != 1)
{
output << "x^" << n;
}
else if(n==1)
{
output << "x";
}
}
}
return output;
}
int main()
{
int test1[] = {1, 2, 3, 4};
Polynomial f(3, test1);
cout << f << endl; // OUTPUT: 1x^3 + 2x^2 + 3x + 4
int test2[] = {1, 0, 0, 0, 0, 0, 0, 0, 0, 4};
Polynomial g(9, test2);
cout << g << endl; // OUTPUT: 1x^9 + 4
int empty[] = {0};
Polynomial h(0, empty);
h = f + g; // do NOT just do cout << f + g << endl;
cout << h << endl;
Polynomial j(0, empty);
j = f - g;
cout << j << endl;
// (FOR * symbol, make sure to NOT use any &s. Otherwise, it thinks the *
// refers to POINTER *, not MULTIPICATION *)
Polynomial k(0, empty);
k = g*3;
cout << k << endl;
return 0;
}
this is my current error
poly.cpp(146): error C2679: binary '*' : no operator found which takes a right-hand operand of type 'int' (or there is no acceptable conversion)
1> poly.cpp(18): could be 'Polynomial Polynomial::operator *(Polynomial)'
1> while trying to match the argument list '(Polynomial, int)'
Thanks!