4 Replies - 4010 Views - Last Post: 16 August 2008 - 09:39 PM Rate Topic: -----

#1 aclark17  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 69
  • Joined: 18-June 08

c++ linked list operator overloading

Post icon  Posted 16 August 2008 - 05:22 PM

I'm trying to put together a a polynomial calculator to perform simple arithmetic 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 statement r=p+q where each is a polynomial object. The crash seems to take place within the function operator=, however it works fine on statements such as p=q or r=p. Before the return statement 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..

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;
	
}


*edit: Please use code tags around your code! :code:

This post has been edited by NickDMax: 16 August 2008 - 07:15 PM


Is This A Good Question/Topic? 0
  • +

Replies To: c++ linked list operator overloading

#2 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2250
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: c++ linked list operator overloading

Posted 16 August 2008 - 07:20 PM

does it crash on p = (r+q); ?

Where is the code for polynomial::clear() ?
Was This Post Helpful? 0
  • +
  • -

#3 aclark17  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 69
  • Joined: 18-June 08

Re: c++ linked list operator overloading

Posted 16 August 2008 - 08:19 PM

View PostNickDMax, on 16 Aug, 2008 - 07:20 PM, said:

does it crash on p = (r+q); ?

Where is the code for polynomial::clear() ?


clear is the 5th definition down, no the parentheses don't help in one of my desperate attempts I did in fact try that with no success, right now I'm witting a base class template called list with a node struct which from which class polynomial inherits private access and try see if that helps anything, I really see no logical explanation for this crash SUM! PLEASE HELP!!!
Was This Post Helpful? 0
  • +
  • -

#4 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2250
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: c++ linked list operator overloading

Posted 16 August 2008 - 08:54 PM

I tried to run your program but I can't even figure out how the input routine works...
Was This Post Helpful? 0
  • +
  • -

#5 perfectly.insane  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 70
  • View blog
  • Posts: 644
  • Joined: 22-March 08

Re: c++ linked list operator overloading

Posted 16 August 2008 - 09:39 PM

You should initialize first from your copy constructor. I'm don't think this is the entire problem, but it is a problem, and a copy constructor is invoked in the effective expression r.operator=(p.operator+(q));

// Add initializer, so that copy doesn't try to destroy first when it is called, as it never contains 
// anything when invoked from a constructor.
polynomial::polynomial(const polynomial& original) : first(NULL)
{  
    copy(original);  
} 


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1