pvjc25/du2/program.c

86 lines
1.8 KiB
C
Raw Normal View History

2025-02-25 14:00:04 +00:00
#include <stdio.h>
#include <stdlib.h>
2025-03-04 13:29:42 +00:00
#include <string.h>
#include <math.h>
2025-02-25 14:00:04 +00:00
2025-03-04 13:25:33 +00:00
int je_cislo(const char *str) {
char *endptr;
strtod(str, &endptr);
return *endptr == '\0';
}
2025-02-25 14:09:29 +00:00
2025-03-04 13:43:31 +00:00
double citaj_cislo(int poradie, int *chyba) {
2025-03-04 13:25:33 +00:00
char buffer[100];
2025-03-04 13:43:31 +00:00
2025-03-04 13:25:33 +00:00
if (fgets(buffer, sizeof(buffer), stdin) == NULL) {
if (poradie == 0) {
2025-03-04 13:45:56 +00:00
printf("Nepodarilo sa nacitat zaklad x\n");
2025-03-04 13:25:33 +00:00
} else {
2025-03-04 13:39:46 +00:00
printf("Nepodarilo sa nacitat polynom na %d mieste.\n", poradie);
2025-03-04 13:25:33 +00:00
}
2025-03-04 13:46:48 +00:00
*chyba = 1;
2025-03-04 13:43:31 +00:00
return NAN;
2025-02-25 14:09:29 +00:00
}
2025-03-04 13:25:33 +00:00
char *newline = strchr(buffer, '\n');
2025-03-04 13:46:48 +00:00
if (newline) *newline = '\0';
2025-02-25 14:00:04 +00:00
2025-03-04 13:25:33 +00:00
if (buffer[0] == '\0') {
2025-03-04 13:46:48 +00:00
return NAN;
2025-02-25 14:00:04 +00:00
}
2025-02-25 14:09:29 +00:00
2025-03-04 13:25:33 +00:00
if (!je_cislo(buffer)) {
if (poradie == 0) {
2025-03-04 13:45:56 +00:00
printf("Nepodarilo sa nacitat zaklad x\n");
2025-02-25 14:15:08 +00:00
} else {
2025-03-04 13:39:46 +00:00
printf("Nepodarilo sa nacitat polynom na %d mieste.\n", poradie);
2025-02-25 14:15:08 +00:00
}
2025-03-04 13:46:48 +00:00
*chyba = 1;
2025-03-04 13:43:31 +00:00
return NAN;
2025-03-04 13:25:33 +00:00
}
return atof(buffer);
}
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];
2025-02-25 14:15:08 +00:00
}
2025-03-04 13:25:33 +00:00
return vysledok;
}
2025-02-25 14:15:08 +00:00
2025-03-04 13:25:33 +00:00
int main() {
double x;
2025-03-04 13:46:48 +00:00
double koef[100];
int n = 0;
int chyba = 0;
2025-03-04 13:43:31 +00:00
x = citaj_cislo(0, &chyba);
2025-03-04 13:46:48 +00:00
if (chyba) return 0;
2025-02-25 14:09:29 +00:00
2025-02-25 14:10:41 +00:00
while (1) {
2025-03-04 13:43:31 +00:00
double koeficient = citaj_cislo(n + 1, &chyba);
2025-03-04 13:46:48 +00:00
if (chyba) return 0;
2025-03-04 13:43:31 +00:00
2025-03-04 13:46:48 +00:00
if (isnan(koeficient)) break;
2025-03-04 13:43:31 +00:00
2025-03-04 13:25:33 +00:00
koef[n++] = koeficient;
if (n >= 100) {
2025-03-04 13:39:46 +00:00
printf("Chyba: Prilis vela koeficientov.\n");
2025-03-04 13:43:31 +00:00
return 0;
2025-02-25 14:10:41 +00:00
}
2025-02-25 14:00:04 +00:00
}
2025-02-25 14:09:29 +00:00
if (n == 0) {
2025-03-04 13:39:46 +00:00
printf("Chyba: Neboli zadane ziadne koeficienty.\n");
2025-03-04 13:43:31 +00:00
return 0;
2025-02-25 14:00:04 +00:00
}
2025-02-25 14:09:29 +00:00
2025-03-04 13:25:33 +00:00
double vysledok = hornerova_schema(koef, n, x);
printf("Vysledok je: %.2f\n", vysledok);
2025-02-25 14:09:29 +00:00
2025-02-25 14:00:04 +00:00
return 0;
2025-02-25 14:06:18 +00:00
}