I have a project about addition and Multiplication polynomial in link list.i have problem in addition ?
// jame 2 chan jomleei
#include<iostream.h>
#include<conio.h>
class node
{public: int coe,exp;
node*link;
};
class polynomial
{public: void get_poly();
void show();
void add(polynomial p1,polynomial p2);
node*start,*ptrn,*ptrp;
};
void polynomial::get_poly()
{char c='y';
ptrn=ptrp=start=NULL;
while(c=='y'||c=='Y')
{ptrn=new node;
ptrp=new node;
ptrp->link=ptrn;
if(start==NULL)
start=ptrn;
ptrp=ptrn;
cout<<"ZARIB jomle vared konid:"; cin>>ptrn->coe;
cout<<"TAVAN jomle vared konid:"; cin>>ptrn->exp;
ptrn->link=NULL;
cout<<"DO you want to adding(y|n)- no ra vared konid:";
cin>>c;}//End while.
}
void polynomial::show()
{node*ptr;
ptr=start;
while(ptr!=NULL)
{cout<<ptr->coe<<"X^"<<ptr->exp<<"+\n";
ptr=ptr->link;}
}//End show.
void polynomial::add(polynomial p1,polynomial p2)
{node *p1ptr,*p2ptr;
int coe,exp;
ptrn=ptrp=start=NULL;
p1ptr=p1.start;
p2ptr=p2.start;
while(p1ptr!=NULL&&p2ptr!=NULL)
{if(p1ptr->exp==p2ptr->exp)
{coe=p1ptr->coe+p2ptr->coe;
exp=p1ptr->exp;
p1ptr=p1ptr->link;
p2ptr=p2ptr->link;}
else if(p1ptr->exp > p2ptr->exp)
{coe=p1ptr->coe;
exp=p1ptr->exp;
p1ptr=p1ptr->link;}
else if(p1ptr->exp < p2ptr->exp)
{coe=p2ptr->coe;
exp=p2ptr->exp;
p2ptr=p2ptr->link;}
ptrn=new node;
if(start==NULL)
start=ptrn;
ptrn->coe=coe;
ptrn->exp=exp;
ptrn->link=NULL;
ptrp->link=ptrn;
ptrp=ptrn;
}// End of while.
if(p1ptr==NULL){ while(p2ptr!=NULL)
{coe=p2ptr->coe;
exp=p2ptr->exp;
p2ptr=p2ptr->link;
ptrn=new node;
if(start==NULL)
start=ptrn;
ptrn->coe=coe;
ptrn->exp=exp;
ptrn->link=NULL;
ptrp->link=ptrn;
ptrp=ptrn;}
}
else if( p2ptr==NULL)
{ while(p1ptr!=NULL)
{coe=p1ptr->coe;
exp=p1ptr->exp;
p1ptr=p1ptr->link;
ptrn=new node;
if(start==NULL)
start=ptrn;
ptrn->coe=coe;
ptrn->exp=exp;
ptrn->link=NULL;
ptrp->link=ptrn;
ptrp=ptrn;}
}//end of conditoin.
}//Ende of void adding.
int main()
{
polynomial p1,p2,sum;
clrscr(); cout<<"äÕÑÊí×ÊÇÌ ÈÎÔ×ÇÈÑÇåíãí\n ";
cout<<"First polynomial:\n";
p1.get_poly();
cout<<"Second polynomial:\n";
p2.get_poly();
cout<<"the First polynomials is:\n";
p1.show();
cout<<"the second polynomial is:\n";
p2.show();
cout<<"the sum of two polynomials is:\n";
sum.add(p1,p2);
sum.show();
getch();
return 0;
}
Put your code inside code tags [code] [/code]. It makes it much easier to read your code.