Page 1 of 1

## 4 Replies - 8210 Views - Last Post: 16 August 2008 - 09:39 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=60867&amp;s=fa78af88f3a1395a4468c3692c080650&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 aclark17

Reputation: -1
• Posts: 71
• Joined: 18-June 08

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 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();
}
{
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.print();
cout<<endl;

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

cout<<endl;

}
```

*edit: Please use code tags around your code!

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

Is This A Good Question/Topic? 0

### #2 NickDMax

• Can grep dead trees!

Reputation: 2255
• Posts: 9,245
• Joined: 18-February 07

Posted 16 August 2008 - 07:20 PM

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

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

### #3 aclark17

Reputation: -1
• Posts: 71
• Joined: 18-June 08

Posted 16 August 2008 - 08:19 PM

NickDMax, 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!!!

### #4 NickDMax

• Can grep dead trees!

Reputation: 2255
• Posts: 9,245
• Joined: 18-February 07

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...

### #5 perfectly.insane

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

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

```