Answered step by step
Verified Expert Solution
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 CYour 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 egxx
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 coefficientsnullptr
Polynomial::Polynomial int b int a
: degree coefficientsnew int
coefficients b;
coefficients a;
normalize;
Polynomial::Polynomial Term term
: degreetermpower coefficientsnew inttermpower
for int i ; i degree; i
coefficientsi;
coefficientsdegree term.coefficient;
normalize;
Polynomial::Polynomial int nC int coeff
: degreenC coefficientsnew intnC
for int i ; i degree; i
coefficientsi coeffi;
normalize;
void Polynomial::normalize
while degree && coefficientsdegree
degree;
int Polynomial::getDegree const
return degree;
int Polynomial::getCoeffint power const
if power && power degree
return coefficientspower;
else
return ;
Polynomial Polynomial::operatorconst Polynomial& p const
if degree pdegree
return Polynomial;
int resultSize maxdegree pdegree;
int resultCoefficients new intresultSize;
int k ;
while k getDegree && k pgetDegree
resultCoefficientsk coefficientsk pcoefficientsk;
k;
for int i k; i getDegree; i
resultCoefficientsi coefficientsi;
for int i k; i pgetDegree; i
resultCoefficientsi pcoefficientsi;
Polynomial resultresultSize resultCoefficients;
delete resultCoefficients;
return result;
Polynomial Polynomial::operatorint scale const
if degree
return Polynomial;
Polynomial result this;
for int i ; i degree; i
result.coefficientsi scale coefficientsi;
result.normalize;
return result;
Polynomial Polynomial::operatorTerm term const
if degree
return Polynomial;
int results new intdegree term.power;
for int i ; i term.power; i
resultsi;
for int i ; i degree ; i
resultsiterm.power coefficientsi term.coefficient;
Polynomial result degree term.power, results;
delete results;
return result;
void Polynomial::operatorint scale
if degree
return;
for int i ; i degree; i
coefficientsi scale coefficientsi;
normalize;
Polynomial Polynomial::operatorconst Polynomial& denominator const
if degree denominator.degree
return Polynomial;
if this Polynomial
return this;
if denominatorgetDegree getDegree
return Polynomial;
int resultSize degree denominator.degree ;
int results new intresultSize;
for int i ; i resultSize; i
resultsi;
Polynomial remainder this;
for int i resultSize; i ; i
Try to divide remainder by denominatorxi
int remainderstCoeff remainder.getCoeffidenominator.getDegree;
int denominatorstCoeff denominator.getCoeffdenominatorgetDegree;
if remainderstCoeff denominatorstCoeff
resultsi remainderstCoeff denominatorstCoeff;
Polynomial subtractor denominator Termresultsi i;
remainder remainder subtractor;
else
Can't divide this
break;
if remainder Polynomial
Polynomial result resultSize results;
delete results;
return result;
else
delete results;
return Polynomial;
Attached is polnomial.h #ifndef POLYNOMIALH
#define POLYNOMIALH
#include
#include
#include "term.h
class Polynomial
public:
Polynomial;
Polynomial int b int a ; Polynomial Term term; Polynomial int nCoeff, int coeff; int getCoeffint power const; int getDegree const; Polynomial operatorconst Polynomial& p const; Polynomial operatorint scale const; Polynomial operatorTerm term const; void operatorint scale; Polynomial operatorconst Polynomial& p const;
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started