Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdio.h>
- #include<stdlib.h>
- #include<math.h>
- typedef struct node * NODE;
- struct node
- {
- int coeff, x, y, z;
- struct node *link;
- };
- NODE POLY1=NULL, POLY2=NULL, POLYSUM=NULL, tailPOLYSUM=NULL, temp, ptr, tail=NULL;
- NODE newNode()
- {
- NODE X;
- X=(struct node*)malloc(sizeof(struct node));
- X->link=NULL;
- X->coeff=X->x=X->y=X->z=-1;
- return X;
- }
- NODE readPolynomial()
- {
- int terms;
- ptr=newNode();
- ptr->link=ptr;
- printf("\nEnter the number of terms in the Polynomial: ");
- scanf("%d",&terms);
- printf("\nEnter the Coefficient, degrees of x, y and z of the %d terms in decreasing order: ",terms);
- for(int i=1;i<=terms;i++)
- {
- temp=newNode();
- temp->link=ptr;
- scanf("%d%d%d%d",&temp->coeff,&temp->x,&temp->y,&temp->z);
- if(ptr->link==ptr)
- ptr->link=tail=temp;
- else
- {
- tail->link=temp;
- tail=temp;
- }
- }
- return ptr;
- }
- void displayPolynomial(NODE head, char Poly)
- {
- printf("\nThe entered Polynomial %c is: ",Poly);
- for(ptr=head->link;ptr!=head;ptr=ptr->link)
- {
- printf("%d*x^%d*y^%d*z^%d",ptr->coeff,ptr->x,ptr->y,ptr->z);
- if(ptr->link!=head)
- printf(" + ");
- }
- printf("\n");
- }
- void storePolynomialC(int coeff, int x, int y, int z)
- {
- if(!POLYSUM)
- {
- POLYSUM=newNode();
- POLYSUM->link=POLYSUM;
- }
- temp=newNode();
- temp->link=POLYSUM;
- temp->coeff=coeff;
- temp->x=x;
- temp->y=y;
- temp->z=z;
- if(POLYSUM->link==POLYSUM)
- POLYSUM->link=tailPOLYSUM=temp;
- else
- {
- tailPOLYSUM->link=temp;
- tailPOLYSUM=temp;
- }
- }
- void addPolynomial()
- {
- NODE ptrA,ptrB,ptrC;
- int found=0;
- for(ptrA=POLY1->link;ptrA!=POLY1;ptrA=ptrA->link)
- {
- for(ptrB=POLY2->link;ptrB!=POLY2;ptrB=ptrB->link)
- {
- if(ptrA->x==ptrB->x && ptrA->y==ptrB->y && ptrA->z==ptrB->z)
- {
- found=1;
- storePolynomialC(ptrA->coeff+ptrB->coeff, ptrA->x, ptrA->y, ptrA->z);
- break;
- }
- }
- if(!found)
- storePolynomialC(ptrA->coeff, ptrA->x, ptrA->y, ptrA->z);
- else
- found=0;
- }
- found=0;
- for(ptrB=POLY2->link;ptrB!=POLY2;ptrB=ptrB->link)
- {
- for(ptrA=POLY1->link;ptrA!=POLY1;ptrA=ptrA->link)
- {
- if(ptrA->x==ptrB->x && ptrA->y==ptrB->y && ptrA->z==ptrB->z)
- {
- found=1;
- break;
- }
- }
- if(!found)
- storePolynomialC(ptrB->coeff, ptrB->x, ptrB->y, ptrB->z);
- else
- found=0;
- }
- printf("\nThe resultant Polynomial C is: ");
- for(ptrC=POLYSUM->link;ptrC!=POLYSUM;ptrC=ptrC->link)
- {
- printf("%d*x^%d*y^%d*z%d",ptrC->coeff,ptrC->x,ptrC->y,ptrC->z);
- if(ptrC->link!=POLYSUM)
- printf(" + ");
- }
- printf("\n");
- }
- void evaluatePolynomialA()
- {
- int x,y,z,result=0;
- displayPolynomial(POLY1,'A');
- printf("\nEnter the values of x, y and z: ");
- scanf("%d%d%d",&x,&y,&z);
- for(ptr=POLY1->link;ptr!=POLY1;ptr=ptr->link)
- result+=ptr->coeff*pow(x,ptr->x)*pow(y,ptr->y)*pow(z,ptr->z);
- printf("\nThe result after evaluating the Polynomial A is: %d",result);
- printf("\n");
- }
- int main()
- {
- int choice;
- while(1)
- {
- 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>| ");
- scanf("%d",&choice);
- switch (choice)
- {
- case 1:
- POLY1=readPolynomial();
- break;
- case 3:
- POLY2=readPolynomial();
- break;
- case 2:
- displayPolynomial(POLY1, 'A');
- break;
- case 4:
- displayPolynomial(POLY2, 'B');
- break;
- case 5:
- addPolynomial();
- break;
- case 6:
- evaluatePolynomialA();
- break;
- case 7:
- {
- printf("\nThank you for using the program");
- exit(0);
- }
- break;
- default:
- printf("\nInvalid Choice\n");
- }
- }
- }
Add Comment
Please, Sign In to add comment