[C++]Polynomial Class

Polynomial Class
The eval member function uses Horner algorithm.

File: polynomial.h

#ifndef POLYNOMIAL_H
#define POLYNOMIAL_H

#include
using std::ostream;
// polynomial:= a[n-1]x^(n-1) + ... + a[0]

class Polyn
{  
    // Parameters must be const to perform opreations like
    //  Polyn y; t = 1+y; or t = 1*y;
    friend Polyn operator+(const Polyn &, const Polyn &);
    friend Polyn operator*(const Polyn &, const Polyn &);
    friend ostream &operator<<(ostream &, Polyn &);

public:

    Polyn();
    Polyn(int, double *);
    // Polyn(5) calls this ctor
    // Polyn y = 5 calls this ctor
    Polyn(const double ); // conversion cotr
    Polyn(const Polyn &); // copy cotr
    ~Polyn();
    Polyn &operator=(const Polyn &p);
    double eval(double x);
    void printCoef();
    
private:
    int degree;
    double *coef;
    // suppress implicit conversion, Polyn y = 5; is
    // not allowed
    // client can't access this cotr
   // explicit Polyn(const int);
    // Polyn(int);    
    void copyMembers(const Polyn &);  
};

#endif

File: Polynomial.cpp
#include
#include
#include
#include
#include "polynomial.h"

using namespace std;

// default cotr
Polyn::Polyn():degree(0)
{
    coef = new double;
}

// cotr
Polyn::Polyn(int d, double *c)
{
    degree = d;
    coef = new double[degree + 1];
    assert(coef != NULL);
    for (int i = 0; i <= degree; ++i)
        coef[i] = c[i];    
}

// conversion cotr
Polyn::Polyn(double c)
{
    degree = 0;
    coef = new double;
    *coef = c;
}

// copy cotr
Polyn::Polyn( const Polyn &p)
{
    copyMembers(p);
}

/*
Polyn &Polyn::operator=(const Polyn &p)
{   
    // return new Polyn(p); //not work

    // *this =  new Polyn(p); // not work
    // return *this; //not work
}    
*/

Polyn::~Polyn()
{
    delete [] coef;
}

// assignment operator
Polyn &Polyn::operator=(const Polyn &p)
{  
    copyMembers(p);
    return *this; 
}   

// Horner Algorithm
double Polyn::eval(double x)
{
    double val = 0;
    for(int i = degree; i >=0; --i)
        val = val*x + coef[i]; 
    return val;
}

//print coefficients starting with a[n]
void Polyn::printCoef()
{
    for (int i = degree; i >= 0; --i )
      cout << coef[i] << " " ;
    cout << endl;

}
/*
// private
// explicit: Polyn y = 5 is not allowed
Polyn::Polyn(const int d):degree(d)
{
    coef = new double[degree + 1];
    assert(coef != NULL);
    for (int i = 0; i <= degree; ++i)
        coef[i] = 0;
}
*/
// private
void Polyn::copyMembers(const Polyn &p)
{
    degree = p.degree;
    coef = new double[degree + 1];
    assert(coef != NULL);
    for (int i = 0; i <= degree; ++i)
        coef[i] = p.coef[i];
}

// Starting of friend function definitions
Polyn operator+( const Polyn &p, const Polyn &q)
{
    int d = (p.degree >= q.degree) ? p.degree : q.degree;
    Polyn temp(d);
    for(int i = 0; i <= p.degree; ++i)
        temp.coef[i] += p.coef[i];
    for ( int j = 0; j<= q.degree; ++j)
        temp.coef[j] += q.coef[j];
    return temp;
}

Polyn operator*(const Polyn &p,const Polyn &q)
{
    int d = p.degree + q.degree;
    Polyn temp(d);
    for(int i = 0; i <= p.degree; ++i)
        for (int j = 0; j <= q.degree; ++j)
        { 
            temp.coef[i + j] += p.coef[i] * q.coef[j] ;
        }
    return temp;
}

ostream &operator<<(ostream &out, Polyn & p)
{
 if (p.degree == 0) 
    cout << p.coef[0] << endl;
 else 
 {

    // print the leading term
    if(p.coef[p.degree] == 1 ) 
        out << "x^" << p.degree << " ";
    else if (p.coef[p.degree] == -1)
        out << "-x^" << p.degree << " ";
    else
        out << p.coef[p.degree] << "x^" << p.degree << " ";

    // print middle terms
    for(int i = p.degree - 1; i > 0; --i)
    {
        if( p.coef[i] != 0) 
        { 
            if (p.coef[i] != 1 ) 
            { 
                out.setf(ios::showpos);
                out  << p.coef[i] << "x^" ;
                out.unsetf(ios::showpos);
                out << i << " ";              
            }
            else
                out << "+x^" << i << " " ;  
        }
    } // end of for

    // print the constant
    out.setf(ios::showpos);
    out <<  p.coef[0] << endl;
    out.unsetf(ios::showpos);
 }
    return out; 
}// end of friend function definitions


No comments: