Help with Polynomial Program

Joined
Mar 5, 2009
Messages
3
Reaction score
0
I've been working on a polynomial program and I've narrowed
down my compiling problem to a binary operator overload.

I did some reading and I understand that a binary operator can
only take two arguments, so I need some help in rewriting it so
that it will compile successfully.

Code is as follows:

Polynomial.h
Code:
#ifndef POLYNOMIAL_H
#define POLYNOMIAL_H

#include <vector>

class Polynomial
{
private:
	//short mnTerms;
	double *mpCoefficients;
	short mCoefficients;

public:
	Polynomial ();
	~Polynomial ();

	Polynomial (const Polynomial &);
	Polynomial (short nTerms);

	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 &);
	const Polynomial &operator* (double, const Polynomial &); // ERROR C2804 originates here.


	double setTerm (short term, double coefficient);
	double getTerm (short term) const;

	double evaluate (double x) const;

	friend ostream & operator<< (ostream &, const Polynomial &);


};

#endif

Polynomial.cpp
Code:
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;

#include "Polynomial.h"


// Constructor
Polynomial::Polynomial() : mCoefficients(3), mpCoefficients(new double[mCoefficients])
{
	//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) : mCoefficients(x.mCoefficients), mpCoefficients(new double [mCoefficients])
{
	std::copy(x.mpCoefficients, x.mpCoefficients + mCoefficients, mpCoefficients);
}

// Constructor - determine numer of coefficients, set as "double".
//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);
//	}
//}

Polynomial::Polynomial(short nTerms) : mCoefficients(nTerms)
{
	if (nTerms < 0)
	throw std::range_error("Number of terms must be non-negative.");

	mpCoefficients = new double[mCoefficients];

	std::fill(mpCoefficients, mpCoefficients + nTerms, 0);
}

//Destructor - Remove used memory.
Polynomial::~Polynomial()
{
	delete [] mpCoefficients;
}

const Polynomial &Polynomial::operator= (const Polynomial &rhs)
	{
	if (this==&rhs)
	return *this;
	{
		if (mCoefficients != rhs.mCoefficients)
		{
		delete [] mpCoefficients;
		mCoefficients=rhs.mCoefficients;
		mpCoefficients=new double [mCoefficients];
		}
	}
	for (int i=0; i<mCoefficients; i++)
	mpCoefficients[i] = rhs.mpCoefficients[i];

	return *this;
	}

const Polynomial Polynomial::operator+ (const Polynomial &rhs) const
{
	short biggest = mCoefficients;
	if (rhs.mCoefficients > biggest)
	biggest = rhs.mCoefficients;
	Polynomial answer(biggest);

	for (int i=biggest-1; i >= 0; i++)
	if ((i < mCoefficients) && (i < rhs.mCoefficients))
	answer.mpCoefficients[i]=mpCoefficients[i]+rhs.mpCoefficients[i];
	else if (i < mCoefficients)
	answer.mpCoefficients[i]=mpCoefficients[i];
	else
	answer.mpCoefficients[i]=rhs.mpCoefficients[i];
	return answer;
}

const Polynomial Polynomial::operator+ (double rhs) const
{
	Polynomial answer (*this); // Pointer to non-overloaded function.
	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=mCoefficients;

	if (rhs.mCoefficients > biggest)
		biggest=rhs.mCoefficients;
		Polynomial answer(biggest);

	for (int i=biggest-1;i >= 0;i++)
	if ((i < mCoefficients)&&(i < rhs.mCoefficients))
		answer.mpCoefficients[i]=mpCoefficients[i]-rhs.mpCoefficients[i];
	else if (i < mCoefficients)
		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- (short lhs, const Polynomial &rhs)
{
	Polynomial answer(lhs);
//	answer.mpCoefficients[0]-=lhs;

	return answer;
}

const Polynomial Polynomial::operator* (const Polynomial &rhs) const
{
	Polynomial answer(mCoefficients+rhs.mCoefficients-1);
	for (int i=0; i < mCoefficients; i++)
	for (int j=0; j < rhs.mCoefficients; 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 < mCoefficients; i++)
		answer.mpCoefficients[i]*=rhs;

	return answer;
}

// This is the matching method to the declaration in the header file.
const Polynomial &Polynomial::operator* (double lhs, const Polynomial &rhs) 
{
	short biggest=mCoefficients;

	if (rhs.mCoefficients>biggest)
		biggest = rhs.mCoefficients;
		Polynomial answer(biggest);

	for (int i = biggest-1; i >= 0; i++)
	if ((i<mCoefficients) && (i < rhs.mCoefficients))
		answer.mpCoefficients[i]=mpCoefficients[i]*rhs.mpCoefficients[i];
	else if (i<mCoefficients)
		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 > mCoefficients)
	return mpCoefficients[term] = coefficient;
}

//double Polynomial::setTerm(short term, double coefficient )
//{
//	if( coefficient.size() <= term )
//	{
//		coefficient.resize( term + 1 );
//		coefficient[term] = aValue;
//	}
//}

double Polynomial::getTerm (short term) const
{
	return mpCoefficients[term];
}

double Polynomial::evaluate (double x) const
{
	double answer;
	for (int i=1;i<mCoefficients+1;i++)
		answer += mpCoefficients[i]*pow(x,i);

	return answer;
}

ostream &operator<< (ostream &stream, const Polynomial &x)
{
	for(int i=x.mCoefficients;i>1;i--)
	{
		stream << x.mpCoefficients[i] << "x^" << i << " + ";
	}

	if (x.mCoefficients > 0)
	{
		stream << x.mpCoefficients[1] << "x + ";
	}

	stream << x.mpCoefficients[0];
	return stream;
}

If you need to see the main, let me know.

Thank you in advance.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,989
Messages
2,570,207
Members
46,783
Latest member
RickeyDort

Latest Threads

Top