#include <cmath> // re. pow( x, exp ) #include "polynomial.h" Polynomial::Polynomial () : order(0) {} // construct a 'zero' poly... Polynomial::Polynomial ( int size, const double coeff[] ) { for( int i = 0; i < size; ++i ) { if( coeff[i] ) { Term tmp( coeff[i], i ); coefficients.push_back( tmp ); } } if( coefficients.size() ) order = coefficients.back().power; else order = 0; } Polynomial::Polynomial ( int nCoeff, double c ) : order(nCoeff-1) { if( c ) { for( int i = 0; i < nCoeff; ++i ) { Term tmp( c, i ); coefficients.push_back( tmp ); } } if( coefficients.size() ) order = coefficients.back().power; else order = 0; } double Polynomial::evaluate ( double x ) const { double sum = 0.0; std::list< Term >::const_iterator it; for( it = coefficients.begin(); it != coefficients.end(); ++it ) sum += it->coefficient * pow( x, it->power ); return sum; } int Polynomial::getOrder() const { return order; } Polynomial Polynomial::operator + ( const Polynomial& p ) const { Polynomial result; // calls default ctor ... order set to zero std::list< Term >::const_iterator a = coefficients.begin(), aend = coefficients.end(), b = p.coefficients.begin(), bend = p.coefficients.end(); while( a != aend && b != bend ) { if( *a < *b ) // comparing powers only { result.coefficients.push_back( *a ); ++a; } else if( *a == *b ) // comparing powers only { if( a->coefficient != - b->coefficient ) { Term tmp( *a ); tmp.coefficient += b->coefficient; result.coefficients.push_back( tmp ); // a non zero term // } ++a; ++b; } else { result.coefficients.push_back( *b ); ++b; } } // at most one of the following two wile loops gets executed while( a != aend ) { result.coefficients.push_back( *a ); ++a; } while( b != bend ) { result.coefficients.push_back( *b ); ++b; } //result.checkOrder(); // redundant here since only NON zero terms appeneded above // if( result.coefficients.size() ) result.order = result.coefficients.back().power; // else power was set by ctor above to zero return result; } Polynomial Polynomial::operator * ( double scale ) const { if( scale ) // if NOT zero { Polynomial result (*this); std::list< Term >::iterator it; for( it = result.coefficients.begin(); it != result.coefficients.end(); ++it ) it->coefficient *= scale; return result; } // else ... return Polynomial(); // return a ZERO poly... } void Polynomial::checkOrder () { while( coefficients.back().coefficient == 0 ) coefficients.pop_back(); if( coefficients.size() ) order = coefficients.back().power; else order = 0; } std::istream& operator >> (std::istream& in, Polynomial& p) { int r = 0; // r is number of coefficents and must be >= 1 p.coefficients.clear(); if( in >> r && r > 0 ) { for( int i = 0; i < r; ++i ) { double tmp; if( ! ( in >> tmp ) ) { std::cout << "Please start over and enter the " << r << " decimal coeff's:\n"; in.clear(); in.sync(); p.coefficients.clear(); i = 0; } else { if( tmp ) { Term tmpTerm; tmpTerm.coefficient = tmp; p.order = tmpTerm.power = i; p.coefficients.push_back( tmpTerm ); } } } // ok ... reached here ... so ... //p.checkOrder(); // redundant here since only NON zero terms appended above // } else { std::cout << "\nEnter a valid integer >= 1 for number of coefficents.\n"; } return in; } std::ostream& operator << (std::ostream& out, const Polynomial& p) { if( p.coefficients.size() == 0 ) return out << 0; // zero poly ... // else ... int count = 0; std::list< Term >::const_iterator it; for ( it = p.coefficients.begin(); it != p.coefficients.end(); ++it ) { if ( count ) // Don't have an extra " + " sign on first term ... { out << " + "; } ++count; /* out << it->coefficient; // Ok ... now print value ... if ( it->power > 0 ) // Don't have an 'x' on first 'ZERO degree' term ... { out << " x"; if ( it->power > 1 ) // if deg higher than 1 ... show ^deg { out << "^" << it->power; } } */ out << *it; } return out; }

#ifndef POLYNOMIAL_H #define POLYNOMIAL_H //#include <iostream> // now inclued by file "term.h" included below ... #include <list> #include "term.h" class Polynomial { public: // Create a simple polonomial Polynomial (); // Create a poly from an array with 'size' coefficents Polynomial ( int size, const double ary[] ); // Create a polynomial with nCoeff coefficients, // all set equal to c. // // E.g., // Polynomial p (3, 1.0); // creates a polynomial p representing: 1.0 + 1.0*x + 1.0*x^2 // Polynomial (int nCoeff, double c); // Compute the value of this polynomial at a given value of x. // E.g., // double c[3] = {1.0, 2.0, 3.0}; // Polynomial p (3, c); // cout << p.evaluate(2.0); // will print 17.0 double evaluate (double x) const; // The order of a polynomial is the largest exponent of x with a // non-zero coefficient. E.g., for the sample polynomial used // above, the order is 2. int getOrder() const; // Add two polynomials, returning the polynomial representing // their sum. Polynomial operator+ (const Polynomial& p) const; // Multiply a polynomial by a floating point value. Polynomial operator* (double scale) const; private: int order; std::list< Term > coefficients; //std::vector< double > coefficients; friend std::istream& operator>> (std::istream&, Polynomial&); friend std::ostream& operator<< (std::ostream&, const Polynomial&); // Internal utility function - computes and remembers the order // of a polynomial. Use this at the end of any function that can change // the coefficients of the polynomial. void checkOrder(); }; inline Polynomial operator* (double scale, const Polynomial& p) { return p * scale; } /* Note: a member function of the Polynomial class must have a polynomial as the first argument (e.g., poly.operator*(x) <===> poly * x). This function simply allows for the multiplication to be written with the polynomial on the right. */ #endif

/* * term.h * * Created on: Jun 10, 2013 * Author: zeil */ #ifndef TERM_H_ #define TERM_H_ #include <iostream> struct Term { double coefficient; int power; Term (double c = 0.0, int pow = 0) : coefficient(c), power(pow) {} bool operator== (const Term& t) const { return power == t.power && coefficient == t.coefficient; } }; inline std::ostream& operator<< (std::ostream& out, const Term& t) { out << t.coefficient; if (t.power > 0) { out << " x"; if (t.power > 1) out << "^" << t.power; return out; } } #endif /* TERM_H_ */

// #include <iostream> #include "polynomial.h" // includes <iostream> // //#include "polynomial.cpp" // to do a quick test... // using namespace std; int main( void ) // (int argc, char** argv) // // NOT used here, compiler complains // // but may be needed for prof testing ??? // { int nCoeff1; cout << "Enter a polynomial.\n"; while( true ) { cout << "How many coefficients will be in the first polynomial? " << flush; cin >> nCoeff1; if( cin.good() && nCoeff1 > 0 ) break; // else ... cout << "\nPlease enter an array size > 0 \n"; cin.clear(); // clear all cin error flags ... cin.sync(); // 'flush' cin stream ... } cin.sync(); // 'flush' any extra numbers (or char's) that might have been input above double* coeff1 = new double[nCoeff1]; cout << "\nEnter the " << nCoeff1 << " coefficients, in order of " << "increasing power of x:" << endl; for ( int i = 0 ; i < nCoeff1; ++i ) { cin >> coeff1[i]; if( !cin.good() ) { cout << "\nInvalid input ... Please start over.\n" << "Enter the " << nCoeff1 << " coefficients, in order " << "of increasing power of x:" << endl; cin.clear(); cin.sync(); i = -1; // *** SET to -1 here so when incremented becomes 0 *** // } } cin.sync(); // 'flush' any extra numbers (or char's) that might have been input above Polynomial p1 ( nCoeff1, coeff1 ); // calls ( size, ary ) ctor... double x; while( true ) { cout << "\nEnter a value at which to evaluate the polynomial: " << flush; cin >> x; if( cin.good() ) break; // else cout << "\nOnly decimal values are valid here..."; cin.clear(); cin.sync(); } cin.sync(); // 'flush' any extra numbers (or char's) that might have been input above cout << p1 << "\n at x=" << x << " evaluates to " << p1.evaluate(x) << endl; Polynomial p2; // calls default ctor ... while( true ) { cout << "\nEnter a second polynomial, in the same format\n" << "(# of coefficients, then each coefficient in order " << "of increasing power of x)." << endl; cin >> p2; if( cin.good() ) break; else { cin.clear(); cin.sync(); } } cin.sync(); // 'flush' any extra numbers (or char's) that might have been input above double scale; while( true ) { cout << "\nEnter a scaling factor (a floating point number): " << flush; cin >> scale; if( cin.good() ) break; // else cin.clear(); cin.sync(); cout << "\nInvalid input ... "; } cin.sync(); // 'flush' any extra numbers (or char's) that might have been input above { // these extra {} not needed in C++ to declare new objects, but ok if wish // Polynomial p3 = p1 * scale; cout << "\n\n(" << p1 << ") * " << scale << " = " << p3 << endl; } { Polynomial p4 = p1 + p2; cout << "\n\n" << p1 << "\n +\n" << " (" << p2 << ")\n" << " = " << p4 << endl; } { Polynomial p5 = p1 + scale * p2; cout << "\n\n" << p1 << "\n +\n" << scale << " * (" << p2 << ")\n" << " = " << p5 << endl; cout << "\nThis polynomial has order " << p5.getOrder() << " and, at x = " << x << ", evaluates to " << p5.evaluate(x) << endl; } delete [] coeff1; cout << "\nPress 'Enter' to continue/exit ... " << flush; cin.get(); return 0; }

ERRORS:

1. polynomial.cpp:66: error: no match for 'operator<' in 'a.std::_List_const_iterator<_Tp>::operator* [with _Tp = Term]() < b.std::_List_const_iterator<_Tp>::operator* [with _Tp = Term]()'

Would I have to overload the operator < as well? Would I have to delcar it in my polynomial.h as Polynomial operator< (const Polynomial& p)const;?

Any advice would be welcomed. Thanks in advance.