horner
This commit is contained in:
parent
92a679b1b5
commit
8fa18c87ad
38
program.c
Normal file
38
program.c
Normal file
@ -0,0 +1,38 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
#define MAX_COEFFS 256
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
double x;
|
||||||
|
double coeffs[MAX_COEFFS];
|
||||||
|
int n = 0;
|
||||||
|
char line[256];
|
||||||
|
|
||||||
|
if (fgets(line, sizeof(line), stdin) == NULL || sscanf(line, "%lf", &x) != 1) {
|
||||||
|
fprintf(stderr, "Chyba: nepodarilo sa nacitat hodnotu x.\n");
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
while (fgets(line, sizeof(line), stdin) != NULL) {
|
||||||
|
if (line[0] == '\n' || line[0] == '\r') break;
|
||||||
|
|
||||||
|
if (sscanf(line, "%lf", &coeffs[n]) != 1) {
|
||||||
|
fprintf(stderr, "Chyba: nepodarilo sa nacitat koeficient a[%d].\n", n);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
n++;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (n == 0) {
|
||||||
|
fprintf(stderr, "Chyba: nepodarilo sa nacitat koeficient a[0].\n");
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
double result = 0.0;
|
||||||
|
for (int i = 0; i < n; i++) {
|
||||||
|
result = result * x + coeffs[i];
|
||||||
|
}
|
||||||
|
|
||||||
|
printf("Vysledok je: %.2f\n", result);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue
Block a user