Register Now

Login

Lost Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Login

Register Now

Welcome to All Test Answers

Develop class Polynomial using C++ programming – Operator Overloading

 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
2x^4
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 (*=).

Answer:

Test program


#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:\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;
} 


 

Polynomial member-function definitions


#include <iostream> 
#include <iomanip> 
#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
      } 
      
      start = 2;
   } 

   // 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 < maxTerms; ++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;
      }
   }

   return *this;
} 

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 ];
   }

   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;
} 

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 ];
   }

   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
         } 

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

   return temp;
}

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

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;
            }

   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;
   }

   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;
}

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

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;
         } 

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

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

         if ( !found ) // add term
         {
            coefficients[ term ] += c;
            exponents[ term ] = e;
         } 
      }
   } 
} 

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

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

int Polynomial::getTermCoefficient( int term ) const
{
   return coefficients[ term ];
} 

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;
} 

// destructor
Polynomial::~Polynomial()
{
   // empty destructor
} 

 

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
}; 

#endif

About

Leave a reply

Captcha Click on image to update the captcha .

error: Content is protected !!