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

Your solution that you submit should consist of two (2) files: Polynomial.h (cla

ID: 3705943 • Letter: Y

Question

Your solution that you submit should consist of two (2) files: Polynomial.h (class specification file) Polynomial.cpp (class implementation file) The application program (Polynomial_app.cpp) has already been completed for you. Your Polynomial class should work with the Polynomial_app.cpp application program that has been given to you.

Sample Run (using polynomial_app.cpp):

This program should not be modified:

polynomial_app.cpp

#include <iostream>
#include "Polynomial.h"
using namespace std;

int main()
{
Polynomial a, b, c, t;

a.enterTerms();
b.enterTerms();
t = a; // save the value of a
cout << " First polynomial is: ";
a.printPolynomial();
cout << " Second polynomial is: ";
b.printPolynomial();
cout << " Adding the polynomials yields: ";
c = a + b;
c.printPolynomial();
cout << " += the polynomials yields: ";
a += b;
a.printPolynomial();
cout << " Subtracting the polynomials yields: ";
a = t; // reset a to original value
c = a - b;
c.printPolynomial();
cout << " -= the polynomials yields: ";
a -= b;
a.printPolynomial();
cout << endl;

system("PAUSE"); //remove this if you are not using windows
return 0;
} // end main

Develop class Polynomial. The internal representation of a Polynomial is an array or vector of terms. Each term contains a coefficient and an exponent, e.g., the term 2x has the coefficient 2 and the exponent 4. Develop a complete class containing proper constructor and destructor functions as well as set and get functions. The class should also provide the following overloaded operator capabilities: a. Overload the addition operator (+) to add two Polynomials b. Overload the subtraction operator) to subtract two Polynomials. c. Overload the assignment operator () to assign one Polynomial to another. d. Overload the addition assignment operator (+) e. Overload the subtraction assignment operator () Beyond overloading these operators, the code in the polynomial_app.cpp will give you an idea of what member functions you need to implement in the Polynomial class

Explanation / Answer

struct Term
{
int coefficient;
int exponent;
Term(int coef, int exp) : coefficient{ coef }, exponent{ exp } {}
};

struct TermComparator
{
bool operator()(const Term& lhs, const Term& rhs){
retutn lhs.exponent<rhs,exponent;
}
};

class Polynomial
{
private :
set<Term, TermComparator> terms;
public:
Polynomial();
~Polynomial();
enterTerms();
Polynomial operator+(Polynomial p);
Polynomial operator-(Polynomial p);
Polynomial operator+=(Polynomial p);
Polynomial operator-=(Polynomial p);
Polynomial operator=(Polynomial p);
};

//---------------------------------------------------------------------------------------------------


void Polynomial::enterTerms()
{
int n,c,e;
cout<<" Enter number of polynomial terms :";
cin>>n;
for(int i=0; i<n; i++)
{
cout<<" Enter coefficient :";
cin>>c;
cout<<"hEnter exponent :";
cin>>e;
terms.insert(Term(c,e));
}
}

//-----------------------------------------------------------------------------------------------------

void Polynomial::printPolynomial()
{
auto it = terms.end();
if(it->exponent == 0)
{ cout<<it->coefficient<<"+";
terms.erase(it);
}
auto my_it = terms.begin();
while (my_it != terms.end())
{
cout<<my_it->coefficient<<"x^"<<my_it->exponent;
if(my_it != terms.end())
cout<<"+";
}
}

//------------------------------------------------------------------------------------------------

Polynomial Polynomial::operator+(Polynomial p)
{
auto my_it = terms.begin();
auto p_it = p.terms.begin();
Polynomial result;

while (my_it != terms.end() && p_it != p.terms.end())
{
if (my_it->exponent > p_it->exponent)
{
result.terms.insert(*my_it);
++my_it;
}
else if (my_it->exponent == p_it->exponent)
{
result.terms.insert(Term(my_it->coefficient + p_it->coefficient, my_it->exponent));
++my_it;
++p_it;
}
else
{
result.terms.insert(*p_it);
++p_it;
}
}

//only one of the for loops will be effective
for (; my_it != terms.end(); ++my_it) result.terms.insert(*my_it);
for (; p_it != p.terms.end(); ++p_it) result.terms.insert(*p_it);

return result;
}

//---------------------------------------------------------------------------------------------------------

Polynomial Polynomial::operator-(Polynomial p)
{
auto my_it = terms.begin();
auto p_it = p.terms.begin();
Polynomial result;

while (my_it != terms.end() && p_it != p.terms.end())
{
if (my_it->exponent > p_it->exponent)
{
result.terms.insert(*my_it);
++my_it;
}
else if (my_it->exponent == p_it->exponent)
{
if(my_it->coefficient > p_it->coefficient)
result.terms.insert(Term(my_it->coefficient - p_it->coefficient, my_it->exponent));
else
result.terms.insert(Term(p_it->coefficient - my_it->coefficient, my_it->exponent));
++my_it;
++p_it;
}
else
{
result.terms.insert(*p_it);
++p_it;
}
}

//only one of the for loops will be effective
for (; my_it != terms.end(); ++my_it) result.terms.insert(*my_it);
for (; p_it != p.terms.end(); ++p_it) result.terms.insert(*p_it);

return result;
}


//----------------------------------------------------------------------------------------------------------

Polynomial Polynomial::operator+=(Polynomial p)
{
auto my_it = terms.begin();
auto p_it = p.terms.begin();
Polynomial result;

while (my_it != terms.end() && p_it != p.terms.end())
{
if (my_it->exponent > p_it->exponent)
{
result.terms.insert(*my_it);
++my_it;
}
else if (my_it->exponent == p_it->exponent)
{
result.terms.insert(Term(my_it->coefficient + p_it->coefficient, my_it->exponent));
++my_it;
++p_it;
}
else
{
result.terms.insert(*p_it);
++p_it;
}
}

//only one of the for loops will be effective
for (; my_it != terms.end(); ++my_it) result.terms.insert(*my_it);
for (; p_it != p.terms.end(); ++p_it) result.terms.insert(*p_it);

terms = result; //assign this result to current polynomial itself
return terms ;
}


//---------------------------------------------------------------------------------------------------------------

Polynomial Polynomial::operator-=(Polynomial p)
{
auto my_it = terms.begin();
auto p_it = p.terms.begin();
Polynomial result;

while (my_it != terms.end() && p_it != p.terms.end())
{
if (my_it->exponent > p_it->exponent)
{
result.terms.insert(*my_it);
++my_it;
}
else if (my_it->exponent == p_it->exponent)
{
if(my_it->coefficient > p_it->coefficient)
result.terms.insert(Term(my_it->coefficient - p_it->coefficient, my_it->exponent));
else
result.terms.insert(Term(p_it->coefficient - my_it->coefficient, my_it->exponent));
++my_it;
++p_it;
}
else
{
result.terms.insert(*p_it);
++p_it;
}
}

//only one of the for loops will be effective
for (; my_it != terms.end(); ++my_it) result.terms.insert(*my_it);
for (; p_it != p.terms.end(); ++p_it) result.terms.insert(*p_it);

terms = result; //assign this result to current polynomial itself
return result;
}

//--------------------------------------------------------------------------------------------------------------------


Polynomial Polynomial::operator=(Polynomial p)
{
terms.clear();
terms = p.terms;
}