Complete the unimplemented functions and operators in polynomial.cpp, and add yo
ID: 644668 • 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
polynomial.h
#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
polynomial.cpp
#include "polynomial.h"
#include <sstream>
#include <cmath>
#include <iostream>
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
{
shared-ptr<Term> a = head;
shared-ptr<Term> b = other.head;
while (a != null && b != null)
{
if (coeff != || exp !=)
a = a-> next;
b= b-> next;
return false;
}
}
bool Polynomial::operator!=( const Polynomial &other ) const
{
return !(*this == other);
}
Polynomial Polynomial::operator+( const Polynomial &other ) const
{
shared-ptr <Term> tail = nullptr;
if (tail == nullptr)
result -> head = tail;
else tail ->next
tail = tail -> next;
shared-ptr <Polynomial> result = shared-ptr <Polynomial> (new Polynomial());
return result;
}
//+---------------------------------------------------------------+
//| Compute and return the value of the polynomial evaluated at x |
//+---------------------------------------------------------------+
double Polynomial::eval( double x ) const
{
double total = 0.0;
for (current = current-> next; current != nullptr; current -> next));
total = current-> coefficient + (x * total);
return total;
}
//+--------------------------------------------------------------------------+
//| 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;
}
polyTest.cpp
#include "polynomial.h"
#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib> // for exit() call
using namespace std;
void makePolynomials( shared_ptr [], int & );
int main() {
shared_ptr poly[ 100 ];
int nPolynomials; makePolynomials( poly, nPolynomials ); // print out the polynomials // -------------------------
for (int j=0; j < nPolynomials; j++)
cout << *(poly[j]);
return 0; }
void makePolynomials( shared_ptr 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(new Polynomial( polynom ));
nPolynomials++; }
}
}
Explanation / Answer
#include #include #include "Polynomial.h" void DisplayPolynomial(const Polynomial & polynomial); void GetPolynomial(Polynomial & polynomial, int polynomial_type); //====================================================================== // Start of main program. //====================================================================== int main(int argc, char* argv[]) { //------------------------------------------------------------------ // Get the type of test. //------------------------------------------------------------------ std::coutRelated Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.