Sathvikks8

PolynomialAdditionCSLL

Dec 17th, 2020 (edited)
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.89 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #include<math.h>
  4. typedef struct node * NODE;
  5. struct node
  6. {
  7.   int coeff, x, y, z;
  8.   struct node *link;
  9. };
  10. NODE POLY1=NULL, POLY2=NULL, POLYSUM=NULL, tailPOLYSUM=NULL, temp, ptr, tail=NULL;
  11. NODE newNode()
  12. {
  13.   NODE X;
  14.   X=(struct node*)malloc(sizeof(struct node));
  15.   X->link=NULL;
  16.   X->coeff=X->x=X->y=X->z=-1;
  17.   return X;
  18. }
  19. NODE readPolynomial()
  20. {
  21.   int terms;
  22.   ptr=newNode();
  23.   ptr->link=ptr;
  24.   printf("\nEnter the number of terms in the Polynomial: ");
  25.   scanf("%d",&terms);
  26.   printf("\nEnter the Coefficient, degrees of x, y and z of the %d terms in decreasing order: ",terms);
  27.   for(int i=1;i<=terms;i++)
  28.   {
  29.     temp=newNode();
  30.     temp->link=ptr;
  31.     scanf("%d%d%d%d",&temp->coeff,&temp->x,&temp->y,&temp->z);
  32.     if(ptr->link==ptr)
  33.       ptr->link=tail=temp;
  34.     else
  35.     {
  36.       tail->link=temp;
  37.       tail=temp;
  38.     }
  39.   }
  40.   return ptr;
  41. }
  42. void displayPolynomial(NODE head, char Poly)
  43. {
  44.   printf("\nThe entered Polynomial %c is: ",Poly);
  45.   for(ptr=head->link;ptr!=head;ptr=ptr->link)
  46.   {
  47.     printf("%d*x^%d*y^%d*z^%d",ptr->coeff,ptr->x,ptr->y,ptr->z);
  48.     if(ptr->link!=head)
  49.       printf(" + ");
  50.   }
  51.   printf("\n");
  52. }
  53. void storePolynomialC(int coeff, int x, int y, int z)
  54. {
  55.   if(!POLYSUM)
  56.   {
  57.     POLYSUM=newNode();
  58.     POLYSUM->link=POLYSUM;
  59.   }
  60.   temp=newNode();
  61.   temp->link=POLYSUM;
  62.   temp->coeff=coeff;
  63.   temp->x=x;
  64.   temp->y=y;
  65.   temp->z=z;
  66.   if(POLYSUM->link==POLYSUM)
  67.     POLYSUM->link=tailPOLYSUM=temp;
  68.   else
  69.   {
  70.     tailPOLYSUM->link=temp;
  71.     tailPOLYSUM=temp;
  72.   }
  73. }
  74. void addPolynomial()
  75. {
  76.   NODE ptrA,ptrB,ptrC;
  77.   int found=0;
  78.   for(ptrA=POLY1->link;ptrA!=POLY1;ptrA=ptrA->link)
  79.   {
  80.     for(ptrB=POLY2->link;ptrB!=POLY2;ptrB=ptrB->link)
  81.     {
  82.       if(ptrA->x==ptrB->x && ptrA->y==ptrB->y && ptrA->z==ptrB->z)
  83.       {
  84.         found=1;
  85.         storePolynomialC(ptrA->coeff+ptrB->coeff, ptrA->x, ptrA->y, ptrA->z);
  86.         break;
  87.       }
  88.     }
  89.     if(!found)
  90.       storePolynomialC(ptrA->coeff, ptrA->x, ptrA->y, ptrA->z);
  91.     else
  92.       found=0;
  93.   }
  94.   found=0;
  95.   for(ptrB=POLY2->link;ptrB!=POLY2;ptrB=ptrB->link)
  96.   {
  97.     for(ptrA=POLY1->link;ptrA!=POLY1;ptrA=ptrA->link)
  98.     {
  99.       if(ptrA->x==ptrB->x && ptrA->y==ptrB->y && ptrA->z==ptrB->z)
  100.       {
  101.         found=1;
  102.         break;
  103.       }
  104.     }
  105.     if(!found)
  106.       storePolynomialC(ptrB->coeff, ptrB->x, ptrB->y, ptrB->z);
  107.     else
  108.       found=0;
  109.   }
  110.   printf("\nThe resultant Polynomial C is: ");
  111.   for(ptrC=POLYSUM->link;ptrC!=POLYSUM;ptrC=ptrC->link)
  112.   {
  113.     printf("%d*x^%d*y^%d*z%d",ptrC->coeff,ptrC->x,ptrC->y,ptrC->z);
  114.     if(ptrC->link!=POLYSUM)
  115.       printf(" + ");
  116.   }
  117.   printf("\n");
  118. }
  119. void evaluatePolynomialA()
  120. {
  121.   int x,y,z,result=0;
  122.   displayPolynomial(POLY1,'A');
  123.   printf("\nEnter the values of x, y and z: ");
  124.   scanf("%d%d%d",&x,&y,&z);
  125.   for(ptr=POLY1->link;ptr!=POLY1;ptr=ptr->link)
  126.     result+=ptr->coeff*pow(x,ptr->x)*pow(y,ptr->y)*pow(z,ptr->z);
  127.   printf("\nThe result after evaluating the Polynomial A is: %d",result);
  128.   printf("\n");
  129. }
  130. int main()
  131. {
  132.   int choice;
  133.   while(1)
  134.   {
  135.     printf("\nSelect an operation\n1) Enter Polynomial A\n2) Display Polynomial A\n3) Enter Polynomial B\n4) Display Polynomial B\n5) Add Polynomial A and Polynomial B\n6) Evaluate Polynomial A\n7) Exit\n>| ");
  136.     scanf("%d",&choice);
  137.     switch (choice)
  138.     {
  139.       case 1:
  140.         POLY1=readPolynomial();
  141.       break;
  142.       case 3:
  143.         POLY2=readPolynomial();
  144.       break;
  145.       case 2:
  146.         displayPolynomial(POLY1, 'A');
  147.       break;
  148.       case 4:
  149.         displayPolynomial(POLY2, 'B');
  150.       break;
  151.       case 5:
  152.         addPolynomial();
  153.       break;
  154.       case 6:
  155.         evaluatePolynomialA();
  156.       break;
  157.       case 7:
  158.       {
  159.         printf("\nThank you for using the program");
  160.         exit(0);
  161.       }
  162.       break;
  163.       default:
  164.         printf("\nInvalid Choice\n");
  165.     }
  166.   }
  167. }
  168.  
Add Comment
Please, Sign In to add comment