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::Polynomial()
{
mnTerms = 3;
mpCoefficients = new double[mnTerms];
}
// copy constructor
Polynomial::Polynomial(const Polynomial &x)
{
mnTerms=x.mnTerms;
mpCoefficients=new double [mnTerms];
for (int i=0;i<mnTerms;i++)
mpCoefficients[i]=x.mpCoefficients[i];
}
//constructor
Polynomial::Polynomial(short nTerms)
{
if (nTerms>=0)
{
mnTerms=nTerms;
mpCoefficients=new double[mnTerms];
for(int i=0;i<mnTerms;i++)
{
mpCoefficients[i]=0;
}
}
else
{
cerr << "Invalid polynomial." << endl;
exit(1);
}
}
//destructor
Polynomial::~Polynomial()
{
delete [] mpCoefficients;
}
const Polynomial &Polynomial::operator= (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[i] = rhs.mpCoefficients[i];
return *this;
}
const Polynomial Polynomial::operator+ (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[i]=mpCoefficients[i]+rhs.mpCoefficients[i];
else if (i<mnTerms)
answer.mpCoefficients[i]=mpCoefficients[i];
else
answer.mpCoefficients[i]=rhs.mpCoefficients[i];
return answer;
}
const Polynomial Polynomial::operator+ (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::operator- (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[i]=mpCoefficients[i]-rhs.mpCoefficients[i];
else if (i<mnTerms)
answer.mpCoefficients[i]=mpCoefficients[i];
else
answer.mpCoefficients[i]=-rhs.mpCoefficients[i];
return answer;
}
const Polynomial Polynomial::operator- (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::operator* (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[i]*rhs.mpCoefficients[j];
return answer;
}
const Polynomial Polynomial::operator* (double rhs) const
{
Polynomial answer (*this);
for (int i=0;i<mnTerms;i++)
answer.mpCoefficients[i]*=rhs;
return answer;
}
const Polynomial Polynomial::operator* (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[i]=mpCoefficients[i]*rhs.mpCoefficients[i];
else if (i<mnTerms)
answer.mpCoefficients[i]=mpCoefficients[i];
else
answer.mpCoefficients[i]*=rhs.mpCoefficients[i];
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[i]*pow(x,i);
return answer;
}
ostream &operator<< (ostream &stream,const Polynomial &x)
{
for(int i=x.mnTerms;i>1;i--)
{
stream << x.mpCoefficients[i] << "x^" << i << " + ";
}
if (x.mnTerms > 0)
{
stream << x.mpCoefficients[1] << "x + ";
}
stream << x.mpCoefficients[0];
return stream;
} 9 5948
"strotee76" <st*****@hotmail.com> wrote in message
news:a0**************************@posting.google.c om... 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.
I just looked through the coded quickly and found some things I'll explain
below. With this header file: ======================= #ifndef Polynomial_h #define Polynomial_h
class Polynomial { public: Polynomial (); Polynomial (const Polynomial &); Polynomial (short nTerms);
~Polynomial ();
You might consider declaring the dtor virtual. 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 &);
The const declarations prevents chaining. Are you sure you want that? 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
[SNIP] ============== poly.cpp file: ============== #include <iostream> #include <iomanip> #include <cmath> using namespace std;
#include "Polynomial.h"
//constructor Polynomial::Polynomial() { mnTerms = 3; mpCoefficients = new double[mnTerms]; } // copy constructor Polynomial::Polynomial(const Polynomial &x) { mnTerms=x.mnTerms; mpCoefficients=new double [mnTerms];
Attention, there might already be some memory allocated which mpCoefficients
points to. Without checking and deallocation this will result in a memory
leak.
for (int i=0;i<mnTerms;i++) mpCoefficients[i]=x.mpCoefficients[i]; }
[SNIP] I did not check the rest
double Polynomial::setTerm (short term,double coefficient) {
}
So what is the problem? Just check whether the variable term is a valid
index and set the value of your data array to coefficient.
[SNIP]
Regards
Chris
strotee76 wrote: 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.
I didn't find many real errors, but I added a note where I would do it
different. With this header file: ======================= #ifndef Polynomial_h #define Polynomial_h
class Polynomial { public: Polynomial (); Polynomial (const Polynomial &); Polynomial (short nTerms);
You probably should make the above constructor explicit to avoid
accidental conversion from short to Polynomal.
~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 &);
Why do all the above operators return const objects?
And you might want to add +=, -= and *=.
double setTerm (short term, double coefficient); double getTerm (short term) const;
double evaluate (double x) const;
friend ostream & operator<< (ostream &, const Polynomial &);
Does that operator need to be a friend? In fact, it would be better if
you could write all the nonmember operators in such a way that they
don't need to be friends. That should be easy.
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;
No need to use endl here. '\n' would suffice. The difference is that
endl will flush the output buffer after sending a '\n', which isn't
really needed at that place. Same for the other endl's. 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::Polynomial() { mnTerms = 3; mpCoefficients = new double[mnTerms]; }
Use initialization intead of assignment:
Polynomial::Polynomial()
: mnTerms(3),
mpCoefficients(new double[mnTerms])
{
}
// copy constructor Polynomial::Polynomial(const Polynomial &x) { mnTerms=x.mnTerms; mpCoefficients=new double [mnTerms]; for (int i=0;i<mnTerms;i++) mpCoefficients[i]=x.mpCoefficients[i]; }
Polynomial::Polynomial(const Polynomial &x)
: mnTerms(x.mnTerms),
mpCoefficients(new double [mnTerms])
{
// #include <algorithm> above
std::copy(x.mpCoefficients, x.mpCoefficients + mnTerms,
mpCoefficients);
}
//constructor Polynomial::Polynomial(short nTerms) { if (nTerms>=0) { mnTerms=nTerms; mpCoefficients=new double[mnTerms]; for(int i=0;i<mnTerms;i++) { mpCoefficients[i]=0; } } else { cerr << "Invalid polynomial." << endl; exit(1); } }
exit() should be used very rarely, if at all. If you decide to use that
code in a bigger program, it's annoying if your code terminates the
program somewhere deep in some library code. So it's better to quit the
program by just returning from main.
Polynomial::Polynomial(short nTerms)
: mnTerms(nTerms)
{
if (nTerms<0)
throw std::range_error("number of terms must be non-negative");
mpCoefficients=new double[mnTerms];
std::fill(mpCoefficients, mpCoefficients + nTerms, 0);
}
//destructor Polynomial::~Polynomial() { delete [] mpCoefficients; } const Polynomial &Polynomial::operator= (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[i] = rhs.mpCoefficients[i];
std::copy(rhs.mpCoefficients, rhs+mpCoefficients + i, mpCoefficients); return *this; }
const Polynomial Polynomial::operator+ (const Polynomial &rhs) const { short biggest=mnTerms; if (rhs.mnTerms>biggest) biggest=rhs.mnTerms;
short biggest = std::max(mnTerms, rhs.mnTerms);
Polynomial answer(biggest);
for (int i=biggest-1;i>=0;i++) if ((i<mnTerms)&&(i<rhs.mnTerms)) answer.mpCoefficients[i]=mpCoefficients[i]+rhs.mpCoefficients[i]; else if (i<mnTerms) answer.mpCoefficients[i]=mpCoefficients[i]; else answer.mpCoefficients[i]=rhs.mpCoefficients[i]; return answer; } const Polynomial Polynomial::operator+ (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; }
I would add an operator+= and implement the two others by using it.
Something like:
Polynomal& Polynomal::operator+=(double rhs)
{
mpCoefficients[0]+=rhs;
}
Polynomial Polynomial::operator+ (double rhs) const
{
return Polynomal(*this)+=rhs;
}
Polynomial operator+ (double lhs,const Polynomial &rhs)
{
return Polynomal(rhs)+=lhs;
}
Same of course for - and *.
const Polynomial Polynomial::operator- (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[i]=mpCoefficients[i]-rhs.mpCoefficients[i]; else if (i<mnTerms) answer.mpCoefficients[i]=mpCoefficients[i]; else answer.mpCoefficients[i]=-rhs.mpCoefficients[i]; return answer; } const Polynomial Polynomial::operator- (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::operator* (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[i]*rhs.mpCoefficients[j]; return answer; } const Polynomial Polynomial::operator* (double rhs) const { Polynomial answer (*this); for (int i=0;i<mnTerms;i++) answer.mpCoefficients[i]*=rhs; return answer; } const Polynomial Polynomial::operator* (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[i]=mpCoefficients[i]*rhs.mpCoefficients[i]; else if (i<mnTerms) answer.mpCoefficients[i]=mpCoefficients[i]; else answer.mpCoefficients[i]*=rhs.mpCoefficients[i]; return answer; } double Polynomial::setTerm (short term,double coefficient) {
if (term < 0 || term > mnTerms)
throw std::range_error();
return mpCoefficients[term] = coefficient;
}
Not sure why this function would have double return type instead of
void.
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[i]*pow(x,i); return answer; } ostream &operator<< (ostream &stream,const Polynomial &x) { for(int i=x.mnTerms;i>1;i--) { stream << x.mpCoefficients[i] << "x^" << i << " + "; } if (x.mnTerms > 0) { stream << x.mpCoefficients[1] << "x + "; } stream << x.mpCoefficients[0];
I suppose that should look different. If there is a mpCoefficients[0],
mnTerms must already be 1. For mpCoefficients[1] to exist, mnTerms must
be 2.
return stream; }
Chris Theis wrote: // copy constructor Polynomial::Polynomial(const Polynomial &x) { mnTerms=x.mnTerms; mpCoefficients=new double [mnTerms];
Attention, there might already be some memory allocated which mpCoefficients points to.
Huh? Did you miss the fact that this is a constructor?
"Rolf Magnus" <ra******@t-online.de> wrote in message
news:bv*************@news.t-online.com... strotee76 wrote:
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. I didn't find many real errors, but I added a note where I would do it different.
With this header file: ======================= #ifndef Polynomial_h #define Polynomial_h
class Polynomial { public: Polynomial (); Polynomial (const Polynomial &); Polynomial (short nTerms);
You probably should make the above constructor explicit to avoid accidental conversion from short to Polynomal.
~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 &);
Why do all the above operators return const objects? And you might want to add +=, -= and *=.
double setTerm (short term, double coefficient); double getTerm (short term) const;
double evaluate (double x) const;
friend ostream & operator<< (ostream &, const Polynomial &);
Does that operator need to be a friend? In fact, it would be better if you could write all the nonmember operators in such a way that they don't need to be friends. That should be easy.
private: short mnTerms; double *mpCoefficients;
std::vector<double> mCoefficients;
and get rid of mnTerms, use mCoefficients.size()
}; #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;
No need to use endl here. '\n' would suffice. The difference is that endl will flush the output buffer after sending a '\n', which isn't really needed at that place. Same for the other endl's.
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::Polynomial() { mnTerms = 3; mpCoefficients = new double[mnTerms]; }
Use initialization intead of assignment:
Polynomial::Polynomial() : mnTerms(3), mpCoefficients(new double[mnTerms])
mCoefficients(3)
Note that the values are initialized to 0.0 by default
The original method is dangerous if the declaration order of the members
were ever reversed, ie: mnTerms would be used before it was initialized.
{ }
// copy constructor Polynomial::Polynomial(const Polynomial &x) { mnTerms=x.mnTerms; mpCoefficients=new double [mnTerms]; for (int i=0;i<mnTerms;i++) mpCoefficients[i]=x.mpCoefficients[i]; } Polynomial::Polynomial(const Polynomial &x) : mnTerms(x.mnTerms), mpCoefficients(new double [mnTerms])
mCoefficients(x.mCoefficients)
{}
size and values are copied
{ // #include <algorithm> above std::copy(x.mpCoefficients, x.mpCoefficients + mnTerms, mpCoefficients); }
//constructor Polynomial::Polynomial(short nTerms)
: mCoefficients(std::max(nTerms,0))
{
if( nTerms<0 ) throw myexception();
}
Again, the values are initialized to 0.0 by default
{ if (nTerms>=0) { mnTerms=nTerms; mpCoefficients=new double[mnTerms]; for(int i=0;i<mnTerms;i++) { mpCoefficients[i]=0; } } else { cerr << "Invalid polynomial." << endl; exit(1); } }
exit() should be used very rarely, if at all. If you decide to use that code in a bigger program, it's annoying if your code terminates the program somewhere deep in some library code. So it's better to quit the program by just returning from main.
Polynomial::Polynomial(short nTerms) : mnTerms(nTerms) { if (nTerms<0) throw std::range_error("number of terms must be non-negative");
mpCoefficients=new double[mnTerms];
std::fill(mpCoefficients, mpCoefficients + nTerms, 0); }
//destructor Polynomial::~Polynomial() { delete [] mpCoefficients; }
Polynomial::~Polynomial()
{}
std::vector's destructor handles this.
const Polynomial &Polynomial::operator= (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[i] = rhs.mpCoefficients[i];
std::copy(rhs.mpCoefficients, rhs+mpCoefficients + i, mpCoefficients);
mCoefficients = rhs.mCoefficients;
Uses std::vector's operator=
In fact the remaining operators can be accomplished safely and easily by
simply forwarding to
std::vector's operators.
.... Polynomial::setTerm( ... aTerm, ... aValue )
{
if( mCoefficients.size() <= aTerm )
{
mCoefficients.resize( aTerm + 1 );
mCoefficients[aTerm] = aValue;
}
}
The rest is left as an excercise to the op.
Jeff F return *this; }
const Polynomial Polynomial::operator+ (const Polynomial &rhs) const { short biggest=mnTerms; if (rhs.mnTerms>biggest) biggest=rhs.mnTerms;
short biggest = std::max(mnTerms, rhs.mnTerms);
Polynomial answer(biggest);
for (int i=biggest-1;i>=0;i++) if ((i<mnTerms)&&(i<rhs.mnTerms)) answer.mpCoefficients[i]=mpCoefficients[i]+rhs.mpCoefficients[i]; else if (i<mnTerms) answer.mpCoefficients[i]=mpCoefficients[i]; else answer.mpCoefficients[i]=rhs.mpCoefficients[i]; return answer; } const Polynomial Polynomial::operator+ (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; }
I would add an operator+= and implement the two others by using it. Something like:
Polynomal& Polynomal::operator+=(double rhs) { mpCoefficients[0]+=rhs; }
Polynomial Polynomial::operator+ (double rhs) const { return Polynomal(*this)+=rhs; }
Polynomial operator+ (double lhs,const Polynomial &rhs) { return Polynomal(rhs)+=lhs; }
Same of course for - and *.
const Polynomial Polynomial::operator- (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[i]=mpCoefficients[i]-rhs.mpCoefficients[i]; else if (i<mnTerms) answer.mpCoefficients[i]=mpCoefficients[i]; else answer.mpCoefficients[i]=-rhs.mpCoefficients[i]; return answer; } const Polynomial Polynomial::operator- (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::operator* (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[i]*rhs.mpCoefficients[j]; return answer; } const Polynomial Polynomial::operator* (double rhs) const { Polynomial answer (*this); for (int i=0;i<mnTerms;i++) answer.mpCoefficients[i]*=rhs; return answer; } const Polynomial Polynomial::operator* (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[i]=mpCoefficients[i]*rhs.mpCoefficients[i]; else if (i<mnTerms) answer.mpCoefficients[i]=mpCoefficients[i]; else answer.mpCoefficients[i]*=rhs.mpCoefficients[i]; return answer; } double Polynomial::setTerm (short term,double coefficient) {
if (term < 0 || term > mnTerms) throw std::range_error();
return mpCoefficients[term] = coefficient;
}
Not sure why this function would have double return type instead of void.
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[i]*pow(x,i); return answer; } ostream &operator<< (ostream &stream,const Polynomial &x) { for(int i=x.mnTerms;i>1;i--) { stream << x.mpCoefficients[i] << "x^" << i << " + "; } if (x.mnTerms > 0) { stream << x.mpCoefficients[1] << "x + "; } stream << x.mpCoefficients[0];
I suppose that should look different. If there is a mpCoefficients[0], mnTerms must already be 1. For mpCoefficients[1] to exist, mnTerms must be 2.
return stream; }
"Rolf Magnus" <ra******@t-online.de> wrote in message
news:bv*************@news.t-online.com... Chris Theis wrote:
// copy constructor Polynomial::Polynomial(const Polynomial &x) { mnTerms=x.mnTerms; mpCoefficients=new double [mnTerms];
Attention, there might already be some memory allocated which mpCoefficients points to.
Huh? Did you miss the fact that this is a constructor?
Oh darn..., I must have been dreaming with my eyes wide open. I guess this
is a sign not to touch anything sensitive anymore today :-)
Thanks Rolf.
Chris
Thanks to everyone who has replied. This is the 1st time I submitted
anything to this group, so thanks again.
I was able to update the code to get it to compile, however, it
immediately crashes. Here is a link to the updated code. I'll be
honest, this code is for an assignment in class, I cannot change or
add any declarations to the header file. http://cpp.enisoc.com/pastebin/?3453
Anymore suggestions are welcome. Thanks for your time.
On Wed, 04 Feb 2004 23:21:05 -0800, strotee76 wrote: 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.
One obvious point. You want to allocate one more element
for your polynomial than the degree of that polynomial.
For example: the quadratic equation
4.5x^2 - 3.6x + 7.1
would need to have a double array of 3 and not 2 to
store 4.5, 3.6 and 7.1 . The same goes for a polynomial
of any other degree.
"Brian M. Dean" <el****@yahoo.com> wrote in message
news:pa****************************@yahoo.com... On Wed, 04 Feb 2004 23:21:05 -0800, strotee76 wrote:
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.
One obvious point. You want to allocate one more element for your polynomial than the degree of that polynomial. For example: the quadratic equation 4.5x^2 - 3.6x + 7.1 would need to have a double array of 3 and not 2 to store 4.5, 3.6 and 7.1 . The same goes for a polynomial of any other degree.
Hi Brian,
It's nice to define a () operator for
polynomial evaluation rather than eval()
Then you can do something like
Polynomial foo(6);
set the coeffs
double a = foo(1.23) ;
also you need to worry about complex
coeffs and evaluation at complex points
Bill
> Hi Brian, It's nice to define a () operator for polynomial evaluation rather than eval() Then you can do something like
Polynomial foo(6); set the coeffs double a = foo(1.23) ;
also you need to worry about complex coeffs and evaluation at complex points
The best way to handle that is with templates so
that instead of double, you can have a polynomial
with any coefficient you want. I would do that
myself except I am lazy, and double is good enough
for what I am doing. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Just |
last post by:
While googling for a non-linear equation solver, I found
Math::Polynomial::Solve in CPAN. It seems a great little module, except
it's not Python... I'm especially looking for its poly_root()...
|
by: Rubén Campos |
last post by:
I've trying to implement polynomials of arbitrary order as a C++ template,
as shown here:
template <unsigned long int N>
class Polynomial
{
public:
Polynomial ();
~Polynomial ();
|
by: Tiza Naziri |
last post by:
Hi,
Anybody have an idea on how to start writing a C code for generating
the inverse of finite field GF(2^8) using extended Euclidean algorithm?
What I mean is how to represent a polynomial,...
|
by: daniel.wolff |
last post by:
I am looking for a quick C program that takes n+1 pairs of values
(integers) (a_i, f(a_i)), i=0,...,n, generates the coefficients
\alpha_i, i=0,...,n of the polynomial of degree n that fits these...
|
by: Peter Olcott |
last post by:
I need to know how to get the solution mentioned below to work. The
solution is from gbayles Jan 29 2001, 12:50 pm, link is provided below:
>...
|
by: learnfpga |
last post by:
Here is a little code I wrote to add the numbers input by the
user.....I was wondering if its possible to have the same functionality
without using dynamic arrays.....just curious.....
...
|
by: assgar |
last post by:
Hi
Developemnt on win2003 server. Final server will be linux
Apache,Mysql and PHP is being used.
I use 2 scripts(form and process).
The form displays multiple dynamic rows with chechboxs,...
|
by: blackstormdragon |
last post by:
I have a feeling I'm doing this whole dynamic array thing wrong, or Im misunderstanding it.
private:
int degree;
int numCoef;
double *coef;
Polynomial::Polynomial( int hdegree)
{
degree...
|
by: madman228 |
last post by:
Hi guys I have run in to a littl bit of trouble. I am writing a class called polynomial
in which i need a derivative method I have everything, just dont know how to start the derivative method. Any...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
| |