Advertisement
Guest User

polymultiply.cxx

a guest
Sep 19th, 2018
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.92 KB | None | 0 0
  1. ;//Program of polynomial multiplication
  2. #include<stdio.h>
  3. #include<malloc.h>
  4. #include<stdlib.h>
  5. int n,m,*p=NULL,*q=NULL;
  6. int *r=NULL,*g=NULL,*b=NULL;
  7. int z,j,y;
  8. static int c,d;
  9. void output(int *s,int z)
  10. {
  11.     if (d==0)
  12.     printf("The %dst poly is:\n",d+1);
  13.     else
  14.     printf("The %dnd poly is:\n",d+1);
  15.     for (int i=z;i>=0;i--)
  16.     {
  17.         if (z==0)
  18.         printf("%d",s[i]);
  19.         else if (i==z&&n>0)
  20.         printf("%d(x^%d)",s[i],i);
  21.         else if (s[i]>0&&i>0)
  22.         printf("+%d(x^%d)",s[i],i);
  23.         else if (s[i]<0&&i>0)
  24.         printf("%d(x^%d)",s[i],i);
  25.         else if (i==0&&s[i]>0)
  26.         printf("+%d",s[i]);
  27.         else if (i==0&&s[i]<0)
  28.         printf("%d",s[i]);
  29.     }
  30.     d++;
  31. }
  32. void input()
  33. {
  34.     int *s=NULL;
  35.     if (c==0)
  36.     {
  37.     printf("Enter degree of %dst polynomial\n",c+1);
  38.     scanf("%d",&n);
  39.     if (n<0)
  40.     {
  41.         printf("Wrong input\n");
  42.         exit(1);
  43.     }
  44.     p=(int *)calloc(n+1,sizeof (int ));
  45.     s=p;
  46.     z=n;
  47.     }
  48.     else
  49.     {
  50.     printf("Enter degree of %dnd polynomial\n",c+1);
  51.         scanf("%d",&m);
  52.         if (m<0)
  53.     {
  54.         printf("Wrong input\n");
  55.         exit(1);
  56.     }  
  57.     q=(int *)calloc(m+1,sizeof (int ));
  58.     s=q;
  59.     z=m;
  60.     }
  61.     if (c==0)
  62.         {
  63.     printf("Enter %dst polynomial:-\n",c+1);
  64.         }
  65.         else
  66.         {
  67.             printf("Enter %dnd polynomial:-\n",c+1);
  68.         }
  69.     for (int i=z;i>=0;i--)
  70.     {
  71.     printf(" x^%d=",i);
  72.     scanf("%d",&s[i]);
  73.     }
  74.     output(s,z);
  75.     printf("\n\n");
  76.     c+=1;
  77. }
  78. void showproduct(int k)
  79. {
  80.     printf("\n\nThe product is:-\n");
  81.     for (int i=k;i>=0;i--)
  82.     {
  83.         if (k==0)
  84.         printf("%d",r[i]);
  85.         else if (i==k&&r[k]>0)
  86.         printf("%d(x^%d)",r[i],i);
  87.         else if (r[i]>0&&i>0)
  88.         printf("+%d(x^%d)",r[i],i);
  89.         else if (r[i]<0&&i>0)
  90.         printf("%d(x^%d)",r[i],i);
  91.         else if (i==0&&r[i]>0)
  92.         printf("+%d",r[i]);
  93.         else if (i==0&&r[i]<0)
  94.         printf("%d",r[i]);
  95.     }
  96.     printf("\n\n");
  97. }
  98. void product(int k,int l)
  99. {
  100.     int o;
  101.     r=(int *)calloc(k+l+1,sizeof (int ));
  102.     o=(k+l+1);
  103.     for (int v=k;v>=0;v--)
  104.     {
  105.     for (int j=l;j>=0;j--)
  106.     {
  107.         r[v+l]+=p[v]*q[j];
  108.      }
  109.     printf("%d %d \n",r[v+l],v+l);
  110.     }
  111.    // showproduct(o);
  112. }
  113. int main()
  114. {
  115.     input();
  116.     input();
  117.     product(n,m);
  118.     return 0;
  119. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement