Logo 
Search:

C Programming Forum

Ask Question   UnAnswered
Home » Forum » C Programming       RSS Feeds

in c language

  Asked By: Monica    Date: Oct 10    Category: C Programming    Views: 997
  

suggest a code for polynomial subtraction in c.

Share: 

 

1 Answer Found

 
Answer #1    Answered By: Abhishek Singh     Answered On: May 22

#include<stdio.h>

#include<conio.h>

#include<stdlib.h>

#define NULL 0

void main()

{

struct poly

{

int coff;

int expo;

struct poly *link;

}*temp1,*start1,*temp2,*start2,*start3,*temp3;

int n,i,z=1,num;

char c;

clrscr();

start1=NULL;

printf("\tPOLYNOMIAL CREATION IN LINK LIST AND SUBTRACTION\n\n");

while(1)

{

if(start1==NULL)

{

temp1=(struct poly*)malloc(sizeof(struct poly));

printf("Enter Coefficient and Exponent for Node %d\n",z);

scanf("%d %d",&temp1->coff,&temp1->expo);

start1=temp1;

temp1->link=NULL;

}

else

{

temp1->link=(struct poly*)malloc(sizeof(struct poly));

temp1=temp1->link;

printf("Enter Coefficient and exponent for Node %d\n",z);

scanf("%d %d",&temp1->coff,&temp1->expo);

}

z++;

printf("Do you want to create another node\n");

fflush(stdin);

scanf("%c",&c);

if(c!='y')

{

temp1->link=NULL;

break;

}

}

start2=NULL;

temp2=NULL;

z=1;

while(1)

{

if(start2==NULL)

{

temp2=(struct poly*)malloc(sizeof(struct poly));

printf("Enter Coefficient and exponent for Node %d\n",z);

scanf("%d %d",&temp2->coff,&temp2->expo);

start2=temp2;

temp2->link=NULL;

}

else

{

temp2->link=(struct poly*)malloc(sizeof(struct poly));

temp2=temp2->link;

printf("Enter Coefficient and exponent for Node %d\n",z);

scanf("%d %d",&temp2->coff,&temp2->expo);

}

z++;

printf("Do you want to create another node\n");

fflush(stdin);

scanf("%c",&c);

if(c!='y')

{

temp2->link=NULL;

break;

}

}

// TARVERSING

temp1=NULL;

temp2=NULL;

temp1=start1;

temp2=start2;

printf("Traversal of Polynomial Linked List 1\n");

while(temp1!=NULL)

{

printf("%2dx^%d-",temp1->coff,temp1->expo);

temp1=temp1->link;

}

printf("\b \b");

printf("\nTraversal of Polynomial Linked List 2\n");

while(temp2!=NULL)

{

printf("%2dx^%d -",temp2->coff,temp2->expo);

temp2=temp2->link;

}

printf("\b \b");

// subtraction

temp1=NULL;

temp2=NULL;

temp1=start1;

temp2=start2;

temp3=NULL;

start3=NULL;

while(1)

{

if((temp1!=NULL)||(temp2!=NULL))

{

if(start3==NULL)

{

if((temp1->expo)==(temp2->expo))

{

temp3=(struct poly*)malloc(sizeof(struct poly));

temp3->coff= (temp1->coff) - (temp2->coff);

temp3->expo=temp1->expo;

start3=temp3;

temp1=temp1->link;

temp2=temp2->link;

temp3->link=NULL;

}

else if(temp1->expo>temp2->expo)

{

temp3=(struct poly*)malloc(sizeof(struct poly));

temp3->coff=temp1->coff;

temp3->expo=temp1->expo;

start3=temp3;

temp1=temp1->link;

temp3->link=NULL;

}

else

{

temp3=(struct poly*)malloc(sizeof(struct poly));

temp3->coff=temp2->coff;

temp3->expo=temp2->expo;

start3=temp3;

temp2=temp2->link;

temp3->link=NULL;

}

}

else

{

if(temp1->expo==temp2->expo)

{

temp3->link=(struct poly*)malloc(sizeof(struct poly));

temp3=temp3->link;

temp3->coff= (temp1->coff) - (temp2->coff);

temp3->expo=temp1->expo;

temp1=temp1->link;

temp2=temp2->link;

}

else if(temp1->expo>temp2->expo)

{

temp3->link=(struct poly*)malloc(sizeof(struct poly));

temp3=temp3->link;

temp3->coff=temp1->coff;

temp3->expo=temp1->expo;

temp1=temp1->link;

}

else

{

temp3->link=(struct poly*)malloc(sizeof(struct poly));

temp3=temp3->link;

temp3->coff=temp2->coff;

temp3->expo=temp2->expo;

temp2=temp2->link;

}

}

}

else

break;

}

temp3->link=NULL;

//traversing temp3

temp3=NULL;

temp3=start3;

printf("\nTraversal of Polynomial Linked List after subtracting Temp1 & Temp2\n");

while(temp3!=NULL)

{

printf("%2dx^%d - ",temp3->coff,temp3->expo);

temp3=temp3->link;

}

printf("\b\b");

printf("\nTHE END\n");

getch();

}



OUTPUT:

POLYNOMIAL CREATION IN LINK LIST AND SUBTRACTION



Enter Coefficient and exponent for Node 1

6

3

Do you want to create another node

Y

Enter Coefficient and exponent for Node 2

8

2

Do u want to create another node

N

Enter Coefficient and exponent for Node 1

7

3

Do you want to create another node

Y

Enter Coefficient and exponent for Node 2

5

2

Do u want to create another node

N

Traversal of Polynomial Linked List 1

6x^3 + 8x^2



Traversal of Polynomial Linked List 2

7x^3 + 5x^2



Traversal of Polynomial Linked List after adding Temp1 & Temp2

-1X^3 + 3X^2

 
Didn't find what you were looking for? Find more on in c language Or get search suggestion and latest updates.




Tagged: