Here is the heather file from assignment3: #ifndef _POLYNOMIAL_H_ #define _POLYN
ID: 669135 • Letter: H
Question
Here is the heather file from assignment3:
#ifndef _POLYNOMIAL_H_
#define _POLYNOMIAL_H_
#include <iostream>
class polynomial
{
public:
static const unsigned int MAXIMUM_DEGREE = 29;
polynomial();
// postcondition: zero polynomical has been created
void add_to_coef(double amount, unsigned int exponent);
// precondition: exponent <= MAXIMUM_DEGREE
// postcondition: amount has been added to the exponent term
void assign_coef(double coefficient, unsigned int exponent);
// precondition: exponent <= MAXIMUM_DEGREE
// postcondition: amount is the exponent term
void clear();
// postcondition: polynomial is the zero polynomial
polynomial antiderivative() const;
// precondition: degree < MAXIMUM_DEGREE
// postcondtion: antiderivative with 0 constant term has been returned
double coefficient(unsigned int exponent) const;
// precondition: exponent <= MAXIMUM_DEGREE
// postcondition: returned coefficient of term with degree exponent
unsigned int degree() const;
// postcondition: returned degree of polynomial - 0 is returned for zero polynomial
polynomial derivative() const;
// postcondition: returned derivative of polynomial
double eval(double x) const;
// postcondition: returned value of polynomial at x
bool is_zero() const;
// postcondition: returned whether polynomial is the zero polynomial
double definite_integral(double x0, double x1);
// precondition: x0 <= x1
// postcondition: returned value of definite integral from x0 to x1
double operator()(double x) const;
// postcondition: returned value of polynomial at x
polynomial mult_by_term (double x, unsigned int exponent) const;
// precondition: exponent + degree <= MAXIMUM_DEGREE
// postcondition: returned polynomial consisting of the original polynomial
// multiplied by the term with coefficient x and specified exponent
private:
void reset_degree();
// postcondition: degree of polynomial has been reset
double coef[MAXIMUM_DEGREE + 1];
unsigned int current_degree;
};
polynomial operator+(const polynomial& p1, const polynomial& p2);
// postcondition: sum of p1 & p2 has been returned
polynomial operator-(const polynomial& p1, const polynomial& p2);
// postcondition: difference of p1 & p2 has been returned
polynomial operator*(const polynomial& p1, const polynomial& p2);
// precondition: degree of p1 + degree of p1 <= MAXIMUM_DEGREE
// postcondition: product of p1 & p2 has been returned
std::ostream& operator<<(std::ostream& output, const polynomial& p);
// postcondition: p has been displayed on output
#endif // _POLYNOMIAL_H_
Explanation / Answer
#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;
}
#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;
}