S
strotee76
What do I need to do to setTerm() for it to work properly?
If you notice any other glaring issues, then please let me know.
With this header file:
=======================
#ifndef Polynomial_h
#define Polynomial_h
class Polynomial
{
public:
Polynomial ();
Polynomial (const Polynomial &);
Polynomial (short nTerms);
~Polynomial ();
const Polynomial & operator= (const Polynomial &);
const Polynomial operator+ (const Polynomial &) const;
const Polynomial operator+ (double) const;
friend const Polynomial operator+ (double, const Polynomial &);
const Polynomial operator- (const Polynomial &) const;
const Polynomial operator- (double) const;
friend const Polynomial operator- (double, const Polynomial &);
const Polynomial operator* (const Polynomial &) const;
const Polynomial operator* (double) const;
friend const Polynomial operator* (double, const Polynomial &);
double setTerm (short term, double coefficient);
double getTerm (short term) const;
double evaluate (double x) const;
friend ostream & operator<< (ostream &, const Polynomial &);
private:
short mnTerms;
double *mpCoefficients;
};
#endif
=======================
and this main.cpp file:
=======================
#include <iostream>
#include <iomanip>
using namespace std;
#include "Polynomial.h"
int main ()
{
Polynomial tmp1;
Polynomial tmp2;
tmp1.setTerm(1,1);
tmp1.setTerm(0,2);
tmp2.setTerm(5,2);
tmp2.setTerm(2,1);
tmp2.setTerm(1,2);
tmp2.setTerm(0,4);
cout << endl;
cout << tmp1 << " + " << tmp2 << endl;
cout << tmp1+tmp2 << endl;
cout << endl;
cout << tmp1 << " + " << 10 << endl;
cout << tmp1+10 << endl;
cout << endl;
cout << 10 << " + " << tmp1 << endl;
cout << 10+tmp1 << endl;
cout << endl;
cout << tmp1 << " - " << tmp2 << endl;
cout << tmp1-tmp2 << endl;
cout << endl;
cout << tmp1 << " - " << 10 << endl;
cout << tmp1-10 << endl;
cout << endl;
cout << 10 << " - " << tmp1 << endl;
cout << 10-tmp1 << endl;
cout << endl;
cout << tmp1 << " X " << tmp2 << endl;
cout << tmp1*tmp2 << endl;
cout << endl;
cout << tmp2 << " X " << -2 << endl;
cout << tmp2*-2 << endl;
cout << endl;
cout << 3 << " X " << tmp2 << endl;
cout << 3*tmp2 << endl;
cout << endl;
cout << tmp2 << " where x is " << 2 << endl;
cout << tmp2.evaluate(2) << endl;
cout << endl;
return 0;
}
==============
poly.cpp file:
==============
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;
#include "Polynomial.h"
//constructor
Polynomial:olynomial()
{
mnTerms = 3;
mpCoefficients = new double[mnTerms];
}
// copy constructor
Polynomial:olynomial(const Polynomial &x)
{
mnTerms=x.mnTerms;
mpCoefficients=new double [mnTerms];
for (int i=0;i<mnTerms;i++)
mpCoefficients=x.mpCoefficients;
}
//constructor
Polynomial:olynomial(short nTerms)
{
if (nTerms>=0)
{
mnTerms=nTerms;
mpCoefficients=new double[mnTerms];
for(int i=0;i<mnTerms;i++)
{
mpCoefficients=0;
}
}
else
{
cerr << "Invalid polynomial." << endl;
exit(1);
}
}
//destructor
Polynomial::~Polynomial()
{
delete [] mpCoefficients;
}
const Polynomial &Polynomial:perator= (const Polynomial &rhs)
{
if (this==&rhs)
return *this;
if (mnTerms!=rhs.mnTerms)
{
delete [] mpCoefficients;
mnTerms=rhs.mnTerms;
mpCoefficients=new double [mnTerms];
}
for (int i=0;i<mnTerms;i++)
mpCoefficients = rhs.mpCoefficients;
return *this;
}
const Polynomial Polynomial:perator+ (const Polynomial &rhs) const
{
short biggest=mnTerms;
if (rhs.mnTerms>biggest)
biggest=rhs.mnTerms;
Polynomial answer(biggest);
for (int i=biggest-1;i>=0;i++)
if ((i<mnTerms)&&(i<rhs.mnTerms))
answer.mpCoefficients=mpCoefficients+rhs.mpCoefficients;
else if (i<mnTerms)
answer.mpCoefficients=mpCoefficients;
else
answer.mpCoefficients=rhs.mpCoefficients;
return answer;
}
const Polynomial Polynomial:perator+ (double rhs) const
{
Polynomial answer (*this);
answer.mpCoefficients[0]+=rhs;
return answer;
}
const Polynomial operator+ (double lhs,const Polynomial &rhs)
{
Polynomial answer(rhs);
answer.mpCoefficients[0]+=lhs;
return answer;
}
const Polynomial Polynomial:perator- (const Polynomial &rhs) const
{
short biggest=mnTerms;
if (rhs.mnTerms>biggest)
biggest=rhs.mnTerms;
Polynomial answer(biggest);
for (int i=biggest-1;i>=0;i++)
if ((i<mnTerms)&&(i<rhs.mnTerms))
answer.mpCoefficients=mpCoefficients-rhs.mpCoefficients;
else if (i<mnTerms)
answer.mpCoefficients=mpCoefficients;
else
answer.mpCoefficients=-rhs.mpCoefficients;
return answer;
}
const Polynomial Polynomial:perator- (double rhs) const
{
Polynomial answer (*this);
answer.mpCoefficients[0]-=rhs;
return answer;
}
const Polynomial operator- (double lhs, const Polynomial &rhs)
{
Polynomial answer(lhs);
answer.mpCoefficients[0]-=lhs;
return answer;
}
const Polynomial Polynomial:perator* (const Polynomial &rhs) const
{
Polynomial answer(mnTerms+rhs.mnTerms-1);
for (int i=0;i<mnTerms;i++)
for (int j=0;j<rhs.mnTerms;j++)
answer.mpCoefficients[i+j]+=mpCoefficients*rhs.mpCoefficients[j];
return answer;
}
const Polynomial Polynomial:perator* (double rhs) const
{
Polynomial answer (*this);
for (int i=0;i<mnTerms;i++)
answer.mpCoefficients*=rhs;
return answer;
}
const Polynomial Polynomial:perator* (double lhs,const Polynomial &rhs)
{
short biggest=mnTerms;
if (rhs.mnTerms>biggest)
biggest=rhs.mnTerms;
Polynomial answer(biggest);
for (int i=biggest-1;i>=0;i++)
if ((i<mnTerms)&&(i<rhs.mnTerms))
answer.mpCoefficients=mpCoefficients*rhs.mpCoefficients;
else if (i<mnTerms)
answer.mpCoefficients=mpCoefficients;
else
answer.mpCoefficients*=rhs.mpCoefficients;
return answer;
}
double Polynomial::setTerm (short term,double coefficient)
{
}
double Polynomial::getTerm (short term) const
{
return mpCoefficients[term];
}
double Polynomial::evaluate (double x) const
{
double answer;
for (int i=1;i<mnTerms+1;i++)
answer += mpCoefficients*pow(x,i);
return answer;
}
ostream &operator<< (ostream &stream,const Polynomial &x)
{
for(int i=x.mnTerms;i>1;i--)
{
stream << x.mpCoefficients << "x^" << i << " + ";
}
if (x.mnTerms > 0)
{
stream << x.mpCoefficients[1] << "x + ";
}
stream << x.mpCoefficients[0];
return stream;
}
If you notice any other glaring issues, then please let me know.
With this header file:
=======================
#ifndef Polynomial_h
#define Polynomial_h
class Polynomial
{
public:
Polynomial ();
Polynomial (const Polynomial &);
Polynomial (short nTerms);
~Polynomial ();
const Polynomial & operator= (const Polynomial &);
const Polynomial operator+ (const Polynomial &) const;
const Polynomial operator+ (double) const;
friend const Polynomial operator+ (double, const Polynomial &);
const Polynomial operator- (const Polynomial &) const;
const Polynomial operator- (double) const;
friend const Polynomial operator- (double, const Polynomial &);
const Polynomial operator* (const Polynomial &) const;
const Polynomial operator* (double) const;
friend const Polynomial operator* (double, const Polynomial &);
double setTerm (short term, double coefficient);
double getTerm (short term) const;
double evaluate (double x) const;
friend ostream & operator<< (ostream &, const Polynomial &);
private:
short mnTerms;
double *mpCoefficients;
};
#endif
=======================
and this main.cpp file:
=======================
#include <iostream>
#include <iomanip>
using namespace std;
#include "Polynomial.h"
int main ()
{
Polynomial tmp1;
Polynomial tmp2;
tmp1.setTerm(1,1);
tmp1.setTerm(0,2);
tmp2.setTerm(5,2);
tmp2.setTerm(2,1);
tmp2.setTerm(1,2);
tmp2.setTerm(0,4);
cout << endl;
cout << tmp1 << " + " << tmp2 << endl;
cout << tmp1+tmp2 << endl;
cout << endl;
cout << tmp1 << " + " << 10 << endl;
cout << tmp1+10 << endl;
cout << endl;
cout << 10 << " + " << tmp1 << endl;
cout << 10+tmp1 << endl;
cout << endl;
cout << tmp1 << " - " << tmp2 << endl;
cout << tmp1-tmp2 << endl;
cout << endl;
cout << tmp1 << " - " << 10 << endl;
cout << tmp1-10 << endl;
cout << endl;
cout << 10 << " - " << tmp1 << endl;
cout << 10-tmp1 << endl;
cout << endl;
cout << tmp1 << " X " << tmp2 << endl;
cout << tmp1*tmp2 << endl;
cout << endl;
cout << tmp2 << " X " << -2 << endl;
cout << tmp2*-2 << endl;
cout << endl;
cout << 3 << " X " << tmp2 << endl;
cout << 3*tmp2 << endl;
cout << endl;
cout << tmp2 << " where x is " << 2 << endl;
cout << tmp2.evaluate(2) << endl;
cout << endl;
return 0;
}
==============
poly.cpp file:
==============
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;
#include "Polynomial.h"
//constructor
Polynomial:olynomial()
{
mnTerms = 3;
mpCoefficients = new double[mnTerms];
}
// copy constructor
Polynomial:olynomial(const Polynomial &x)
{
mnTerms=x.mnTerms;
mpCoefficients=new double [mnTerms];
for (int i=0;i<mnTerms;i++)
mpCoefficients=x.mpCoefficients;
}
//constructor
Polynomial:olynomial(short nTerms)
{
if (nTerms>=0)
{
mnTerms=nTerms;
mpCoefficients=new double[mnTerms];
for(int i=0;i<mnTerms;i++)
{
mpCoefficients=0;
}
}
else
{
cerr << "Invalid polynomial." << endl;
exit(1);
}
}
//destructor
Polynomial::~Polynomial()
{
delete [] mpCoefficients;
}
const Polynomial &Polynomial:perator= (const Polynomial &rhs)
{
if (this==&rhs)
return *this;
if (mnTerms!=rhs.mnTerms)
{
delete [] mpCoefficients;
mnTerms=rhs.mnTerms;
mpCoefficients=new double [mnTerms];
}
for (int i=0;i<mnTerms;i++)
mpCoefficients = rhs.mpCoefficients;
return *this;
}
const Polynomial Polynomial:perator+ (const Polynomial &rhs) const
{
short biggest=mnTerms;
if (rhs.mnTerms>biggest)
biggest=rhs.mnTerms;
Polynomial answer(biggest);
for (int i=biggest-1;i>=0;i++)
if ((i<mnTerms)&&(i<rhs.mnTerms))
answer.mpCoefficients=mpCoefficients+rhs.mpCoefficients;
else if (i<mnTerms)
answer.mpCoefficients=mpCoefficients;
else
answer.mpCoefficients=rhs.mpCoefficients;
return answer;
}
const Polynomial Polynomial:perator+ (double rhs) const
{
Polynomial answer (*this);
answer.mpCoefficients[0]+=rhs;
return answer;
}
const Polynomial operator+ (double lhs,const Polynomial &rhs)
{
Polynomial answer(rhs);
answer.mpCoefficients[0]+=lhs;
return answer;
}
const Polynomial Polynomial:perator- (const Polynomial &rhs) const
{
short biggest=mnTerms;
if (rhs.mnTerms>biggest)
biggest=rhs.mnTerms;
Polynomial answer(biggest);
for (int i=biggest-1;i>=0;i++)
if ((i<mnTerms)&&(i<rhs.mnTerms))
answer.mpCoefficients=mpCoefficients-rhs.mpCoefficients;
else if (i<mnTerms)
answer.mpCoefficients=mpCoefficients;
else
answer.mpCoefficients=-rhs.mpCoefficients;
return answer;
}
const Polynomial Polynomial:perator- (double rhs) const
{
Polynomial answer (*this);
answer.mpCoefficients[0]-=rhs;
return answer;
}
const Polynomial operator- (double lhs, const Polynomial &rhs)
{
Polynomial answer(lhs);
answer.mpCoefficients[0]-=lhs;
return answer;
}
const Polynomial Polynomial:perator* (const Polynomial &rhs) const
{
Polynomial answer(mnTerms+rhs.mnTerms-1);
for (int i=0;i<mnTerms;i++)
for (int j=0;j<rhs.mnTerms;j++)
answer.mpCoefficients[i+j]+=mpCoefficients*rhs.mpCoefficients[j];
return answer;
}
const Polynomial Polynomial:perator* (double rhs) const
{
Polynomial answer (*this);
for (int i=0;i<mnTerms;i++)
answer.mpCoefficients*=rhs;
return answer;
}
const Polynomial Polynomial:perator* (double lhs,const Polynomial &rhs)
{
short biggest=mnTerms;
if (rhs.mnTerms>biggest)
biggest=rhs.mnTerms;
Polynomial answer(biggest);
for (int i=biggest-1;i>=0;i++)
if ((i<mnTerms)&&(i<rhs.mnTerms))
answer.mpCoefficients=mpCoefficients*rhs.mpCoefficients;
else if (i<mnTerms)
answer.mpCoefficients=mpCoefficients;
else
answer.mpCoefficients*=rhs.mpCoefficients;
return answer;
}
double Polynomial::setTerm (short term,double coefficient)
{
}
double Polynomial::getTerm (short term) const
{
return mpCoefficients[term];
}
double Polynomial::evaluate (double x) const
{
double answer;
for (int i=1;i<mnTerms+1;i++)
answer += mpCoefficients*pow(x,i);
return answer;
}
ostream &operator<< (ostream &stream,const Polynomial &x)
{
for(int i=x.mnTerms;i>1;i--)
{
stream << x.mpCoefficients << "x^" << i << " + ";
}
if (x.mnTerms > 0)
{
stream << x.mpCoefficients[1] << "x + ";
}
stream << x.mpCoefficients[0];
return stream;
}