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.
1. The declaration of the Polynomial class has been changed, in the private area, to use a std::list. Dont change this data structure.
2. 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.
3. Add to polynomial.h appropriate declarations of iterator and const_iterator types and associated functions allowing access to a Polynomial objects terms. Replace the indexing-based code from your former version of polynomial.cpp with iterator-based code.
4. 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 <0)
{
terms.clear();
return;
}
auto it = terms.begin();
while (it != terms.end())
{
if (it->coefficient ==0)
it = terms.erase(it);
else
++it;
}
if (terms.begin()!= terms.end())
degree = terms.back().power;
else
degree =0;
}
Polynomial::Polynomial()
: degree(-1)
{
}
Polynomial::Polynomial (int b, int a)
: degree(1)
{
terms.emplace_back(a, b);
normalize();
}
Polynomial::Polynomial(std::initializer_list terms)
: degree(-1), terms(terms)
{
normalize();
}
Polynomial::Polynomial (int nC, int coeff[])
: degree(nC-1)
{
for (int i =0; i <= degree; ++i)
terms.emplace_back(coeff[i], i);
normalize();
}
int Polynomial::getDegree() const
{
return degree;
}
int Polynomial::getCoeff(int power) const
{
if (power >=0 && power <= degree){
auto it = std::find_if(terms.begin(), terms.end(),[power](const Term& term){
return term.power == power;
});
if (it != terms.end())
return it->coefficient;
}
return 0;
}
Polynomial Polynomial::operator+(const Polynomial& p) const
{
if (degree ==-1|| p.degree ==-1)
return Polynomial();
int resultSize = std::max(degree +1, p.degree +1);
std::list resultTerms;
for (int i =0; i <= resultSize; ++i){
int coeff = getCoeff(i)+ p.getCoeff(i);
if (coeff !=0)
resultTerms.emplace_back(coeff, i);
}
Polynomial result;
result.terms = resultTerms;
result.normalize();
return result;
}
Polynomial Polynomial::operator*(int scale) const
{
if (degree ==-1)
return Polynomial();
Polynomial result (*this);
for (auto& term : result.terms)
term.coefficient *= scale;
result.normalize();
return result;
}
Polynomial Polynomial::operator*(Term term) const
{
if (degree ==-1)
return Polynomial();
std::list resultTerms;
for (const auto& t : terms){
resultTerms.emplace_back(t.coefficient * term.coefficient, t.power + term.power);
}
Polynomial result;
result.terms = resultTerms;
result.normalize();
return result;
}
void Polynomial::operator*=(int scale)
{
if (degree ==-1)
return;
for (auto& term : terms)
term.coefficient *= scale;
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;
std::list resultTerms;
Polynomial remainder =*this;
for (int i = resultSize -1; i >=0; --i){
int remainder1stCoeff = remainder.getCoeff(i + denominator.degree);
int denominator1stCoeff = denominator.getCoeff(denominator.degree);
if (remainder1stCoeff % denominator1stCoeff ==0){
int coeff = remainder1stCoeff / denominator1stCoeff;
resultTerms.emplace_front(coeff, i);
Polynomial subtractor = denominator * Term(-coeff, i);
remainder = remainder + subtractor;
} else {
break;
}
}
if (remainder == Polynomial(0)){
Polynomial result;
result.terms = resultTerms;
result.normalize();
return result;
} else {
return Polynomial();
}
}
bool Polynomial::operator==(const Polynomial& p) const
{
if (getDegree()!= p.getDegree())
return false;
return terms == p.terms;
}
Polynomial::iterator Polynomial::begin()
{
return terms.begin();
}
Polynomial::iterator Polynomial::end()
{
return terms.end();
}
Polynomial::const_iterator Polynomial::begin() const
{
return terms.begin();
}
Polynomial::const_iterator Polynomial::end() constfo

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!