Skip to content

Instantly share code, notes, and snippets.

Created April 25, 2017 04:14
Show Gist options
  • Save anonymous/704fdbac1caab8fe807906a06f0be7ef to your computer and use it in GitHub Desktop.
Save anonymous/704fdbac1caab8fe807906a06f0be7ef to your computer and use it in GitHub Desktop.
#include "function.h"
using namespace std;
Polynomial::Polynomial()
{
greatestPower=0;
coefficients[0]=0;
}
Polynomial::Polynomial(const int N, const int C[51])
{
greatestPower=N;
for(int i=N; i>=0; i--)
{
coefficients[i]=C[i];
}
}
Polynomial Polynomial::add( const Polynomial b) const
{
Polynomial c;
int a=(greatestPower>b.greatestPower)?greatestPower:b.greatestPower;
c.greatestPower =a;
for(int i=a; i>=0; i--)
{
c.coefficients[i]=coefficients[i]+b.coefficients[i];
}
return *c;
} // add function
Polynomial Polynomial::subtract( const Polynomial b) const
{
Polynomial c;
int a=(greatestPower>b.greatestPower)?greatestPower:b.greatestPower;
c.greatestPower =a;
for(int i=a; i>=0; i--)
{
c.coefficients[i]=coefficients[i]-b.coefficients[i];
}
return *this;
} // subtract function
Polynomial Polynomial::multiplication( const Polynomial b) const
{
Polynomial c;
int a=greatestPower*b.greatestPower;
c.greatestPower =a;
for(int i=greatestPower; i>=0; i--)
{
for(int j=b.greatestPower; j>=0; j--)
{
c.coefficients[i+j]+=coefficients[i]*b.coefficients[j];
}
}
return *this;
} // multiplication function
void Polynomial::output() const
{
for(int i=this->greatestPower; i>0; i--)
{
cout<< this->coefficients[i] << " ";
}
cout << this->coefficients[0] << endl;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment