// Implementation for 1st version of the Polynomial class. Each // coefficient, including zeroes, is represented up to the maximum // degree. #include #include using namespace std; #include "polynomial.h" // In the constructors and destructors and throughout the code, note // the use of the vector member functions. Polynomial::Polynomial() {} Polynomial::Polynomial( double a0 ) { coefficients_.push_back( a0 ); } Polynomial::Polynomial( const Polynomial & old ) { coefficients_ = old.coefficients_; // vector assignment operator } Polynomial::~Polynomial( ) {} const Polynomial & Polynomial::operator = ( const Polynomial& right ) { if ( this != &right ) coefficients_ = right.coefficients_; return *this; } double Polynomial::operator() ( double x ) const { if ( coefficients_.size() == 0 ) return 0.0; double value=coefficients_[0]; // result double x_i = 1.0; // x^i, built incrementally for ( int i=1; i= 0 ); if ( exponent >= coefficients_.size() ) return 0.0; else return coefficients_[exponent]; } // Find the maximum degree. Start at the largest represented // exponent and go down until a term with non-zero exponent is found. // Usually, the while loop will have no iterations, but the code does // not attempt to reduce the highest degree represented when the // coefficient of the highest exponent is assigned 0. int Polynomial::max_degree() const { int max_d = coefficients_.size()-1; while ( max_d >= 0 && coefficients_[max_d] == 0.0 ) -- max_d; return max_d; } // Assign the coefficient for the given exponent. If this exponent // is larger than the largest store, expand the vector by padding // with an appropriate number of 0's. void Polynomial::assign_coefficient( double coefficient, int exponent ) { assert( exponent >= 0 ); if ( exponent >= coefficients_.size() ) { for ( int i=coefficients_.size(); i g_size ) { h.coefficients_ = f.coefficients_; for( int i=0; i