You just need to write the implementation file, the driver and the header are al
ID: 3639766 • Letter: Y
Question
You just need to write the implementation file, the driver and the header are already complete.Write an implementation file for a polynomial class. A polynomial of 3x4+4x2-5x-2 will be stored in a dynamic array named coef:
-2 -5 4 0 3 0 0 0 0 0
0 1 2 3 4 5 6 7 8 9
The coefficient of the term will be stored in the array (double) and the exponent will be the index of the array (unsigned int). You will also keep track of the current size of the dynamic array (initially set to 10) and the current_degree of the polynomial (4 for the above example). Below is given the header file which shows you the functions you will need to write in the implementation file.
poly.h contains the header file included documentation for each function (preconditions and postconditions)
proj2.cpp contains a simple driver program, you may need to add more to the driver
//HEADER FILE
#ifndef POLY_H
#define POLY_H
#include <iostream> // Provides ostream
using namespace std;
const unsigned int DEFAULT_CAPACITY = 10;
class polynomial
{
public:
// CONSTRUCTORS and DESTRUCTOR
polynomial( );
polynomial(const polynomial& source);
~polynomial( );
// MODIFICATION MEMBER FUNCTIONS
void operator =(const polynomial& source);
void assign_coef(double coefficient, unsigned int exponent);
void reserve(unsigned int number);
void setcurrent_degree(int init_degree);
void setsize(int init_size);
// CONSTANT MEMBER FUNCTIONS
double coefficient(unsigned int exponent) const;
unsigned int degree( ) const;
double eval(double x) const;
unsigned int getsize( ) const;
private:
double *coef; // Pointer to the dynamic array
unsigned int size; // Current size of the dynamic array
unsigned int current_degree; // Current degree of the polynomial
};
// NON-MEMBER OUTPUT FUNCTIONS
ostream& operator << (ostream& cout, const polynomial& r);
polynomial operator +(const polynomial& l, const polynomial& r);
polynomial operator -(const polynomial& l, const polynomial& r);
#endif
//DRIVER
#include <iostream>
#include "poly.h"
using namespace std;
void cctest(polynomial);
int main()
{
polynomial P1, P2, P3;
int howmany;
double c, x, result;
unsigned int e;
cout << "Enter how many coeffiecients the polynomial : ";
cin >> howmany;
while (howmany > 0)
{
cout << "Enter coefficient : ";
cin >> c;
cout << "Enter exponent : ";
cin >> e;
P1.assign_coef(c,e);
howmany--;
}
cout << "Enter how many coeffiecients the polynomial : ";
cin >> howmany;
while (howmany > 0)
{
cout << "Enter coefficient : ";
cin >> c;
cout << "Enter exponent : ";
cin >> e;
P2.assign_coef(c,e);
howmany--;
}
cout << "The first polynomial is " << P1 << endl
<< "The second polynomial is " << P2 << endl << endl;
P3 = P2;
cout << "Testing = operator ... " << endl
<< "The second polynomial is " << P3 << endl << endl;
cout << "Results of Addition : " << P1 + P2 << endl;
cout << "Results of Subtraction : " << P1 - P2 << endl << endl;
cout << "Testing Evaluate ..." << endl;
cout << " Enter value of x : ";
cin >> x;
result = P1.eval(x);
cout << " The result of P1 when x is " << x << " is " << result << endl << endl;
cout << "Testing copy constructor..." << endl;
cctest(P1);
return 0;
}
void cctest(polynomial P)
{
cout << " The first polynomial is " << P << endl << endl;
}
The most difficult function may be the operator<< function as there are many special cases for this function. Full credit will be given for polynomials that print in the following format:
3x4+4x2-5x-2 will display 3x^4 + 4x^2 - 5x - 2
3x3 - 7 will display 3x^3 - 7
0 will display 0
5x4+x2-5x+7 will display 5x^4 + x^2 - 5x + 7
Explanation / Answer
Generally, the first character in a class name should be upper case. You should change your class name from "polynomial" to "Polynomial"
Don't use underscore _ to seperate your method names or variables (use for constants only)
current_degree ~> currentDegree
assign_coef ~> assignCoef
getsize ~> getSize
degree ~> getDegree, so you may change currentDegree to degree
//HEADER FILE
#ifndef POLY_H
#define POLY_H
#include <iostream> // Provides ostream
using namespace std;
const unsigned int DEFAULT_CAPACITY = 10;
class polynomial
{
public:
// CONSTRUCTORS and DESTRUCTOR
polynomial();
polynomial(const polynomial& source);
~polynomial();
// NON-MEMBER OUTPUT FUNCTIONS
friend ostream& operator << (ostream& strm, const polynomial& r); //friend function
polynomial operator +(const polynomial& r) const; //don't need l
polynomial operator -(const polynomial& r) const; //don't need l
// MODIFICATION MEMBER FUNCTIONS
const polynomial& operator=(const polynomial& source); //operator= should return "const polynomial&" instead of "void"
void assign_coef(double coefficient, unsigned int exponent); //you may change the array size and current_degree while calling this method
//void reserve(unsigned int number); //???
//void setcurrent_degree(int init_degree); //? avoid any setters if possible
//void setsize(int init_size); //? avoid any setters if possible
// CONSTANT MEMBER FUNCTIONS
double coefficient(unsigned int exponent) const { return coef[exponent]; }
double eval(double x) const;
unsigned int degree( ) const { return current_degree; }
unsigned int getsize( ) const { return size; }
private:
double *coef; // Pointer to the dynamic array
unsigned int size; // Current size of the dynamic array
unsigned int current_degree; // Current degree of the polynomial
};
#endif
//IMPLEMENTATION FILE
#include "Poly.h"
#include "math.h"
//-------------------------------------------
polynomial::polynomial()
:size(DEFAULT_CAPACITY), current_degree(0)
{
coef = new double[size];
for (int i = 0; i < size; i++)
{
coef[i] = 0;
}
}
//-------------------------------------------
polynomial::polynomial(const polynomial& rhs)
:size(rhs.size), current_degree(rhs.current_degree)
{
coef = new double[size];
for (int i = 0; i < size; i++)
{
coef[i] = rhs.coef[i];
}
}
//-------------------------------------------
polynomial::~polynomial()
{
delete [] coef;
}
//-------------------------------------------
ostream& operator <<(ostream& strm, const polynomial& rhs)
{
if (rhs.current_degree > 0)
{
if (rhs.coef[rhs.current_degree] != 1 && rhs.coef[rhs.current_degree] != -1)
strm << rhs.coef[rhs.current_degree];
if (rhs.coef[rhs.current_degree] == -1)
strm << "-";
strm << "x";
if (rhs.current_degree > 1)
{
strm << "^" << rhs.current_degree;
}
for (int i = rhs.current_degree - 1; i >= 0; i--)
{
if (rhs.coef[i])
{
strm << (rhs.coef[i] > 0 ? " + " : " - ");
if ((rhs.coef[i] != 1 && rhs.coef[i] != -1) || !i)
{
strm << (rhs.coef[i] > 0 ? rhs.coef[i] : -rhs.coef[i]);
}
if (i)
cout << "x";
if (i > 1)
strm << "^" << i;
}
}
}
else
{
strm << *rhs.coef;
}
return strm;
}
//-------------------------------------------
const polynomial& polynomial::operator=(const polynomial& rhs)
{
if (this != &rhs)
{
size = rhs.size;
current_degree = rhs.current_degree;
delete [] coef;
coef = new double[size];
for (int i = 0; i < size; i++)
{
coef[i] = rhs.coef[i];
}
}
return *this;
}
//-------------------------------------------
polynomial polynomial::operator+(const polynomial& rhs) const
{
polynomial temp;
bool isGreater = current_degree > rhs.current_degree;
int greaterDegree = (isGreater ? current_degree : rhs.current_degree);
int smallerDegree = (isGreater ? rhs.current_degree : current_degree);
temp.current_degree = (isGreater ? current_degree : rhs.current_degree);
if (greaterDegree > 9)
{
temp.size = greaterDegree + 1;
delete [] temp.coef;
temp.coef = new double[temp.size];
for (int i = 0; i <= greaterDegree; i++)
{
temp.coef[i] = (isGreater ? coef[i] : rhs.coef[i]);
}
for (int i = 0; i <= smallerDegree; i++)
{
temp.coef[i] += (isGreater ? rhs.coef[i] : coef[i]);
}
}
else
{
for (int i = 0; i <= greaterDegree; i++)
{
temp.coef[i] = coef[i] + rhs.coef[i];
}
}
return temp;
}
//-------------------------------------------
polynomial polynomial::operator-(const polynomial& rhs) const
{
polynomial temp;
bool isGreater = current_degree > rhs.current_degree;
int greaterDegree = (isGreater ? current_degree : rhs.current_degree);
int smallerDegree = (isGreater ? rhs.current_degree : current_degree);
temp.current_degree = (isGreater ? current_degree : rhs.current_degree);
if (greaterDegree > 9)
{
temp.size = greaterDegree + 1;
delete [] temp.coef;
temp.coef = new double[temp.size];
for (int i = 0; i <= greaterDegree; i++)
{
temp.coef[i] = (isGreater ? coef[i] : -rhs.coef[i]);
}
for (int i = 0; i <= smallerDegree; i++)
{
temp.coef[i] += (isGreater ? -rhs.coef[i] : coef[i]);
}
}
else
{
for (int i = 0; i <= greaterDegree; i++)
{
temp.coef[i] = coef[i] - rhs.coef[i];
}
}
return temp;
}
//-------------------------------------------
void polynomial::assign_coef(double coefficient, unsigned int exponent)
{
if (exponent > size - 1)
{
double *oldCoef = new double[size];
unsigned int oldSize = size;
for (int i = 0; i < oldSize; i++)
{
oldCoef[i] = coef[i];
}
size = exponent + 1;
delete [] coef;
coef = new double[size];
for (int i = 0; i < size; i++)
{
coef[i] = 0;
}
for (int i = 0; i < oldSize; i++)
{
coef[i] = oldCoef[i];
}
delete [] oldCoef;
}
coef[exponent] = coefficient;
if (current_degree < exponent)
current_degree = exponent;
}
//-------------------------------------------
double polynomial::eval(double x) const
{
double total = 0.0;
for (int i = 0; i <= current_degree; i++)
{
total += coef[i] * pow(x, (double)i);
}
return total;
}
(I don't change anything in your test driver file)
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.