From 0bff677e4f759474bb1143abbe3c6ec8ad88afbb Mon Sep 17 00:00:00 2001 From: VIliam Date: Wed, 28 Feb 2024 15:53:40 +0100 Subject: [PATCH] schema --- cv3/program.c | 20 +++++++++----------- 1 file changed, 9 insertions(+), 11 deletions(-) diff --git a/cv3/program.c b/cv3/program.c index a53d755..33f32c1 100644 --- a/cv3/program.c +++ b/cv3/program.c @@ -13,8 +13,8 @@ bool IsValidNumber(const char *line) { int main() { char line[MAX_LINE_LENGTH]; - double result = 1; - int coefficientIndex = 0; + double result = 0; + int exponent = 2; if (fgets(line, MAX_LINE_LENGTH, stdin) == NULL || !IsValidNumber(line)) { @@ -22,24 +22,22 @@ int main() { return 1; } + double x = strtod(line, NULL); + result += x; + while (1) { if (fgets(line, MAX_LINE_LENGTH, stdin) == NULL || !IsValidNumber(line)) { if (line[0] == '\n') { break; } - printf("Nepodarilo sa nacitat polynom na %d mieste. \n", coefficientIndex); + printf("Nepodarilo sa nacitat polynom\n"); return 1; } - char *endPtr; - double x = strtod(line, &endPtr); - if (*endPtr != '\n' && *endPtr != '\0') { - printf("Nespravny format cisla\n"); - return 1; - } + x = strtod(line, NULL); + result += x * exponent; - result *= x; - coefficientIndex++; + exponent--; } printf("Vysledok je: %.2f\n", result);