Question: why arent my tests passing? In C + + , fix polynomial.h and polynomial.cpp . 1 . The declaration of the Polynomial class has been
why arent my tests passing? In C fix polynomial.h and polynomial.cpp
The declaration of the Polynomial class has been changed, in the private area, to use a std::list. Dont change this data structure.
Your task is to follow through on that change, making the necessary alterations to polynomial.h and polynomial.cpp to make this a working class.
Add to polynomial.h appropriate declarations of iterator and constiterator types and associated functions allowing access to a Polynomial objects terms. Replace the indexingbased code from your former version of polynomial.cpp with iteratorbased code.
An additional change has been made to the interface of class Polynomial. In the earlier assignment, Polynomial had a constructor that took as a parameter a sing #include "polynomial.h
#include
#include
#include
#include
#include
using namespace std;
void Polynomial::normalize
if degree
terms.clear;
return;
auto it terms.begin;
while it terms.end
if itcoefficient
it terms.eraseit;
else
it;
if termsbegin terms.end
degree terms.backpower;
else
degree ;
Polynomial::Polynomial
: degree
Polynomial::Polynomial int b int a
: degree
terms.emplacebacka b;
normalize;
Polynomial::Polynomialstd::initializerlist terms
: degree termsterms
normalize;
Polynomial::Polynomial int nC int coeff
: degreenC
for int i ; i degree; i
terms.emplacebackcoeffi i;
normalize;
int Polynomial::getDegree const
return degree;
int Polynomial::getCoeffint power const
if power && power degree
auto it std::findiftermsbegin terms.endpowerconst Term& term
return term.power power;
;
if it terms.end
return itcoefficient;
return ;
Polynomial Polynomial::operatorconst Polynomial& p const
if degree pdegree
return Polynomial;
int resultSize std::maxdegree pdegree ;
std::list resultTerms;
for int i ; i resultSize; i
int coeff getCoeffi pgetCoeffi;
if coeff
resultTerms.emplacebackcoeff i;
Polynomial result;
result.terms resultTerms;
result.normalize;
return result;
Polynomial Polynomial::operatorint scale const
if degree
return Polynomial;
Polynomial result this;
for auto& term : result.terms
term.coefficient scale;
result.normalize;
return result;
Polynomial Polynomial::operatorTerm term const
if degree
return Polynomial;
std::list resultTerms;
for const auto& t : terms
resultTerms.emplacebacktcoefficient term.coefficient, tpower term.power;
Polynomial result;
result.terms resultTerms;
result.normalize;
return result;
void Polynomial::operatorint scale
if degree
return;
for auto& term : terms
term.coefficient scale;
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 ;
std::list resultTerms;
Polynomial remainder this;
for int i resultSize ; i ; i
int remainderstCoeff remainder.getCoeffi denominator.degree;
int denominatorstCoeff denominator.getCoeffdenominatordegree;
if remainderstCoeff denominatorstCoeff
int coeff remainderstCoeff denominatorstCoeff;
resultTerms.emplacefrontcoeff i;
Polynomial subtractor denominator Termcoeff, i;
remainder remainder subtractor;
else
break;
if remainder Polynomial
Polynomial result;
result.terms resultTerms;
result.normalize;
return result;
else
return Polynomial;
bool Polynomial::operatorconst Polynomial& p const
if getDegree pgetDegree
return false;
return terms pterms;
Polynomial::iterator Polynomial::begin
return terms.begin;
Polynomial::iterator Polynomial::end
return terms.end;
Polynomial::constiterator Polynomial::begin const
return terms.begin;
Polynomial::constiterator Polynomial::end constfo
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
