Sample Assignment: Preliminary Polynomial Class
poly0.h:The header file for the preliminary polynomial class. Actually, you don't have to write much of this file. Just start with our version of poly0.h and add your name and other information at the top.
poly0.cxx:The implementation file for the new polynomial class. You will write most of this file, which will have the implementations of all the polynomial's member functions.
polytest0.cxx:This is a small interactive test program.
polyexam0.cxx:A small non-interactive test program.
Most of the discussion of this assignment is in Section 4.6 of the
textbook. Start by making sure that you understand the description of
how the coefficients of a polynomial will be stored in an
array. The size of the array will be fixed by a constant
CAPACITY. Because of this fixed
capacity, the polynomials that you create will have a maximum exponent
static const unsigned int MAX_EX = CAPACITY-1;Both
MAX_EXare already defined for you in our version of poly0.h . In your
poly0.cxxfile, you must declare the memory for these two constants (but not redeclare their values), like this:
const unsigned int polynomial::CAPACITY; const unsigned int polynomial::MAX_EX;A few additional notes about the polynomial operations are given below.
polynomial(double c = 0.0, unsigned int exponent = 0);
exponent. Notice that the
exponentparameter has a default value of zero, so that we can also call the constructor with a single parameter
c, for example:
polynomial p(42.0);. This creates the polynomial
pwith the only non-zero term being 42.0 times x to the zero power (which is the same as the number 42.0 since x to the zero power is defined as equal to 1). The parameter
calso has a default value of 0.0, so that the constructor can be used with no arguments (in other words, as a default constructor). For example:
polynomial p;will create a polynomial
pin which all coefficients are zero.
void add_to_coef(double amount; unsigned int exponent);
void assign_coef(double new_coefficient; unsigned int exponent);
void clear( );
reservefunction for now.
double coefficient(unsigned int exponent) const;
unsigned int degree( ) const;
double eval(double x) const;
unsigned int next_term(unsigned int e) const;
double operator( ) (double x) const;
NEXT_TERMfor the case where there is no next term. In this case, the function should return zero. I have also added a new function (
previous_term), specified here:
unsigned int previous_term(unsigned int e) const POSTCONDITION: The return value is the next exponent n which is SMALLER than e such that coefficient(n) != 0. If there is no such term, then the return value is UINT_MAX from
polynomial derivative( ) const;
p, the value of
p.derivative( )is the first derivative of
polynomial operator +(const polynomial& p1, const polynomial& p2);
polynomial operator -(const polynomial& p1, const polynomial& p2);
polynomial operator *(const polynomial& p1, const polynomial& p2);
p + q.
0 3.4x^3 + 2.7x^2 + 7.9x + 4 3.4x^3 - 7.9x -3.4x^3 + 5.3Note: Writing this function is possibly the hardest part of the assignment because there are many special cases to consider. Nevertheless, if you give some thought to the organization of these cases beforehand, you can complete the implementation with about 35 lines of code. Be careful if you use an unsigned int to count backward from the degree down to zero (because unsigned ints are not allowed to actually go below zero).
const polynomial& p,
const char filename[ ],
The constructor add_to_coef assign_coef clear coefficient degree
coefarray. After these changes are made, the functions must also adjust the value of
current_degree. For example, if a non-zero coefficient is given for an exponent
ethat is larger than
current_degree, then current degree must be adjusted upward to
e. There is also several situations where the
current_degreemust be adjusted downward.