C++ Definition: unique_ptr p; Allocation: unique_ptr p(new Polynomial); (1) An object of the cla
Posted: Sat May 14, 2022 7:16 pm
C++
Definition: unique_ptr<Polynomial> p;
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> &p1,
unique_ptr<Polynomial> &p2);
It adds the polynomials represented by p1 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 0. 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> &p1,
unique_ptr<Polynomial> &p2);
It subtracts the polynomial p2 from p1, 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> &p1,
unique_ptr<Polynomial> &p2);
It multiplies the polynomials represented by p1 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).
Of course, test your code in the main function.
IMPORTANT Submission Instructions: You are to submit the whole
project (preferably in CodeBloacks, but might
be in another IDE), containing the header file Polynomial.h,
together with main.cpp testing your program
Definition: unique_ptr<Polynomial> p;
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> &p1,
unique_ptr<Polynomial> &p2);
It adds the polynomials represented by p1 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 0. 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> &p1,
unique_ptr<Polynomial> &p2);
It subtracts the polynomial p2 from p1, 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> &p1,
unique_ptr<Polynomial> &p2);
It multiplies the polynomials represented by p1 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).
Of course, test your code in the main function.
IMPORTANT Submission Instructions: You are to submit the whole
project (preferably in CodeBloacks, but might
be in another IDE), containing the header file Polynomial.h,
together with main.cpp testing your program