Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

Complete in C++.
1. 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+4x31+2x+4x3).
o You must not change the private data members in polynomial.h.(You may, if you wish, add additional private function members if doing so would aid you in completing the implementation.)
o You may need to change some of the public functions in polynomial.h
2. Your code will be evaluated both on its ability to function correctly within the polyfactor application (the systems tests) and on its ability to pass the various unit tests provided.
Files that you may change: polynomial.h, polynomial.cpp
Attached is polynomial.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;
private:
int degree;
int* coefficients;
void normalize();
friend std::ostream& operator<<(std::ostream&, const Polynomial&);
};
std::ostream& operator<<(std::ostream&, const Polynomial&);
inline
Polynomial operator*(int by, const Polynomial& p)
{ return p * by; }
#endif
Attached is polynomial.cpp.
#include "polynomial.h"
#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)
{
int remainder1stCoeff = remainder.getCoeff(i+denominator.getDegree());
int 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 {
break;
}
}
if (remainder == Polynomial(0)){
Polynomial result (resultSize, results);
delete [] results;
return result;
}
else {
delete [] results;
return Polynomial();
}
}

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

The Accidental Data Scientist

Authors: Amy Affelt

1st Edition

1573877077, 9781573877077

More Books

Students also viewed these Databases questions