The goal of this assignment is to reinforce the contain class concepts in C++. S
ID: 668014 • Letter: T
Question
The goal of this assignment is to reinforce the contain class concepts in C++. Specifically, the assignment is to implement the polynomial class. Specifically, you should use the following header file as you starting point. You need to supply a test program. Please email your submission to your instructor by 8:00 am on Wednesday, September 9th.
header file:
Explanation / Answer
//poly.h #ifndef POLY2_H #define POLY2_H #include // Provides nullptr #include // Provides ostream #include // provides pow() #include // Provides UINT_MAX #include // provides machine epsilon // If your compiler does not support namespaces, then please delete the // following line and the set of brackets that follow. namespace main_savitch_5 { class polynode { public: // CONSTRUCTOR: Creates a node containing a specified initial // coefficient (init_coef), initial exponent (init_exponent), and // initial links forward and backward (init_fore and init_back). polynode( double init_coef = 0.0, unsigned int init_exponent = 0, polynode* init_fore = nullptr, polynode* init_back = nullptr ) { coef_field = init_coef; exponent_field = init_exponent; link_fore = init_fore; link_back = init_back; } // Member functions to set the fields: 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; } // Const member functions to retrieve current coefficient or exponent: double coef( ) const { return coef_field; } unsigned int exponent( ) const { return exponent_field; } // Two slightly different member functions to retrieve each link: 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: // CONSTRUCTORS and DESTRUCTOR polynomial(double c = 0.0, unsigned int exponent = 0); polynomial(const polynomial& source); ~polynomial( ); // MODIFICATION MEMBER FUNCTIONS polynomial& operator =(const polynomial& source); void add_to_coef(double amount, unsigned int exponent); void assign_coef(double coefficient, unsigned int exponent); void clear( ); // CONSTANT MEMBER FUNCTIONS 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; // CONSTANT OPERATORS double operator( ) (double x) const { return eval(x); } private: double EPSILON; polynode *head_ptr; // Head pointer for list of terms polynode *tail_ptr; // Tail pointer for list of terms mutable polynode *recent_ptr; // Most recently used term unsigned int current_degree; // Current degree of the polynomial // A private member function to aid the other functions: void set_recent(unsigned int exponent) const; }; // NON-MEMBER BINARY OPERATORS polynomial operator +(const polynomial& p1, const polynomial& p2); polynomial operator -(const polynomial& p1, const polynomial& p2); polynomial operator *(const polynomial& p1, const polynomial& p2); // NON-MEMBER OUTPUT FUNCTIONS std::ostream& operator EPSILON && exponent > 0){ tail_ptr= new polynode(c,exponent,nullptr,head_ptr); head_ptr->set_fore(tail_ptr); current_degree=exponent; } if(head_ptr->fore()== nullptr) { tail_ptr=head_ptr; } recent_ptr=head_ptr; } polynomial& polynomial::operator=(const polynomial& source) { // store machine epsilon EPSILON = std::numeric_limits::epsilon(); //write the rest if(head_ptr==source.head_ptr){ return *this; } if(head_ptr != nullptr){ clear(); } delete head_ptr; head_ptr= new polynode(source.head_ptr->coef(),source.head_ptr->exponent()); tail_ptr=head_ptr; recent_ptr=head_ptr; current_degree=0; for(int i = source.next_term(0); i!= 0; i=source.next_term(i)){ assign_coef(source.coefficient(i),i); } current_degree=source.current_degree; return *this; } polynomial::polynomial(const polynomial& source) { // store machine epsilon EPSILON = std::numeric_limits::epsilon(); // write the rest copy head_ptr=nullptr; *this = source; } polynomial::~polynomial() { clear(); } void polynomial::clear() { while(head_ptr != tail_ptr) { recent_ptr=head_ptr; head_ptr=head_ptr->fore(); delete recent_ptr; } recent_ptr=head_ptr; head_ptr->set_coef(0); head_ptr->set_exponent(0); tail_ptr=head_ptr; current_degree=0; } double polynomial::coefficient(unsigned int exponent) const { set_recent(exponent); if(recent_ptr->exponent() == exponent) { return recent_ptr->coef(); } return 0; } void polynomial::add_to_coef(double amount, unsigned int exponent) { set_recent(exponent); if(recent_ptr->exponent() != exponent){ assign_coef(amount,exponent); } else{ double sum = amount+recent_ptr->coef(); assign_coef(sum,exponent); } } void polynomial::assign_coef(double coefficient, unsigned int exponent) { set_recent(exponent); if(exponent==0){ recent_ptr->set_coef(coefficient); } else if(coefficient == 0 && exponent > current_degree){ return; } else if(fabs(coefficient)back(); temp->set_fore(nullptr); delete tail_ptr; tail_ptr=temp; set_recent(tail_ptr->exponent()); current_degree=tail_ptr->exponent(); } else{ polynode* temp=recent_ptr; recent_ptr=recent_ptr->back(); recent_ptr->set_fore(temp->fore()); recent_ptr=temp->fore(); recent_ptr->set_back(temp->back()); delete temp; set_recent(exponent); } } else if(recent_ptr->exponent() fore(),recent_ptr); if(recent_ptr->fore() != nullptr){ recent_ptr->fore()->set_back(temp); } else{ current_degree=exponent; tail_ptr=temp; } recent_ptr->set_fore(temp); } else if(fabs(coefficient) > EPSILON || exponent==0){ recent_ptr->set_coef(coefficient); } else if(exponent == current_degree){ tail_ptr=recent_ptr->back(); delete recent_ptr; recent_ptr=tail_ptr; current_degree=tail_ptr->exponent(); } else if(exponent > current_degree){ recent_ptr= new polynode(coefficient,exponent,nullptr,tail_ptr); tail_ptr->set_fore(recent_ptr); tail_ptr=recent_ptr; current_degree=exponent; } } unsigned int polynomial::next_term(unsigned int exponent) const { set_recent(exponent); if(recent_ptr->fore() == nullptr){ return 0; } else{ recent_ptr=recent_ptr->fore(); return recent_ptr->exponent(); } } unsigned int polynomial::previous_term(unsigned int exponent) const { set_recent(exponent); if(head_ptr->coef()==0){ if(head_ptr->fore()->exponent()==exponent){ return UINT_MAX; } } else if(exponent == recent_ptr->exponent() && recent_ptr->back() != nullptr && exponent){ return recent_ptr->back()->exponent(); } else if(exponent > current_degree){ return tail_ptr->exponent(); } else if(exponent == current_degree && current_degree!= 0){ return tail_ptr->back()->exponent(); } else if(exponent > recent_ptr->exponent()){ return recent_ptr->exponent(); } return UINT_MAX; } void polynomial::set_recent(unsigned int exponent) const { recent_ptr= head_ptr; if(exponent==0){ recent_ptr=head_ptr; return; } else if(exponent >= current_degree){ recent_ptr=tail_ptr; return; } while(recent_ptr->fore() != nullptr && recent_ptr->exponent() fore(); } if(recent_ptr->exponent() >exponent){ recent_ptr=recent_ptr->back(); } return; } double polynomial::eval(double x) const { double total = 0; polynode* curr=head_ptr; while(curr->fore() != nullptr){ total=total+curr->coef()*pow(x,curr->exponent()); curr=curr->fore(); } total=total+curr->coef()*pow(x,curr->exponent()); return total; } polynomial polynomial::derivative() const { polynomial p_prime; recent_ptr=head_ptr->fore(); while(recent_ptr->fore() != nullptr) { p_prime.assign_coef(recent_ptr->coef()*recent_ptr->exponent(),recent_ptr->exponent()-1); recent_ptr=recent_ptr->fore(); } p_prime.assign_coef(recent_ptr->coef()*recent_ptr->exponent(),recent_ptr->exponent()-1); p_prime.current_degree=recent_ptr->exponent()-1; return p_prime; } polynomial operator+(const polynomial& p1, const polynomial& p2) { polynomial p=p1; polynomial temp=p2; int degree=temp.degree(); int i=0; while(iRelated Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.