/*
* File: main.c
* Author: Lenka
*
* Created on 10. listopad 2012, 14:05
*/
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
/*
*
*/
int koefA[30]; // zbytek cisel jsou nuly
int koefB[30];
int soucetClenu(int call, int clen, int * a, int * b, int * c) {
int i, j;
i=call;
j=clen;
int koef, suma = 0;
if(clen >= 0){
koef = a[i] * b[j];
call++;
clen--;
suma = suma + koef + soucetClenu(call, clen, a, b, c);
}
if(clen < 0) {
*c = suma;
return * c;
}
*c = suma;
return * c;
}
int soucin(int max, int clen, int * kA, int * kB) {
int i;
int call = 0;
int * a = kA;
int * b = kB;
int * c;
c = (int*)malloc(sizeof(int) * max);
if(clen > max) {
// free(kA);
// free(kB);
return 0;
} else {
c[clen] = soucetClenu(call, clen, a, b, c);
for(i=0; i<1; i++){
//if(c[clen-1] != 0 && clen-1 != 0) break;
if(c[clen]>0 && clen != 0) {printf("+");}
if(c[clen] == 1) {
if(max-clen == 1) {
printf("%dx", c[clen]);
if(max == clen) printf("\n"); break;}
if(max-clen == 0) {
printf("%d", c[clen]);
if(max == clen) printf("\n"); break;}
printf("x<sup>%d</sup>", max-clen);
if(max == clen) printf("\n"); break;}
if(c[clen] == -1) {
if(max-clen == 1) {
printf("%dx", c[clen]);
if(max == clen) printf("\n"); break;}
if(max-clen == 0) {
printf("%d", c[clen]);
if(max == clen) printf("\n"); break;}
printf("-x<sup>%d</sup>", max-clen);
if(max == clen) printf("\n"); break;}
if(c[clen] == 0 && max == clen && a[0] == 0) {
printf("0\n"); break;}
if(c[clen] == 0) {
if(max == clen) printf("\n"); break;}
if(max-clen == 1) {
printf("%dx", c[clen]);
if(max == clen) printf("\n"); break;}
if(max-clen == 0) {
printf("%d", c[clen]);
if(max == clen) printf("\n"); break;}
printf("%dx<sup>%d</sup>", c[clen], max-clen);
if(max == clen) printf("\n");
}
clen++;
soucin(max, clen, a, b);
}
free(c);
return 0;
}
int main(void) {
int a, aa, b, bb, i;
int * kA = koefA;
int * kB = koefB;
printf("Zadejte stupen polynomu A:\n");
if(scanf("%d", &a) != 1 || a<0) {
printf("Nespravny vstup.\n"); return 1;
}
if((kA = (int*)malloc((a+1)*sizeof(int))) == NULL){
printf("Chyba pri alokaci pameti, program bude ukoncen");
exit(0);
}
// kA = (int *)malloc(a * sizeof(int));
printf("Zadejte koeficienty polynomu A:\n");
for(i=0; i<a+1; i++) {
if(scanf("%d", &aa) != 1) {
printf("Nespravny vstup.\n"); return 1;
}
kA[i] = aa;
}
printf("Zadejte stupen polynomu B:\n");
if(scanf("%d", &b) != 1 || b<0) {
printf("Nespravny vstup.\n"); return 1;
}
kB = (int *)malloc((b+1) * sizeof(int));
printf("Zadejte koeficienty polynomu B:\n");
for(i=0; i<b+1; i++) {
if(scanf("%d", &bb) != 1) {
printf("Nespravny vstup.\n"); return 1;
}
kB[i] = bb;
}
/*
if(a>b) {
kA = koefB;
kB = koefA;
}
else {
kA = koefA;
kB = koefB;
}
*/
int max = a+b;
int clen = 0;
if(a>b)
soucin(max, clen, kB, kA);
else soucin(max, clen, kA, kB);
free(kA);
free(kB);
/*
for(i=0; i<a+1; i++) {
for(j=0; j<b+1; j++) {
koefC[i][j] = koefA[i] * koefB[j];
}
}
int x2, x1, x0;
for(i=a, j=b; i>=0, j>=0; i-2, j-2) {
x0 = koefC[a-i][b-j];
x2 = koefC[a-i-2][b-j-2];
x1 = (koefA[a-i-2]+koefA[a-i])*(koefB[b-j-2]+koefB[b-j]) - x0 - x2;
for(i=0; i<a+1; i++) {
for(j=0; j<b+1; j++) {
if(koefC[i][j] == 1) {printf("%x");
if(i!=a && j!=b) printf("\n+\n");
if(i==a && j==b) printf("\n"); break;}
if(koefC[i][j] == -1) {printf("-%x");
if(i!=a && j!=b) printf("\n+\n");
if(i==a && j==b) printf("\n"); break;}
if(koefC[i][j] == 0) {if(i!=a && j!=b) printf("\n+\n");
if(i==a && j==b) printf("\n"); break;}
if((a-i)+(b-j) == 1) {printf("%dx", koefC[i][j]);
if(i<a || j<b) printf("\n+\n");
if(i==a && j==b) printf("\n"); break;}
if((a-i)+(b-j) == 0) {printf("%d", koefC[i][j]);
if(i!=a && j!=b) printf("\n+\n");
if(i==a && j==b) printf("\n"); break;}
printf("%dx<sup>%d</sup>", koefC[i][j], (a-i)+(b-j));
if(i!=a && j!=b) printf("\n+\n");
if(i==a && j==b) printf("\n");
}}*/
// printf("%dx<sup>2</sup>+%dx<sup>1</sup>+%dx<sup>0</sup>", x2, x1, x0);
return (EXIT_SUCCESS);
}
Úkolem je program násobící 2 polynomy + specifický výpis