All pastes #1603 Raw Edit

Anonymous

public text v1 · immutable
#1603 ·published 2004-10-20 16:54 UTC
rendered paste body

// Ryan Brown
// HW 5
// Fig. 8.19: Polynomial.cpp
// Polynomial class member function definitions.

#include <iostream>
#include <stdlib.h>												// For malloc, realloc, free

using std::cout;

#include "polynomial.h"  // Polynomial class definition

///////////////////////////////////////////////////////////////////////////////////
// Constructor
Polynomial::Polynomial() 
{ 
	coeffArray = (double *)malloc(sizeof(double));						// Allocate memory
	coeffArray[0] = 0;
	power = 0;

} // end Polynomial constructor


///////////////////////////////////////////////////////////////////////////////////
// Deconstructor
Polynomial::~Polynomial()
{
	free(coeffArray);
} // end Deconstructor


///////////////////////////////////////////////////////////////////////////////////
// Add operator
void Polynomial::add(double coeff, int pow)
{

	if(pow > power){
		coeffArray =  (double *)realloc(coeffArray, (power - pow)*sizeof(double));
		for(int i = power + 1; i <= pow; i++) coeffArray[i] = 0;
		power = pow;
	}

	coeffArray[pow] += coeff;
} // end Add operator


///////////////////////////////////////////////////////////////////////////////////
// Constructor
Polynomial::Polynomial ( const Polynomial & p )
{ 
	coeffArray = (double *)malloc((p.power + 1) * sizeof(double));
	
	power = p.power;
	
	memcpy(coeffArray, p.coeffArray, (p.power + 1) * sizeof(double));
} // end Polynomial constructor


///////////////////////////////////////////////////////////////////////////////////
// addition operator
Polynomial Polynomial::operator+( const Polynomial & p ) const
{
	Polynomial poly = *this;
    
	for(int i = 0; i <= p.power; i++) poly.add(p.coeffArray[i], i);
                                                                        
	return poly;

} // end function operator+


///////////////////////////////////////////////////////////////////////////////////
// subtraction operator
Polynomial Polynomial::operator-( const Polynomial & p ) const
{
	Polynomial poly = *this;

	for(int i = 0; i <= p.power; i++) poly.add(-p.coeffArray[i], i);

	return poly;

} // end function operator-


///////////////////////////////////////////////////////////////////////////////////
// multiplication operator
Polynomial Polynomial::operator*( const Polynomial & p) const
{
	Polynomial poly;

	for(int i = 0; i <= power; i++){
		for(int j = 0; j <= p.power; j++){
			poly.add(coeffArray[i] * p.coeffArray[j], i + j);
		}
	}

	return poly;

} // end function operator*


///////////////////////////////////////////////////////////////////////////////////
// assignment operator
Polynomial & Polynomial::operator = (const Polynomial & p)
{
	free(coeffArray);
	
	coeffArray = (double *)malloc((p.power + 1) * sizeof(double));
	
	coeffArray[0] = 0;
	
	power = p.power;
	
	for(int i = 0; i < p.power; i++){
		add(p.coeffArray[i], i);
	}
	
	return *this;
} // end function operator =


///////////////////////////////////////////////////////////////////////////////////
// addition assignment operator
Polynomial & Polynomial::operator += (const Polynomial & p)
{
	*this = *this + p;
	return *this;

} // end function operator +=


///////////////////////////////////////////////////////////////////////////////////
// subtraction assignment operator
Polynomial & Polynomial::operator -= (const Polynomial & p)
{
	*this = *this - p;
	return *this;

} // end function operator -=


///////////////////////////////////////////////////////////////////////////////////
// multiplication assignment operator
Polynomial & Polynomial::operator *= (const Polynomial & p)
{
	*this = *this * p;
	return *this;

} // end function operator *=


///////////////////////////////////////////////////////////////////////////////////
// Outstream operator
ostream & operator << (ostream & out, const Polynomial & power)
{
	for(int i = 0; i <= power; i++) {
		out << coeffArray[i] << "*x^" << i << "+";
	}

	return out;

} // end function operator <<