In: Computer Science
This is C++ programming.
Use separate compilation to implement a polynomial ADT that manipulates polynomials in a single variable x (e.g., p = 4 x^5 + 7 x^3 – x^2 + 9 ). For this problem, consider only polynomials whose exponents are non-negative integers. You are required to identify a proper data representation schema to store such polynomials and hide such data from external users of this ADT. Additionally, your ADT will at least include the following member functions:
One default constructor, which creates a polynomial with no terms.
Ex: Polynomial p; // This should create a polynomial with no terms.
One method allowing one to get an entire polynomial by interacting with the user (by writing to cout and reading from cin) to obtain the degree and coefficient of each term in a polynomial. If the user enters nonsense, your program should not crash! It is up to you how to handle this case. The method can look something like this when called:
myPolynomial.readFromUser(); // prompts the user for the degree
and
// coefficients and stores the
// results in myPolynomial
degree() // Returns the degree of a polynomial, which is the highest power of a term with a nonzero coefficient.
E.g: int deg = myPolynomial.degree();
coefficient(power) // Returns the coefficient of the x p o w e r term.
changeCoefficient(newCoefficient, power) // Replaces the coefficient of the x p o w e r term with newCoefficient. (Note: This method should handle the invalid case too. what happens if the power is out of range?? )
A method that multiplies a polynomial by an integer (make this a normal named method - do not overload the multiplication (*) operator).
A method that adds two polynomials ( overload the addition(+) operator as a standalone function).
Overload the division operator (/) as a member function to multiple divide a polynomial by a scalar variable.
A method that prints out a polynomial. Ex: cout<<p; // should print 4 x^5 + 7 x^3 – x^2 + 9 (Here you have to overload the "<<" put operator as a friend function. Because you cannot overload it as a member function)
Overload the subtraction operator(-) as a member function to subtract two polynomials.
Example: p1 = 3 x^3 - 2 x^2 - 3; p2 = 4 x^4 - x^2 + 3 x^1 - 5;
p3 = p1 - p2; // p3 should result in: - 4 x^4 + 3 x^3 - x^2 - 3 x + 2;
Overload the negation operator (-) as a member function to negate a polynomial. (NOTE: This is different from the subtraction operator! The subtraction operator takes an argument - the polynomial you are subtracting - whereas the negation operator takes no arguments.)
Example: p1 = 3 x^3 - 2 x^2 - 3; // -p1 should result in - 3 x^3 + 2 x^2 +3;