#include #include using namespace std; #include "polynomial.h" // Implementation of the polynomial class member functions. Polynomial::Polynomial() { } Polynomial::Polynomial( double a0 ) { terms_.push_back( PTerm(a0, 0) ); } Polynomial::Polynomial( const Polynomial & old ) { terms_ = old.terms_; } Polynomial::~Polynomial() { } const Polynomial & Polynomial::operator = ( const Polynomial & right ) { if ( this != &right ) { terms_ = right.terms_; } return *this; } double Polynomial::operator() ( double x ) const { double value = 0.0; for ( int i=0; i=0 && terms_[k].coefficient == 0.0 ) -- k; if ( k<0 ) return -1; else return terms_[ k ].exponent; } // void Polynomial::assign_coefficient( double coefficient, int exponent ) { int last_sub = terms_.size()-1; int k=last_sub; while ( k>=0 && terms_[ k ].exponent > exponent ) -- k; if ( k >= 0 && terms_[ k ].exponent == exponent ) { terms_[k].coefficient = coefficient; } else if ( k == last_sub ) { terms_.push_back( PTerm(coefficient, exponent) ); } else { terms_.push_back( terms_[last_sub] ); for ( int i = last_sub-1; i>k; i-- ) terms_[i+1] = terms_[i]; terms_[k+1] = PTerm( coefficient, exponent ); } } // The addition operator implements the design given in the class notes. Polynomial operator + ( const Polynomial& f, const Polynomial& g ) { Polynomial h; int i = 0, j = 0; while( i