Advertisement
Guest User

Untitled

a guest
May 24th, 2018
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.62 KB | None | 0 0
  1. #define _CRT_SECURE_NO_WARNINGS
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. typedef struct poly {
  5.     int pow;
  6.     int *data;
  7. }poly;
  8. poly* multiplication(poly *a, poly *b)
  9. {
  10.     poly *c = (poly*)malloc(sizeof(poly));
  11.     c->pow = a->pow + b->pow;
  12.     c->data = (int*)malloc(sizeof(int)*c->pow + 1);
  13.     int **coefficient;
  14.     coefficient = (int**)malloc(sizeof(int*)*a->pow + 1);
  15.     for (int q = 0; q < a->pow + 1; q++)
  16.     {
  17.         coefficient[q] = (int*)malloc(sizeof(int)*b->pow + 1);
  18.     }
  19.     for (int w = 0; w < a->pow + 1; w++)
  20.     {
  21.         for (int e = 0; e < b->pow + 1; e++)
  22.         {
  23.             coefficient[w][e] = 0;
  24.             coefficient[w][e] = a->data[w] * b->data[e];
  25.             printf("%d", coefficient[w][e]);
  26.         }
  27.     }
  28.     for (int p = 0; p <= c->pow; p++) { c->data[p] = 0; }
  29.     int k = 0;
  30.     int i = 0;
  31.     int j = 0;
  32.     while (k <= a->pow)
  33.     {
  34.         for (i = k, j = 0; i > 0; i--, j++)
  35.         {
  36.             c->data[k] += coefficient[i][j];
  37.         }
  38.         k++;
  39.     }
  40.     int m = 0;
  41.     int l = 0;
  42.     while (k <= c->pow)
  43.     {
  44.         m = a->pow;
  45.         l = k - a->pow;
  46.         for (m, l; l <= a->pow; m--, l++)
  47.         {
  48.             c->data[k] += coefficient[m][l];
  49.         }
  50.         k++;
  51.     }
  52.     return c;
  53. }
  54. poly* fill(FILE*f)
  55. {
  56.     int N = 0;
  57.     fscanf_s(f, "%d", &N);
  58.     poly *a = (poly*)malloc(sizeof(poly));
  59.     a->pow = N;
  60.     a->data = (int*)malloc(sizeof(int)*N + 1);
  61.     for (int l = 0; l < N + 1; l++)
  62.     {
  63.         fscanf_s(f, "%d", &a->data[l]);
  64.     }
  65.     return a;
  66. }
  67. int main() {
  68.     FILE*f = fopen("C:\\Users\\kiril\\source\\repos\\Project2\\Project2\\Source.txt", "r");
  69.     poly *a = fill(f);
  70.     poly *b = fill(f);
  71.     poly *c = multiplication(a, b);
  72.  
  73.     printf("Polynom: \n");
  74.     for (int g = 0; g < c->pow + 1; g++) {
  75.         printf("%d", c->data[g]);
  76.     }
  77.     getchar();
  78.     return 0;
  79. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement