From cef61faf0c3967134265f6493f790c65596efedd Mon Sep 17 00:00:00 2001 From: VIliam Date: Thu, 7 Mar 2024 22:27:00 +0100 Subject: [PATCH] cv3 --- cv3/program.c | 26 +++++++++----------------- 1 file changed, 9 insertions(+), 17 deletions(-) diff --git a/cv3/program.c b/cv3/program.c index 68cf33e..f2bbd62 100644 --- a/cv3/program.c +++ b/cv3/program.c @@ -1,6 +1,5 @@ #include #include -#include #include #define MAX_LINE_LENGTH 256 @@ -14,32 +13,25 @@ bool IsValidNumber(const char *line) { int main() { char line[MAX_LINE_LENGTH]; double result = 0; - int exponent = 2; + double x; if (fgets(line, MAX_LINE_LENGTH, stdin) == NULL || !IsValidNumber(line)) { printf("Nepodarilo sa nacitat zaklad x\n"); return 1; } - double x = strtod(line, NULL); - result += 1; + x = strtod(line, NULL); - while (1) { - if (fgets(line, MAX_LINE_LENGTH, stdin) == NULL || !IsValidNumber(line)) { - if (line[0] == '\n') { - break; - } - printf("Nepodarilo sa nacitat polynom\n"); - return 1; - } + while (fgets(line, MAX_LINE_LENGTH, stdin) != NULL && IsValidNumber(line)) { + double coefficient = strtod(line, NULL); + result = result * x + coefficient; + } - x = strtod(line, NULL); - result = result * x + 1; - - exponent--; + if (line[0] != '\n') { + printf("Nepodarilo sa nacitat koeficient polynomu\n"); + return 1; } printf("Vysledok je: %.2f\n", result); return 0; } -