Skip to content

Instantly share code, notes, and snippets.

@yun-cloud
Forked from anonymous/hw7-2
Created April 25, 2017 11:46
Show Gist options
  • Save yun-cloud/1e24a37ceab1946f148ebb3a674b3530 to your computer and use it in GitHub Desktop.
Save yun-cloud/1e24a37ceab1946f148ebb3a674b3530 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 c;
} // subtract function
Polynomial Polynomial::multiplication( const Polynomial b) const
{
Polynomial c;
c.greatestPower = greatestPower + b.greatestPower;
// clear the coefficients array in Polynomial c
for (int i=0; i<=c.greatestPower; ++i) {
c.coefficients[i] = 0;
}
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 c;
} // 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