The following is a C program to implement polynomial addition. If you have any doubts please let me know.
#include<stdio.h>
#include<stdlib.h>
struct node
{
int d;
int coef;
struct node *next;
}*newn,*n=NULL,*p=NULL,*q=NULL,*r=NULL,*start1=NULL,*start2=NULL,*start3=NULL,*ptr=NULL,*prev=NULL;
void display(struct node *,struct node *);
void display1();
main()
{
int a,b,i;
i=0;
printf("\nEnter values for polynomial1:\n");
do
{
printf("Enter the coeff of x^%d:",i);
newn=(struct node *)malloc(sizeof(struct node));
scanf("%d",&newn->coef);
newn->d=i;
newn->next=p;
p=newn;
i++;
printf("\n Do you wish to add more elements in polynomial 1:");
printf("\nPress 1 to continue");
scanf("%d",&a);
}while(a==1);
start1=p;
printf("POLYNOMIAL 1:");
display(start1,p);
i=0;
printf("\n\nEnter values for polynomial2:\n");
do
{
printf("Enter the coef of x^%d:",i);
newn=(struct node *)malloc(sizeof(struct node));
scanf("%d",&newn->coef);
newn->d=i;
newn->next=q;
q=newn;
i++;
printf("\n Do you wish to add more elements in polynomial 2:");
printf("\nPress 1 to continue");
scanf("%d",&b);
}while(b==1);
start2=q;
printf("POLYNOMIAL 2:");
display(start2,q);
printf("\n\nSUM:");
p=start1;
q=start2;
while((p!=NULL)&&(q!=NULL))
{
if(p->d==q->d)
{
newn=(struct node *)malloc(sizeof(struct node));
newn->next=r;
newn->coef=p->coef+q->coef;
newn->d=p->d;
p=p->next;
q=q->next;
r=newn;
}
else if(p->d>q->d)
{
newn=(struct node *)malloc(sizeof(struct node));
newn->next=r;
newn->coef=p->coef;
newn->d=p->d;
p=p->next;
r=newn;
}
else if(p->d<q->d)
{
newn=(struct node *)malloc(sizeof(struct node));
newn->next=r;
newn->coef=q->coef;
newn->d=q->d;
q=q->next;
r=newn;
}
}
if(p==NULL)
{
while(q!=NULL)
{
newn=(struct node *)malloc(sizeof(struct node));
newn->next=r;
newn->coef=q->coef;
newn->d=q->d;
q=q->next;
r=newn;
}
}
else if(q==NULL)
{
while(p!=NULL)
{
newn=(struct node *)malloc(sizeof(struct node));
newn->next=r;
newn->coef=p->coef;
newn->d=p->d;
p=p->next;
r=newn;
}
}
start3=r;
display1();
}
void display(struct node *start,struct node *ptr)
{
ptr=start;
while(ptr!=NULL)
{
printf("%dx^%d",ptr->coef,ptr->d);
if(ptr->next!=NULL)
printf("+");
ptr=ptr->next;
}
}
void display1()
{
r=start3;
prev=NULL;
while(r!=NULL)
{
ptr=prev;
prev=r;
r=r->next;
prev->next=ptr;
}
start3=prev;
r=start3;
while(r!=NULL)
{
printf("%dx^%d",r->coef,r->d);
if(r->next!=NULL)
printf("+");
r=r->next;
}
}
#include<stdio.h>
#include<stdlib.h>
struct node
{
int d;
int coef;
struct node *next;
}*newn,*n=NULL,*p=NULL,*q=NULL,*r=NULL,*start1=NULL,*start2=NULL,*start3=NULL,*ptr=NULL,*prev=NULL;
void display(struct node *,struct node *);
void display1();
main()
{
int a,b,i;
i=0;
printf("\nEnter values for polynomial1:\n");
do
{
printf("Enter the coeff of x^%d:",i);
newn=(struct node *)malloc(sizeof(struct node));
scanf("%d",&newn->coef);
newn->d=i;
newn->next=p;
p=newn;
i++;
printf("\n Do you wish to add more elements in polynomial 1:");
printf("\nPress 1 to continue");
scanf("%d",&a);
}while(a==1);
start1=p;
printf("POLYNOMIAL 1:");
display(start1,p);
i=0;
printf("\n\nEnter values for polynomial2:\n");
do
{
printf("Enter the coef of x^%d:",i);
newn=(struct node *)malloc(sizeof(struct node));
scanf("%d",&newn->coef);
newn->d=i;
newn->next=q;
q=newn;
i++;
printf("\n Do you wish to add more elements in polynomial 2:");
printf("\nPress 1 to continue");
scanf("%d",&b);
}while(b==1);
start2=q;
printf("POLYNOMIAL 2:");
display(start2,q);
printf("\n\nSUM:");
p=start1;
q=start2;
while((p!=NULL)&&(q!=NULL))
{
if(p->d==q->d)
{
newn=(struct node *)malloc(sizeof(struct node));
newn->next=r;
newn->coef=p->coef+q->coef;
newn->d=p->d;
p=p->next;
q=q->next;
r=newn;
}
else if(p->d>q->d)
{
newn=(struct node *)malloc(sizeof(struct node));
newn->next=r;
newn->coef=p->coef;
newn->d=p->d;
p=p->next;
r=newn;
}
else if(p->d<q->d)
{
newn=(struct node *)malloc(sizeof(struct node));
newn->next=r;
newn->coef=q->coef;
newn->d=q->d;
q=q->next;
r=newn;
}
}
if(p==NULL)
{
while(q!=NULL)
{
newn=(struct node *)malloc(sizeof(struct node));
newn->next=r;
newn->coef=q->coef;
newn->d=q->d;
q=q->next;
r=newn;
}
}
else if(q==NULL)
{
while(p!=NULL)
{
newn=(struct node *)malloc(sizeof(struct node));
newn->next=r;
newn->coef=p->coef;
newn->d=p->d;
p=p->next;
r=newn;
}
}
start3=r;
display1();
}
void display(struct node *start,struct node *ptr)
{
ptr=start;
while(ptr!=NULL)
{
printf("%dx^%d",ptr->coef,ptr->d);
if(ptr->next!=NULL)
printf("+");
ptr=ptr->next;
}
}
void display1()
{
r=start3;
prev=NULL;
while(r!=NULL)
{
ptr=prev;
prev=r;
r=r->next;
prev->next=ptr;
}
start3=prev;
r=start3;
while(r!=NULL)
{
printf("%dx^%d",r->coef,r->d);
if(r->next!=NULL)
printf("+");
r=r->next;
}
}
No comments:
Post a Comment