Saturday, July 24, 2021

Using C Language implement Polynomial Addition, Subtraction and Multiplication
..............................................................................



#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#include<alloc.h>
struct node
{
int exp;
int coeff;
struct node *next;
};
struct node *create(struct node *head)
{
struct node *ptr,*new1;
int n,x;
ptr=(struct node*)malloc(sizeof(struct node));
head=NULL;
printf("\n Enter the polynomial degree=");
scanf("%d",&n);
while(n>=0)
{
printf("\n enter coefficient=");
scanf("%d",&x);
if(head==NULL)
{
head=(struct node*)malloc(sizeof(struct node));
head->exp=n;
head->coeff=x;
head->next=NULL;
}
else
{
ptr=head;
while(ptr->next!=NULL)
ptr=ptr->next;
new1=(struct node*)malloc(sizeof(struct node));
new1->exp=n;
new1->coeff=x;
new1->next=NULL;
ptr->next=new1;
}
n--;
}
return head;
}
void show(struct node *head)
{
struct node *ptr;
ptr=head;
while(ptr!=NULL)
{
printf("\t%dx^%d+",ptr->coeff,ptr->exp);
ptr=ptr->next;
}
}
struct node *add_node(struct node *head,int n,int c)
{
struct node *ptr,*new1;
if(head==NULL)
{
head=(struct node*)malloc(sizeof(struct node));
head->exp=c;
head->coeff=n;
head->next=NULL;
}
else
{
ptr=head;
while(ptr->next!=NULL)
ptr=ptr->next;
new1=(struct node*)malloc(sizeof(struct node));
new1->exp=c;
new1->coeff=n;
new1->next=NULL;
ptr->next=new1;
}
return head;
}
void poly_add(struct node *head1,struct node *head2,struct node *head3)
{
struct node *ptr1,*ptr2;
int sum;
ptr1=head1;
ptr2=head2;
head3=NULL;
while(ptr1!=NULL||ptr2!=NULL)
{
if(ptr1->exp==ptr2->exp)
{
ptr1->coeff=ptr1->coeff+ptr2->coeff;
head3=add_node(head3,ptr1->coeff,ptr1->exp);
ptr1=ptr1->next;
ptr2=ptr2->next;
}
else if(ptr1->exp>ptr2->exp)
{
head3=add_node(head3,ptr1->coeff,ptr1->exp);
ptr1=ptr1->next;
}
else if(ptr1->exp<ptr2->exp)
{
head3=add_node(head3,ptr2->coeff,ptr2->exp);
ptr2=ptr2->next;
}
}
if(ptr1==NULL)
{
while(ptr2!=NULL)
{
head3=add_node(head3,ptr2->coeff,ptr2->exp);
ptr2=ptr2->next;
}
}
if(ptr2==NULL)
{
while(ptr1!=NULL)
{
head3=add_node(head3,ptr1->coeff,ptr1->exp);
ptr1=ptr1->next;
}
}
show(head3);
}
void poly_sub(struct node *head1,struct node *head2,struct node *head3)
{
struct node *ptr1,*ptr2;
int sub;
ptr1=head1;
ptr2=head2;
head3=NULL;
while(ptr1!=NULL||ptr2!=NULL)
{
if(ptr1->exp==ptr2->exp)
{
ptr1->coeff=ptr1->coeff-ptr2->coeff;
head3=add_node(head3,ptr1->coeff,ptr1->exp);
ptr1=ptr1->next;
ptr2=ptr2->next;
}
else if(ptr1->exp>ptr2->exp)
{
head3=add_node(head3,ptr1->coeff,ptr1->exp);
ptr1=ptr1->next;
}
else if(ptr1->exp<ptr2->exp)
{
head3=add_node(head3,ptr2->coeff,ptr2->exp);
ptr2=ptr2->next;
}
}
if(ptr1==NULL)
{
while(ptr2!=NULL)
{
head3=add_node(head3,ptr2->coeff,ptr2->exp);
ptr2=ptr2->next;
}
}
if(ptr2==NULL)
{
while(ptr1!=NULL)
{
head3=add_node(head3,ptr1->coeff,ptr1->exp);
ptr1=ptr1->next;
}
}
show(head3);
}
void removeduplicates(struct node *head3)
{
struct node *ptr1,*ptr2,*dup;
ptr1=head3;
while(ptr1!=NULL && ptr1->next!=NULL)
{
ptr2=ptr1;
while(ptr2->next!=NULL)
{
if(ptr1->exp==ptr2->next->exp)
{
ptr1->coeff=ptr1->coeff+ptr2->next->coeff;
dup=ptr2->next;
ptr2->next=ptr2->next->next;
free(dup);
}
else
ptr2=ptr2->next;
}
ptr1=ptr1->next;
}

}
void poly_product(struct node *head1,struct node *head2,struct node *head3)
{
struct node *ptr1,*ptr2;
int coeff,exp;
ptr1=head1;
ptr2=head2;
head3=NULL;
while(ptr1!=NULL)
{
while(ptr2!=NULL)
{
coeff=ptr1->coeff*ptr2->coeff;
exp=ptr1->exp+ptr2->exp;
head3=add_node(head3,coeff,exp);
ptr2=ptr2->next;
}
ptr2=head2;
ptr1=ptr1->next;
}
removeduplicates(head3);

show(head3);
}
int main()
{
struct node *head1,*q1,*q2,*head2,*head3,*q3,*q4,*q5;
int n;
clrscr();
head1=NULL;
head2=NULL;
head3=NULL;
while(1)
{
printf("\nPress 1 for creating first polynomial=");
printf("\nPress 2 for creating second polynomial=");
printf("\nPress 3 for showing first polynomial=");
printf("\nPress 4 for showing second polynomial=");
printf("\nPress 5 for  addition of two polynomials=");
printf("\nPress 6 for exit=");
printf("\nPress 7 for substraction of two polynomials=");
printf("\nPress 8 for mutiplication of two polynomials=");
printf("\nEnter your choice=");
scanf("%d",&n);
switch(n)
{
case 1:q1=create(head1);
break;
case 2:q2=create(head2);
break;
case 3:show(q1);
break;
case 4:show(q2);
break;
case 5:poly_add(q1,q2,q3);
break;
case 6:exit(0);
case 7:poly_sub(q1,q2,q4);
break;
case 8:poly_product(q1,q2,q5);
break;
}
}
}


No comments:

Post a Comment