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

Complete the unimplemented functions and operators in polynomial.cpp, and add yo

ID: 642971 • Letter: C

Question

Complete the unimplemented functions and operators in polynomial.cpp, and add your tests to the testing program polyTest.cpp.

The only function you need to write is eval () which computes and returns the value of the variable . you also need to implement some operations including addition , assignment , and the equality testing operators == and != (you can implement one of the equality testing operators by having it call the other one).

this is what I have so far

#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 &rhs )
   {
   }

bool Polynomial::operator==( const Polynomial &other ) const
   {
   }

bool Polynomial::operator!=( const Polynomial &other ) const
   {
   }


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


//+---------------------------------------------------------------+
//| Compute and return the value of the polynomial evaluated at x |
//+---------------------------------------------------------------+
double Polynomial::eval( double x ) 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;
   }

#include "polynomial.h"
#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>       // for exit() call

using namespace std;


void makePolynomials( shared_ptr<Polynomial> [], int );


int main()
   {
   shared_ptr<Polynomial> poly[ 100 ];
   int nPolynomials;

   makePolynomials( poly, int &nPolynomials );

   // print out the polynomials
   // -------------------------
   for (int j=0; j < i; j++)
      cout << *(poly[j]);

   return 0;
   }

void makePolynomials( shared_ptr<Polynomial> poly[], int nPolynomials )
   {
   // get the filename from the user and open the file
   // ------------------------------------------------
   char filename[20];
   cout << "Enter the filename: ";
   cin >> filename;

   ifstream infile;
   infile.open( filename );
   if (! infile.is_open()) {
      cerr << "ERROR: could not open file " << filename << endl;
      exit(1);
      }

   // read in the data and construct a polynomial for each input line
   // ---------------------------------------------------------------
   string polynom;
   while (getline( infile, polynom ))
      {
      poly[ nPolynomials ] = shared_ptr<Polynomial>(new Polynomial( polynom ));
      nPolynomials++;
      }
   }

#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 <memory>    // for shared_ptr

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
   };

#endif

Explanation / Answer

//evaluation

public int evaluate(double x) {
double p = 0.0;
for (current = current->next; current != nullptr; current = current->next)
p = current->coeff + (x * p);
return p;
}

implementing addition...i given exmaple...your given program making me more confusing hope it helps

public LinkedPolynomial plus(LinkedPolynomial b){
LinkedPolynomial a = this;
LinkedPolynomial c = new LinkedPolynomial();
Node x=a.first.next;
Node y=b.first.next;
while(x!=null || y!=null){
Node t=null;
if(x==null){
t=new Node(y.coef,y.exp);
y=y.next;
}
else if(y==null){
t=new Node(x.coef,x.exp);
x=x.next;
}
else if(x.exp>y.exp){
t=new Node(x.coef,x.exp);
x=x.next;
}
else if {
t=new Node(y.coef,y.exp);
y=y.next;
}
else{
int coef=x.coef+y.coef;
int exp=x.exp;
x=x.next;
y=y.next;
if(coef==0)
continue;
t=new Node(coef,exp);
}
c.last.next=t;
c.last=c.last.next;

}
return c;

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote