For this project you will create a polynomial class to repre

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 -1 1 0 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^2 - 3x + 1) - (2x - 2) returns (x^2 - 5x + 3) The following friend function operators are required: operator

Solution

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 < power-1; i += 1) {
result *= (*this);
}
this->_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 << std::endl;
}

void
Polynomial::_show (Node* l, int level) {
if (length(l) == 0) {
return;
}
_show(tail(l), level+1);
real coeff = head(l);
if (coeff != 0) {
std::stringstream coeff_str;
std::stringstream level_str;

if (coeff > 0 && (length(l) > 1)) {
coeff_str << \"+\";
} else if (coeff < 0) {
coeff_str << \"-\";
coeff = -coeff;
}
if (!(coeff == 1.0 && level != 0)) {
coeff_str << coeff;
}

if (level == 0) {
level_str << \"\";
} else if (level == 1) {
level_str << \"x\";
} else if (level > 0) {
level_str << \"x^\" << level;
} else if (level < 0) {
level_str << \"x^(\" << level << \")\";
}
std::cout << coeff_str.str() << level_str.str();
}
}

real
Polynomial::valueAt (real x) {
return std::pow(x, _base) * _value0(_coeff, x);
}

real
Polynomial::_value0 (Node* l, int x) {
if (length(l) == 0) return 0;
return head(l) + x * _value0(tail(l), x);
}

 For this project you will create a polynomial class to represent polynomial expressions and write functions to operate on them. Some examples of polynomial exp
 For this project you will create a polynomial class to represent polynomial expressions and write functions to operate on them. Some examples of polynomial exp
 For this project you will create a polynomial class to represent polynomial expressions and write functions to operate on them. Some examples of polynomial exp
 For this project you will create a polynomial class to represent polynomial expressions and write functions to operate on them. Some examples of polynomial exp

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site