Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Complete a polynomial class with overload operator functions. For this project y

ID: 3775502 • Letter: C

Question

Complete a polynomial class with overload operator functions.

For this project you will create a polynomial class to represent polynomial expressions and write functions to operate on them. Some examples of polynomial expressions include (x^2 + 2x + 4), (4x^3 - x^2 + x), (x^4 + 5x + 2), or (x - 2). The class should contain the following private data members: An integer array representing the coefficients of each term and the constant. An integer representing the maximum degree of the polynomial. As an example: 4x^3 - x^2 + x would be represented by an integer array with values 4-110 and an integer value of 3 representing the degree. The following member function operators are required: default constructor - Initializes the degree to default size of 3. parameterized constructor - Takes an integer representing the degree and an int* pointing to an array of coefficients and the constant. copy constructor destructor solve: Evaluates the polynomial at an integer (parameter) value and returns the result. As an example: x^2 + 2 when evaluated at 5 returns 27. operator =: Copies one polynomial to another. operator ==: Returns true if two polynomials are equal. operator !=: Returns true if two polynomials are not equal. operator *: Multiplies together two polynomial expressions and returns a new polynomial of the product. This should work for polynomials of different sizes. As an example: (x^2 + 2x + 1) * (x + 3) returns (x^3 + 5x^2 + 7x + 3). operator *: Takes an integer parameter scalar and returns a new scaled polynomial. As an example: (x^3 + 2x^2 - 3x - 4) * 5 returns (5x^3 + 10x^2 - 15x - 20). operator +: Takes two polynomials and adds their coefficients and returns a new polynomial of the sum. As an example: (x^2 - 3x + 1) + (2x - 2) returns (x^2 - x -1). operator -: Takes two polynomials and subtracts their coefficients and returns a new polynomial of the difference. As an example: (x - 3x + 1) - (2x - 2) returns (x^2 -5x + 3) The following friend function operators are required: operator NestedLessLess: Outputs the polynomial to screen. It can be assumed that the variable being used is 'x'. An example output: x^3 - 3x + 2. Do not print out any term with a zero coefficient. operator NestedGreaterGreater: Reads in from file the degree of the polynomial, then reads in all of the coefficients. See below for data file organization. The data file should be organized as follows: Create a data file that contains at least three pairs of polynomials of varying degrees. The first line in the data file should contain the number of polynomials listed. Each subsequent line in the file will represent a polynomial with the degree of the polynomial followed by its coefficients and constant. A missing degree term or missing constant will be represented by a zero. As an example the polynomial 3x^4+ 2x^2 - 5 would be represented in the file as: 4 3 0 2 0 -5 You are also required to write a main driver that tests all of the functionality. You should test using your polynomial pairs from the data file. To test the solve function and the operator* (scalar) function, both of which require an integer, prompt the user for that value. Const must be used as appropriate. No setter/getter functions are needed for this project. You are now allowed to use arrow notation, bracket notation, the string class, and the string functions provided by the standard library. All other requirements such as documentation, etc. are required as indicated in prior projects. Turn in your class specification file, class implementation file, main driver, make file, and documentation file.

Explanation / Answer

class Polynomial { private: int _base; Node* _coeff; void _show (Node* l, int level); real _value0 (Node* l, int x); public: Polynomial (void); Polynomial (int base, Node* coeff); Polynomial (const Polynomial& a); // clone ~Polynomial (); // delete Polynomial& operator= (const Polynomial& rhs); // assignment Polynomial& operator+= (const Polynomial& rhs); // add const Polynomial operator+ (const Polynomial& other) const; //add const Polynomial operator- (void) const; //neg Polynomial& operator-= (const Polynomial& rhs); // subtract const Polynomial operator- (const Polynomial& other) const; //subtract Polynomial& operator*= (const Polynomial& rhs); // multiply const Polynomial operator* (const Polynomial& other) const; //multiply Polynomial& operator^= (int power); // power const Polynomial operator^ (int power) const; //power real valueAt (real x); // valueAt void show (void); // print }; ////////////Implementation Below///////////// Polynomial::Polynomial (void) { _base = 0; _coeff = EMPTY; } Polynomial::Polynomial (int base, Node* coeff) { _base = base; _coeff = copy(coeff); } Polynomial::Polynomial (const Polynomial& a) { _base = a._base; _coeff = copy(a._coeff); } Polynomial::~Polynomial (void) { freeAll(_coeff); } Polynomial& Polynomial::operator= (const Polynomial& rhs) { if (this == &rhs) return *this; freeAll(this->_coeff); this->_base = rhs._base; this->_coeff = copy(rhs._coeff); return *this; } Polynomial& Polynomial::operator+= (const Polynomial& rhs) { Node* temp_coeff_1; Node* temp_coeff_2; int result_base; if (rhs._base > this->_base) { result_base = this->_base; temp_coeff_1 = copy(rhs._coeff); for (int i = 0; i < (rhs._base - this->_base); i += 1) { temp_coeff_1 = prepend(temp_coeff_1, 0); } temp_coeff_2 = this->_coeff; } else { result_base = rhs._base; temp_coeff_1 = copy(this->_coeff); for (int i = 0; i < (this->_base - rhs._base); i += 1) { temp_coeff_1 = prepend(temp_coeff_1, 0); } temp_coeff_2 = rhs._coeff; } Node* result_coeff = add(temp_coeff_1, temp_coeff_2); freeAll(temp_coeff_1); freeAll(this->_coeff); this->_base = result_base; this->_coeff = result_coeff; return *this; } const Polynomial Polynomial::operator+ (const Polynomial& other) const { Polynomial result = *this; result += other; return result; } const Polynomial Polynomial::operator- (void) const { Polynomial result = *this; Node* coeff = scalar_multiply(this->_coeff, -1.0); freeAll(result._coeff); result._coeff = coeff; return result; } Polynomial & Polynomial::operator-= (const Polynomial& rhs) { Polynomial result = *this + (-rhs); this->_base = result._base; freeAll(this->_coeff); this->_coeff = copy(result._coeff); return *this; } const Polynomial Polynomial::operator- (const Polynomial& rhs) const { Polynomial result = *this; result -= rhs; return result; } Polynomial& Polynomial::operator*= (const Polynomial& rhs) { this->_base = this->_base + rhs._base; Node* result_coeff = multiply(this->_coeff, rhs._coeff); freeAll(this->_coeff); this->_coeff = result_coeff; return *this; } const Polynomial Polynomial::operator* (const Polynomial& other) const { Polynomial result = *this; result *= other; return result; } Polynomial& Polynomial::operator^= (int power) { assert (power >= 1); Polynomial result = (*this); for (int i = 0; i _base = result._base; freeAll(this->_coeff); this->_coeff = copy(result._coeff); return *this; } const Polynomial Polynomial::operator^ (int power) const { Polynomial result = *this; result ^= power; return result; } void Polynomial::show (void) { _show(_coeff, _base); std::cout 1)) { coeff_str
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote