#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
typedef struct node
{
int coeff;
int expon;
struct node *link;
}*nodes;
nodes getnode()
{
nodes x;
x=(nodes)malloc(sizeof(nodes));
return x;
}
nodes attach(int coeff,int expon,nodes head)
{
nodes temp,cur;
temp=getnode();
temp->coeff=coeff;
temp->expon=expon;
cur=head->link;
while(cur->link!=head)
{
cur=cur->link;
}
cur->link=temp;
temp->link=head;
return head;
}
nodes readpoly(nodes head)
{
int i=1,coeff,expon;
printf("\nEnter the coeff as -999 to end the polynomial");
while(1)
{
printf("\nEnter the %d term:\n",i++);
printf("\n\tCoeff=");
scanf("%d",&coeff);
if(coeff==-999)
{break;}
printf("\n\tPow x=");
scanf("%d",&expon);
head=attach(coeff,expon,head);
fflush(stdin);
}
return head;
}
nodes polyadd(nodes head1,nodes head2,nodes head3)
{
nodes a,b;
int coeff;
a=head1->link;
b=head2->link;
while(a!=head1&&b!=head2)
{
if(a->expon==b->expon)
{
coeff=a->coeff+b->coeff;
if(coeff!=0)
head3=attach(coeff,a->coeff,head3);
a=a->link;
b=b->link;
}
else if(a->expon>b->expon)
{
head3=attach(a->coeff,a->expon,head3);
a=a->link;
}
else
{
head3=attach(b->coeff,b->expon,head3);
b=b->link;
}
}
while(a!=head1)
{
head3=attach(a->coeff,a->expon,head3);
a=a->link;
}
while(b!=head2)
{
head3=attach(b->coeff,b->expon,head3);
b=b->link;
}
return head3;
}
void display(nodes head)
{
nodes temp;
if(head->link==head)
{
printf("\nPolynomial does not exist");
return;
}
temp=head->link;
while(temp!=head)
{
printf("%dx^%d",temp->coeff,temp->expon);
temp=temp->link;
if(temp!=head)
printf("+");
}
}
nodes polymul(nodes head1,nodes head2,nodes head4)
{
nodes cur1,cur2;
if(head1->link==head1||head2->link==head2)
{
printf("\nMultipied polynomial is zero polynomial");
return;
}
cur1=head1->link;
while(cur1!=head1)
{
cur2=head2->link;
while(cur2!=head2)
{
head4=attach(cur1->coeff*cur2->coeff,cur1->expon+cur2->expon,head4);
cur2=cur2->link;
}
cur1=cur1->link;
}
return head4;
}
int menu()
{
int a;
printf("\n*****Menu*****");
printf("\nEnter 1 to accept polynomial");
printf("\nEnter 2 to display polynomial");
printf("\nEnter 3 to add polynomial");
printf("\nEnter 4 to multiply polynomial");
printf("\nEnter 5 to exit");
printf("\nEnter your choice :");
scanf("%d",&a);
return a;
}
void main()
{
int a=0;
nodes head1,head2,head3,head4;
head1=getnode();
head2=getnode();
head3=getnode();
head4=getnode();
head1->link=head1;
head2->link=head2;
head3->link=head3;
head4->link=head4;
clrscr();
do
{
switch(a=menu())
{
case 1:printf("\nEnter the first poly\n");
head1=readpoly(head1);
printf("\nEnter the second poly\n");
head2=readpoly(head2);
break;
case 2:printf("\n1 polynomial is : \n");
display(head1);
printf("\n2 polynomial is : \n");
display(head2);
break;
case 3:printf("\nAdd of polynomial is : \n");
head3=polyadd(head1,head2,head3);
display(head3);
break;
case 4:printf("\nMulti of polynomial is : \n");
head3=polyadd(head1,head2,head3);
display(head4);
break;
case 5:printf("\n****Thankyou****\n");
break;
}
}while(a!=5);
getch();
}
No comments:
Post a Comment
If you have any problems related to solutions or any concept please let me know.