Aktualizovat du2/program.c
This commit is contained in:
parent
420615385e
commit
f720dabad9
@ -1,7 +1,6 @@
|
|||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <ctype.h>
|
|
||||||
#include <math.h>
|
#include <math.h>
|
||||||
|
|
||||||
int je_cislo(const char *str) {
|
int je_cislo(const char *str) {
|
||||||
@ -10,15 +9,17 @@ int je_cislo(const char *str) {
|
|||||||
return *endptr == '\0';
|
return *endptr == '\0';
|
||||||
}
|
}
|
||||||
|
|
||||||
double citaj_cislo(int poradie) {
|
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) {
|
||||||
if (poradie == 0) {
|
if (poradie == 0) {
|
||||||
printf("Chyba: Nepodarilo sa nacitat hodnotu x.\n");
|
printf("Nepodarilo sa nacitat hodnotu x.\n");
|
||||||
} else {
|
} else {
|
||||||
printf("Nepodarilo sa nacitat polynom na %d mieste.\n", poradie);
|
printf("Nepodarilo sa nacitat polynom na %d mieste.\n", poradie);
|
||||||
}
|
}
|
||||||
exit(1);
|
*chyba = 1;
|
||||||
|
return NAN;
|
||||||
}
|
}
|
||||||
|
|
||||||
char *newline = strchr(buffer, '\n');
|
char *newline = strchr(buffer, '\n');
|
||||||
@ -30,16 +31,18 @@ double citaj_cislo(int poradie) {
|
|||||||
|
|
||||||
if (!je_cislo(buffer)) {
|
if (!je_cislo(buffer)) {
|
||||||
if (poradie == 0) {
|
if (poradie == 0) {
|
||||||
printf("Chyba: Nepodarilo sa nacitat hodnotu x.\n");
|
printf("Nepodarilo sa nacitat hodnotu x.\n");
|
||||||
} else {
|
} else {
|
||||||
printf("Nepodarilo sa nacitat polynom na %d mieste.\n", poradie);
|
printf("Nepodarilo sa nacitat polynom na %d mieste.\n", poradie);
|
||||||
}
|
}
|
||||||
exit(1);
|
*chyba = 1;
|
||||||
|
return NAN;
|
||||||
}
|
}
|
||||||
|
|
||||||
return atof(buffer);
|
return atof(buffer);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
double hornerova_schema(double *koef, int n, double x) {
|
double hornerova_schema(double *koef, int n, double x) {
|
||||||
double vysledok = 0.0;
|
double vysledok = 0.0;
|
||||||
for (int i = 0; i < n; i++) {
|
for (int i = 0; i < n; i++) {
|
||||||
@ -48,29 +51,35 @@ double hornerova_schema(double *koef, int n, double x) {
|
|||||||
return vysledok;
|
return vysledok;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
double x;
|
double x;
|
||||||
double koef[100];
|
double koef[100];
|
||||||
int n = 0;
|
int n = 0;
|
||||||
|
int chyba = 0;
|
||||||
|
|
||||||
|
|
||||||
|
x = citaj_cislo(0, &chyba);
|
||||||
|
if (chyba) return 0;
|
||||||
|
|
||||||
x = citaj_cislo(0);
|
|
||||||
|
|
||||||
while (1) {
|
while (1) {
|
||||||
double koeficient = citaj_cislo(n + 1);
|
double koeficient = citaj_cislo(n + 1, &chyba);
|
||||||
if (isnan(koeficient)) {
|
if (chyba) return 0;
|
||||||
break;
|
|
||||||
}
|
if (isnan(koeficient)) break;
|
||||||
|
|
||||||
koef[n++] = koeficient;
|
koef[n++] = koeficient;
|
||||||
|
|
||||||
if (n >= 100) {
|
if (n >= 100) {
|
||||||
printf("Chyba: Prilis vela koeficientov.\n");
|
printf("Chyba: Prilis vela koeficientov.\n");
|
||||||
exit(1);
|
return 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (n == 0) {
|
if (n == 0) {
|
||||||
printf("Chyba: Neboli zadane ziadne koeficienty.\n");
|
printf("Chyba: Neboli zadane ziadne koeficienty.\n");
|
||||||
return 1;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
double vysledok = hornerova_schema(koef, n, x);
|
double vysledok = hornerova_schema(koef, n, x);
|
||||||
|
Loading…
Reference in New Issue
Block a user