(Polynomial Class) Develop class Polynomial. The internal representation of a Polynomial is an array of terms. Each term contains a coefficient and an exponent, e.g., the 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 (*=).


```
// Polynomial class definition.
#ifndef POLYNOMIAL_H
#define POLYNOMIAL_H

class Polynomial
{
public:
static const int maxTerms = 100; // maximum number of terms

Polynomial();
Polynomial operator+( const Polynomial & ) const; // addition
Polynomial operator-( const Polynomial & ) const; // subtraction
Polynomial operator*( const Polynomial & ) const; // multiplication
Polynomial &operator=( const Polynomial & ); // assignment
Polynomial &operator+=( const Polynomial & );
Polynomial &operator-=( const Polynomial & );
Polynomial &operator*=( const Polynomial & );
void enterTerms();
void printPolynomial() const;
int getNumberOfTerms() const; // user can only retrieve value
int getTermExponent( int ) const;
int getTermCoefficient( int ) const;
void setCoefficient( int, int ); // set coefficient of a specific term
~Polynomial(); // destructor
private:
int numberOfTerms;
int exponents[ maxTerms ]; // exponent array
int coefficients[ maxTerms ]; // coefficients array
static void polynomialCombine( Polynomial & ); // combine common terms
}; // end class Polynomial

#endif
```
```
// Polynomial member-function definitions.
#include
#include
#include "Polynomial.h"
using namespace std;

Polynomial::Polynomial()
{
for ( int t = 0; t < maxTerms; t++ )
{
coefficients[ t ] = 0;
exponents[ t ] = 0;
} // end for

numberOfTerms = 0;
} // end Polynomial constructor

void Polynomial::printPolynomial() const
{
int start;
bool zero = false;

if ( coefficients[ 0 ] ) // output constants
{
cout << coefficients[ 0 ];
start = 1;
zero = true; // at least one term exists
}
else
{
if ( coefficients[ 1 ] )
{
cout << coefficients[ 1 ] << 'x'; // constant does not exist
// so output first term
// without a sign
if ( ( exponents[ 1 ] != 0 ) && ( exponents[ 1 ] != 1 ) )
cout << '^' << exponents[ 1 ];

zero = true; // at least one term exists
} // end inner if

start = 2;
} // end else

// output remaining polynomial terms
for ( int x = start; x < maxTerms; x++ )
{
if ( coefficients[ x ] != 0 )
{
cout << showpos << coefficients[ x ] << noshowpos << 'x';

if ( ( exponents[ x ] != 0 ) && ( exponents[ x ] != 1 ) )
cout << '^' << exponents[ x ];

zero = true; // at least one term exists
} // end if
} // end for

if ( !zero ) // no terms exist in the polynomial
cout << '0';

cout << endl;
} // end function printPolynomial

Polynomial &Polynomial::operator=( const Polynomial &r )
{
exponents[ 0 ] = r.exponents[ 0 ];
coefficients[ 0 ] = r.coefficients[ 0 ];

for ( int s = 1; s < 100maxTerms; s++ )
{
if ( r.exponents[ s ] != 0 )
{
exponents[ s ] = r.exponents[ s ];
coefficients[ s ] = r.coefficients[ s ];
}
else
{
if ( exponents[ s ] == 0 )
break;

exponents[ s ] = 0;
coefficients[ s ] = 0;
} // end else
} // end for

return *this;
} // end function operator=

Polynomial Polynomial::operator+( const Polynomial &r ) const
{
Polynomial temp;
bool exponentExists;
int s;

// process element with a zero exponent
temp.coefficients[ 0 ] = coefficients[ 0 ] + r.coefficients[ 0 ];

// copy right arrays into temp object; s will be used to keep
// track of first open coefficient element
for ( s = 1; ( s < maxTerms ) && ( r.exponents[ s ] != 0 ); s++ )
{
temp.coefficients[ s ] = r.coefficients[ s ];
temp.exponents[ s ] = r.exponents[ s ];
} // end for

for ( int x = 1; x < maxTerms; x++ )
{
exponentExists = false; // assume exponent will not be found

for ( int t = 1; ( t < maxTerms ) && ( !exponentExists ); t++ )
if ( exponents[ x ] == temp.exponents[ t ] )
{
temp.coefficients[ t ] += coefficients[ x ];
exponentExists = true; // exponent found
} // end if

// exponent was not found, insert into temp
if ( !exponentExists )
{
temp.exponents[ s ] = exponents[ x ];
temp.coefficients[ s ] += coefficients[ x ];
s++;
} // end if
} // end for

return temp;
} // end function operator+

Polynomial &Polynomial::operator+=( const Polynomial &r )
{
*this = *this + r;
return *this;
} // end function operator+=

Polynomial Polynomial::operator-( const Polynomial &r ) const
{
Polynomial temp;
bool exponentExists;
int s;

// process element with a zero exponent
temp.coefficients[ 0 ] = coefficients[ 0 ] - r.coefficients[ 0 ];

// copy left arrays into temp object; s will be used to keep
// track of first open coefficient element
for ( s = 1; ( s < maxTerms ) && ( exponents[ s ] != 0 ); s++ )
{
temp.coefficients[ s ] = coefficients[ s ];
temp.exponents[ s ] = exponents[ s ];
} // end for

for ( int x = 1; x < maxTerms; x++ )
{
exponentExists = false; // assume exponent will not be found

for ( int t = 1; ( t < maxTerms ) && ( !exponentExists ); t++ )

if ( r.exponents[ x ] == temp.exponents[ t ] )
{
temp.coefficients[ t ] -= r.coefficients[ x ];
exponentExists = true; // exponent found
} // end if

// exponent was not found, insert into temp
if ( !exponentExists )
{
temp.exponents[ s ] = r.exponents[ x ];
temp.coefficients[ s ] -= r.coefficients[ x ];
s++;
} // end if
} // end for

return temp;

} // end function operator-


Polynomial &Polynomial::operator-=( const Polynomial &r )
{
*this = *this - r;
return *this;
} // end function operator-=

Polynomial Polynomial::operator*( const Polynomial &r ) const
{
Polynomial temp;
int s = 1; // subscript location for temp coefficient and exponent

for ( int x = 0; ( x < maxTerms ) &&
( x == 0 || coefficients[ x ] != 0 ); x++ )

for ( int y = 0; ( y < maxTerms ) &&
( y == 0 || r.coefficients[ y ] != 0 ); y++ )

if ( coefficients[ x ] * r.coefficients[ y ] )

if ( ( exponents[ x ] == 0 ) && ( r.exponents[ y ] == 0 ) )
temp.coefficients[ 0 ] +=
coefficients[ x ] * r.coefficients[ y ];
else
{
temp.coefficients[ s ] =
coefficients[ x ] * r.coefficients[ y ];
temp.exponents[ s ] = exponents[ x ] + r.exponents[ y ];
s++;
} // end else

polynomialCombine( temp ); // combine common terms
return temp;
} // end function operator*

void Polynomial::polynomialCombine( Polynomial &w )
{
Polynomial temp = w;

// zero out elements of w
for ( int x = 0; x < maxTerms; x++ )
{
w.coefficients[ x ] = 0;
w.exponents[ x ] = 0;
} // end for

for ( int x = 1; x < maxTerms; x++ )
{
for ( int y = x + 1; y < maxTerms; y++ )
if ( temp.exponents[ x ] == temp.exponents[ y ] )
{
temp.coefficients[ x ] += temp.coefficients[ y ];
temp.exponents[ y ] = 0;
temp.coefficients[ y ] = 0;
} // end if
} // end outer for

w = temp;
} // end function polynomialCombine

Polynomial &Polynomial::operator*=( const Polynomial &r )
{
*this = *this * r;
return *this;
} // end function operator*=

void Polynomial::enterTerms()
{
bool found = false;
int c, e, term;

cout << "\nEnter number of polynomial terms: ";
cin >> numberOfTerms;

for ( int n = 0; n < maxTerms && n < numberOfTerms; n++ )
{
cout << "\nEnter coefficient: ";
cin >> c;
cout << "Enter exponent: ";
cin >> e;

if ( c != 0 )
{
// exponents of zero are forced into first element
if ( e == 0 )
{
coefficients[ 0 ] += c;
continue;
} // end if

for ( term = 1; ( term < maxTerms ) &&
( coefficients[ term ] != 0 ); term++ )

if ( e == exponents[ term ] )
{
coefficients[ term ] += c;
exponents[ term ] = e;
found = true; // existing exponent updated
} // end if

if ( !found ) // add term
{
coefficients[ term ] += c;
exponents[ term ] = e;
} // end if
} // end outer if
} // end outer for
} // end function endTerms

int Polynomial::getNumberOfTerms() const
{
return numberOfTerms;
} // end function getNumberOfTerms

int Polynomial::getTermExponent( int term ) const
{
return exponents[ term ];
} // end function getTermExponent

int Polynomial::getTermCoefficient( int term ) const
{
return coefficients[ term ];
} // end function getTermsCoefficient

void Polynomial::setCoefficient( int term, int coefficient )
{
if ( coefficients[ term ] == 0 ) // no term at this location
cout << "No term at this location, cannot set term." << endl;
else // otherwise, set term
coefficients[ term ] = coefficient;
} // end function setTerm

// destructor
Polynomial::~Polynomial()
{
// empty destructor
} // end destructor
```
```
// Polynomial test program.
#include
#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:\n";
a.printPolynomial();
cout << "Second polynomial is:\n";
b.printPolynomial();
cout << "\nAdding the polynomials yields:\n";
c = a + b;
c.printPolynomial();
cout << "\n+= the polynomials yields:\n";
a += b;
a.printPolynomial();
cout << "\nSubtracting the polynomials yields:\n";
a = t; // reset a to original value
c = a - b;
c.printPolynomial();
cout << "\n-= the polynomials yields:\n";
a -= b;
a.printPolynomial();
cout << "\nMultiplying the polynomials yields:\n";
a = t; // reset a to original value
c = a * b;
c.printPolynomial();
cout << "\n*= the polynomials yields:\n";
a *= b;
a.printPolynomial();
cout << endl;
} // end main
```
Enter number of polynomial terms: 2
Enter coefficient: 2
Enter exponent: 2
Enter coefficient: 3
Enter exponent: 3
Enter number of polynomial terms: 3
Enter coefficient: 1
Enter exponent: 1
Enter coefficient: 2
Enter exponent: 2
Enter coefficient: 3
Enter exponent: 3
First polynomial is:
2x^2+3x^3
Second polynomial is:
1x+2x^2+3x^3
Adding the polynomials yields:
1x+4x^2+6x^3
+= the polynomials yields:
1x+4x^2+6x^3
Subtracting the polynomials yields:
-1x
-= the polynomials yields:
-1x
Multiplying the polynomials yields:
2x^3+7x^4+12x^5+9x^6
*= the polynomials yields:
2x^3+7x^4+12x^5+9x^6

Computer Science & Information Technology

You might also like to view...

Suppose we want to create a linear hash file with a file load factor of 0.7 and a blocking factor of 20 records per bucket, which is to contain 112,000 records initially.

(a) How many buckets should we allocate in primary areas? (b) What should be the number of bits used for bucket addresses?

Computer Science & Information Technology

Each individual characteristic in a record that displays as a single column in a datasheet

A) Subset B) Record C) Field

Computer Science & Information Technology

____ mode hides all the commands and just displays the document.

A. Full Screen B. Mouse C. Read D. Document

Computer Science & Information Technology

Anidselector is simply a pound sign (#) followed by theid____ for the element.

A. name B. alignment C. contact D. value

Computer Science & Information Technology