Answered step by step
Verified Expert Solution
Question
1 Approved Answer
NOTE: PLEASE DO NOT USE THE FRIENDS FUNCTION! Defination of Polynom.h file (PLEASE DO NOT CHANGE ITS CONTENTS!) // IMPORTANT: Do not modify the contents
NOTE: PLEASE DO NOT USE THE FRIENDS FUNCTION!
Defination of Polynom.h file (PLEASE DO NOT CHANGE ITS CONTENTS!)
// IMPORTANT: Do not modify the contents of this file // // Polynom.h // Definition of a simple polynomial class of degree n #ifndef POLYNOM_H #define POLYNOM_H #includeIn this assignment you will implement and test a C++ class called Polynom from a given definition, to represent and use polynomials. A polynomial function of independent variable x can be written as p(x)=anxn+an1xn1++a1x+a0 The highest power of variable x that occurs in the polynomial with nonzero coefficient (in this case n with an=0 ) is called the degree of the polynomial. The quantities an,,a0 are constants known as coefficients. In this assignment coefficients are int type and can be positive, negative, or 0. A basic operation for polynomials is to evaluate it at a specific value of x where x is a real number. For example, we can evaluate the quadratic polynomial q(x), q(x)=x2+5x+6 for x=2, by considering the polynomial in the following nested form, q(x)=((x+5)x+6) and then substituting x=2 to obtain, q(2)=((2+5)2+6)=((7)2+6)=(14+6)=20 Notice that the method shown above to evaluate a polynomial is much more efficient than the method in which each term of type xk in the polynomial is explicitly computed by using for example cmath power function double pow (double base, double exponent) which is shown below, pow(2,2)+5pow(2,1)+6=20 Your implementation to evaluate a polynomial at given x should use the method nested form as explained above. We can add two polynomials and subtract one from the other. Examples are shown below. p(x)=3x3+2x2+x+16,q(x)=x2+5x+6p(x)+q(x)=(3+0)x3+(2+1)x2+(1+5)x+(16+6)=3x3+3x2+6x+22p(x)q(x)=(30)x3+(21)x2+(15)x+(166)=3x3+x2+(4)x+10 A simple way to represent a polynomial object of degree n is to use a vector of length n+1 to store the coefficients. For example, the polynomials p and q can be represented by vectors of length 4 and 3 , respectively. p:[32116],q:[156] It is possible that some of the coefficients in a polynomial are 0 . Consider the polynomial r(x)=5x9+2x4+19 where the largest power of x is 9 so that we need a vector of length 10 to store the polynomial: r:[50000200019] 2 What to submit 1. Complete implementation of class Polynom in files named Polynom.h and Polynom.cc. 2. A test program in a file named test_Polynom.cc to thoroughly test each member function of the class Polynom. The assignment (Assignment 2) will be graded as follows. 1. The program is complete and compiles without errors: 10 points. 2. The class implementation meets the specification given above and in file Polynom.h: 70 points. 3. Program is appropriately commented and indented: 10 points. 4. The file test_Polynom.cc contains code that demonstrates the correct working of each implemented member function: 10 points. Total: 100 points#include using namespace std; class Polynom { public: // Default constructor: Polynom() // // Postcondition: Creates an "empty" polynomial Polynom(); // Type Conversion Constructor: Polynom(const vector & ) // // Postcondition: Creates a polynomial with coefficients provided in the vector parameter; // Polynom(const vector & coeff); // Overloaded add operator: operator+(const Polynom& ) const // // Precondition : Polynomials "this" and RHS are nonempty // Postcondition: The sum of this polynomial and RHS is returned; this polynomial // and RHS are unchanged Polynom operator+(const Polynom& RHS) const; // Overloaded subtract operator: operator-(const Polynom& ) const // // Precondition : Polynomials "this" and RHS are nonempty // Postcondition: The RHS is subtracted from this and the result is returned; this polynomial // and RHS are unchanged Polynom operator-(const Polynom& RHS) const; // Overloaded function evaluation: operator()(double x) const // Precondition : Polynomials "this" is nonempty // Postcondition: this polynomial is evaluated at x and the the value // is returned; this polynomial is unchanged double operator()(double x) const; // Get the coefficient of x^k: bool getCoeff(int k, int& c) const // Precondition: k >= 0 and k P; // coefficients of the polynomial; if P is nonempty and P.size() is n // then P[i] is the coefficient // of x raised to power (n-i-1) // private member function: printTerm(char sign, int pwr, int coeff, ostream& sout) // Postcondition: display the string "sign coeff x^pwr" to sout. For example // the call // printTerm('-', 2, 10, cout) // displays the string "-10x^2" to the standard output // (computer screen) // // the call // printTerm('e', 2, 10, cout) // displays the string "10x^2" to the standard output; Note: 'e' is used for empty sign. // // the call // printTerm('+', 1, 3, cout) // displays the string "+3x" to the standard output // // the call // printTerm('-', 0, 8, cout) // displays the string "-8" to the standard output // ostream& printTerm(char sign, int pwr, int coeff, ostream& sout); // Set the coefficient of x^k: bool setCoeff(int k, int c) // Precondition: k >= 0 and k > to read the coefficients for polynomial RHS // from istream In // calls member function extract istream& operator>>(istream& In, Polynom& RHS); #endif
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started