Aktualizovat du2/program.c

This commit is contained in:
Denys Sanchuk 2025-03-04 13:50:14 +00:00
parent d78d23d8aa
commit 7575e3d387

View File

@ -3,13 +3,7 @@
#include <string.h>
#include <math.h>
int je_cislo(const char *str) {
char *endptr;
strtod(str, &endptr);
return *endptr == '\0';
}
double citaj_cislo(int poradie, int *chyba) {
double citaj_cislo(int poradie, int *koniec) {
char buffer[100];
if (fgets(buffer, sizeof(buffer), stdin) == NULL) {
@ -18,7 +12,7 @@ double citaj_cislo(int poradie, int *chyba) {
} else {
printf("Nepodarilo sa nacitat polynom na %d mieste.\n", poradie);
}
*chyba = 1;
*koniec = 1;
return NAN;
}
@ -26,20 +20,24 @@ double citaj_cislo(int poradie, int *chyba) {
if (newline) *newline = '\0';
if (buffer[0] == '\0') {
*koniec = 1;
return NAN;
}
if (!je_cislo(buffer)) {
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);
}
*chyba = 1;
*koniec = 1;
return NAN;
}
return atof(buffer);
*koniec = 0;
return cislo;
}
double hornerova_schema(double *koef, int n, double x) {
@ -54,16 +52,14 @@ int main() {
double x;
double koef[100];
int n = 0;
int chyba = 0;
int koniec = 0;
x = citaj_cislo(0, &chyba);
if (chyba) return 0;
x = citaj_cislo(0, &koniec);
if (koniec) return 1;
while (1) {
double koeficient = citaj_cislo(n + 1, &chyba);
if (chyba) return 0;
if (isnan(koeficient)) break;
while (!koniec) {
double koeficient = citaj_cislo(n + 1, &koniec);
if (koniec) break;
koef[n++] = koeficient;
@ -75,7 +71,7 @@ int main() {
if (n == 0) {
printf("Chyba: Neboli zadane ziadne koeficienty.\n");
return 0;
return 1;
}
double vysledok = hornerova_schema(koef, n, x);