Aktualizovat du2/program.c
This commit is contained in:
parent
d78d23d8aa
commit
7575e3d387
@ -3,13 +3,7 @@
|
|||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <math.h>
|
#include <math.h>
|
||||||
|
|
||||||
int je_cislo(const char *str) {
|
double citaj_cislo(int poradie, int *koniec) {
|
||||||
char *endptr;
|
|
||||||
strtod(str, &endptr);
|
|
||||||
return *endptr == '\0';
|
|
||||||
}
|
|
||||||
|
|
||||||
double citaj_cislo(int poradie, int *chyba) {
|
|
||||||
char buffer[100];
|
char buffer[100];
|
||||||
|
|
||||||
if (fgets(buffer, sizeof(buffer), stdin) == NULL) {
|
if (fgets(buffer, sizeof(buffer), stdin) == NULL) {
|
||||||
@ -18,7 +12,7 @@ double citaj_cislo(int poradie, int *chyba) {
|
|||||||
} else {
|
} else {
|
||||||
printf("Nepodarilo sa nacitat polynom na %d mieste.\n", poradie);
|
printf("Nepodarilo sa nacitat polynom na %d mieste.\n", poradie);
|
||||||
}
|
}
|
||||||
*chyba = 1;
|
*koniec = 1;
|
||||||
return NAN;
|
return NAN;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -26,20 +20,24 @@ double citaj_cislo(int poradie, int *chyba) {
|
|||||||
if (newline) *newline = '\0';
|
if (newline) *newline = '\0';
|
||||||
|
|
||||||
if (buffer[0] == '\0') {
|
if (buffer[0] == '\0') {
|
||||||
|
*koniec = 1;
|
||||||
return NAN;
|
return NAN;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!je_cislo(buffer)) {
|
char *endptr;
|
||||||
|
double cislo = strtod(buffer, &endptr);
|
||||||
|
if (*endptr != '\0') {
|
||||||
if (poradie == 0) {
|
if (poradie == 0) {
|
||||||
printf("Nepodarilo sa nacitat zaklad x\n");
|
printf("Nepodarilo sa nacitat zaklad x\n");
|
||||||
} else {
|
} else {
|
||||||
printf("Nepodarilo sa nacitat polynom na %d mieste.\n", poradie);
|
printf("Nepodarilo sa nacitat polynom na %d mieste.\n", poradie);
|
||||||
}
|
}
|
||||||
*chyba = 1;
|
*koniec = 1;
|
||||||
return NAN;
|
return NAN;
|
||||||
}
|
}
|
||||||
|
|
||||||
return atof(buffer);
|
*koniec = 0;
|
||||||
|
return cislo;
|
||||||
}
|
}
|
||||||
|
|
||||||
double hornerova_schema(double *koef, int n, double x) {
|
double hornerova_schema(double *koef, int n, double x) {
|
||||||
@ -54,16 +52,14 @@ int main() {
|
|||||||
double x;
|
double x;
|
||||||
double koef[100];
|
double koef[100];
|
||||||
int n = 0;
|
int n = 0;
|
||||||
int chyba = 0;
|
int koniec = 0;
|
||||||
|
|
||||||
x = citaj_cislo(0, &chyba);
|
x = citaj_cislo(0, &koniec);
|
||||||
if (chyba) return 0;
|
if (koniec) return 1;
|
||||||
|
|
||||||
while (1) {
|
while (!koniec) {
|
||||||
double koeficient = citaj_cislo(n + 1, &chyba);
|
double koeficient = citaj_cislo(n + 1, &koniec);
|
||||||
if (chyba) return 0;
|
if (koniec) break;
|
||||||
|
|
||||||
if (isnan(koeficient)) break;
|
|
||||||
|
|
||||||
koef[n++] = koeficient;
|
koef[n++] = koeficient;
|
||||||
|
|
||||||
@ -75,7 +71,7 @@ int main() {
|
|||||||
|
|
||||||
if (n == 0) {
|
if (n == 0) {
|
||||||
printf("Chyba: Neboli zadane ziadne koeficienty.\n");
|
printf("Chyba: Neboli zadane ziadne koeficienty.\n");
|
||||||
return 0;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
double vysledok = hornerova_schema(koef, n, x);
|
double vysledok = hornerova_schema(koef, n, x);
|
||||||
|
Loading…
Reference in New Issue
Block a user