Tak jsem to překopala, mimo jiné objevila víc chyb, než by mě napadlo, ale stále mám problém s pamětí...
/*
* File: main.c
* Author: Lenka
*
* Created on 10. listopad 2012, 14:05
*/
/*
*
*/
#include <stdio.h>
#include <stdlib.h>
int koefA[30]; // zbytek cisel jsou nuly
int koefB[30];
int soucetClenu(int call, int clen, int * a, int * b, int aa, int bb, int * c) {
int koef, suma = 0;
if(clen >= 0){
if(call>aa) a[call]=0;
if(clen>bb) b[clen]=0;
koef = a[call] * b[clen];
call++;
clen--;
suma = suma + koef + soucetClenu(call, clen, a, b, aa, bb, c);
}
(*c) = suma;
return (*c);
}
int soucin(int max, int clen, int * kA, int * kB, int aa, int bb) {
int call = 0, i=1, c[60];
int * a = kA;
int * b = kB;
int * cc;
cc = (int *)malloc(sizeof(int) * (max+1));
for(clen=0; clen<=max; clen++) {
i++;
c[clen] = soucetClenu(call, clen, a, b, aa, bb, cc);
}
if(i>max) {
for(clen=max; clen >= 0; clen--){
if(c[clen]>0 && clen != max) {printf("+");}
if(c[clen] == 1) {
if(clen == 1) {
printf("%dx", c[clen]);
if(clen == 0) printf("\n"); continue;}
if(clen == 0) {
printf("%d", c[clen]);
if(clen == 0) printf("\n"); continue;}
printf("x<sup>%d</sup>", clen);
if(clen == 0) printf("\n"); continue;}
if(c[clen] == -1) {
if(clen == 1) {
printf("-x"); continue;}
if(clen == 0) {continue;}
printf("-x<sup>%d</sup>", clen);
if(clen == 0) printf("\n"); continue;}
if(c[clen] == 0 && clen == max && a[0] == 0) {
printf("0\n"); continue;}
if(c[clen] == 0) {
if(clen == 0) printf("\n"); continue;}
if(clen == 1) {
printf("%dx", c[clen]); continue;}
if(clen == 0) {
printf("%d\n", c[clen]); continue;}
printf("%dx<sup>%d</sup>", c[clen], clen);
}}
free(cc);
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;
}
kA = (int *)malloc((a+1) * sizeof(int));
printf("Zadejte koeficienty polynomu A:\n");
for(i=a; i>=0; 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=b; i>=0; i--) {
if(scanf("%d", &bb) != 1) {
printf("Nespravny vstup.\n"); return 1;
}
kB[i] = bb;
}
int max = a+b;
int clen = 0;
if(a>b)
soucin(max, clen, kB, kA, b, a);
else soucin(max, clen, kA, kB, a, b);
free(kA);
free(kB);
return 0;
}
V soucinu by asi šlo zapisovat a poté vypisovat z *cc, ale mě to nějak nešlo... Takže jsem to zkusila složitěji...