Answer to Question #276132 in C++ for Kimchi

Question #276132
Construct a polynomial class (Polynom) to represent polynomials of the form
P=a0+a1x+a2x^2+….+anx^n. Request:

Polynomials with dynamic memory management to hold coefficients (int bac; double *heso)
The coefficients of a polynomial of type double
It is possible to declare a polynomial with a given number of degrees, given array of coefficients.
By default, the polynomial has degree n=0, the coefficients are set to the initial value 0.0 except the last coefficient of order n is 1.0.
Defining the destructor
Has a copy constructor
Polynomial assignment can be performed using the = . sign
Write a main program using the above class
1
Expert's answer
2021-12-06T05:27:15-0500
#include <iostream>
#include <cassert>
using namespace std;

#include "polynomial.h"

// Constructor
Polynomial::Polynomial( )
{
   degree = -1;
   for ( int i = 0; i <= MAX_DEGREE; i++ )
      coef[i] = 0.0;
}

int Polynomial::getDegree( ) const
{
   return degree;
}

double Polynomial::getCoef( int k ) const
{
   if ( k <= degree )
      return coef[k];
   else
      return 0.0;  // all coefficients where the exponent is larger than the degree
                   // must be zero             
}

void Polynomial::setCoef(int k, double value)
{
   assert( k <= MAX_DEGREE );
   
   coef[k] = value;
   
   // fix the degree if necessary
   if ( value != 0.0 && k > degree )   // degree inhcreased (but not past maxDegree)
      degree = k;
   // if terms with largest exponents are 0, decrease degree
   int i = degree;
   while ( i >= 0 && coef[i] == 0.0  )
   {
      degree--;
      i--;
   }
}

//-- Definition of <<
ostream & operator<<(ostream & out, const Polynomial & p)
{
   if ( p.getDegree( ) == -1 )
   {
      cout << 0;
      return out;
   }      
   out << p.getCoef( p.getDegree( ) ) << "x^" << p.getDegree( );
   for (int i = p.getDegree( )-1; i >= 0; i--)
   {
      out << " + ";
      if ( p.getCoef( i ) < 0.0 )
         out << '(' << p.getCoef( i ) << ')';
      else
         out << p.getCoef( i ); 
      out << "x^" << i;
   }

   return out;
}

void Polynomial::clear( )
{
     for ( int i = 0; i <= degree; i++ )
        coef[i] = 0.0;
     degree = -1;
}

void Polynomial::fixDegree( )
{
   // if terms with largest exponents are 0, fix the degree
   for ( int i = degree; i >= 0; i-- )
      if ( coef[i] == 0.0 )
         degree--;
}

//-- Definition of evaluate()
double Polynomial::evaluate(double value) const
{
   double power = 1,
          result = 0;

   for (int i = 0; i <= degree; i++)
   {
      result += coef[i] * power;
      power *= value;
   }
   return result;
}

//-- Definition of +
Polynomial operator+(const Polynomial & leftPoly, const Polynomial & rightPoly )
{
   int leftDegree = leftPoly.getDegree( ), 
       rightDegree = rightPoly.getDegree( ),
       degree;
   double sum;
       
   if ( leftDegree > rightDegree )
      degree = leftDegree;
   else
      degree = rightDegree;
      
   Polynomial resPoly;
   
   for ( int i=degree; i >= 0; i-- )
   {
      sum = leftPoly.getCoef( i ) + rightPoly.getCoef( i );
      if ( sum != 0.0 )               // Don't need to set coefficients that are 0.0
         resPoly.setCoef( i, sum );   // Also makes sure that degree is correctly set
   }

   return resPoly;
}

USE THIS PROGRAM TO TEST THE ABOVE POLYNOMIAL CLASS

#include <cctype>       // Provides toupper
#include <iostream>     // Provides cout and cin
#include <cstdlib>      // Provides EXIT_SUCCESS
using namespace std;

#include "polynomial.h"  

// PROTOTYPES:
void print_menu( );
// Postcondition: A menu of choices has been printed.

int get_number( );
// Postcondition: The user has been prompted to enter an integer number. The
// number has been read, echoed to the screen, and returned by the function.

Polynomial readPolynomial( )
{
   int degree;
   double value;
   Polynomial poly;
   
   cout << "Enter degree for polynomial: ";
   cin >> degree;
   for ( int i=degree; i>=0; i-- )
   {
       cout << "Enter coefficient for x^" << i << ": ";
       cin >> value;
       poly.setCoef( i, value );
   } 
   return poly;         
}
     

int main( )
{
    char choice;   // Command entered by the user
    int num, exp;
    double value;
    Polynomial poly1, poly2;
    
    cout << "creating 2 polynomials" << endl;
    cout << "Initialize polynomial 1" << endl;
    poly1 = readPolynomial( );
    cout << "Initialize polynomial 2" << endl;
    poly2 = readPolynomial( );
    
    do
    {
        print_menu( );
        cout << "Enter choice: ";
        cin >> choice; 
        choice = toupper(choice);
        
        switch (choice)
        {
            case 'C': // clear
                      break;
            case 'E': // evaluate
                      break;
            case 'P': // print polynomial
                      cout << "Print polynomial 1 or 2: ";
                      cin >> num;
                      if ( num == 1 )
                         cout << "poly1 is " << poly1 << endl;
                      else if ( num == 2 )
                         cout << "poly2 is " << poly2 << endl;
                      else
                         cout << "There are only 2 polynomials - 1 and 2" << endl;   
                      break;
            case 'S': // set a polynomial
                      cout << "Set polynomial 1 or 2: ";
                      cin >> num;
                      if ( num == 1 )
                      {
                         cout << "Enter exponent to select term (or a negative number to quit): ";
                         cin >> exp;
                         while ( exp >= 0 )
                         {
                            cout << "Enter value of coefficient: ";
                            cin >> value;
                            poly1.setCoef( exp, value );
                            cout << "Enter exponent to select term (or a negative number to quit): ";
                            cin >> exp;
                         }
                         cout << "poly1 is " << poly1 << endl;
                      }
                      else if ( num == 2 )
                      {
                         cout << "Enter exponent to select term (or a negative number to quit): ";
                         cin >> exp;
                         while ( exp >= 0 )
                         {
                            cout << "Enter value of coefficient: ";
                            cin >> value;
                            poly2.setCoef( exp, value );
                            cout << "Enter exponent to select term (or a negative number to quit): ";
                            cin >> exp;
                         }
                         cout << "poly2 is " << poly2 << endl;
                      }
                      else
                         cout << "There are only 2 polynomials - 1 and 2" << endl;   
                      break;
            case '+': // add polynomials 1 and 2
                      cout << "The sum of " << poly1 << endl;
                      cout << "       and " << poly2 << endl;
                      cout << "        is " << (poly1 + poly2) << endl; 
                      break;     
            case 'Q': cout << "Test program ended." << endl;
                      break;
            default:  cout << choice << " is invalid." << endl;
        }
    }
    while ((choice != 'Q'));

    return EXIT_SUCCESS;
}

void print_menu( )
{
    cout << endl; 
    cout << "The following choices are available: " << endl;
    cout << " C   Clear a polynomial" << endl;
    cout << " E   Evaluate a polynomial" << endl;
    cout << " P   Print a polynomial with degree" << endl;
    cout << " S   Set a polynomial" << endl;
    cout << " +   Add 2 polynomials" << endl;
    cout << " Q   Quit this test program" << endl;
}

char get_user_command( )
// Library facilities used: iostream
{
    char command;

    cout << "Enter choice: ";
    cin >> command; // Input of characters skips blanks and newline character

    return command;
}

int get_number( )
// Library facilities used: iostream
{
    int result;
    
    cout << "Please enter an integer number for the list: ";
    cin  >> result;
    cout << result << " has been read." << endl;
    return result;
}





Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment