From ebdb1ceaacada4bbec930c10f524f664fff28865 Mon Sep 17 00:00:00 2001 From: VIliam Date: Tue, 27 Feb 2024 10:04:02 +0100 Subject: [PATCH] schema --- cv3/program.c | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 cv3/program.c diff --git a/cv3/program.c b/cv3/program.c new file mode 100644 index 0000000..be66fe6 --- /dev/null +++ b/cv3/program.c @@ -0,0 +1,46 @@ +#include +#include +#include +#include + +#define MAX_LINE_LENGTH 256 + +bool IsValidNumber(const char *line) { + char *endFtr; + strtod(line, &endFtr); + return *endFtr == '\0'; +} + +int main() { + char line[MAX_LINE_LENGTH]; + double result = 1; + int coefficientIndex = 0; + + printf("Zadajte zaklad x: "); + if (fgets(line, MAX_LINE_LENGTH, stdin) == NULL || !IsValidNumber(line)) { + printf("Nepodarilo sa nacitat zaklad x\n"); + return 1; + } + + while (1) { + printf("Zadajte polynom na %d mieste: ", coefficientIndex); + if (fgets(line, MAX_LINE_LENGTH, stdin) == NULL || !IsValidNumber(line)) { + printf("Nepodarilo sa nacitat polynom na %d mieste. \n", coefficientIndex); + return 1; + } + + char *endPtr; + double x = strtod(line, &endPtr); + if (*endPtr != '\n' && *endPtr != '\0') { + printf("Nespravny format cisla\n"); + return 1; + } + + result = result * x; + coefficientIndex++; + } + + printf("Vysledok je: %f\n", result); + return 0; +} +