61 lines
1.1 KiB
C
61 lines
1.1 KiB
C
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
|
|
#define SIZE 256
|
|
#define MAX 1000
|
|
|
|
int main() {
|
|
|
|
char line[SIZE];
|
|
char *end;
|
|
|
|
double x;
|
|
double pole[MAX];
|
|
int n = 0;
|
|
|
|
/* nacitanie x */
|
|
if (fgets(line, SIZE, stdin) == NULL) {
|
|
printf("Chyba: nepodarilo sa nacitat x.\n");
|
|
return 1;
|
|
}
|
|
|
|
x = strtod(line, &end);
|
|
|
|
if (end == line) {
|
|
printf("Chyba: nepodarilo sa nacitat x.\n");
|
|
return 1;
|
|
}
|
|
|
|
/* nacitanie koeficientov */
|
|
while (fgets(line, SIZE, stdin) != NULL) {
|
|
|
|
if (line[0] == '\n') // prazdny riadok
|
|
break;
|
|
|
|
pole[n] = strtod(line, &end);
|
|
|
|
if (end == line) {
|
|
printf("Chyba: nepodarilo sa nacitat koeficient %d.\n", n);
|
|
return 1;
|
|
}
|
|
|
|
n++;
|
|
}
|
|
|
|
if (n == 0) {
|
|
printf("Chyba: nebol zadany ziadny koeficient.\n");
|
|
return 1;
|
|
}
|
|
|
|
/* Horner */
|
|
double vysledok = pole[0];
|
|
|
|
for (int i = 1; i < n; i++) {
|
|
vysledok = vysledok * x + pole[i];
|
|
}
|
|
|
|
printf("%.2f\n", vysledok);
|
|
|
|
return 0;
|
|
} |