10/13/05

Finally, I think I can breath now. The big project I’ve been working on went live today. Training the users starts tomorrow. The waiting for the big bomb of a major bug starts tomorrow, but for now I can sit back and enjoy listening to “99 Luftballons” over and over until I leave for class. Tonight I get to turn in my third project that I am quite pleased with – especially after hearing the other students fret and moan about it on Tuesday.
I don’t have anything else to say at the moment, so enjoy my polynomial object class template in C++! I could have used this in Pre-Calc and Calc! Well okay, so I wouldn’t have understood how to write this code without taking those classes first, and this doesn’t even handle decimals or negative exponents; but it still would have been handy for checking my work. Maybe I’ll enhance it some more before I take Calc II to refresh my memory. Anyway, enjoy!

using namespace std;

template
class Polynomial
{
private:
/*** Term Class ***/
class Term
{
public:
CoeffType coeff;
unsigned expo;
}; //end Term class

/*** Node Class ***/
class Node
{
public:
Term data;
Node * next;

Node(CoeffType co = 0, int ex = 0, Node * ptr = 0)
{
data.coeff = co;
data.expo = ex;
next = ptr;
}

const void display()
{
if (data.expo==0)
cout << " " << data.coeff << " ";
else if (data.expo==1)
{
if (data.coeff==1)
cout << " x ";
else
cout << " " << data.coeff << "x" << " ";
}
else
{
if (data.coeff==1)
cout << " x^" << data.expo << " ";
else
cout << " " << data.coeff << "x^" << data.expo << " ";

}
}
}; //end Node class
typedef Node * NodePointer;

public:
/*** Function members ***/
Polynomial();
void addTerm(CoeffType newCoeff, unsigned newExpo);
void getInput();
bool isEmpty() const;
void display() const;
//Operations on polynomials
Polynomial operator+(const Polynomial & secondPoly) const;
Polynomial operator-(const Polynomial & secondPoly) const;
Polynomial operator*(const Polynomial & secondPoly) const;
Polynomial getDerivative() const;
CoeffType value(CoeffType x) const;

private:
/*** Data members ***/
unsigned degree;
NodePointer terms;
NodePointer end;

}; //end Polynomial class

/**** Implementations ****/
// ——— Constructor ———-
template
Polynomial::Polynomial()
{
terms = new Node();
end = terms;
degree = 0;
}

template
void Polynomial::addTerm(CoeffType newCoeff, unsigned newExpo)
{
NodePointer newNode = new Node(newCoeff, newExpo);
//Figure out where to insert the node to keep the terms in standard order (highest to lowest.)
if (newExpo > degree || isEmpty()) //special cases, insert at top
{
//The largest exponent belongs at the top of the list.
degree = newExpo;
newNode->next = terms;
terms = newNode;
}
else
{
NodePointer predecessor = terms;
NodePointer current = terms->next;
//loop through current terms to find the proper place
do
{
if (current == end || newExpo > current->data.expo)
{
predecessor->next = newNode;
newNode->next = current;
}
else if (newExpo == current->data.expo)
{
//just add the coefficients
current->data.coeff += newCoeff;
delete newNode;
break;
}
else if (current->next == end)
{
newNode->next = end;
current->next = newNode;
}
predecessor = current;
current = current->next;
}
while (current != end && newNode->next == 0);
}

}

template
void Polynomial::getInput()
{
CoeffType inputCoeff = 0;
unsigned inputExpo = 0;
cout << "Enter a coefficient followed by an exponent." << endl;
cout << "Enter a coefficient of zero (0) to end input." << endl;
do
{
cout << "Coefficient:t";
cin >> inputCoeff;
if (inputCoeff == 0) break;
cout << "Exponent:t";
cin >> inputExpo;
addTerm(inputCoeff, inputExpo);
}
while (inputCoeff != 0);
}

template
bool Polynomial::isEmpty() const
{
return (terms == end);
}

template
void Polynomial::display() const
{
NodePointer current = terms;
while (current->next != 0)
{
current->display();
if (current->next != end)
cout << "+";
current = current->next;
}
cout << endl;
cout << "Degree: " << degree << endl << endl;
}

template
Polynomial Polynomial::operator+(const Polynomial & secondPoly) const
{
Polynomial polySum;
Polynomial::NodePointer
ptra = terms,
ptrb = secondPoly.terms;

while (ptra != end || ptrb != secondPoly.end)
{
if ((ptrb == secondPoly.end) || (ptra != end && ptra->data.expo data.expo))
{
polySum.addTerm(ptra->data.coeff, ptra->data.expo);
ptra = ptra->next;
}
else if ((ptra == end) || (ptrb != secondPoly.end && ptrb->data.expo data.expo))
{
polySum.addTerm(ptrb->data.coeff, ptrb->data.expo);
ptrb = ptrb->next;
}
else
{
CoeffType sum = ptra->data.coeff + ptrb->data.coeff;
if (sum != 0)
{
polySum.addTerm(sum, ptra->data.expo);
}
ptra = ptra->next;
ptrb = ptrb->next;
}
}
return polySum;
}

template
Polynomial Polynomial::operator-(const Polynomial & secondPoly) const
{
Polynomial polyDiff;
Polynomial::NodePointer
ptra = terms,
ptrb = secondPoly.terms;

while (ptra != end || ptrb != secondPoly.end)
{
if ((ptrb == secondPoly.end) || (ptra != end && ptra->data.expo data.expo))
{
polyDiff.addTerm(ptra->data.coeff, ptra->data.expo);
ptra = ptra->next;
}
else if ((ptra == end) || (ptrb != secondPoly.end && ptrb->data.expo data.expo))
{
//subtract the coefficient
polyDiff.addTerm(-1*ptrb->data.coeff, ptrb->data.expo);
ptrb = ptrb->next;
}
else
{
CoeffType diff = ptra->data.coeff – ptrb->data.coeff;
if (diff != 0)
{
polyDiff.addTerm(diff, ptra->data.expo);
}
ptra = ptra->next;
ptrb = ptrb->next;
}
}
return polyDiff;
}

template
Polynomial Polynomial::operator*(const Polynomial & secondPoly) const
{
Polynomial polyProduct;
NodePointer
ptra = terms,
ptrb = secondPoly.terms;

while (ptra != end)
{
while (ptrb != secondPoly.end)
{
CoeffType expoSum = ptra->data.expo + ptrb->data.expo;
CoeffType coeffProduct = ptra->data.coeff * ptrb->data.coeff;
polyProduct.addTerm(coeffProduct, expoSum);
ptrb = ptrb->next;
}
ptra = ptra->next;
ptrb = secondPoly.terms; //reset to the beginning of the list
}
return polyProduct;
}

template
Polynomial Polynomial::getDerivative() const
{
Polynomial polyDerivative;
NodePointer ptra = terms;

while (ptra != end && ptra->data.expo > 0)
{
CoeffType newCoeff = ptra->data.expo * ptra->data.coeff;
CoeffType newExpo = ptra->data.expo – 1;
polyDerivative.addTerm(newCoeff, newExpo);
ptra = ptra->next;
}
return polyDerivative;
}

template
CoeffType Polynomial::value(CoeffType x) const
{
CoeffType result = 0;
NodePointer ptra = terms;

while (ptra != end)
{
if (ptra->data.expo == 1)
result += ptra->data.coeff * x;
else if (ptra->data.expo == 0)
result += ptra->data.coeff;
else
result += ptra->data.coeff * pow(x, ptra->data.expo);
ptra = ptra->next;
}
return result;
}

Can you find the bug?

Log in to write a note