Question: Poly1.h : // FILE: poly1.h // CLASS PROVIDED: // class polynomial // A polynomial has one variable x, real number coefficients, and // non-negative integer

 Poly1.h : // FILE: poly1.h // CLASS PROVIDED: // class polynomial// A polynomial has one variable x, real number coefficients, and //non-negative integer exponents. Such a polynomial can be viewed // as havingthe form: // A[n]*x^n + A[n-1]*x^(n-1) + ... A[2]*x^2 + A[1]*x +

Poly1.h :

// FILE: poly1.h // CLASS PROVIDED: // class polynomial // A polynomial has one variable x, real number coefficients, and // non-negative integer exponents. Such a polynomial can be viewed // as having the form: // A[n]*x^n + A[n-1]*x^(n-1) + ... A[2]*x^2 + A[1]*x + A[0] // where the A[n] are the real number coefficients and x^i represents // the variable x raised to the i power. The coefficient A[0] is // called the "constant" or "zeroth" term of the polynomial. // // NOTES: // This poly1.h version works by storing the coefficients in // a dynamic array, as described in Section 4.6 of // "Data Structures and Other Objects" (2nd edition) by // Michael Main and Walter Savitch // // CONSTRUCTORS and DESTRUCTOR for the polynomial class // polynomial( ) // POSTCONDITION: This polynomial has been created with all zero // coefficients. // // polynomial(double a0) // POSTCONDITION: This polynomial has been create with all zero // coefficients, except for coefficient c for the specified exponent. // // polynomial(const polynomial& source) // POSTCONDITION: This polynomial has been created as a copy of the source. // // ~polynomial( ) // POSTCONDITION: This polynomial has been deleted from memeory. // // MODIFICATION MEMBER FUNCTIONS for the polynomial class // void add_to_coef(double amount, unsigned int k) // POSTCONDITION: Adds the given amount to the coefficient of the // specified exponent k. // // void assign_coef(double coefficient, unsigned int k) // POSTCONDITION: Sets the coefficient for the specified exponent k. // // void clear( ) // POSTCONDITION: All coefficients of this polynomial are set to zero. // // void reserve(size_t number) // POSTCONDITION: The size of the array for coefficients has been changed to // the requested number (but not less that the size needed to store the // current non-zero coefficients). In effect, this guarantees that member // functions will not need to allocate new memory for exponents through // at least number-1. // // MODIFICATION OPERATORS for the polynomial class // polynomial& operator =(const polynomial& source) // POSTCONDITION: This polynomial is exactly the same as the // parameter. The return value is a reference to this polynomial. // // polynomial& operator =(double a0) // POSTCONDITION: This polynomial has been create with all zero // coefficients, except for coefficient c for the specified exponent. // // CONSTANT MEMBER FUNCTIONS for the polynomial class // double coefficient(unsigned int k) const // POSTCONDITION: Returns coefficient at specified exponent k. // // unsigned int degree( ) const // POSTCONDITION: The function returns the value of the largest exponent // with a non-zero coefficient. // If all coefficients are zero, then the function returns zero. // // unsigned int next_term(unsigned int k) const // POSTCONDITION: The function returns the exponent of the next term // with a non-zero coefficient after specified exponent k. // // EVALUATION MEMBER FUNCTIONS for the polynomial class // double eval(double x) const // POSTCONDITION: The return value is the value of this polynomial with the // given value for the variable x. // // double operator( ) (double x) const // Same as the eval member function. // // NON-MEMBER BINARY OPERATORS for the polynomial Class // polynomial operator -(const polynomial& p1, const polynomial& p2) // POSTCONDITION: return-value is a polynomial with each coefficient // equal the difference of the coefficients of p1 & p2 for any given // exponent. // // polynomial operator +(const polynomial& p1, const polynomial& p2) // POSTCONDITION: return-value is a polynomial with each coefficient // equal the sum of the coefficients of p1 & p2 for any given // exponent. // // DYNAMIC MEMORY usage by the polynomial class // If there is insufficient dynamic memory, the following functions throw a // bad_alloc exception: // the constructors, add_to_coef, assign_coef, reserve, // and any of the operators with a polynomial parameter.

#ifndef POLY1_H #define POLY1_H #include // Provides size_t type #include // Provides istream and ostream

// If your compiler does not support namespaces, then please delete the // following line and the set of brackets that follow.

namespace main_savitch_3

{ class polynomial { public: // CONSTRUCTORS and DESTRUCTOR polynomial(); polynomial(double a0); polynomial(const polynomial& source); ~polynomial();

// MODIFICATION MEMBER FUNCTIONS void add_to_coef(double amount, unsigned int k); void assign_coef(double coefficient, unsigned int k); void clear(); void reserve(size_t number);

// MODIFICATION OPERATORS polynomial& operator=(const polynomial& source); polynomial& operator=(double a0) // in line function { clear(); assign_coef(a0, 0); return *this; }

// CONSTANT MEMBER FUNCTIONS double coefficient(unsigned int k) const; unsigned int degree() const; unsigned int next_term(unsigned int k) const;

// EVALUATION MEMBER FUNCTIONS double eval(double x) const; double operator()(double x) const // in line function { return eval(x); }

private: double* coef; size_t current_array_size; };

// NON-MEMBER BINARY OPERATORS polynomial operator+(const polynomial& p1, const polynomial& p2); polynomial operator-(const polynomial& p1, const polynomial& p2); } #endif

PolyTest1.cpp:

https://home.cs.colorado.edu/~main/projects/polytest1.cxx

Above is the link to the file.

1. Read chapter 4 from the textbook and understand the discussion of dynamic memory allocation and pointer variables. Understand how to work with pointer variables and dynamic memory allocation. Study the re-implement class bag using dynamic arrays. 2. Work the programming project Polynomial" (discussed in section 4.6) to develop C++ program poly1.cpp that uses a dynamic array to implement the requirements A through G as specified in the textbook, pages 213 - 215. 3. In part G, do not implement the multiplication (*) operation. 4. Any other functions you may need to implement the specified functions should be done as private functions. 5. The following files are provided for your reference. You should start by modifying the header file first. a. poly1.h: The header file for the polynomial class. b. Polytesti.cpp: A small interactive test program. Important note: The provided file Polytest1.cpp requires some modification to work for our header file. Re-use this text program as you see fit. 6. The test program is given to guide you in developing your own text program for the functions required for this assignment. The test program shows how to test other functions that are not part of this assignment, please ignore such functions. A. Constructors and destructor. polynomial(); // Default constructor polynomial(double al); // Set the x coefficient only polynomial(const polynomial& source); // Copy constructor -polynomial(); The default constructor creates a polynomial with all zero coefficients. The sec- ond constructor creates a polynomial with the specified parameter as the coeffi- cient of the x term, and all other coefficients are zero. For example: polynomial p(4.2); // p has only one nonzero term, 4.2x", which is the // same as the number 4.2 (since x is defined as // equal to 1). B. Assignment operator. polynomial& operator = (const polynomial& source); This is the usual overloaded assignment operator, with one change: The return type is polynomial& rather than void. This return type is similar to an ordi- nary polynomial, but the extra symbol & makes it a reference return type, similar to the return type ostream& of our output operators. The complete details of a reference return type are beyond this project. For your implementation, you should know two facts: 1. The function implementation should return the object that activated the assignment. This is accomplished with the keyword this (which we also saw on page 188). The syntax is: return * this; , which means return the object that this points to." Since this always points to the object that activates the function, the return statement has the effect that we need. 2. Using polynomial& as the return type permits a sequence of chained assignments. For example, if a, b, and c are three polynomials, we can write a = b = c, which copies the value of c to b, and then copies the new value of b to a (chained assignments work from right to left). Remember to have your implementation check for a possible self-assignment. C. A second assignment operator. polynomial& operator =(double a0); For a polynomial b, this assignment can be activated in a statement such as b = 4.2. The double number, 4.2, becomes the argument al for this assign- ment. The implementation will use this number as the coefficient for the x term, and all other coefficients are set to zero. If you read the information on constructor-generated conversions (page 210), then you might notice that this second version of the assignment operator isn't entirely needed. Even without this assignment operator, we could write an assignment b = 4.2; in this case, the compiler would apply the polynomial constructor to the number 4.2 (creating the polynomial 4.2x), and then this poly- nomial would be assigned to b. However, writing an explicit assignment operator to allow b = 4.2 is generally more efficient because we avoid the overhead of the constructor-generated conversion. D. Modification member functions. void add_to_coef(double amount, unsigned int k); void assign_coef(double new_coefficient, unsigned int k); void clear(); void reserve(size_t number); The add_to_coef function adds the specified amount to the coefficient of the xk term. The assign_coef function sets the xk coefficient to new_coefficient. In both cases, the parameter k is an unsigned int, which is the C++ data type that is like an int, but may never have a negative value. The clear function sets all coefficients to zero. The reserve function works like reserve for the bag class, making sure that the underlying array has at least the requested size. E. Constant member functions. double coefficient (unsigned int k) const; unsigned int degree( const; unsigned int next_term(unsigned int k) const; The coefficient function returns the coefficient of the xk term. The degree function returns the degree of the polynomial. For a polynomial where all coefficients are zero, our degree function returns 0 (although mathema- ticians usually use -1 for the degree of such a polynomial). The next_term function returns the exponent of the next term with a nonzero coefficient after xk. For example, if the x term of p is 0 and the x* term of p is 6x*, then p.next_term(2) returns the exponent 4 (since 4 is the next exponent after 2 with a nonzero coefficient). If there are no nonzero terms after xk, then next_term(k) should return the constant UINT_MAX from the library facility . (This constant is the largest unsigned int.) F. Evaluation functions. double eval(double x) const; double operator (double x) const; The eval function evaluates a polynomial at the given value of x. For example, if p is 0.3x +0.5x2 -0.9x +1.0, then p.eval(2) is 0.3(2) +0.5(2)2 - 0.9(2) +1.0, which is 3.6. The second function also evaluates the polynomial, but it does so with some strange syntax. The name of this second function is operator O," and it has one parameter (the double number x). To activate the operator () for a poly- nomial p, you write the name p followed by the parameter in parentheses. For example: p(2). The implementation of the operator () does the same work as the eval function; the two separate implementations just give the programmer a choice of syntax. You can write p.eval(2), or you can write p(2) in a program. G. Arithmetic operators. You can overload the binary arithmetic operators of addition, subtraction, and multiplication to add, subtract, and multiply two poly- nomials in the usual manner. (Division is not possible, because it can result in fractional exponents. For example: Suppose q = 2x + 4x + 3x +1 and r = 7x + 6x +5. Then: q+r = 2x + 11x2 + 9x +6 9- r = 2x3 - 3x2 3x - 4 qxr = 14x" + 40x* +55x +45x +21x +5 The product, qxr, is obtained by multiplying each separate term of q times each separate term of r and adding the results together. 1. Read chapter 4 from the textbook and understand the discussion of dynamic memory allocation and pointer variables. Understand how to work with pointer variables and dynamic memory allocation. Study the re-implement class bag using dynamic arrays. 2. Work the programming project Polynomial" (discussed in section 4.6) to develop C++ program poly1.cpp that uses a dynamic array to implement the requirements A through G as specified in the textbook, pages 213 - 215. 3. In part G, do not implement the multiplication (*) operation. 4. Any other functions you may need to implement the specified functions should be done as private functions. 5. The following files are provided for your reference. You should start by modifying the header file first. a. poly1.h: The header file for the polynomial class. b. Polytesti.cpp: A small interactive test program. Important note: The provided file Polytest1.cpp requires some modification to work for our header file. Re-use this text program as you see fit. 6. The test program is given to guide you in developing your own text program for the functions required for this assignment. The test program shows how to test other functions that are not part of this assignment, please ignore such functions. A. Constructors and destructor. polynomial(); // Default constructor polynomial(double al); // Set the x coefficient only polynomial(const polynomial& source); // Copy constructor -polynomial(); The default constructor creates a polynomial with all zero coefficients. The sec- ond constructor creates a polynomial with the specified parameter as the coeffi- cient of the x term, and all other coefficients are zero. For example: polynomial p(4.2); // p has only one nonzero term, 4.2x", which is the // same as the number 4.2 (since x is defined as // equal to 1). B. Assignment operator. polynomial& operator = (const polynomial& source); This is the usual overloaded assignment operator, with one change: The return type is polynomial& rather than void. This return type is similar to an ordi- nary polynomial, but the extra symbol & makes it a reference return type, similar to the return type ostream& of our output operators. The complete details of a reference return type are beyond this project. For your implementation, you should know two facts: 1. The function implementation should return the object that activated the assignment. This is accomplished with the keyword this (which we also saw on page 188). The syntax is: return * this; , which means return the object that this points to." Since this always points to the object that activates the function, the return statement has the effect that we need. 2. Using polynomial& as the return type permits a sequence of chained assignments. For example, if a, b, and c are three polynomials, we can write a = b = c, which copies the value of c to b, and then copies the new value of b to a (chained assignments work from right to left). Remember to have your implementation check for a possible self-assignment. C. A second assignment operator. polynomial& operator =(double a0); For a polynomial b, this assignment can be activated in a statement such as b = 4.2. The double number, 4.2, becomes the argument al for this assign- ment. The implementation will use this number as the coefficient for the x term, and all other coefficients are set to zero. If you read the information on constructor-generated conversions (page 210), then you might notice that this second version of the assignment operator isn't entirely needed. Even without this assignment operator, we could write an assignment b = 4.2; in this case, the compiler would apply the polynomial constructor to the number 4.2 (creating the polynomial 4.2x), and then this poly- nomial would be assigned to b. However, writing an explicit assignment operator to allow b = 4.2 is generally more efficient because we avoid the overhead of the constructor-generated conversion. D. Modification member functions. void add_to_coef(double amount, unsigned int k); void assign_coef(double new_coefficient, unsigned int k); void clear(); void reserve(size_t number); The add_to_coef function adds the specified amount to the coefficient of the xk term. The assign_coef function sets the xk coefficient to new_coefficient. In both cases, the parameter k is an unsigned int, which is the C++ data type that is like an int, but may never have a negative value. The clear function sets all coefficients to zero. The reserve function works like reserve for the bag class, making sure that the underlying array has at least the requested size. E. Constant member functions. double coefficient (unsigned int k) const; unsigned int degree( const; unsigned int next_term(unsigned int k) const; The coefficient function returns the coefficient of the xk term. The degree function returns the degree of the polynomial. For a polynomial where all coefficients are zero, our degree function returns 0 (although mathema- ticians usually use -1 for the degree of such a polynomial). The next_term function returns the exponent of the next term with a nonzero coefficient after xk. For example, if the x term of p is 0 and the x* term of p is 6x*, then p.next_term(2) returns the exponent 4 (since 4 is the next exponent after 2 with a nonzero coefficient). If there are no nonzero terms after xk, then next_term(k) should return the constant UINT_MAX from the library facility . (This constant is the largest unsigned int.) F. Evaluation functions. double eval(double x) const; double operator (double x) const; The eval function evaluates a polynomial at the given value of x. For example, if p is 0.3x +0.5x2 -0.9x +1.0, then p.eval(2) is 0.3(2) +0.5(2)2 - 0.9(2) +1.0, which is 3.6. The second function also evaluates the polynomial, but it does so with some strange syntax. The name of this second function is operator O," and it has one parameter (the double number x). To activate the operator () for a poly- nomial p, you write the name p followed by the parameter in parentheses. For example: p(2). The implementation of the operator () does the same work as the eval function; the two separate implementations just give the programmer a choice of syntax. You can write p.eval(2), or you can write p(2) in a program. G. Arithmetic operators. You can overload the binary arithmetic operators of addition, subtraction, and multiplication to add, subtract, and multiply two poly- nomials in the usual manner. (Division is not possible, because it can result in fractional exponents. For example: Suppose q = 2x + 4x + 3x +1 and r = 7x + 6x +5. Then: q+r = 2x + 11x2 + 9x +6 9- r = 2x3 - 3x2 3x - 4 qxr = 14x" + 40x* +55x +45x +21x +5 The product, qxr, is obtained by multiplying each separate term of q times each separate term of r and adding the results together

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!