//program for perfomr arithmetic operation on Two Polynomials using linklist
//writter : K@rtik
//20-03-11 sunday
#include<stdio.h>
#include<conio.h>
struct linklist
{
int data;
int power;
struct linklist *next;
}*fnode=NULL,*start1=NULL,*last,*start2,*start3,*snode=NULL;
typedef struct linklist lk;
int item,i,pow,c;
void insert(lk **);
void add();
void sub();
void mul();
void disp(lk **);
void main()
{
char con;
int ch;
fnode=(lk *)malloc(sizeof(lk));
snode=(lk *)malloc(sizeof(lk));
fnode=NULL;
snode=NULL;
clrscr();
do
{
printf("\n\n ********************************************************");
printf("\n K@rtik's LINKLIST OPERATION");
printf("\n\n -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*");
printf("\n *-----> 1.Add Polynomial equation *");
printf("\n *-----> 2.Adddition of Two Polynomial *");
printf("\n *-----> 3.Subtraction of Two Polynomial *");
printf("\n *-----> 4.Multiplication of Two Polynomial *");
printf("\n\n -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*");
printf("\n\n=> enter ur choice:");
scanf("%d",&ch);
switch(ch)
{
case 1:
printf("\n Enter 1st Equation\n");
insert(&fnode);
printf("\n enter 2nd Equation\n");
insert(&snode);
printf("\n 1st Polynomial equation is====>");
disp(&fnode);
printf("\n 2nd Polynomial equation is====>");
disp(&snode);
break;
case 2:
add();
break;
case 3:
sub();
break;
case 4:
mul();
break;
default:
printf("\n u must enter any number from 1 to 5");
}
fflush(0);
printf("\n\n==> do u want to con...(Y/N)?");
scanf("%c",&con);
}while (con!='n');
getch();
}
//user define code for add item at last
void insert(lk **start)
{
lk *node,*temp,*first;
while(1)
{
if((*start)==NULL)
{
printf("\n enter an item:");
scanf("%d",&item);
printf("\n enter power:");
scanf("%d",&pow);
(*start)=(lk *)malloc(sizeof(lk));
(*start)->data=item;
(*start)->power=pow;
(*start)->next=NULL;
}
else
{
node=(lk *)malloc(sizeof(lk));
printf("\n enter an item:");
scanf("%d",&item);
l: printf("\n enter power:");
scanf("%d",&pow);
temp=(*start);
while(temp->next!=NULL)
{
if(pow >= temp->power)
{
printf("\n ==> u must i/p power value less then %d ",temp->power);
goto l;
}
temp=temp->next;
}
node->data=item;
node->power=pow;
node->next=NULL;
temp->next=node;
} //end of else
if(pow==0)
break;
}
}
//Udf for display Polynomial
void disp(lk **head)
{
lk *node,*node1;
node=(lk *)malloc(sizeof(lk));
node=(*head);
printf("%d",node->data);
printf("x^%d",node->power);
node=node->next;
while(node!=NULL)
{
if(node->data > 0)
printf("+%d",node->data);
else
printf("%d",node->data);
if (node->power==0)
goto l1;
else
printf("x");
printf("^%d",node->power);
l1:
node=node->next;
}
}
//udf for addition of two polynomial
void add()
{
lk *temp1,*temp2,*temp,*node;
temp1=fnode;
temp2=snode;
while(temp1 || temp2)
{
temp=(lk *)malloc(sizeof(lk));
temp->next=NULL;
if(temp1->power==temp2->power)
{
temp->power=temp1->power;
temp->data=temp1->data + temp2->data;
temp1=temp1->next;
temp2=temp2->next;
}
else if(temp1->power>temp2->power)
{
temp->power=temp1->power;
temp->data=temp1->data;
temp1=temp1->next;
}
else
{
temp->power=temp2->power;
temp->data=temp2->data;
temp2=temp2->next;
}
if(start2==NULL)
start2=temp;
else
node->next=temp;
node=temp;
last=temp;
}//end of while loop
printf("\n 1st Polynomial equation is====>");
disp(&fnode);
printf("\n\n 2nd Polynomial equation is====>");
disp(&snode);
printf("\n\n After addition the equation is ====>");
disp(&start2);
}
//udf for addition of two polynomial
void sub()
{
lk *temp1,*temp2,*temp,*node,*start5=NULL;
temp1=fnode;
temp2=snode;
while(temp1 || temp2)
{
temp=(lk *)malloc(sizeof(lk ));
temp->next=NULL;
if(temp1->power==temp2->power)
{
temp->power=temp1->power;
temp->data=temp1->data - temp2->data;
temp1=temp1->next;
temp2=temp2->next;
}
else if(temp1->power>temp2->power)
{
temp->power=temp1->power;
temp->data=temp1->data;
temp1=temp1->next;
}
else
{
temp->power=temp2->power;
temp->data=temp2->data;
temp2=temp2->next;
}
if(start5==NULL)
start5=temp;
else
node->next=temp;
node=temp;
last=temp;
}//end of while loop
printf("\n 1st Polynomial equation is====>");
disp(&fnode);
printf("\n\n 2nd Polynomial equation is====>");
disp(&snode);
printf("\n\n After Subtraction the equation is ====>");
disp(&start5);
}
//mul
void mul()
{
int max=0,c=0;
lk *temp1,*temp2,*temp,*node,*start3=NULL,*start4=NULL;
lk *tmp1,*tmp2;
temp1=fnode;
while(temp1)
{
temp2=snode;
while(temp2)
{
temp=(lk *)malloc(sizeof(lk));
temp->next=NULL;
temp->data=temp1->data * temp2->data;
temp->power=temp1->power+temp2->power;
temp2=temp2->next;
if(max < temp->power)
max=temp->power;
if(start3==NULL)
start3=temp;
else
node->next=temp;
node=temp;
last=temp;
}
temp1=temp1->next;
}
tmp1=start3;
while(tmp1)
{
c=0;
tmp2=tmp1;
temp=(lk *)malloc(sizeof(lk));
temp->next=NULL;
temp->data=0;
while(tmp2)
{
if(max==tmp2->power)
{
c=1;
temp->power=tmp2->power;
temp->data =temp->data+ tmp2->data;
}
tmp2=tmp2->next;
}//end of inner loop
if(c==0)
{
temp->power=tmp1->power;
temp->data=tmp1->data;
}
if(start4==NULL)
start4=temp;
else
node->next=temp;
node=temp;
last=temp;
max--;
if (max<0)
break;
tmp1=tmp1->next;
}//end of while loop
printf("\n 1st Polynomial equation is====>");
disp(&fnode);
printf("\n\n 2nd Polynomial equation is====>");
disp(&snode);
printf("\n\n After Multiplication the equation is ====>");
disp(&start4);
}
----------------------------
RAJ SOLUTION'S
www.rajsolution.com
www.sahinraj.blogspot.com
No comments:
Post a Comment