The offending line is the one commented in the first post. Here are Poly and Term:
Code:
//term.h
#ifndef _TERM_H
#define _TERM_H
#include <cmath>
#include <iostream>
using namespace std;
class Term
{
private:
double coef;
int expn;
public:
Term() { set(0.0, 0); }
Term(double c, int x) { set(c, x); }
double getCoef() const { return coef; }
int getExpn() const { return expn; }
void setCoef(double c) { coef = c; }
void setExpn(int x) { expn = x; }
void set(double c, int x) { setCoef(c); setExpn(x); }
double eval(double x) const { return (coef * pow(x, expn)); }
friend ostream & operator << (ostream &, const Term &);
};
#endif
Code:
//poly.h
#ifndef _POLY_H
#define _POLY_H
#include <iostream>
#include "llist.h"
#include "term.h"
using namespace std;
class Poly
{
private:
LList<Term> P;
public:
Poly() {}
Poly(double c, int x) { Term temp(c,x); P.insert(temp); }
void addTerm(double c, int x)
{
Iterator<Term> it(P);
Term temp(c,x);
do
{
double cof = it.getData().getCoef();
int xpn = it.getData().getExpn();
if (xpn == x)
{
it.getData().setCoef(cof+c);
break;
}
} while (it.next());
}
void subTerm(double c, int x) { addTerm(-c, x); }
double solve(double x)
{
Term temp;
double total = 0;
Iterator<Term> it(P);
it.seekStart();
do
{
temp = it.getData();
total += temp.eval(x);
} while (it.next());
return total;
}
friend class Iterator<Term>;
friend ostream & operator << (ostream &, const Poly &);
};
#endif