Using dynamic arrays, implement a polynomial class with polynomial adittion, sub
ID: 3531360 • Letter: U
Question
Using dynamic arrays, implement a polynomial class with polynomial adittion, subtraction, and multiplication.
Discussion: A variable in a polynomial does very little other than act as a placeholder for the coefficients. Hense, the only interesting things about polynomials is the array of coefficients and the corresponding exponent. Think about the polynomial
x*x*x + x + 1
One simple way to implement the polynomial class is to use an array of doubles to sotore the coefficients. The index of the array is the exponent of the corresponding term. Where is the term in x*x in the previous example? If a term is missing, then it simply has a zero coefficient.
There are techniques for representing polynomials of high degree with many missing terms. These use so-called sparse polynomial techniques. Unless you already know these techniques, or learn very quickly, don't use these techniques.
Provide a default constructor, a copy constructor, and a parameterized constructor that enables an arbitrary polynomial to be constructed. Also supply an overloaded operator = and a destructor.
Provide these operations:
polynomial + polynomial
constant + polynomial
polynomial + constant
polynomial - polynomial
constant - polynomial
polynomial - constant
polynomial * polynomial
constant * polynomial
polynomial * constant
Supply functions to assign and extract coefficients, indexed by exponent. Supply a function to evaluate the polynomial at a value of type double. You should decide whether to implement these functions as members, friends, or standalone functions.
Explanation / Answer
final answer
#include<iostream>
#include<string>
#include<vector>
#include<cmath>
using namespace std;
class Polynomial
{
std::vector<double> coeff;
public:
Polynomial(){coeff.push_back(0);}
Polynomial(std::vector<double>& b):coeff(b)
{
}
Polynomial(const Polynomial& a)
{
for (int i = 0; i < a.coeff.size(); i++)
{
coeff.push_back(a.coeff[i]);
}
}
~Polynomial()
{
}
Polynomial& Polynomial::operator=(const Polynomial&m)
{
if(this==&m)
return *this;
else
{
this->~Polynomial();
for (int i = 0; i < m.coeff.size(); i++)
{
coeff.push_back(m.coeff[i]);
}
}
}
Polynomial& operator+=(const Polynomial& m){
bool large= this->coeff.size()>m.coeff.size()?true:false;
if (large)
{
for (int i = 0; i < m.coeff.size(); i++)
{
this->coeff[i]+=m.coeff[i];
}
}
else
{
int i;
for (i = 0; i < this->coeff.size(); i++)
{
this->coeff[i]+=m.coeff[i];
}
for (; i<m.coeff.size() ; i++)
{
this->coeff.push_back(m.coeff[i]);
}
}
return *this;
}
Polynomial& operator+=(double m)
{
this->coeff[this->coeff.size()-1]+=m;
return *this;
}
Polynomial operator+(const Polynomial& m)
{
Polynomial temp(*this);
return temp+=m;
}
Polynomial operator+(double m)
{
Polynomial temp(*this);
return temp+=m;
}
friend Polynomial operator+(double d,Polynomial& m)
{
Polynomial temp(m);
return temp+=d;
}
void display()
{
for (int i = 0; i < this->coeff.size(); i++)
{
cout<<coeff[i]<<" ";
}
cout<<endl;
}
Polynomial& operator-=(const Polynomial& m)
{
bool large= this->coeff.size()>m.coeff.size()?true:false;
if (large)
{
for (int i = 0; i < m.coeff.size(); i++)
{
this->coeff[i]-=m.coeff[i];
}
}
else
{
int i;
for (i = 0; i < this->coeff.size(); i++)
{
this->coeff[i]-=m.coeff[i];
}
for (; i<m.coeff.size() ; i++)
{
this->coeff.push_back(-m.coeff[i]);
}
}
return *this;
}
Polynomial& operator-=(double m)
{
this->coeff[this->coeff.size()-1]-=m;
return *this;
}
Polynomial operator-(const Polynomial& m)
{
Polynomial temp(*this);
return temp-=m;
}
Polynomial operator-(double m)
{
Polynomial temp(*this);
return temp-=m;
}
friend Polynomial operator-(double d,Polynomial& m)
{
Polynomial temp(m);
return temp-=d;
}
Polynomial& operator*=(const Polynomial&m)
{
int k=this->coeff.size(),l=m.coeff.size();
Polynomial temp(*this);
temp.coeff.resize(this->coeff.size()+m.coeff.size()-1);
std::fill(temp.coeff.begin(),temp.coeff.end(),0);
bool large=k>l?true:false;
for (int i = 0; i < k; i++)
{
for (int j = 0; j < l; j++)
{
temp.coeff[i+j]+=this->coeff[i]*m.coeff[j];
}
}
*this=temp;
temp.~Polynomial();
return *this;
}
Polynomial& operator*=(double d)
{
for (int i = 0; i < this->coeff.size(); i++)
{
this->coeff[i]*=d;
}
return *this;
}
Polynomial operator*(const Polynomial& m)
{
Polynomial temp(*this);
return temp*=m;
}
Polynomial operator*(double m)
{
Polynomial temp(*this);
return temp*=m;
}
friend Polynomial operator*(double d,Polynomial& m)
{
Polynomial temp(m);
return temp*=d;
}
double extractcoeff(int i)
{
return this->coeff[i];
}
double Polynomialvalue(int x)
{
int temp=0;
for (int i = 0; i < this->coeff.size(); i++)
{
temp+=(this->coeff[i])*pow(x,this->coeff.size()-i-1);
}
}
};
int main()
{
std::vector<double> k(3);
for (int i = 0; i < k.size(); i++)
{
k[i]=i;
}
Polynomial a(k);
std::vector<double> j(3);
for (int i = 0; i < j.size(); i++)
{
j[i]=i+1;
}
Polynomial b(j);
Polynomial c;
c=b;
//b.display();
c.display();
a.display();
c*=a;
c.display();
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.