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

Overload the addition operator for the class Polyomial header file: #ifndef H_PO

ID: 644651 • Letter: O

Question

Overload the addition operator for the class Polyomial

header file:

#ifndef H_POLYNOMIAL_H
#define H_POLYNOMIAL_H

#include <ostream>   // to be able to declare overloaded << operator as a friend
#include <string>    // input type to the main constructor
#include <tr1/memory> // for shared_ptr
#include <tr1/shared_ptr.h>

using namespace std;


class Polynomial
   {
   friend ostream &operator<<( ostream &, const Polynomial & );

   public:
      Polynomial();             // default polynomial is empty (= 0)
      Polynomial( string & );   // Set the polynomial according to the string

      Polynomial &operator=( const Polynomial & );   // assignment

      bool operator==( const Polynomial & ) const;   // equality test
      bool operator!=( const Polynomial & ) const;   // not equal test

      Polynomial operator+( const Polynomial & ) const;   // addition

      double eval( double x ) const ;   // evaluate the polynomial at x

   private:
      class Term   // a Term of the polynomial
         {
         public:
            Term( double c, int e, shared_ptr<Term> n );
            double coeff;       // the coefficient
            int exponent;       // the exponent
            shared_ptr<Term> next;
         };

      shared_ptr<Term> head;    // The head of the list
      static double TOL;        // Tolerance for floating point equality
   };

Implementation:

#include "polynomial.h"
#include <sstream>
#include <cmath>

double Polynomial::TOL = .000000000001;   // tolerance for floating pt. equality

//+------------------+
//| Term constructor |
//+------------------+
Polynomial::Term::Term( double c, int e, shared_ptr<Term> n )
   {

   coeff = c; exponent = e; next = n;

   }

//+--------------------------------------+
//| Default Constructor: Polynomial is 0 |
//+--------------------------------------+
Polynomial::Polynomial()
   {
   head = nullptr;
   }

//+-------------------------------------------------------------+
//| Constructor: The input string contains coefficient-exponent |
//| pairs where everything is separated by whitespace           |
//+-------------------------------------------------------------+
Polynomial::Polynomial( string & str )
   {
   stringstream ss( str ); // stringstream lets us extract items separated by
                            // whitespace simply by using the >> operator
   double coefficient;      // to hold the coefficient
   int exp;                 // to hold the exponent
   head = nullptr;          // initialize head to null

   // read in coefficient-exponent pairs and add each term to the list
   // ----------------------------------------------------------------
   while (ss >> coefficient >> exp)
      if (coefficient != 0)   // don't make a 0 term
         head = shared_ptr<Term>(new Term( coefficient, exp, head ));
   }


Polynomial Polynomial::operator+( const Polynomial &other ) const

//+--------------------------------------------------------------------------+
//| Overload the outstream << operator. Doesn't print the variable for the |
//| constant term. Doesn't print "1" coefficients unless it is the constant |
//| term. Prints the sign of the coefficient as addition or subtraction.    |
//+--------------------------------------------------------------------------+
ostream &operator<<( ostream &os, const Polynomial &poly )
   {
   // special case for 0 polynomial
   // -----------------------------
   if (poly.head == nullptr)
      {
      os << "0" << endl;
      return os;
      }

   shared_ptr<Polynomial::Term> current = poly.head;

   // print the first term separately since no leading + or - sign
   // ------------------------------------------------------------
   if (current->exponent == 0)
      os << current->coeff;
   else if (current->coeff == 1)
      os << "x^" << current->exponent;
   else
      os << current->coeff << "x^" << current->exponent;

   // print each remaining term along with leading addition or subt. sign
   // -------------------------------------------------------------------
   for (current = current->next; current != nullptr; current = current->next)

      if (current->coeff > 0)   // positive term?
         {
         if (current->exponent == 0)
            os << " + " << current->coeff;        // a constant term
         else if (current->coeff == 1)
            os << " + x^" << current->exponent;   // no 1 coeffienct
         else
            os << " + " << current->coeff << "x^" << current->exponent;
         }
      else // negative term
         {
         if (current->exponent == 0)
            os << " - " << -current->coeff;       // a constant term
         else if (current->coeff == -1)
            os << " - x^" << current->exponent;   // don't print the 1
         else
            os << " - " << -current->coeff << "x^" << current->exponent;
         }

   os << endl; // print the newline character and flush the output stream
   return os;
   }

Explanation / Answer

#ifndef H_POLYNOMIAL_H
#define H_POLYNOMIAL_H

#include <ostream>   // to be able to declare overloaded << operator as a friend
#include <string>    // input type to the main constructor
#include <tr1/memory> // for shared_ptr
#include <tr1/shared_ptr.h>

using namespace std;


class Polynomial
   {
   friend ostream &operator<<( ostream &, const Polynomial & );

   public:
      Polynomial();             // default polynomial is empty (= 0)
      Polynomial( string & );   // Set the polynomial according to the string

      Polynomial &operator=( const Polynomial & );   // assignment
      bool operator==( const Polynomial & ) const;   // equality test
      bool operator!=( const Polynomial & ) const;   // not equal test

      Polynomial operator+( const Polynomial & ) const;   // addition

      double eval( double x ) const ;   // evaluate the polynomial at x

   private:
      class Term   // a Term of the polynomial
         {
         public:
            Term( double c, int e, shared_ptr<Term> n );
            double coeff;       // the coefficient
            int exponent;       // the exponent
            shared_ptr<Term> next;
         };
      void addNode(Term *t);

      shared_ptr<Term> head;    // The head of the list
      static double TOL;        // Tolerance for floating point equality
   };

Implementation:

#include "polynomial.h"
#include <sstream>
#include <cmath>

double Polynomial::TOL = .000000000001;   // tolerance for floating pt. equality

//+------------------+
//| Term constructor |
//+------------------+
Polynomial::Term::Term( double c, int e, shared_ptr<Term> n )
   {

   coeff = c; exponent = e; next = n;

   }

//+--------------------------------------+
//| Default Constructor: Polynomial is 0 |
//+--------------------------------------+
Polynomial::Polynomial()
   {
   head = nullptr;
   }

//+-------------------------------------------------------------+
//| Constructor: The input string contains coefficient-exponent |
//| pairs where everything is separated by whitespace           |
//+-------------------------------------------------------------+
Polynomial::Polynomial( string & str )
   {
   stringstream ss( str ); // stringstream lets us extract items separated by
                            // whitespace simply by using the >> operator
   double coefficient;      // to hold the coefficient
   int exp;                 // to hold the exponent
   head = nullptr;          // initialize head to null

   // read in coefficient-exponent pairs and add each term to the list
   // ----------------------------------------------------------------
   while (ss >> coefficient >> exp)
      if (coefficient != 0)   // don't make a 0 term
         head = shared_ptr<Term>(new Term( coefficient, exp, head ));
   }


void Polynomial::addNode(Term *t){
   if(head == nullptr){
       head = t;
   }
   else{
       shared_ptr<Polynomial::Term> p1 = head;
       while(p1->next != nullptr){
           p1 = p1->next;
       }
       p1->next = t;
   }
}

Polynomial Polynomial::operator+( const Polynomial &other ) const

//+--------------------------------------------------------------------------+
//| Overload the outstream << operator. Doesn't print the variable for the |
//| constant term. Doesn't print "1" coefficients unless it is the constant |
//| term. Prints the sign of the coefficient as addition or subtraction.    |
//+--------------------------------------------------------------------------+
{
   Polynomial *p = new Polynomial();
   shared_ptr<Polynomial::Term> p1 = head;
   shared_ptr<Polynomial::Term> p2 = other.head;
   while(p1 != nullptr){
       if(p2 != nullptr && p1->exponent == p2->exponent){
           p->add(new Term(p1->coeff + p2->coeff, p1->exponent, nullptr));
       }
       else if(p2 == nullptr || p1->exponent < p2->exponent){
           p->add(new Term(p1->coeff, p1->exponent, nullptr));
       }
       while (p2 != nullptr && p1->exponent > p2.exponent){
           p->add(new Term(p2->coeff, p2->exponent, nullptr));
           p2 = p2->next;
           if(p2 != nullptr && p1->exponent == p2->exponent){
               p->add(new Term(p1->coeff + p2->coeff, p1->exponent, nullptr));
               break;
           }
           else if(p2 == nullptr || p1->exponent < p2->exponent){
               p->add(new Term(p1->coeff, p1->exponent, nullptr));
               break;
           }
       }
   }
   while(p2 != nullptr){
       p->add(new Term(p2->coeff, p2->exponent, nullptr));
       p2 = p2->next;
   }
   return *p;
}
ostream &operator<<( ostream &os, const Polynomial &poly )
   {
   // special case for 0 polynomial
   // -----------------------------
   if (poly.head == nullptr)
      {
      os << "0" << endl;
      return os;
      }

   shared_ptr<Polynomial::Term> current = poly.head;

   // print the first term separately since no leading + or - sign
   // ------------------------------------------------------------
   if (current->exponent == 0)
      os << current->coeff;
   else if (current->coeff == 1)
      os << "x^" << current->exponent;
   else
      os << current->coeff << "x^" << current->exponent;

   // print each remaining term along with leading addition or subt. sign
   // -------------------------------------------------------------------
   for (current = current->next; current != nullptr; current = current->next)

      if (current->coeff > 0)   // positive term?
         {
         if (current->exponent == 0)
            os << " + " << current->coeff;        // a constant term
         else if (current->coeff == 1)
            os << " + x^" << current->exponent;   // no 1 coeffienct
         else
            os << " + " << current->coeff << "x^" << current->exponent;
         }
      else // negative term
         {
         if (current->exponent == 0)
            os << " - " << -current->coeff;       // a constant term
         else if (current->coeff == -1)
            os << " - x^" << current->exponent;   // don't print the 1
         else
            os << " - " << -current->coeff << "x^" << current->exponent;
         }

   os << endl; // print the newline character and flush the output stream
   return os;
}