From 862ae424caccdb7ba71dd626ede99702ddea0c18 Mon Sep 17 00:00:00 2001 From: Anzhelika Nikolaieva Date: Fri, 17 Mar 2023 21:48:25 +0000 Subject: [PATCH] Update 'du4/program.c' --- du4/program.c | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/du4/program.c b/du4/program.c index 8a16117..7ebebce 100644 --- a/du4/program.c +++ b/du4/program.c @@ -17,7 +17,6 @@ int main() { int valid = 1; float num1 = strtof(start, &end); if (end == start) { - printf("KONIEC\n"); continue; } start = end; @@ -33,6 +32,7 @@ int main() { // Parse second number float num2 = strtof(start, &end); if (end == start) { + printf("CHYBA\n"); valid = 0; } start = end; @@ -48,7 +48,7 @@ int main() { if (valid) { // Evaluate expression - float result = 0; + float result; switch (op) { case '+': result = num1 + num2; @@ -62,19 +62,26 @@ int main() { case '/': result = num1 / num2; break; - } - } - result = round(result * 100) / 100; - if (num2(result - result) < 0.001) { + } + + // Round result and compare to expected value + float expected; + if (sscanf(start, "%f", &expected) != 1) { + printf("KONIEC\n"); + continue; + } + expected = round(expected * 100) / 100; + if (fabs(result - expected) < 0.001) { printf("OK\n"); } else { printf("ZLE\n"); } } - + } return EXIT_SUCCESS; } + \ No newline at end of file