#include #include #include #include double citaj_cislo(int poradie, int *koniec) { char buffer[100]; if (fgets(buffer, sizeof(buffer), stdin) == NULL) { if (poradie == 0) { printf("Nepodarilo sa nacitat zaklad x\n"); } else { printf("Nepodarilo sa nacitat polynom na %d mieste.\n", poradie); } *koniec = 1; return NAN; } char *newline = strchr(buffer, '\n'); if (newline) *newline = '\0'; if (buffer[0] == '\0') { *koniec = 1; return NAN; } char *endptr; double cislo = strtod(buffer, &endptr); if (*endptr != '\0') { if (poradie == 0) { printf("Nepodarilo sa nacitat zaklad x\n"); } else { printf("Nepodarilo sa nacitat polynom na %d mieste.\n", poradie); } *koniec = 1; return NAN; } *koniec = 0; return cislo; } double hornerova_schema(double *koef, int n, double x) { double vysledok = 0.0; for (int i = 0; i < n; i++) { vysledok = vysledok * x + koef[i]; } return vysledok; } int main() { double x; double koef[100]; int n = 0; int koniec = 0; x = citaj_cislo(0, &koniec); if (koniec) return 1; while (!koniec) { double koeficient = citaj_cislo(n + 1, &koniec); if (koniec) break; koef[n++] = koeficient; if (n >= 100) { printf("Chyba: Prilis vela koeficientov.\n"); return 0; } } if (n == 0) { printf("Chyba: Neboli zadane ziadne koeficienty.\n"); return 1; } double vysledok = hornerova_schema(koef, n, x); printf("Vysledok je: %.2f\n", vysledok); return 0; }