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

// FILE polynomial.h // CLASS: Polynomial_T (in the namespace csc161) // // Defi

ID: 658256 • Letter: #

Question

// FILE polynomial.h
// CLASS: Polynomial_T (in the namespace csc161)
//
// Definition:
// A polynomial has one variable x, real number coefficients, and
// non-negative integer exponents. Such a polynomial can be viewed
// as having the form:
// A[n]*x^n + A[n-1]*x^(n-1) + ... A[2]*x^2 + A[1]*x + A[0]
// where the A[n] are the real number coefficients and x^i represents
// the variable x raised to the i power. The coefficient A[0] is
// called the "constant" or "zeroth" term of the polynomial.
//   
// NOTES:
// 1. This version works by storing the coefficients in
// a dynamic array. The coefficient for the x^k term is stored
// in location [k] of the dynamic array. When a new term is
// added beyond the current size of the array, then the
// dynamic array is replaced by a new, larger array.
// 2. Note that two functions have been implemented as inline functions
// in this file (the degree and operator() functions).
//   
//
// MEMBER CONSTANTS:
// const static size_t MAX_DEGREE
// The size of the initial array to store the coefficients.
//
// CONSTRUCTOR:
// POSTCONDITION: Creates a polynomial shell with all zero
// coefficients.
//
// MODIFICATION MEMBER FUNCTIONS:
//   
// void assign_coef(double p_coefficient, unsigned int p_exponent)
// POSTCONDITION: Sets the coefficient for the specified exponent.
//
// void clear( )
// POSTCONDITION: All coefficients of this polynomial are set to zero.
//
// void reserve(unsigned int p_additionalTerms)
// POSTCONDITION: Allocates additional memory to store additional terms.
// This guarantees that member functions will not need to worry about it.
//
// CONSTANT MEMBER FUNCTIONS:
// double getCoefficient(unsigned int p_exponent) const
// POSTCONDITION: Returns coefficient at specified exponent of this
// polynomial.
//
// unsigned int getDegree( ) const
// POSTCONDITION: The function returns the value of the largest exponent
// with a non-zero coefficient.
// If all coefficients are zero, then the function returns zero.
//
// Polynomial_T derivative( ) const
// POSTCONDITION: The return value is the first derivative of this
// polynomial.
//
// double eval(double p_x) const
// POSTCONDITION: The return value is the value of this polynomial with the
// given value for the variable x.
//
// unsigned int nextTerm(unsigned int p_e) const
// POSTCONDITION: The return value is the next exponent n which is LARGER
// than e such that coefficient(n) != 0.
// If there is no such term, then the return value is zero.
//
// unsigned int previousTerm(unsigned int p_e) const
// POSTCONDITION: The return value is the next exponent n which is SMALLER
// than e such that coefficient(n) != 0.
// If there is no such term, then the return value is UINT_MAX
// from <climits>.
//
// CONSTANT OPERATORS:
// double operator( ) (double p_x) const
// Same as the eval member function.
//
// NON-MEMBER BINARY OPERATORS for the polynomial Class
// Polynomial_T operator -(const Polynomial_T& p1, const Polynomial_T& p2)
// POSTCONDITION: return-value is a polynomial with each coefficient
// equal to the difference of the coefficients of p1 & p2 for any given
// exponent.
//
// Polynomial_T operator +(const Polynomial_T& p1, const Polynomial_T& p2)
// POSTCONDITION: return-value is a polynomial with each coefficient
// equal to the sum of the coefficients of p1 & p2 for any given
// exponent.
//
// Polynomial_T operator *(const Polynomial_T& p1, const Polynomial_T& p2)
// POSTCONDITION: Each term of p1 has been multiplied by each term of p2,
// and the answer is the sum of all these term-by-term products.
// For example, if p1 is 2x^2 + 3x + 4 and p2 is 5x^2 - 1x + 7, then the
// return value is 10x^4 + 13x^3 + 31x^2 + 17x + 28.
//
// NON-MEMBER OUTPUT FUNCTIONS for the polynomial Class
// ostream& operator << (ostream& out, const Polynomial_T& p)
// POSTCONDITION: The polynomial has been printed to ostream out, which,
// in turn, has been returned to the calling function.
//
//
// DYNAMIC MEMORY
// Since this class uses dynamic memory, the copy constructor and assignment
// operator are overridden, and there is a destructor implemented. Also,
// if there is insufficient dynamic memory, the following functions throw
// a bad_alloc exception: the constructors, assignment, reserve, add_to_coef,
// assign_coef, and any function that returns a polynomial.

#ifndef POLY_H
#define POLY_H
#include <iostream> // Provides ostream
#include <vector> // If you decide on Vector

namespace csc161
{
  
class Polynomial_T
{
public:
   // CONSTANTS
   static const unsigned int MAX_DEGREE = 30;
  
// CONSTRUCTORS and DESTRUCTOR
   Polynomial_T();
   Polynomial_T(const Polynomial_T& source); // Copy constructor
   ~Polynomial_T( );

   // MODIFICATION MEMBER FUNCTIONS
   void operator =(const Polynomial_T& source);
   void assign_coef(double p_coefficient, unsigned int p_exponent);
   void clear( );
void reserve(unsigned int p_additionalTerms);
  
   // CONSTANT MEMBER FUNCTIONS
   double getCoefficient(unsigned int p_exponent) const;
   unsigned int getDegree( ) const { return current_degree; }
   Polynomial_T derivative( ) const;
   double eval(double p_x) const;
   unsigned int nextTerm(unsigned int p_e) const; // Helpers, might not be needed, if trivial
   unsigned int previousTerm(unsigned int p_e) const; // Helper, might not be needed, if trivial
  
   // CONSTANT OPERATORS - optional, we will cover this next time
   double operator( ) (double x) const { return eval(x); }
  
   // PRIVATE MEMBERS
private:

   // Make a choice of data structure, leave only applies
   double *coef; // Pointer to the dynamic array that stores coefs (if you decide on array)

   vector<double> coef_v; // Vector to store coefs (if you decide on vector)

   Term_T *coef; // Head of linked list (if you decide to on linked list).
   // Rename Node class to Term_T, change to store exponent and coef; include
   // term.h etc
   ///// Choice made. Please note that this might affect how data must be entered, if zeros are explicit etc.

   unsigned int size; // Current size of the coef data container (array, vector)
   unsigned int current_degree; // Current degree of the polynomial


};
  
// NON-MEMBER BINARY OPERATORS
Polynomial_T operator +(const Polynomial_T& p1, const Polynomial_T& p2);
Polynomial_T operator -(const Polynomial_T& p1, const Polynomial_T& p2); // Optional
Polynomial_T operator *(const Polynomial_T& p1, const Polynomial_T& p2); // Optional
  
// NON-MEMBER OUTPUT FUNCTIONS
std::ostream& operator << (std::ostream& out, const Polynomial_T& p);
  
}
#endif

Explanation / Answer

Polynomial.cpp

#include "Polynomial.h"
#include "Term.h"
#include <list>

using namespace std;

Polynomial::Polynomial(Term term) {
this->terms.push_front(term);
}

Polynomial::Polynomial(list<Term> terms) {
this->terms = terms;
}

Polynomial::Polynomial(const Polynomial& orig) {
this->terms = orig.getTerms();
}

list <Term> Polynomial::getTerms() const {
return this->terms;
}

Polynomial Polynomial::operator+(const Polynomial& other) {
list <Term> newPoly;
bool matchFound = false;
list <Term>::iterator otherIter;
list <Term>::iterator thisIter;
list <Term> otherList = other.getTerms();
for (otherIter = otherList.begin();
otherIter != otherList.end(); ++otherIter) {
matchFound = false;
for (thisIter = this->terms.begin();
thisIter != this->terms.end(); ++thisIter) {

if ((*otherIter).getExpnt() == (*thisIter).getExpnt())
{
matchFound = true;
newPoly.push_back(Term((*otherIter)+(*thisIter)));
}

}
if (!matchFound)
{
newPoly.push_back(Term((*otherIter)));
}


}

return Polynomial(newPoly);
}

Polynomial Polynomial::operator-(const Polynomial& other) {
list <Term> newPoly;
bool matchFound = false;
list <Term>::iterator otherIter;
list <Term>::iterator thisIter;
list <Term> otherList = other.getTerms();

for (otherIter = otherList.begin()
otherIter != otherList.end(); ++otherIter)
{

matchFound = false;
for (thisIter = this->terms.begin();
thisIter != this->terms.end(); ++thisIter) {

if ((*otherIter).getExpnt() == (*thisIter).getExpnt())
{
matchFound = true;
newPoly.push_back(Term((*otherIter)-(*thisIter)));
}

}
if (!matchFound)
{
newPoly.push_back(Term((*otherIter)));
}
}

return Polynomial(newPoly);
}

Polynomial Polynomial::operator *(const Polynomial& other)
{
list <Term> newPoly;
list <Term>::iterator otherIter;
list <Term>::iterator thisIter;
list <Term> otherList = other.getTerms();

for (otherIter = otherList.begin();
otherIter != otherList.end(); ++otherIter) {

for (thisIter = this->terms.begin();
thisIter != this->terms.end(); ++thisIter) {

newPoly.push_back(Term((*otherIter) * (*thisIter)));
}
}
  
return Polynomial(newPoly);
}

Polynomial Polynomial::operator =(const Polynomial& other){
this->terms = other.getTerms();
return *this;
}


Polynomial Polynomial::operator +=(const Polynomial& other){
Polynomial poly(Term(0));
poly = *this + other;
*this = poly;
return *this;
}

Polynomial Polynomial::operator -=(const Polynomial& other){
*this = Polynomial(*this - other);
return *this;
}

Polynomial Polynomial::operator *=(const Polynomial& other){
*this = Polynomial(*this * other);
return *this;
}