Advertisement
Unique144

Polynomial

Oct 27th, 2021
1,099
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.06 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3.  
  4. struct node {
  5.     int co;
  6.     int exp;
  7.     struct node *next;
  8. };
  9.  
  10.  
  11. struct node *PolynomailInput(int k) {
  12.     struct node *p = (struct node*)malloc(sizeof(struct node));
  13.     struct node *t = p;
  14.     while(k) {
  15.         t->next = (struct node*)malloc(sizeof(struct node));
  16.         t= t->next;
  17.         printf("Enter Coefficient and Exponent: ");
  18.         scanf("%d %d", &t->co, &t-> exp);
  19.         t->next = NULL;
  20.         k--;
  21.     }
  22.     p=p->next;
  23.     return p;
  24. }
  25.  
  26. void display(struct node *p) {
  27.     if(p == NULL) printf("Empty Linked List");
  28.     else {
  29.         struct node*x=p;
  30.         while(x!=NULL) {
  31.             printf("%dx^%d ", x->co, x->exp);
  32.             x=x->next;
  33.         }
  34.     }
  35.    
  36. }
  37.  
  38. struct node *AddPolynomial(struct node *p, struct node*q) {
  39.     struct node *add = (struct node*)malloc(sizeof(struct node));
  40.     struct node *r = add;
  41.     while(p!=NULL && q!=NULL) {
  42.         r->next = (struct node*)malloc(sizeof(struct node));
  43.         r=r->next;
  44.         if(p->exp==q->exp) {
  45.             r->co = p->co+q->co;
  46.             r->exp = p->exp;
  47.             r->next = NULL;
  48.             p=p->next;
  49.             q=q->next;
  50.         }
  51.         else if(q->exp>p->exp) {
  52.             r->exp = q->exp;
  53.             r->co=q->co;
  54.             r->next = NULL;
  55.             q=q->next;
  56.         }
  57.         else if(p->exp>q->exp) {
  58.             r->exp = p->exp;
  59.             r->co=p->co;
  60.             r->next = NULL;
  61.             p=p->next;
  62.         }
  63.     }
  64.     while(p!=NULL) {
  65.         r->next = (struct node*)malloc(sizeof(struct node));
  66.         r=r->next;
  67.         r->co = p->co;
  68.         r->exp = p->exp;
  69.         r->next = NULL;
  70.         p=p->next;
  71.     }
  72.     while(q!=NULL) {
  73.         r->next = (struct node*)malloc(sizeof(struct node));
  74.         r=r->next;
  75.         r->co = q->co;
  76.         r->exp = q->exp;
  77.         r->next = NULL;
  78.         q=q->next;
  79.     }
  80.     add=add->next;
  81.     return add;
  82. }
  83.  
  84. struct node *SubPolynomial(struct node *p, struct node *q) {
  85.     struct node *qtemp = (struct node*)malloc(sizeof(struct node));
  86.     struct node *r = qtemp;
  87.     while(q!=NULL) {
  88.         r->next = (struct node*)malloc(sizeof(struct node));
  89.         r=r->next;
  90.         r->co = q->co * (-1);
  91.         r->exp = q->exp;
  92.         r->next = NULL;
  93.         q=q->next;
  94.     }
  95.     qtemp=qtemp->next;
  96.     return AddPolynomial(p,qtemp);
  97. }
  98.  
  99. struct node *MulPolynomial(struct node *p1, struct node *p2) {
  100.     struct node *p3 = NULL;
  101.     struct node *q = p2;
  102.     while(q!=NULL) {
  103.         struct node *temp = (struct node*)malloc(sizeof(struct node));
  104.         struct node *r = temp;
  105.         struct node *p = p1;
  106.         while(p!=NULL) {
  107.             r->next = (struct node*)malloc(sizeof(struct node));
  108.             r= r->next;
  109.             r->co = p->co*q->co;
  110.             r->exp = p->exp + q->exp;
  111.             r->next = NULL;
  112.             p=p->next;
  113.         }
  114.         temp=temp->next;
  115.         p3=AddPolynomial(p3, temp);
  116.         q=q->next;
  117.     }
  118.     return p3;
  119. }
  120.  
  121. int main() {
  122.     printf("Enter number of terms of first polynomial function: \n");
  123.     int n;
  124.     scanf("%d", &n);
  125.     struct node* p1= PolynomailInput(n);
  126.     printf("\nEnter number of terms of second polynomial function: \n");
  127.     int m;
  128.     scanf("%d", &m);
  129.     struct node* p2= PolynomailInput(m);
  130.     struct node* sum = AddPolynomial(p1,p2);
  131.     struct node* diff = SubPolynomial(p1,p2);
  132.     struct node* mul = MulPolynomial(p1,p2);
  133.     printf("\nSum of polynomial functions: ");
  134.     display(sum);
  135.     printf("\nDifference of polynomial functions: ");
  136.     display(diff);
  137.     printf("\nMultiplication of polynomial functions: ");
  138.     display(mul);
  139. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement