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);