The Program has been started but I am having issues finishing the public class in the header and finishing the functions inside the main file.
Header File:
#ifndef POLYNOMIAL_H
#define POLYNOMIAL_H
#include <iostream>
/**
This class implements a polynomial of the form
c0 + c1 x + c2 x^2 + c3 x^3 + ...
As a simplification, this class will be limited to polynomials
of order 99 or less. (The order of a polynomial is the largest
power of x having a non-zero coefficient, or zero if the polynomial
has all zero coefficients.) Note that adding polynomials
together or scaling polynomials (multiplying by a constant) could
reduce the order of the result.
*/
class Polynomial {
public:
Polynomial ();
// Create a polynomial with nCoeff coefficients,
// drawn from the coeff array.
//
// E.g.,
//double c[3] = {1.0, 2.0, 3.0};
//Polynomial p (3, c);
// creates a polynomial p representing: 1.0 + 2.0*x + 3.0*x^2
//
Polynomial (int nCoeff, double coeff[]);
// Create a polynomial with nCoeff coefficients,
// all set equal to c.
//
// E.g.,
//Polynomial p (3, 1.0);
// creates a polynomial p representing: 1.0 + 1.0*x + 1.0*x^2
//
Polynomial (int nCoeff, double c);
// Compute the value of this polynomial at a given value of x.
// E.g.,
//double c[3] = {1.0, 2.0, 3.0};
//Polynomial p (3, c);
//cout << p.evaluate(2.0);
// will print 17.0
double evaluate (double x) const;
// The order of a polynomial is the largest exponent of x with a
// non-zero coefficient. E.g., for the sample polynomial used
// above, the order is 2.
int getOrder() const;
// Add two polynomials, returning the polynomial representing
// their sum.
Polynomial operator+ (const Polynomial& p) const;
// Multiply a polynomial by a floating point value.
Polynomial operator* (double scale) const;
private:
int order;
double* coefficients; // array of coefficients.
// Positions [0..order] are significant
// All positions > order are ignored and
// assumed to be zero.
friend std::istream& operator>> (std::istream&, Polynomial&);
friend std::ostream& operator<< (std::ostream&, const Polynomial&);
// Internal utility function - coputes and remembers the order
// of a polynomial. Use this at the end of any function that can change
// the coefficients of the polynomial.
void checkOrder();
};
// Read a polynomial from an istream.
// Polynomials are input in the format:
// #coefficients c0 c1 ...
// where the ci are floating point coefficients of x^i
std::istream& operator>> (std::istream&, Polynomial&);
// Write a polynomial to an ostream
std::ostream& operator<< (std::ostream&, const Polynomial&);
inline
Polynomial operator* (double scale, const Polynomial& p)
{ return p * scale; }
/* Note: a member function of the Polynomial class must have a polynomial
as the first argument (e.g., poly.operator*(x) <===> poly * x). This
function simply allows for the multiplication to be written with the
polynomial on the right. */
#endif
Main File:
#include "polynomial.h"
using namespace std;
Polynomial::Polynomial ()
: order(0)
{
coefficients = new double[1];
coefficients[0] = 0.0;
}
Polynomial::Polynomial (int nCoeff, double coeff[])
// code here
: order(1)
{
checkOrder();
}
Polynomial::Polynomial (int nCoeff, double c)
: order(nCoeff-1)
{
coefficients = new double[nCoeff];
for (int i = 0; i < nCoeff; ++i)
coefficients[i] = c;
checkOrder();
}
void Polynomial::checkOrder ()
{
while (order > 0 && coefficients[order] == 0.0)
--order;
}
double Polynomial::evaluate (double x) const
{
double sum = 0.0;
for (int i = 0; i <= order; ++i)
sum = x * sum + coefficients[order - i];
return sum;
}
int Polynomial::getOrder() const
{
return order;
}
Polynomial Polynomial::operator+ (const Polynomial& p) const
{
//code here
Polynomial result (1, 0.0);
result.checkOrder();
return result;
}
Polynomial Polynomial::operator* (double scale) const
{
Polynomial result (*this);
for (int i = 0; i <= order; ++i)
result.coefficients[i] = scale * coefficients[i];
return result;
}
std::istream& operator>> (std::istream& in, Polynomial& p)
{
int r;
in >> r;
Polynomial result(r, 1.0);
result.order = r - 1;
for (int i = 0; i < r; ++i)
in >> result.coefficients[i];
result.checkOrder();
p = result;
return in;
}
std::ostream& operator<< (std::ostream& out, const Polynomial& p)
{
for (int i = 0; i <= p.order; ++i)
{
if (i > 0)
{
out << " + ";
}
out << p.coefficients[i];
if (i > 0)
{
out << " x";
if (i > 1)
{
out << "^" << i;
}
}
}
return out;
}