Im trying to put together a a polynomial calculator to perform simple arithmatic operations on short polynomials.

I decided to represent each polynomial as a linked list of terms.My problem is that the program crashes on the statment r=p+q where each is a polynomial object. The crash seems to take place within the function operator=, however it works fine on statments such as p=q or r=p. Before the return statment within operator+ the values of the return object show to be correct so it seems as if the problem is in passing the return object to operator=.Here it is..
Code:
struct term
{
	double a;
	int b;
	int sign;
	term* next;
	term(const double c, const int e, const int s);
};

class polynomial
{
public:
	polynomial();
	polynomial(const polynomial&);
	
	~polynomial();
	void read();
	void print();
	const polynomial& operator=(const polynomial& p);
	polynomial operator+(const polynomial& p)const;
	//polynomial operator-(const polynomial&)const;
	//polynomial operator*(const polynomial&)const;
	//polynomial operator/(const polynomial&)const;

private:
	//void sort();
	void clear();
	void copy(const polynomial&);
	term* first;
	
};
 

term::term(const double c, const int e, const int s)
{
	a=c;
	b=e;
	sign=s;
	next=NULL;
}
polynomial::polynomial()
{
	first=NULL;
}
void polynomial::copy(const polynomial& original)
{
	if(first!=NULL)
		clear();

	term* temp1= original.first;
	first=new term(original.first->a,original.first->b,original.first->sign);
	term* temp2=first;
	for(temp1=temp1->next;temp1!=NULL;temp1=temp1->next){
		temp2->next=new term(temp1->a,temp1->b,temp1->sign);
		temp2=temp2->next;}
	
}
polynomial::polynomial(const polynomial& original)
{
	copy(original);
}
void polynomial::clear()
{
	term* temp=first;
	while(temp!=NULL)
	{
		first=first->next;
		delete temp;
		temp=first;
	}
}
polynomial::~polynomial()
{
	clear();
}
void polynomial::read()
{
	double x;
	int y,s;
	
	term* temp;
	char punc,op;
	cout<<"Enter terms, include the degree of each term:";
	
	cin>>x;
	cin>>punc>>punc;
	cin>>y;
	s=1;
	
	first=new term(x,y,s);
	
	temp=first;
	
	cin>>op;
	cin>>x;
	cin>>punc>>punc;
	cin>>y;
		if(op=='-')
		s=-1;
	else
		s=1;
	while(x>0)
	{
	temp->next=new term(x,y,s);
	temp=temp->next;
	cin>>op;
	cin>>x;
	cin>>punc>>punc;
	cin>>y;
		if(op=='-')
		s=-1;
	else
		s=1;
	}
}

//void polynomial::sort()
//{
	
//}
void polynomial::print()
{
	term* temp1=first->next;
	if(first->sign==-1)
		cout<<'-';
	cout<<first->a<<"x^"<<first->b;
	while(temp1!=NULL)
	{
		if(temp1->sign==-1)
			cout<<'-';
		else
			cout<<'+';
		cout<<temp1->a<<"x^"<<temp1->b;
		temp1=temp1->next;
	}
}
const polynomial& polynomial::operator=(const polynomial& p)
{
	cout<<'2'<<endl;
	term* temp1;
	term* temp2;
	
	if(first!=NULL)
		clear();
//crash here
cout<<'3'<<endl;
first=new term(p.first->a,p.first->b,p.first->sign);
cout<<'4'<<endl;
temp1=first;
	
temp2=p.first->next;
	
while(temp2!=NULL)
	{
		temp1->next=new term(temp2->a,temp2->b,temp2->sign);
		temp1=temp1->next;
		temp2=temp2->next;
	}
return *this;
}
polynomial polynomial::operator+(const polynomial& p)const
{
	polynomial sum;
	int tempSign;
	term* temp1;
	term* temp2;
	//term* tempNew=sum.first;

	if(first->b==p.first->b)
	{
		if((first->a*first->sign+p.first->b*p.first->sign)<0)
			tempSign=-1;
		else
			tempSign=1;

		sum.first=new term(abs(first->a*first->sign+p.first->a*p.first->sign),first->b,tempSign);
		temp1=first->next;
		temp2=p.first->next;
	}
	else if(first->b>p.first->b)
	{
		sum.first=new term(first->a,first->b,first->sign);
		temp1=first->next;
	}
	else if(first->b<p.first->b)
	{
		sum.first=new term(p.first->a,p.first->b,p.first->sign);
		temp2=p.first->next;
	}
//values of sum are correct. 
	cout<<sum.first->a<<' '<<sum.first->b<<' '<<sum.first->sign<<endl;
	cout<<'1'<<endl;
	return sum;
//omit further terms



	//while(temp1!=NULL)
	//{
		//if(temp1->b==temp2->b){
		//if((temp->a*temp->sign+temp2->b*temp2->sign)<0)
			//temp3->next->sign=-1;
		//else
			//temp3->next->sign=1;

		//temp3->next->a=abs(temp1->a*temp1->sign+temp2->b*temp2->sign);
		//temp1=temp1->next;
		//temp2=temp2->next;
	//}
	//else if(temp1->b>temp2->b)
	//{

	//}
}
void main()
{
	polynomial p,q,r;
	p.read();
	p.print();
	cout<<endl;

	q.read();
	q.print();
	cout<<endl;
	r=p+q;
	r.print();
	
	cout<<endl;
	
}

Last edited by shabbir; 17Aug2008 at 10:10.. Reason: Code block