Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Complete in C + + . Your focus in this assignment will on the implementation of the Polynomial class in files polynomial.h and polynomial.cpp .

Complete in C++.Your focus in this assignment will on the implementation of the Polynomial class in files polynomial.h and polynomial.cpp. This class stores the set of coefficients that define a polynomial (e.g.,1+2x+4x^3).
Although the immediate use of Polynomial is in support of this polyfactor program, we anticipate the possibility that Polynomial may be reused in other future projects, so we want to make sure that it is designed and implemented to facilitate that reuse. You must not change the private data members in polynomial.h.
You may need to change some of the public functions in polynomial.h, but keep in mind that the Polynomial class must continue to compile with the other code in this program. Attached is polnomial.cpp #include "polynomial."
#include
using namespace std;
Polynomial::Polynomial ()
: degree(-1), coefficients(nullptr)
{
}
Polynomial::Polynomial (int b, int a)
: degree(1), coefficients(new int[2])
{
coefficients[0]= b;
coefficients[1]= a;
normalize();
}
Polynomial::Polynomial (Term term)
: degree(term.power), coefficients(new int[term.power+1])
{
for (int i =0; i < degree; ++i)
coefficients[i]=0;
coefficients[degree]= term.coefficient;
normalize();
}
Polynomial::Polynomial (int nC, int coeff[])
: degree(nC-1), coefficients(new int[nC])
{
for (int i =0; i <= degree; ++i)
coefficients[i]= coeff[i];
normalize();
}
void Polynomial::normalize ()
{
while (degree+1>1 && coefficients[degree]==0)
--degree;
} int Polynomial::getDegree() const
{
return degree;
}
int Polynomial::getCoeff(int power) const
{
if (power >=0 && power <= degree)
{
return coefficients[power];
}
else
{
return 0.0;
}
}
Polynomial Polynomial::operator+(const Polynomial& p) const
{
if (degree ==-1|| p.degree ==-1)
return Polynomial();
int resultSize = max(degree+1, p.degree+1);
int* resultCoefficients = new int[resultSize];
int k =0;
while (k <= getDegree() && k <= p.getDegree())
{
resultCoefficients[k]= coefficients[k]+ p.coefficients[k];
++k;
}
for (int i = k; i <= getDegree(); ++i)
resultCoefficients[i]= coefficients[i];
for (int i = k; i <= p.getDegree(); ++i)
resultCoefficients[i]= p.coefficients[i];
Polynomial result(resultSize, resultCoefficients);
delete[] resultCoefficients;
return result;
}
Polynomial Polynomial::operator*(int scale) const
{
if (degree ==-1)
return Polynomial();
Polynomial result (*this);
for (int i =0; i <= degree; ++i)
result.coefficients[i]= scale * coefficients[i];
result.normalize();
return result;
}
Polynomial Polynomial::operator*(Term term) const
{
if (degree ==-1)
return Polynomial();
int* results = new int[degree +1+ term.power];
for (int i =0; i < term.power; ++i)
results[i]=0;
for (int i =0; i < degree +1; ++i)
results[i+term.power]= coefficients[i]* term.coefficient;
Polynomial result (degree +1+ term.power, results);
delete [] results;
return result;
}
void Polynomial::operator*=(int scale)
{
if (degree ==-1)
return;
for (int i =0; i <= degree; ++i)
coefficients[i]= scale * coefficients[i];
normalize();
}
Polynomial Polynomial::operator/(const Polynomial& denominator) const
{
if (degree ==-1|| denominator.degree ==-1)
return Polynomial();
if (*this == Polynomial(0))
return *this;
if (denominator.getDegree()> getDegree())
return Polynomial();
int resultSize = degree - denominator.degree +1;
int* results = new int[resultSize];
for (int i =0; i < resultSize; ++i)
results[i]=0;
Polynomial remainder =*this;
for (int i = resultSize-1; i >=0; --i)
{
// Try to divide remainder by denominator*x^(i-1)
int remainder1stCoeff = remainder.getCoeff(i+denominator.getDegree());
int denominator1stCoeff = denominator.getCoeff(denominator.getDegree());
if (remainder1stCoeff % denominator1stCoeff ==0){
results[i]= remainder1stCoeff / denominator1stCoeff;
Polynomial subtractor = denominator * Term(-results[i], i);
remainder = remainder + subtractor;
} else {
// Can't divide this
break;
}
}
if (remainder == Polynomial(0)){
Polynomial result (resultSize, results);
delete [] results;
return result;
}
else
{
delete [] results;
return Polynomial();
}
} Attached is polnomial.h #ifndef POLYNOMIAL_H
#define POLYNOMIAL_H
#include
#include
#include "term.h"
class Polynomial {
public:
Polynomial();
Polynomial (int b, int a =0); Polynomial (Term term); Polynomial (int nCoeff, int coeff[]); int getCoeff(int power) const; int getDegree() const; Polynomial operator+(const Polynomial& p) const; Polynomial operator*(int scale) const; Polynomial operator*(Term term) const; void operator*=(int scale); Polynomial operator/(const Polynomial& p) const;

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Lab Manual For Database Development

Authors: Rachelle Reese

1st Custom Edition

1256741736, 978-1256741732

More Books

Students also viewed these Databases questions

Question

b. Why were these values considered important?

Answered: 1 week ago