Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Poly0: http://pastebin.com/Cbh7FLW7 Polytest0: http://pastebin.com/V92xPc9R Poly

ID: 3665610 • Letter: P

Question

Poly0: http://pastebin.com/Cbh7FLW7
Polytest0: http://pastebin.com/V92xPc9R
Polyexam0: http://pastebin.com/ayFFcFPM The goal of assignment 4 is to reinforce the dynamic classes in C++. Specifically, the assignment is to implement the polynomial class from chapter 4.6 on page 212. But the additional requirement is to implement the class using a dynamic array to store the coefficients. The class needs to have a destructor, copy constructor, and an overloaded assignment operator. The following files are provided for your reference. You should start by modifying the header file first. 1. polyOh: The header file for the preliminary polynomial class. 2. polytest0.cpp: A small interactive test program. 3. polyexam0.cpp: A small non-interactive test program Remember, all those files are only work for static array implementation. You should modify them to make the dynamic array implementation work.

Explanation / Answer

#ifndef POLY2_H
#define POLY2_H
#include <cstdlib>   
#include <iostream>
#include <cmath>     
#include <climits>   
#include <limits>
namespace main_
{
class polynode
{
public:

polynode(
double init_coef = 0.0,
unsigned int init_exponent = 0,
polynode* init_fore = NULL,
polynode* init_back = NULL
)
{
coef_field = init_coef;
exponent_field = init_exponent;
link_fore = init_fore;
link_back = init_back;
}
void set_coef(double new_coef)
{
coef_field = new_coef;
}
void set_exponent(unsigned int new_exponent)
{
exponent_field = new_exponent;
}
void set_fore(polynode* new_fore)
{
link_fore = new_fore;
}
void set_back(polynode* new_back)
{
link_back = new_back;
}
double coef( ) const
{
return coef_field;
}
unsigned int exponent( ) const
{
return exponent_field;
}
const polynode* fore( ) const
{
return link_fore;
}
polynode* fore( )   
{
return link_fore;
}
const polynode* back( ) const
{
return link_back;
}
polynode* back( )   
{
return link_back;
}
private:
double coef_field;
unsigned int exponent_field;
polynode *link_fore;
polynode *link_back;
};
class polynomial
{
public:
polynomial(double c = 0.0, unsigned int exponent = 0);
polynomial(const polynomial& source);
~polynomial( );
polynomial& operator =(const polynomial& source);
void add_to_coef(double amount, unsigned int exponent);
void assign_coef(double coefficient, unsigned int exponent);
void clear( );
double coefficient(unsigned int exponent) const;
unsigned int degree( ) const
{
return current_degree;
}
polynomial derivative( ) const;
double eval(double x) const;
void find_root(
double& answer,
bool& success,
unsigned int& iterations,
double guess = 0,
unsigned int maximum_iterations = 100,
double epsilon = 1e-8
)
const;
unsigned int next_term(unsigned int e) const;
unsigned int previous_term(unsigned int e) const;
double operator( ) (double x) const { return eval(x); }
private:
double EPSILON;
polynode *head_ptr;   
polynode *tail_ptr;   
mutable polynode *recent_ptr;   
unsigned int current_degree;   
void set_recent(unsigned int exponent) const;
};
polynomial operator +(const polynomial& p1, const polynomial& p2);
polynomial operator -(const polynomial& p1, const polynomial& p2);
polynomial operator *(const polynomial& p1, const polynomial& p2);
std::ostream& operator << (std::ostream& out, const polynomial& p);
}