Solved

# Need help with Polynomial class

Posted on 2003-03-04

I am having problems with my implimenation file. The functions reserve, eval and addition keep are where the bulk of my errors coming up. I can't understand why because i have seen it written the same way by other and they had no problems.

// Driver program for the polynomial class

#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;

}

// CLASS IMPLEMENTED: polynomial (see poly1.h for documentation)

// INVARIANT for the polynomial class:

// 1. coef points to a dynamic array with size elements.

// 2. For each k < size, the coefficient of the x^k term is

// stored in coef[k].

// 3. The degree of the polynomial is stored in current_degree

// (using zero for the case of all zero coefficients).

#include <cassert> // Provides assert

#include <cmath> // Provides pow

#include <iostream> // Provides ostream

#include "poly.h"

using namespace std;

polynomial::polynomial(const polynomial& source)

{

coef = new double[source.size];

size = source.size;

current_degree = source.current_degree;

for (int i = 0; i < size; i++)

coef[i] = source.coef[i];

}

void polynomial::operator=(const polynomial& source)

{

double *new_coef;

if (this == &source)

return;

if (size != source.size)

{

new_coef = new double[source.size];

delete [ ] coef;

coef = new_coef;

size = source.size;

}

current_degree = source.current_degree;

for(size_t i = 0; i < size; i++)

coef[i] = source.coef[i];

}

void polynomial::assign_coef(double coefficient, unsigned int exponent)

{

if(exponent >= current_degree)

reserve(exponent);

coef[exponent] = coefficient;

size++;

}

void polynomial::reserve(unsigned int number)

{

double new_poly;

if(number == current_degree)

return;

if(number < size)

number = size;

new_poly = new double[number];

for(int i = 0; i < size; i++)

new_poly[i] = coef[i];

delete[] coef;

coef = new_poly;

current_degree = number;

}

double polynomial::coefficient(unsigned int exponent)const

{

return (coef[exponent]);

}

double polynomial::eval(double x)const

{

size_t i = 0;

double answer = 0.0;

answer = coef;

for(i = 1; i< size; i++)

answer = answer +(coef[i] * pow(x,i));

return answer;

}

polynomial operator+(const polynomial & p1, const polynomial & p2)

{

polynomial t;

if (size !=0)

{

reserve(p1.size);

t.coefficient = p1.coefficient;

for(int i = 0; i > p2.size; i--)

t.coefficient[i] += p2.coefficient[i];

}

else

{

reserve(p2.size);

t.coefficient = p2.coefficient;

for(int i = 0; i < p1.size; i++)

t.coefficient[i] += p1.coefficient[i];

}

return t;

}

polynomial operator-(const polynomial & p1, const polynomial & p2)

{

return p1 + (-1 * p2);

}

ostream & operator<<(ostream & out, const polynomial & p)

{

size_t i = 0;

if(size != 0)

{

out << "The Polynomial is: " << size << endl;

for(i = size - 1; i > 0; i--)

{/*125*/

cout << coef[i] << "x^" << i;

if(i != 0)

cout << "+";

}

if (i == 0)

cout << coef[0] << endl;

}

else

out << "Sorry no polynomials" << endl;

return out;

}

// FILE: poly.h

// CLASS PROVIDED:

// class polynomial

// A polynomial has one variable x, real number coefficients, and

// non-negative integer exponents. Such a polynomial can be viewed

// as having the form:

// A[n]*x^n + A[n-1]*x^(n-1) + ... A[2]*x^2 + A[1]*x + A[0]

// where the A[n] are the real number coefficients and x^i represents

// the variable x raised to the i power. The coefficient A[0] is

// called the "constant" or "zeroth" term of the polynomial.

// 1. This version works by storing the coefficients in

// a dynamic array. The coefficient for the x^k term is stored

// in location [k] of the dynamic array. When a new term is

// added beyond the current size of the array, then the

// dynamic array is replaced by a new, larger array.

// 2. Note that one function has been implemented as inline functions

// in this file (the degree function).

// MEMBER CONSTANTS

// const size_t DEFAULT_CAPACITY

// The size of the initial array to store the coefficients.

//

// CONSTRUCTOR for the polynomial class

// POSTCONDITION: This polynomial has been create with all zero

// coefficients, except for coefficient c for the specified exponent.

// When used as a default constructor (using default values for

// both arguments), the result is a polynomial with all zero

// coefficients.

// MODIFICATION MEMBER FUNCTIONS for the polynomial class

// void assign_coef(double coefficient, unsigned int exponent)

// POSTCONDITION: Sets the coefficient for the specified exponent.

//

// void reserve(unsigned int number)

// POSTCONDITION: The size of the array for coefficients has been changed

// to the requested number (but not less that the size needed to store the

// current non-zero coefficients). In effect, this guarantees that member

// functions will not need to allocate new memory for exponents through

// CONSTANT MEMBER FUNCTIONS for the polynomial class

// double coefficient(unsigned int exponent) const

// POSTCONDITION: Returns coefficient at specified exponent of this

// polynomial.

//

// unsigned int degree( ) const

// POSTCONDITION: The function returns the value of the largest exponent

// with a non-zero coefficient.

// If all coefficients are zero, then the function returns zero.

//

// NON-MEMBER BINARY OPERATORS for the polynomial Class

// polynomial operator -(const polynomial& p1, const polynomial& p2)

// POSTCONDITION: return-value is a polynomial with each coefficient

// equal to the difference of the coefficients of p1 & p2 for any given

// exponent.

//

// polynomial operator +(const polynomial& p1, const polynomial& p2)

// POSTCONDITION: return-value is a polynomial with each coefficient

// equal to the sum of the coefficients of p1 & p2 for any given

// exponent.

//

// NON-MEMBER OUTPUT FUNCTIONS for the polynomial Class

// ostream& operator << (ostream& out, const polynomial& p)

// POSTCONDITION: The polynomial has been printed to ostream out, which,

// in turn, has been returned to the calling function.

//

// DYNAMIC MEMORY

// Since this class uses dynamic memory, the copy constructor and assignment

// operator are overridden, and there is a destructor implemented.

#ifndef POLY_H

#define POLY_H

#include <iostream> // Provides ostream

using namespace std;

const unsigned int DEFAULT_CAPACITY = 30;

class polynomial

{

public:

// CONSTRUCTORS and DESTRUCTOR

polynomial(double c = 0.0, unsigned int exponent = 0);

polynomial(const polynomial& source);

~polynomial( ){delete[] coef;}

// 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) {current_degree = init_degree;}

// CONSTANT MEMBER FUNCTIONS

double coefficient(unsigned int exponent) const;

unsigned int degree( ) const { return current_degree; }

double eval(double x) 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 FUNCTIONS

ostream& operator << (ostream& out, const polynomial& p);

polynomial operator +(const polynomial& p1, const polynomial& p2);

polynomial operator -(const polynomial& p1, const polynomial& p2);

#endif