Addition of polynomials

// Addition of polynomials //

#include
#include
#define NULL 0
struct list
{
int coeff ;
int power ;
struct list *next;
};
typedef struct list poly;
void create(poly *t)
{
cout<<"Enter coeff :";
cin>>t->coeff;
if(t->coeff==0)
{
t->next=NULL;
}
else
{
cout<<"Enter power:";
cin>>t->power;
t->next=new poly;
create(t->next);
}
return;
}
void display(poly *t)
{
if(t->next!=NULL)
{
cout<coeff<<"X^"<power<display(t->next);
}
return;
}
poly *add(poly *t1,poly *t2)
{
poly *r=new poly;
poly *st=r;
poly *addcreate(int,int,poly *);
while(t1->next!=NULL && t2->next!=NULL)
{
if(t1->power==t2->power)
{
if(t1->coeff>coeff!=0)
{
r=addcreate(t1->power,t1->coeff>coeff,r);
t1=t1->next;
t2=t2->next;
}
}
else if(t1->power>t2->power)
{
r=addcreate(t1->power,t1->coeff,r);
t1=t1->next;
}
else
{
t=addcreate(t2->power,t2->coeff,r);
t2=t2->next;
}
}
if(t1->next!=NULL && t2->next==NULL)
while(t1->next!=NULL)
{
r=addcreate(t1->power,t1->coeff,r);
t1=t1->next;
}
if(t1->next==NULL&&t2->next!=NULL)
while(t1->next!=NULL)
{
r=addcreate(t2->power,t2->coeff,r);
t2=t2->next;
}
r->next=NULL;
return(st);
}
poly *addcreate(int n,int c,poly *t)
{
t->power=n;
t->coeff=c;
t->next-new poly;
return(t->next);
}
int main()
{
poly *p1,*p2,*result;
void create(poly *);
void display(poly *);
poly *add(poly *,poly *);
clrscr();
cout<<"\nEnter the first polynomial\n";
p1=new poly;
create(p1);
cout<<"\nEnter the second polynomial\n";
p2=new poly;
create(p2);
cout<<"\nThe first polynomial is\n";
display(p1);
cout<<"\nThe second polynomial is\n";
display(p2);
result=add(p1,p2);
cout<<"\nAddition of two polynomial is \n";
display(result);
getch();
return(0);
}

Post a Comment

Previous Post Next Post