Here is the file that needs implementing: // IMPORTANT: Do not modify the conten
ID: 3597881 • Letter: H
Question
Here is the file that needs implementing:
// IMPORTANT: Do not modify the contents of this file
// Polynom.h
// A simple ploynomial class of degree n
#ifndef POLYNOM_H
#define POLYNOM_H
#include <vector>
#include <iostream>
using namespace std;
class Polynom {
public:
// Postcondition: Creates an "empty" polynomial
Polynom();
// Postcondition: Creates a polynomial with coefficients provided in the vector parameter;
// the degree of the polynomial is the size of the vector plus 1
Polynom(const vector<int>& coeff);
// Basic arithmetic operations:
// Operator +
// Postcondition: The sum of this polynomial and RHS is returned; this
// and RHS are unchanged
const Polynom& operator+(const Polynom& RHS) const;
// Operator -
// Postcondition: The RHS is subtracted from this and the result is returned; this
// and RHS are unchanged
const Polynom& operator-(const Polynom& RHS) const;
// Evaluation;
// Operator ()
// Postcondition: this polynomial is evaluated at x and the the value
// is returned; this polynomial is unchanged
double operator()(double x) const;
// Set the coefficient of x^k
// Precondition: k >= 0 and k <= n, where n is the degree of this polynomial
// Postcondition: If precondition is met, then coefficient of x^k is changed to
// c and returns true; otherwise no change is made and returns false;
bool setCoeff(int k, int c);
// Get the coefficient of x^k
// Precondition: k >= 0 and k <= n, where n is the degree of this polynomial
// Postcondition: If precondition is met, then the function assigns the
// coefficient of x^k in parameter c and return true;
// otherwise returns false and parameter c remains unchanged
bool getCoeff(int k, int& c);
// Stream insertion
// Postcondition: Write the polynomial onto stream ostr.
ostream& insert(ostream& ostr);
private:
vector<int> P; // coefficients of the polynomial; P[i] is the coefficient
// of x raised to power i;
};
// overloaded stream to write the polynomial RHS onto stream Out
ostream& operator<<(ostream& Out, const Polynom& RHS);
#endif
Explanation / Answer
#include <iostream>
#include <sstream>
#include <stdexcept>
#include<iomanip>
#include <cmath>
#include "Polynom.h"
using namespace std;
// Default Class constructor
// Allocate an array of DEFAULTPOLY elements and initializes it to the constant 0
// post: Class object is initialized to degree-0 polynomial of 0
Polynom::Polynom()
{
coeff = new double [DEFAULTPOLY];
for (int i = 0; i < (int) DEFAULTPOLY; i++)
{
coeff[i] = 0.0;
}
maxPoly = DEFAULTPOLY;
}
// Non-default (alternate) Class constructor
// Allocate an array of 'size' elements and initializes it to the constant 0
// post: Class object is initialized to degree-0 polynomial of 0
Polynom::Polynom(size_t size)
{
coeff = new double[size];
for (int i = 0; i < (int) size; i++)
{
coeff[i] = 0.0;
}
maxPoly = size;
}
// Copy constructor
// Construct a new Poly that is a copy of an existing Poly
// post: Class object is initialized to be a copy of the argument Poly
Polynom::Polynom(const Polynom& aPoly)
{
coeff = new double[aPoly.maxPoly];
for (int i = 0; i < (int) aPoly.maxPoly; i++)
{
coeff[i] = aPoly.coeff[i];
}
maxPoly = aPoly.maxPoly;
}
// Destructor
// Destroy a poly object by freeing the dynamically allocated array
Polynom::~Polynom()
{
delete[] coeff;
}
// Assignment operator
// Assign rhs Poly object to 'this' Poly object
const Polynom& Polynom::operator= (const Polynom& rhs)
{
if (this == &rhs){
return *this;
}
Polynom tmp(rhs);
std::swap(maxPoly, tmp.maxPoly);
std::swap(coeff, tmp.coeff);
return *this;
}
// maxSize
// Return the size of the coefficient array
size_t Polynom::maxSize() const
{
size_t maxSize = maxPoly;
return maxSize;
}
// grow
// This method will allow us to increase the size of the dynamically allocated
// array by allocating a new array of the desired size, copying the data from
// the old array to the new array, and then releasing the old array.
// If the newSize is less than or equal to the current size, then no actions
// are taken.
// Note: the maximum degree of a polynomial is one less than the size of the
// array. The parameter newSize represents the size of the array.
void Polynom::grow(size_t newSize)
{
int arrSize = (int) maxPoly;
if ((int) newSize > arrSize)
{
double* newArrPtr = new double[newSize];
for (int i = 0; i < arrSize; i++)
{
newArrPtr[i] = coeff[i];
}
std::swap(newArrPtr, coeff);
std::swap(newSize, maxPoly);
}
}
// degree
// Finds the degree of a polynomial (the highest power with a
// non-zero coefficient)
// pre: Class object exists
// post: Returns the degree of the polynomial object.
size_t Polynom::degree() const
{
size_t degree = 0;
for (int i = 0; i < (int)maxPoly; i++)
{
if (coeff[i] != 0)
{
degree = (size_t)i;
}
}
return degree;
}
// setCoeff
// Sets a term, value*x^i, in a polynomial, growing the array if necessary.
// pre: Class object has been initialized. 0 <= i.
// post: In the polynomial, the term with power i has coefficient
// value. The polynomical was grown if required.
// Throws <std::out_of_range> if index i does not meet the precondition.
void Polynom::setCoeff(double value, size_t i)
{
if ((size_t)0 <= i && i <= maxPoly)
{
if (i > maxPoly)
{
grow(i);
}
coeff[i] = value;
}
else
{
throw std::out_of_range("Index out of range");
}
}
// retrieveCoeff
// Finds the coefficient of the x^i term in poly
// pre: Class object has been initialized. 0 <= i
// post: Returns the value of the coefficient of the term with power i
// note: If the object does not contain a term with power i (e.g.,
// i>=maxPoly), a coefficient value of zero is returned.
// Throws <std::out_of_range> if index i does not meet the precondition.
double Polynom::retrieveCoeff(size_t i) const
{
if (0 <= (int)i)
{
return coeff[i];
}
else if (i >= (int)maxPoly)
{
return 0.0;
}
else
{
throw std::out_of_range("Index out of range");
}
}
// incrementCoeff
// Changes a term, value*x^i, in a polynomial, growing the polynomial if required.
// pre: Class object has been initialized. 0 <= i.
// post: In the Class object the term with power i has its coefficient
// incremented by the given value. The array has grown if necessary.
// Throws <std::out_of_range> if index i does not meet the precondition.
void Polynom::incrementCoeff(double value, size_t i)
{
if (0 <= (int)i)
{
if (i > (int)maxPoly)
{
grow(i);
}
coeff[i] *= value;
}
else
{
throw std::out_of_range("Index out of range");
}
}
// toString
// Produce a string representation of a Poly object
// pre: The class object has been initialized.
// post: A string representation is returned.
// dependencies: This function requires that the degree() and
// retrieveCoeff() functions are implemented.
// note: This function has been provided for you -- DO NOT CHANGE IT!
string Polynom::toString() const
{
ostringstream result;
bool printedSomething = false;
for (int i = (int)degree(); i >= 0; i--)
{
double c = retrieveCoeff(i);
if (c != 0.0)
{
printedSomething = true;
if (i == 0)
{
result.setf(ios::showpos);
result << " " << c;
result.unsetf(ios::showpos);
}
else
{
result.setf(ios::showpos);
result << " " << c;
result.unsetf(ios::showpos);
result << "*X^" << i;
}
}
}
if (!printedSomething)
{
result.setf(ios::showpos);
result << " " << 0;
result.unsetf(ios::showpos);
}
return result.str();
}
// numOfTerms
// Returns the number of terms in the polynomial.
// pre: The class object has been initialized.
// post: The number of non-zero terms of the polynomial is returned.
size_t Polynom::numOfTerms() const
{
size_t numTerms = 0;
for (int i = 0; i < (int)maxPoly; i++)
{
if (coeff[i] != 0)
{
numTerms++;
}
}
return numTerms;
}
// evaluate
// Evaluate a polynomial for a specified value of X
// pre: The class object has been initialized
// post: The polynomial will be evaluated for the value of
// X received as an argument. The resulting value is
// returned.
double Polynom::evaluate(double x) const
{
double polyTotal = 0.0;
double coeff = 0.0;
for (int i = 0; i < (int) maxPoly; i++)
{
coeff = retrieveCoeff((size_t) i);
polyTotal += (coeff * pow(x, (double)i));
}
return polyTotal;
}
// add
// Add one polynomial to another
// pre: The class object has been initialized. The received
// argument is also an initialized poly object.
// post: The argument polynomial is added to the object polynomial.
// The object polynomial is changed to hold the sum. The object
// polynomial is grown if required to hold the resulting sum.
// Note: the poly object being operated upon may be of a different
// size (maxPoly) than the aPoly parameter. If the aPoly parameter
// has a degree larger than the array in the 'this' Poly object,
// then the array is grown large enough to hold the sum.
void Polynom::add(const Polynom& aPoly)
{
if (aPoly.maxPoly > maxPoly)
{
grow(aPoly.maxPoly);
}
for (int i = 0; i < (int)maxPoly; i++)
{
coeff[i] += aPoly.coeff[i];
}
}
// subtract
// Subtract one polynomial from another
// pre: The class object has been initialized. The received
// argument is also an initialized poly object.
// post: The argument polynomial is subtracted from the object polynomial.
// The object polynomial is changed to hold the result. The object
// polynomial is grown if required to hold the result.
// Note: the poly object being operated upon may be of a different
// size (maxPoly) than the aPoly parameter. If the aPoly parameter
// has a degree larger than the array in the 'this' Poly object,
// then the array is grown large enough to hold the result.
void Polynom::subtract(const Polynom& aPoly)
{
if (aPoly.maxPoly > maxPoly)
{
grow(aPoly.maxPoly);
}
for (int i = 0; i < (int) maxPoly; i++)
{
coeff[i] -= aPoly.coeff[i];
}
}
// addition operator
// Add two polynomials together and return a new polynomial that is the result
// pre: The class object has been initialized. The received
// argument is also an initialized poly object.
// post: The argument polynomial is added to the object polynomial, and the
// result is stored in a new polynomial which is returned.
// The object polynomial is not changed.
// note: This function has been provided for you -- DO NOT CHANGE IT!
Polynom Polynom::operator+ (const Polynom& rhs) const
{
Polynom result;
result.add(*this);
result.add(rhs);
return result;
}
// subtraction operator
// Subtracts one polynomial from another and return a new polynomial that is the result
// pre: The class object has been initialized. The received
// argument is also an initialized poly object.
// post: The argument polynomial is subtracted from the object polynomial, and the
// result is stored in a new polynomial which is returned.
// The object polynomial is not changed.
// note: This function has been provided for you -- DO NOT CHANGE IT!
Polynom Polynom::operator- (const Polynom& rhs) const
{
Polynom result;
result.subtract(*this);
result.subtract(rhs);
return result;
}
// equals
// Determine if two polynomials are equal
// pre: The class object has been initialized. The received
// argument is also an initialized poly object.
// post: Returns true if the two polynomials are equal, false otherwise.
bool Polynom::equals(const Polynom& aPoly) const
{
if (maxPoly != aPoly.maxPoly)
{
return(false);
}
for (int i = 0; i < (int) maxPoly; i++)
{
if (aPoly.coeff[i] != coeff[i])
{
return(false);
}
}
return(true);
}
// Equality/inequality operators
// note: These functions have been provided for you -- DO NOT CHANGE IT!
bool Polynom::operator== (const Polynom& rhs) const
{
return equals(rhs);
}
bool Polynom::operator!= (const Polynom& rhs) const
{
return !equals(rhs);
}
// negate
// Negate a polynomial
// pre: The class object has been initialized.
// post: The polynomial has been changed to represent its
// multiplication by -1.0.
void Polynom::negate()
{
multByConst(-1.0);
}
// multByConst
// Multiply a polynomial by a constant
// pre: The class object has been initialized.
// post: The polynomial has been changed to represent its
// multiplication by the value of argument val.
void Polynom::multByConst(double val)
{
double* tmpArray = new double[maxPoly];
for (int i = 0; i < (int)maxPoly; i++)
{
tmpArray[i] = retrieveCoeff((size_t)i) * val;
}
std::swap(tmpArray, coeff);
}
// derivative
// Compute the derivative of a polynomial
// pre: The class object has been initialized.
// post: The polynomial has been changed to represent its
// derivative.
void Polynom::derivative()
{
size_t derivArrSize = maxPoly - 1;
double* derivArrPtr = new double[derivArrSize];
for (int i = 1; i < (int) maxPoly; i++)
{
double coeffOfDeriv = retrieveCoeff((size_t)i) * (double)i;
i -= 1;
derivArrPtr[i] = coeffOfDeriv;
}
std::swap(derivArrPtr, coeff);
std::swap(derivArrSize, maxPoly);
}
// insertion operator for output
// note: This function has been provided for you -- DO NOT CHANGE IT!
ostream & operator << (ostream &out, const Poly& p)
{
out << p.toString();
return out;
}