// A class implementing polynomials and operations on polynomials. // Publically, the polynomial appears to have all of the expected // algebraic operations. // #ifndef POLYNOMIAL_H #define POLYNOMIAL_H #include using namespace std; #include // Polynomial class, version 2. Publically, this version and the // other have the same interface. The class represents an individual // polynomial. It provides member functions to read a polynomial, // evaluate a polynomial, assign one polynomial to another, add two // polynomials, multiply two polynomials, and output a polynomial. // // The implementation represents only those terms that have non-zero // coefficients. This is appropropriate for polynomials such as // // f(x) = 15 + 2 x^10000 // // Each term is represented using a simple PTerm object. // zero, up to a highest degree. This degree can be increased by the // assign_coefficient function. // // The implementation given here goes beyond the design given in the // text. One important way is that there is no artificial limit on // the size of the exponent. // // // PTerm is the class for the individual terms of a polynomial. // class PTerm { public: PTerm( double c = 0.0, int e = 0 ) { coefficient = c; exponent = e; } ~PTerm( ) {} double operator() ( double x ) const { return coefficient * pow( x, (double) exponent ); } friend bool operator < ( const PTerm & left, const PTerm & right) { return left.exponent < right.exponent; } friend bool operator > ( const PTerm & left, const PTerm & right) { return left.exponent > right.exponent; } friend bool operator == ( const PTerm & left, const PTerm & right ) { return left.exponent == right.exponent; } double coefficient; int exponent; }; // // The class that represents an individual polynomial. It provides // member functions to read a polynomial, evaluate a polynomial, // assign one polynomial to another, add two polynomials, multiply // two polynomials, and output a polynomial. // // This goes beyond the design given in the text. One important way // is that there is no artificial limit on the size of the exponent. // class Polynomial { public: // Constructors and destructors. Polynomial( ); Polynomial( double a0 ); // convert a constant to a polynomial Polynomial( const Polynomial & old ); ~Polynomial( ); // Assignment operator. const Polynomial & operator = ( const Polynomial& right ); // Evaluate the polynomial at the given value. double operator() ( double x ) const; // Access double get_coefficient( int exponent ) const; int max_degree() const; // Modification void assign_coefficient( double coefficient, int exponent ); // A few of the many operators we would want to provide. These are // friend functions because compiler issues don't generally arise // when the class is not templated. friend Polynomial operator+ (const Polynomial& f, const Polynomial& g); friend Polynomial operator- (const Polynomial& f, const Polynomial& g); friend Polynomial operator* (const Polynomial& f, const Polynomial& g ); friend Polynomial operator* (double a, const Polynomial& f); friend Polynomial operator* (const Polynomial& f, double a); // Output operator as a friend function. friend ostream& operator<< ( ostream& ostr, const Polynomial& f ); private: vector terms_; }; #endif