I need help with creating the cpp file for the header file for the polynomial dy
ID: 669587 • Letter: I
Question
I need help with creating the cpp file for the header file for the polynomial dynamic array.
#ifndef polynomial_h
#define polynomial_h
#include <cstdlib> // Provides size_t type
#include <iostream.h> // Provides istream and ostream
class polynomial
{
public:
// CONSTRUCTORS and DESTRUCTOR
polynomial( );
polynomial(const polynomial& source);
polynomial(double c, unsigned int exponent = 0);
~polynomial( );
// MODIFICATION MEMBER FUNCTIONS
void add_to_coef(double amount, unsigned int exponent);
void assign_coef(double coefficient, unsigned int exponent);
void clear( );
void reserve(size_t number);
void trim();
// MODIFICATION OPERATORS
polynomial& operator =(const polynomial& source);
polynomial& operator =(double c)
{ clear( ); assign_coef(c, 0); return *this; }
polynomial& operator -=(const polynomial& p);
polynomial& operator -=(double c)
{ add_to_coef(-c, 0); return *this; };
polynomial& operator +=(const polynomial& p);
polynomial& operator +=(double c)
{ add_to_coef(c, 0); return *this; };
polynomial& operator *=(const polynomial& p);
polynomial& operator *=(double c);
// CONSTANT MEMBER FUNCTIONS
double coefficient(unsigned int exponent) const;
double definite_integral(double low_bound, double high_bound) const;
unsigned int degree( ) const;
polynomial derivative(unsigned int n = 1) const;
double eval(double x) const;
void find_root(
double& answer,
bool& success,
unsigned int& iterations,
double guess = 0,
unsigned int maximum_iterations = 100,
double epsilon = 1e-8
)
const;
polynomial integral(unsigned int n = 1) const;
unsigned int next_term(unsigned int e) const;
unsigned int previous_term(unsigned int e) const;
double numeric_integral(
double low_bound,
double high_bound,
unsigned int many_trapezoids = 100
)
const;
polynomial substitution(const polynomial& p) const;
// CONSTANT OPERATORS
double operator( ) (double x) const { return eval(x); }
polynomial operator( ) (const polynomial& p) const { return substitution(p); }
polynomial operator -( ) const;
polynomial operator +( ) const { return (*this); };
private:
size_t current_array_size;
double *coef;
};
// NON-MEMBER BINARY OPERATORS
polynomial operator +(const polynomial& p1, const polynomial& p2);
polynomial operator +(const polynomial& p, double c);
inline
polynomial operator +(double c, const polynomial& p) { return p + c; };
polynomial operator -(const polynomial& p1, const polynomial& p2);
inline
polynomial operator -(const polynomial& p, double c) { return p + (-c); };
inline
polynomial operator -(double c, const polynomial& p) { return p + (-c); };
polynomial operator *(const polynomial& p1, const polynomial& p2);
polynomial operator *(const polynomial& p, double c);
inline
polynomial operator *(double c, const polynomial& p) { return p * c; };
polynomial operator ^(const polynomial& p, unsigned int n);
// NON-MEMBER INPUT/OUTPUT FUNCTIONS
istream& operator >> (istream& in, polynomial& p);
ostream& operator << (ostream& out, const polynomial& p);
#endif /* polynomial_h */
Explanation / Answer
chegg is there to solve your doubts not to solve your assignements
you have to ask some doubts if you stuch some where how to solve or code
but here you have whole api and still you are asking here to program your code
Please don't do it next time
the following code is almost somilar to your rquirement and you may get some errors
please modify the code to solve your purpose
// The file polynomial.cpp
#include "polynomial.h"
#include <cmath>
using namespace std;
#define EPS 1e-6
Polynomial::Polynomial(double coeff, int degree)
{
if (fabs(coeff) >= EPS)
{
t = new Term;
t[0].co = coeff;
t[0].de = degree;
number = 1;
}
else
{
t = 0;
number = 0;
}
}
Polynomial::Polynomial(const Polynomial& source)
{
number=source.number;
t=new Term[number];
for (int i=0; i<number; i++)
t[i] = source.t[i];
}
const Polynomial& Polynomial::operator= (const Polynomial& p)
{
if (this != &p)
{
delete[] t;
number=p.number;
t=new Term[number];
for (int i=0; i<number; i++)
t[i] = p.t[i];
}
return *this;
}
Polynomial Polynomial::operator- () const
{
Polynomial resultat(*this);
for (int i=0; i<number; i++)
resultat.t[i].co = -resultat.t[i].co;
return resultat;
}
Polynomial Polynomial::operator+ (const Polynomial& p) const
{
Polynomial temp(number+p.number);
int i=0, j=0, n=0;
// terms with highest degree come first
while (i<number || j<p.number)
{
if ((i<number && j<p.number && t[i].de>p.t[j].de) || j>=p.number)
temp.t[n++] = t[i++];
else if ((i<number && j<p.number && t[i].de<p.t[j].de) || i>=number)
temp.t[n++] = p.t[j++];
else // same degree
{
temp.t[n].de = t[i].de;
temp.t[n++].co = t[i++].co + p.t[j++].co;
if (fabs(temp.t[n-1].co) < EPS)
n--; // do not include terms with coefficient zero
}
}
temp.number = n; // let the copy constructor
return Polynomial(temp); // make a copy with correct length
}
Polynomial Polynomial::operator- (const Polynomial& p) const
{
return (*this) + (-p);
}
Polynomial Polynomial::operator* (const Polynomial& p) const
{
Polynomial resultat, temp(p.number);
for (int k=0; k<number; k++)
{
// multiply p by term number k in this polnomial
for (int i=0; i<p.number; i++)
{
temp.t[i].co = t[k].co * p.t[i].co;
temp.t[i].de = t[k].de + p.t[i].de;
}
resultat = resultat + temp;
}
return resultat;
}
Polynomial Polynomial::der() const
{
int i, n=number;
// find out if there is a term with degree zero
for (i = 0; i < number; i++)
if (t[i].de == 0)
{
n--; break;
}
Polynomial resultat(n);
// build the derivative
for (i = 0; i < number; i++)
if (t[i].de != 0)
{
resultat.t[i].co = t[i].co*t[i].de;
resultat.t[i].de = t[i].de-1;
}
return resultat;
}
double Polynomial::value(double x) const
{
double r=0;
for (int i=0; i<number; i++)
{
double xn = 1;
if (t[i].de >= 0)
for (int j=1; j<= t[i].de; j++)
xn *= x;
else
for (int j=1; j<= -t[i].de; j++)
xn *= 1/x;
r += t[i].co*xn;
}
return r;
}
ostream& operator<<(ostream& o, const Polynomial& p)
{
if (p.number == 0)
cout << 0;
else
{
for (int i=0; i<p.number; i++)
{
if (p.t[i].co >= 0 && i > 0)
cout << '+';
cout << p.t[i].co;
if (p.t[i].de != 0)
{
cout << 'x';
if (p.t[i].de != 1)
cout << p.t[i].de;
}
}
}
return o;
}
istream& operator>>(istream& i, Polynomial& p)
{
double c;
int d;
while(true)
{
cout << "Enter coefficient && degree (q to quit)" << endl;
cin >> ws;
if (cin.peek() == 'q')
{
cin.ignore();
break;
}
cin >> c >> d;
p = p + Polynomial(c, d);
}
return i;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.