Task : Write a complete Project file ( main file, specification file and Impleme
ID: 3889537 • Letter: T
Question
Task: Write a complete Project file ( main file, specification file and Implementation file). Write the program in C++. Use linked lists to represent two polynomials. Each polynomial will be an object of the Polynomial class. Each node in the linked list will represent a term of the polynomial, so data members of the nodes will include coefficient and exponent.
Processing: The program should accept appropriate values (i.e., coefficient and exponent values) from the user to populate two polynomials. Simple data checking should verify that the integer exponents are non-negative and that coefficients are not zero. Polynomials may have varying numbers of terms. The terms should be stored in the linked list in decreasing order of their exponents.
For example, valid polynomials might be:
3 x to power 7 - 1.5 x to power 4 + 3 (stored as 3 nodes)
7.1 x to power 2 - 6x (stored as 2 nodes)
12 (stored as 1 node)
The program will perform three basic operations on the polynomials: add the two original polynomials together; output the sum subtract one of the original polynomials from the other one; output the difference perform monomial multiplication on one of the original polynomials (i.e., multiply each polynomial term by a given monomial term, not necessarily a scalar); output the monomial and the result
Note: Do not change the original two polynomials during the addition, subtraction or multiplication operations. Include appropriate functions in the class (constructors, etc.) to produce an effective object-oriented program.
Input: Be sure the program is flexible enough that polynomial terms may be supplied by the user out of proper order, but still be stored in proper order.
Output: Test program should allow the user to input the two polynomials and the monomial multiplier. Then output the following:
a) each of the two original polynomials
b) the sum of the polynomials
c) the difference (first polynomial minus the second)
d) another difference (second polynomial minus the first)
e) the result for each of the two polynomials being multiplied by a (different) monomial
Make sure your program in program is well documented both internally and externally.
Explanation / Answer
#ifndef POLY_H
#define POLY_H
#include <string>
using namespace std;
// default size of our dynamic coefficient array
const size_t DEFAULTPOLY = 50;
class Poly
{
private:
// Data members [implementation of ADT's data object]
// Feel free to change to meet your needs.
size_t maxPoly; // size of array
double *coeff; // dynamic array
public:
// 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
Poly ();
// 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
Poly (size_t 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
Poly (const Poly& aPoly);
// Destructor
// Destroy a poly object by freeing the dynamically allocated array
~Poly ();
// Assignment operator
// Assign rhs Poly object to 'this' Poly object
const Poly& operator= (const Poly& rhs);
// Member functions [specs for ADT's operations]
// maxSize
// Return the size of the coefficient array
size_t maxSize() const;
// 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 grow (size_t newSize);
// 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 degree () const;
// 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 setCoeff (double value, size_t i);
// 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 retrieveCoeff (size_t i) const;
// 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 incrementCoeff (double value, size_t i);
// 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 toString () const;
// 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 numOfTerms () const;
// 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 evaluate (double x) const;
// 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 add (const Poly& aPoly);
// 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 subtract (const Poly& aPoly);
// 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!
Poly operator+ (const Poly& rhs) const;
// 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!
Poly operator- (const Poly& rhs) const;
// 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 equals (const Poly& aPoly) const;
// Equality/inequality operators
// note: These functions have been provided for you -- DO NOT CHANGE IT!
bool operator== (const Poly& rhs) const;
bool operator!= (const Poly& rhs) const;
// 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 negate ();
// 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 multByConst (double val);
// 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 derivative ();
// insertion operator for output
// note: This function has been provided for you -- DO NOT CHANGE IT!
friend ostream& operator<< (ostream& os, const Poly &p);
#endif
};
An finally, here is the .cpp file I wrote
#include <iostream>
#include <sstream>
#include <stdexcept>
#include <cmath>
#include "Poly.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
Poly::Poly()
{
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
Poly::Poly(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
Poly::Poly(const Poly& 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
Poly::~Poly()
{
delete[] coeff;
}
// Assignment operator
// Assign rhs Poly object to 'this' Poly object
const Poly& Poly::operator= (const Poly& rhs)
{
if (this == &rhs){
return *this;
}
Poly tmp(rhs);
std::swap(maxPoly, tmp.maxPoly);
std::swap(coeff, tmp.coeff);
return *this;
}
//////////////////////////////////////////////////
//
// Member functions [specs for ADT's operations]
//
//////////////////////////////////////////////////
// maxSize
// Return the size of the coefficient array
size_t Poly::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 Poly::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 Poly::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 Poly::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 Poly::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 Poly::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 Poly::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 Poly::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 Poly::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 Poly::add(const Poly& 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 Poly::subtract(const Poly& 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!
Poly Poly::operator+ (const Poly& rhs) const
{
Poly 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!
Poly Poly::operator- (const Poly& rhs) const
{
Poly 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 Poly::equals(const Poly& 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 Poly::operator== (const Poly& rhs) const
{
return equals(rhs);
}
bool Poly::operator!= (const Poly& 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 Poly::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 Poly::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 Poly::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;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.