51 lines
1.1 KiB
C
51 lines
1.1 KiB
C
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include <assert.h>
|
|
#include <ctype.h>
|
|
|
|
#define LINE_SIZE 100
|
|
#define MAX_COEFFS 100
|
|
|
|
int is_valid_number(const char *str) {
|
|
char *endptr;
|
|
strtod(str, &endptr);
|
|
return *endptr == '\0' || isspace(*endptr);
|
|
}
|
|
|
|
double horner(double coeffs[], int n, double x) {
|
|
double result = coeffs[0];
|
|
for (int i = 1; i < n; i++) {
|
|
result = result * x + coeffs[i];
|
|
}
|
|
return result;
|
|
}
|
|
|
|
int main() {
|
|
char line[LINE_SIZE];
|
|
double coeffs[MAX_COEFFS];
|
|
int n = 0;
|
|
|
|
|
|
if (fgets(line, LINE_SIZE, stdin) == NULL || !is_valid_number(line)) {
|
|
fprintf(stderr, "Error: Invalid input for x.\n");
|
|
return 1;
|
|
}
|
|
double x = strtod(line, NULL);
|
|
|
|
|
|
while (fgets(line, LINE_SIZE, stdin) != NULL && line[0] != '\n') {
|
|
if (!is_valid_number(line)) {
|
|
fprintf(stderr, "Nepodarilo sa nacitat polynom na %d mieste.\n", n + 1);
|
|
return 1;
|
|
}
|
|
coeffs[n++] = strtod(line, NULL);
|
|
}
|
|
|
|
|
|
double result = horner(coeffs, n, x);
|
|
printf("Vysledok je: %.2f\n", result);
|
|
|
|
return 0;
|
|
}
|