Expand|Select|Wrap|Line Numbers
- #ifndef POLYNOMIAL_H
- #define POLYNOMIAL_H
- class Polynomial
- {
- public:
- Polynomial( ){}
- Polynomial(int theCoefficient);
- Polynomial(int theCoefficient, int thePower);
- Polynomial(Polynomial&);//copy constructor
- Polynomial* getNext( ) {return next;}
- void setNext(Polynomial* polyLoc) {next = polyLoc;}
- double evaluate(double value);
- Polynomial& operator =(Polynomial& rightSide);
- Polynomial operator +(Polynomial& polyObj);
- Polynomial operator -(Polynomial& polyObj);
- Polynomial operator *(Polynomial& polyObj);
- virtual ~Polynomial( );
- private:
- int coefficient;
- int power;
- Polynomial* next;
- Polynomial* head;
- };
- #endif //POLYNOMIAL_H
- //begin Polynomial.cpp
- #include <iostream>
- #include <math.h>
- #include "polynomial.h"
- using namespace std;
- Polynomial::Polynomial(int theCoefficient)
- {
- head = new Polynomial;
- coefficient = theCoefficient;
- power = 0;
- next = new Polynomial;
- setNext(NULL);
- }
- Polynomial::Polynomial(int theCoefficient, int thePower)
- {
- head = new Polynomial;
- coefficient = theCoefficient;
- power = thePower;
- next = new Polynomial;
- setNext(NULL);
- }
- Polynomial::Polynomial(Polynomial& polyObj)
- {/*not completed*/ }
- double Polynomial::evaluate(double value)
- {
- double total = 1;
- Polynomial* temp = head;
- while(temp->getNext() != NULL)
- {
- total = total + pow((coefficient*value), power);
- temp = temp->getNext();
- }
- return total;
- }
- Polynomial& Polynomial::operator =(Polynomial& rightSide)
- {
- power = rightSide.power;
- coefficient = rightSide.coefficient;
- next = rightSide.next;
- return *this;
- }
- Polynomial Polynomial::operator +(Polynomial& polyObj)
- {
- Polynomial* temp = head;
- while(temp->getNext() != NULL)
- {
- if (power == polyObj.power)
- coefficient = coefficient + polyObj.coefficient;
- temp = temp->getNext();
- }
- return *this;
- }
- Polynomial Polynomial::operator -(Polynomial& polyObj)
- {
- Polynomial* temp = head;
- while(temp->getNext() != NULL)
- {
- if (power == polyObj.power)
- coefficient = coefficient - polyObj.coefficient;
- temp = temp->getNext();
- }
- return *this;
- }
- Polynomial Polynomial::operator *(Polynomial& polyObj)
- {
- Polynomial* temp = head;
- while(temp->getNext() != NULL)
- {
- coefficient = coefficient*polyObj.coefficient;
- power = power+polyObj.power;
- temp = temp->getNext();
- }
- return *this;
- }
- Polynomial::~Polynomial( )
- {
- Polynomial* temp = head;
- while(temp->getNext() != NULL)
- {
- temp = temp->getNext();
- delete head;
- head = temp;
- }
- delete head;
- }
THanks