The internal representation of a polynomial is an array of terms. Each term cont
ID: 3544501 • Letter: T
Question
The internal representation of a polynomial is an array of terms. Each term contains a coefficient and an exponent. For example, term 2x4 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 multiplication operator (*) to multiply two polynomials.
e) Overload the addition assignment operator (+=), subtraction assignment operator (-=) and multiplication assignment operator (*=)
f) Overload the output operator (<<) so that it can display the polynomials. For example, if in the main function we have:
sample output:
Enter the number of polynomial terms: 3
Enter coefficient and exponent : 1 2
Enter coefficient and exponent: 3 4
Enter coefficient and exponent: 4 5
Explanation / Answer
#include<iostream>
#include<cmath>
using namespace std;
int displayMenu();
const int MAXSIZE=10;
class Polynomial{
private:
int x, numOfTerms, termsArray[MAXSIZE];
public:
//constructor
Polynomial()
{
x=0;
numOfTerms=1;
for (int i=0; i<MAXSIZE; i++)
termsArray[i]=0;
}
//----------------------------------------------------
//setting polynomial
void setPolynomial()
{
int expo ; // exponent
int xValue;
int nTerms;
double coef; // coefficient
cout<<"Enter the value of x : ";
cin>>xValue;
x = xValue;
cout<<endl<<"Enter the number of terms : ";
cin>>nTerms;
numOfTerms = nTerms;
for(int i = 0 ; i < numOfTerms ; ++i )
{
cout<<endl<<"***** TERM # "<<i+1<<"******";
cout<<endl<<"Enter the exponent : ";
cin>>expo;
cout<<endl<<"Enter the coefficient : ";
cin>>coef;
termsArray[expo] = coef;
}// end for
}//end function setPolynomial
//-----------------------------------------------------------------
void print()
{
bool firstTermWritten = false;
cout<<endl<<"The polynomial is :"<<endl;
for(int expo = MAXSIZE-1 ; expo >= 0 ; --expo)
{
if(termsArray[expo] != 0) // term not zero(term coefficient not
zero)
if(expo == 0) // constant term
cout<<" + "<<termsArray[expo]; //display constant term
without nx0
else // not constant term
{
if(!firstTermWritten)// then don't preceed the term with
+
{
cout<<termsArray[expo]<<"x"<<expo;
firstTermWritten = true;
}
else // first term written before, then preceed the term
with +
cout<<" + "<<termsArray[expo]<<"x"<<expo;
}
}// end for
cout<<endl;
}// end function print
//-----------------------------------------------------------------------------
//Evaluating the polynomial
void evaluate()
{
double val = 0;
double xToExpo;
int num;
cout<<"Enter the value of x to evaluate the polynomial "<<endl;
cin>>num;
x = num;
for( int expo = 0 ; expo < MAXSIZE ; ++expo )
{
xToExpo = pow((double)x,expo);
val += termsArray[expo] * xToExpo;
}
cout<<endl<<"The value of the polynomial when x = "<<x<<" is "<<val<<endl;
}// end function evaluate
//------------------------------------------------------------
Polynomial operator+(Polynomial p2)
{
Polynomial tempPoly;
for( int expo = 0 ; expo < MAXSIZE ; ++expo)
tempPoly.termsArray[expo] = termsArray[expo] + p2.termsArray[expo] ;
return tempPoly;
}// end function operator+
//---------------------------------------------------------------------
Polynomial operator-(Polynomial p2)
{
Polynomial tempPoly;
for( int expo = 0 ; expo < MAXSIZE ; ++expo)
tempPoly.termsArray[expo] = termsArray[expo] - p2.termsArray[expo] ;
return tempPoly;
}// end function operator+
//---------------------------------------------------------------------
Polynomial operator=(Polynomial p1)
{
for( int expo = 0 ; expo < MAXSIZE ; ++expo )
termsArray[expo] = p1.termsArray[expo]; // assign the terms
x = p1.x; // assign x
numOfTerms = p1.numOfTerms; // assign number of terms
//end Assignment
return *this;
}// end function operator=
//---------------------------------------------------------------------
Polynomial operator *(int n)
{
Polynomial tempPoly;
for( int expo = 0 ; expo < MAXSIZE ; ++expo )
tempPoly.termsArray[expo] = termsArray[expo] * n;
return tempPoly;
}// end function operator*
};
int main()
{
int choice;
bool choose_set_or_exit = false;
bool exit = false;
Polynomial polyno1, polyno2, polyno3;
do // to drive the menu
{
do // to test if the user choose set before print,display.....etc
{
choice = displayMenu();
if ( choice == 1 || choice == 8 )
choose_set_or_exit = true ;
else // choice == 2 -> 8
if ( choose_set_or_exit == false )
{
cout<<endl<<"There is no polynomial for processing choose Set
before or Exit !"<<endl<<endl;
system("pause"); // print the message Press any key to
continue . . .
system("cls");
}
}while (choose_set_or_exit == false);
//-----------------------------------
system("cls");//Clear the screen
//-----------------------------------
switch(choice)
{
case 1: // Set
polyno1.setPolynomial();
break;
case 2: // Print
polyno1.print();
break;
case 3: // Evaluate
polyno1.evaluate();
break;
case 4: // Add
cout << " Set the second polynomial ";
polyno2.setPolynomial();
polyno3=polyno1+polyno2;
polyno3.print();
break;
case 5: // Subtract
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.