/* A header file for class Polynomial. An object of this class represents a polynomial in the variable x. Various operations on polynomials are defined in the class. */ /* Declare some classes so that pointers and references can * be used in the definition of Polynomial. (Note that to * declare a pointer or reference, the compiler only needs * to know that the type exists; it does not need to know * the definition of the type.) */ class ostream; class istream; struct TermNode; class Polynomial { TermNode* head; // A polynomial is stored as a linked list of // terms. Each term is represented by a struct // of type TermNode, which contains a coefficient // (type double) and an exponent (type int). // The nodes are are arranged on the list in // order of decreasing exponent. A given exponent // occurs in only one node. None of the coefficients // are zero. public: Polynomial(double coefficient = 0.0, unsigned int exponent = 0); // Create a polynomial that has a single term with the given // coefficient and exponent, except that if the coefficient is // zero, then the polynomial is the zero polynomial, which // has no terms at all. Polynomial(const Polynomial &source); // Copy constructor. ~Polynomial(); // Destructor. Calls the clear() function before deletion. Polynomial& operator=(const Polynomial &source); // Assignment operator. double value(double x); // Find the value of this polynomial for the given value of x. unsigned int degree(); // Returns the largest exponent of any (non-zero) term. double coefficient(unsigned int exponentOfTerm); // Returns the coefficient of the term with the given exponent. // (The return value is zero if the exponent does not occur // in any term of the polynomial.) void multiplyByConstant(double k); // Multiply the coefficient in each term by k. void multiplyByTerm(double coefficient, unsigned int exponent); // Multiply the coefficient in each term by the given coefficient, // and add the given exponent to the exponent in each term. void addTerm(double coefficient, unsigned int exponent); // Add a term with given coefficient and exponent to the polynomial. void clear(); // Set the polynomial to zero by deleting all the terms. // (Uses the delete operator to reclaim storage!) bool isZero(); // Test if this polynomial is zero. void operator+=(const Polynomial &p); // Add p to this polynomial (by adding each term of p). void operator-=(const Polynomial &p); // Add -p to this poly (by adding the negative of each term of p). friend Polynomial operator+(const Polynomial& p1, const Polynomial& p2); // Compute and return the sum of p1 and p2. (Add each term of p2 // to a copy of p1, and return the copy.) friend Polynomial operator-(const Polynomial& p1, const Polynomial& p2); // Compute and return the difference, p1 - p2. (Add the negative // of each term of p2 to a copy of p1 and return the copy.) friend Polynomial operator*(const Polynomial& p1, const Polynomial& p2); // Compute the product, p1*p2. (Start with the zero polynomial. // For each term of p2, make a copy, p3, of p1; multiply the copy by // the term from p2; and add the result to p3. Return p3.) friend Polynomial power(const Polynomial& p, unsigned int exponent); // Compute p multiplied by itself exponent times. friend istream& operator>>(istream& in, Polynomial& p); // Read a value for p from the stream. An example input is // 3*x^3 + 1.5*x^2 + 7*x + 12. (Ideally, the *'s and ^'s could // be omitted. The terms don't necessarily have to be in order // of decreasing exponent. An easier version would require // "7*x^1" instead of "2*x" and "12*x^0" instead of "12".) friend ostream& operator<<(ostream& out, const Polynomial& p); // Output p to the stream. friend bool operator==(const Polynomial& p1, const Polynomial& p2); // Test whether p1 and p2 represent the same polynomial (same // terms, i.e. the same coefficients and exponents). friend bool operator!=(const Polynomial& p1, const Polynomial& p2); // Returns !(p1 == p2) };