Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define _CRT_SECURE_NO_WARNINGS
- #include <stdio.h>
- #include <stdlib.h>
- typedef struct poly {
- int pow;
- int *data;
- }poly;
- poly* multiplication(poly *a, poly *b)
- {
- poly *c = (poly*)malloc(sizeof(poly));
- c->pow = a->pow + b->pow;
- c->data = (int*)malloc(sizeof(int)*c->pow + 1);
- int **coefficient;
- coefficient = (int**)malloc(sizeof(int*)*a->pow + 1);
- for (int q = 0; q < a->pow + 1; q++)
- {
- coefficient[q] = (int*)malloc(sizeof(int)*b->pow + 1);
- }
- for (int w = 0; w < a->pow + 1; w++)
- {
- for (int e = 0; e < b->pow + 1; e++)
- {
- coefficient[w][e] = 0;
- coefficient[w][e] = a->data[w] * b->data[e];
- printf("%d", coefficient[w][e]);
- }
- }
- for (int p = 0; p <= c->pow; p++) { c->data[p] = 0; }
- int k = 0;
- int i = 0;
- int j = 0;
- while (k <= a->pow)
- {
- for (i = k, j = 0; i > 0; i--, j++)
- {
- c->data[k] += coefficient[i][j];
- }
- k++;
- }
- int m = 0;
- int l = 0;
- while (k <= c->pow)
- {
- m = a->pow;
- l = k - a->pow;
- for (m, l; l <= a->pow; m--, l++)
- {
- c->data[k] += coefficient[m][l];
- }
- k++;
- }
- return c;
- }
- poly* fill(FILE*f)
- {
- int N = 0;
- fscanf_s(f, "%d", &N);
- poly *a = (poly*)malloc(sizeof(poly));
- a->pow = N;
- a->data = (int*)malloc(sizeof(int)*N + 1);
- for (int l = 0; l < N + 1; l++)
- {
- fscanf_s(f, "%d", &a->data[l]);
- }
- return a;
- }
- int main() {
- FILE*f = fopen("C:\\Users\\kiril\\source\\repos\\Project2\\Project2\\Source.txt", "r");
- poly *a = fill(f);
- poly *b = fill(f);
- poly *c = multiplication(a, b);
- printf("Polynom: \n");
- for (int g = 0; g < c->pow + 1; g++) {
- printf("%d", c->data[g]);
- }
- getchar();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement