You are to write a class named Polynomial. In what follows, we do not describe all the implementation details, but just

Business, Finance, Economics, Accounting, Operations Management, Computer Science, Electrical Engineering, Mechanical Engineering, Civil Engineering, Chemical Engineering, Algebra, Precalculus, Statistics and Probabilty, Advanced Math, Physics, Chemistry, Biology, Nursing, Psychology, Certifications, Tests, Prep, and more.
Post Reply
answerhappygod
Site Admin
Posts: 899604
Joined: Mon Aug 02, 2021 8:13 am

You are to write a class named Polynomial. In what follows, we do not describe all the implementation details, but just

Post by answerhappygod »

You Are To Write A Class Named Polynomial In What Follows We Do Not Describe All The Implementation Details But Just 1
You Are To Write A Class Named Polynomial In What Follows We Do Not Describe All The Implementation Details But Just 1 (125.53 KiB) Viewed 46 times
You are to write a class named Polynomial. In what follows, we do not describe all the implementation details, but just provide the main functionality that must be provided by the class. Your class will be tested using this functionality. If it is indicated that you MUST use a structure or perform a certain task in the class or in a function, do so. Otherwise, you are free to make the choice by yourself. Of course, you are expected to perform good programming practices of object oriented programming, e.g. conforming data encapsulation principle. IMPORTANT: Recall how to allocate objects using smart pointers. Definition: unique_ptr<Polynomial> pi Allocation: unique_ptr<Polynomial> p (new Polynomial); (1) An object of the class Polynomial represents a mathematical polynomial in the usual sense. Here are some examples of polynomials expressed by starting from the exponent 0 (which represents the constant of the polynomial) and listing the terms in ascending exponents: 3 + x + x^3 + 7*x^6 x^2 + 1000*x^4 Here x^k represents x raised to the power k, and is the multiplication operator. Note that when the coefficient of a specific term is 0, we do not list it for saving space. Thus, for example, the second polynomial above is in fact 0 + 0*x + x^2 + 0*x^3 + 1000*x^4 But we only list the two non-zero terms. Note also that we do not write the coefficient and exponent 1. (2) The class Polynomial MUST keep vector objects of C++ to represent a polynomial as above. You may assume that the coefficients of the polynomial are integers. Note that in order to represent a term, you must keep both the exponent and the coefficient. Thus, it might be a good idea to use two different data structures, one for exponents, and one for coefficients. For example, the second polynomial above might have the values [2, 4] in the exponents vector, and [1, 1000) in the coefficients vector. Provide two constructors, one the default constructor creating the 0 polynomial, and the other taking two vectors (exponents and coefficients) representing the polynomial. (3) The class Polynomial has the following member function static unique_ptr<Polynomial> add (unique_ptr<Polynomial> &pl, unique_ptr<Polynomial> sp2); It adds the polynomials represented by pl and p2, and returns a smart pointer to a new Polynomial object representing the result. Please, BE CAREFUL: You should not represent the terms with coefficient O. Furthermore, if there are terms in one polynomial not appearing in the other, you may have to create new terms in the result. (4) The class Polynomial has the following member function static unique_ptr<Polynomial> subtract (unique_ptr<Polynomial> ipl, unique_ptr<Polynomial> <p2); It subtracts the polynomial p2 from pl, and returns a smart pointer to a Polynomial object representing the result. [Hint: This is easy once you implement add). (5) The class Polynomial has the following member function static unique_ptr<Polynomial> multiply (unique_ptr<Polynomial> spl, unique_ptr<Polynomial> 5p2); It multiplies the polynomials represented by pl and p2, and returns a new Polynomial object representing the result. Please, BE CAREFUL: Multiplication of polynomials are more complicated than addition. If you do not know how to do this, please learn it from an appropriate source. This is usually taught in high school. (6) The class Polynomial has the following member function void print (); This prints out the underlying Polynomial object, based on the information contained in the vectors, in the format given in 1).
Join a community of subject matter experts. Register for FREE to view solutions, replies, and use search function. Request answer by replying!
Post Reply