Polyadd PDF

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 5

DATE:01/11/2022

https://sourceforge.net/projects/orwe...
#include <stdio.h>
#include<stdlib.h>
struct node
{
int coef;
int expo;
struct node *next;
} *newnode,*temp;
struct node *h1=NULL;
struct node *h2=NULL;
struct node *t1=NULL;
struct node *t2=NULL;
struct node *head=NULL;

struct node *sum=NULL;

void create()
{
int expo,coef,n;
printf("\n Enter Highest degree of polynomial 1:");
scanf("%d",&n);
while(n>0)
{
printf("\n Enter Coeff into P1:");
scanf("%d",&coef);
printf("\n Enter expo of Coeff of P1:");
scanf("%d",&expo);
newnode=(struct node*)malloc(sizeof(struct node));
newnode->expo=expo;
newnode->coef=coef;
newnode->next=NULL;
if(h1==NULL)
{
h1=newnode;
t1=newnode;

}
else
{
t1->next=newnode;
t1=newnode;
}
n=n-1;
}
printf("\n Enter Highest degree of polynomial 2:");
scanf("%d",&n);
while(n>0)
{
printf("\n Enter Coeff into P2:");
scanf("%d",&coef);
printf("\n Enter expo of Coeff of P2:");
scanf("%d",&expo);
newnode=(struct node*)malloc(sizeof(struct node));
newnode->expo=expo;
newnode->coef=coef;
newnode->next=NULL;
if(h2==NULL)
{
h2=newnode;
t2=newnode;

}
else
{
t2->next=newnode;
t2=newnode;
}
n=n-1;
}

void display(struct node *p)


{
temp=p;
if(p==NULL)
{
printf("\n There is no polynomial");
}
else
{
while(temp!=NULL)
{
printf("+%dX^%d",temp->coef,temp->expo);
temp=temp->next;
}
}
}

struct node* addpoly(struct node *h1,struct node *h2,struct node *sum)


{
struct node *res;
if(h1!=NULL && h2==NULL)
{
sum=h1;
return sum;
}
else if(h1==NULL && h2!=NULL)
{
sum=h2;
return sum;
}
while(h1!=NULL && h2!= NULL)
{
if(sum==NULL)
{
sum=(struct node*)malloc(sizeof(struct node));
res=sum;
}
else
{
res->next=(struct node*)malloc(sizeof(struct node));
res=res->next;
}
if(h1->expo > h2->expo)
{
res->coef=h1->coef;
res->expo=h1->expo;
h1=h1->next;
}
else if(h1->expo < h2->expo)
{
res->expo=h2->expo;
res->coef=h2->coef;
h2=h2->next;
}
else if(h1->expo == h2->expo)
{
res->expo=h1->expo;
res->coef=(h1->coef+h2->coef);
h1=h1->next;
h2=h2->next;
}

}
while(h1 != NULL)
{
res->next=(struct node*)malloc(sizeof(struct node));
res=res->next;
res->expo=h1->expo;
res->coef=h1->coef;

h1=h1->next;
}
while(h2 !=NULL)
{
res->next=(struct node*)malloc(sizeof(struct node));
res=res->next;
res->expo=h2->expo;
res->coef=h2->coef;
h2=h2->next;
}
res->next=NULL;
return sum;
}

struct node* mulpoly(struct node *h1,struct node *h2,struct node *mul)


{
struct node *h2b,*temp;

h2b=h2;
while(h1!=NULL)
{
temp=(struct node*)malloc(sizeof(struct node));
temp->next=NULL;
h2=h2b;
while(h2!=NULL)
{
temp->coef=(h1->coef)*(h2->coef);
temp->expo=(h1->expo)+(h2->expo);
temp->next=(struct node*)malloc(sizeof(struct node));
temp=temp->next;
temp->next=NULL;
h2=h2->next;

}
addpoly(temp,mul,mul);
h1=h1->next;
}
}
int main() {
int ch;
struct node *mul=NULL;
while(9)
{
printf("\n1.Create two polynomials \n2.Display the two polynomials \n3.Addition of two
polynomials \n4.Multiplication of two polynomials \n5.Exit");
printf("\n Enter Your choice:");
scanf("%d",&ch);
switch(ch)
{
case 1:create();
break;
case 2:printf("\n Polynomial 1 is:");
display(h1);
printf("\n Polynomial 2 is:");
display(h2);
break;
case 3:printf("\n The addition of two polynomials is:");
sum=addpoly(h1,h2,sum);
display(sum);
break;
case 4:printf("\n MUltiplication of two polynomials is:");
mulpoly(h1,h2,mul);
display(mul);
break;

case 5:printf("\n Control is out of execution");


exit(0);

}
}

return 0;
}

You might also like