Here is the entire code, the header and implementation file.
Code:
//HEADER class.h
#include <iostream>
using namespace std;
class POLY;
class POLY //begin class POLY
{
private:
struct node //begin struct node
{
int pow; //power
double coeff; //coefficient
node *link; //link
}*t; //end struct node, node t
public:
//PROJECT REQUIRE FUNCTIONS
POLY(); //default constructor
POLY(const POLY&); //copy constructor
~POLY(); //destructor
int degree(); //get degree
void print(); //print polynomial
double coefficient(int); //get coefficient of specified term
void changecoefficient(double, int);
void mult(double); //multiply polynomial by a constant
//EXTRA FUNCTIONS (NOT PROJECT REQUIRED)
void ChangeDegree(int,int);
void append(double,int);
}; //end class POLY
POLY::POLY() //being default constructor
{
t = new node;
t->pow = 0;
t->coeff = 0.0;
t->link = NULL;
} //end default constructor
POLY::~POLY() //destructor, delete all objects
{
node *temp;
if(t == NULL)
return;
while(t != NULL){
temp = t->link;
delete t;
t = temp;
}
} //end destructor
POLY::POLY(const POLY& copy) //being copy constructor
{
POLY NewNode;
NewNode = copy;
} //end copy constructor
int POLY::degree() //get degree
{
return t->pow;
}
void POLY::print()
{
node *temp;
temp = t;
do{
if(temp->coeff != 0){
cout<< temp->coeff <<"x^"<< temp->pow;
if(temp->link != NULL) cout<<" + ";
}
temp = temp->link;
}while(temp != NULL);
return;
}
void POLY::ChangeDegree(int New, int old)
{
node *temp;
if(t->pow == old){
t->pow = New;
}
temp = t;
while(temp->link != NULL){
if(temp->link->pow == old)
temp->link->pow = New;
temp->link = temp->link->link;
}
}
//add new polynomial to list
void POLY::append(double co, int power)
{
node *temp = new node;
temp = t;
while(temp->link != NULL)
temp = temp->link;
node *temp2 = new node;
temp2->coeff = co;
temp2->pow = power;
temp2->link = NULL;
temp->link = temp2;
}
double POLY::coefficient(int power)
{
node *temp;
temp = t;
if(t->pow == power)
return t->coeff;
else if(t->link != NULL){
do{
temp = temp->link;
if(temp->pow == power) return temp->coeff;
}while(temp->link != NULL);
}
return 0.0;//return of 0.0 means function failed to find power
}
void POLY::changecoefficient(double newcoeff, int power)
{
node *temp;
temp = t;
if(temp->pow == power){
temp->coeff = newcoeff;
}
else if(t->link != NULL){
do{
temp = temp->link;
if(temp->pow == power) temp->coeff = newcoeff;
}while(temp->link != NULL);
}
else cout<<"Changing coefficient failed!";
}
void POLY::mult(double real_const)
{
node* temp;
temp = t;
if(temp->link == NULL)
temp->coeff = temp->coeff*real_const;
else if(temp->link != NULL)
do{
temp->coeff = temp->coeff*real_const;
temp = temp->link;
}while(temp->link != NULL);
}
//IMPLEMENTATION main.cpp
#include <iostream>
#include <conio.h>
#include "class.h"
POLY Sum(POLY &one, POLY &two);
int main()
{
POLY poly, poly2, ret;
//ret = new POLY;
poly.ChangeDegree(4,0);
poly.changecoefficient(4,4);
poly.append(3,3);
poly.append(2,2);
poly.append(1,1);
cout<<endl<<endl;
poly.print();
poly2.ChangeDegree(5,0);
poly2.changecoefficient(5,5);
poly2.append(4,4);
poly2.append(3,3);
poly2.append(2,2);
cout<<endl<<endl;
poly2.print();
ret = Sum(poly,poly2);
cout<<endl<<endl;
while(!_kbhit());
}
POLY Sum(POLY &one, POLY &two)
{
cout<<"checkpoint 0\n";
POLY ret;
int deg1, deg2, high;
double co1,co2;
deg1 = one.degree();
deg2 = two.degree();
if(deg1 >= deg2){
cout<<"checkpoint 1\n";
high = deg1;
ret.ChangeDegree(deg1,0);
ret.changecoefficient(one.coefficient(one.degree()),deg1);
}
else{
cout<<"checkpoint 2\n";
high = deg2;
ret.ChangeDegree(deg2,0);
ret.changecoefficient(two.coefficient(two.degree()),deg2);
}
cout<<"\nchecking high value...\n"<<ret.coefficient(high)<<"\n\n";
high--;
cout<<"checkpoint 3\n";
while(high >= 0){
ret.append(one.coefficient(high)+two.coefficient(high),high);
high--;
}
cout<<"checkpoint 4\n";
cout<<"checkpoint 5\n";
return ret;
}
nadroj, I debug my programs in a similar, or same way. I narrow things down by either cout, printing, or some form of message saying it got to some point. By doing that I found it got to the comment in blue. Somehow my append function is messing up. I changed the functions that are inside the first argument to a constant number. It then compiled but it went through an infinite loop. Then I checked the value of one.degree() and two.degree(). These are returning false numbers, and I'm not sure why. They work find outside of the function Sum(), but inside the function they don't. What's going on there?