#include<stdio.h>
#include<stdlib.h>
struct node
{
int coeff,expon;
struct node *link;
};
typedef struct node *NODE;
NODE insert(NODE,int,int);
NODE add(NODE,NODE);
void display(NODE);
main()
{
NODE poly1=NULL,poly2=NULL,poly3=NULL;
int coef,expo,choice=0;
printf("\n enter details of first polynomail\n");
do
{
printf("\n enter coefficient:");
scanf("%d",&coef);
printf("\n enter exponent:");
scanf("%d",&expo);
poly1=insert(poly1,coef,expo);
printf("\n enter 1 to continue:");
scanf("%d",&choice);
}while(choice==1);
printf("\n enter details of second polynomail\n");
do
{
printf("\n enter coefficient:");
scanf("%d",&coef);
printf("\n enter exponent:");
scanf("%d",&expo);
poly2=insert(poly2,coef,expo);
printf("\n enter 1 to continue:");
scanf("%d",&choice);
}while(choice==1);
printf("\nfirst polynomail entered is\n");
display(poly1);
printf("\nsecond polynomail entered is\n");
display(poly2);
poly3=add(poly1,poly2);
printf("\n the sum of polynomails is\n");
display(poly3);
}
NODE insert(NODE poly,int coef,int expo)
{
NODE temp,cur;
temp=(NODE)malloc(sizeof(NODE));
temp->coeff=coef;
temp->expon=expo;
temp->link=NULL;
if(poly==NULL)
{
poly=temp;
return poly;
}
cur=poly;
while(cur->link!=NULL)
cur=cur->link;
cur->link=temp;
return poly;
}
NODE add(NODE poly1,NODE poly2)
{
NODE poly3=NULL;
while(poly1!=NULL&&poly2!=NULL)
{
if(poly1->expon>poly2->expon)
{
poly3=insert(poly3,poly1->coeff,poly1->expon);
poly1=poly1->link;
}
else
if(poly1->expon<poly2->expon)
{
poly3=insert(poly3,poly2->coeff,poly2->expon);
poly2=poly2->link;
}
else
{
poly3=insert(poly3,poly1->coeff+poly2->coeff,poly1->expon);
poly1=poly1->link;
poly2=poly2->link;
}
}
while(poly1!=NULL)
{
poly3=insert(poly3,poly1->coeff,poly1->expon);
poly1=poly1->link;
}
while(poly2!=NULL)
{
poly3=insert(poly3,poly2->coeff,poly2->expon);
poly2=poly2->link;
}
return poly3;
}
void display(NODE first)
{
printf("%dX%d",first->coeff,first->expon);
first=first->link;
while(first!=NULL)
{
if(first->expon==0)
{
printf("+%d",first->coeff);
break;
}
printf("+%dX%d",first->coeff,first->expon);
first=first->link;
}
}
#include<stdlib.h>
struct node
{
int coeff,expon;
struct node *link;
};
typedef struct node *NODE;
NODE insert(NODE,int,int);
NODE add(NODE,NODE);
void display(NODE);
main()
{
NODE poly1=NULL,poly2=NULL,poly3=NULL;
int coef,expo,choice=0;
printf("\n enter details of first polynomail\n");
do
{
printf("\n enter coefficient:");
scanf("%d",&coef);
printf("\n enter exponent:");
scanf("%d",&expo);
poly1=insert(poly1,coef,expo);
printf("\n enter 1 to continue:");
scanf("%d",&choice);
}while(choice==1);
printf("\n enter details of second polynomail\n");
do
{
printf("\n enter coefficient:");
scanf("%d",&coef);
printf("\n enter exponent:");
scanf("%d",&expo);
poly2=insert(poly2,coef,expo);
printf("\n enter 1 to continue:");
scanf("%d",&choice);
}while(choice==1);
printf("\nfirst polynomail entered is\n");
display(poly1);
printf("\nsecond polynomail entered is\n");
display(poly2);
poly3=add(poly1,poly2);
printf("\n the sum of polynomails is\n");
display(poly3);
}
NODE insert(NODE poly,int coef,int expo)
{
NODE temp,cur;
temp=(NODE)malloc(sizeof(NODE));
temp->coeff=coef;
temp->expon=expo;
temp->link=NULL;
if(poly==NULL)
{
poly=temp;
return poly;
}
cur=poly;
while(cur->link!=NULL)
cur=cur->link;
cur->link=temp;
return poly;
}
NODE add(NODE poly1,NODE poly2)
{
NODE poly3=NULL;
while(poly1!=NULL&&poly2!=NULL)
{
if(poly1->expon>poly2->expon)
{
poly3=insert(poly3,poly1->coeff,poly1->expon);
poly1=poly1->link;
}
else
if(poly1->expon<poly2->expon)
{
poly3=insert(poly3,poly2->coeff,poly2->expon);
poly2=poly2->link;
}
else
{
poly3=insert(poly3,poly1->coeff+poly2->coeff,poly1->expon);
poly1=poly1->link;
poly2=poly2->link;
}
}
while(poly1!=NULL)
{
poly3=insert(poly3,poly1->coeff,poly1->expon);
poly1=poly1->link;
}
while(poly2!=NULL)
{
poly3=insert(poly3,poly2->coeff,poly2->expon);
poly2=poly2->link;
}
return poly3;
}
void display(NODE first)
{
printf("%dX%d",first->coeff,first->expon);
first=first->link;
while(first!=NULL)
{
if(first->expon==0)
{
printf("+%d",first->coeff);
break;
}
printf("+%dX%d",first->coeff,first->expon);
first=first->link;
}
}
No comments:
Post a Comment