Update 'du4/program.c'

This commit is contained in:
Anzhelika Nikolaieva 2023-03-17 21:48:25 +00:00
parent 531e534f3d
commit 862ae424ca

View File

@ -17,7 +17,6 @@ int main() {
int valid = 1; int valid = 1;
float num1 = strtof(start, &end); float num1 = strtof(start, &end);
if (end == start) { if (end == start) {
printf("KONIEC\n");
continue; continue;
} }
start = end; start = end;
@ -33,6 +32,7 @@ int main() {
// Parse second number // Parse second number
float num2 = strtof(start, &end); float num2 = strtof(start, &end);
if (end == start) { if (end == start) {
printf("CHYBA\n");
valid = 0; valid = 0;
} }
start = end; start = end;
@ -48,7 +48,7 @@ int main() {
if (valid) { if (valid) {
// Evaluate expression // Evaluate expression
float result = 0; float result;
switch (op) { switch (op) {
case '+': case '+':
result = num1 + num2; result = num1 + num2;
@ -62,19 +62,26 @@ int main() {
case '/': case '/':
result = num1 / num2; result = num1 / num2;
break; 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"); printf("OK\n");
} else { } else {
printf("ZLE\n"); printf("ZLE\n");
} }
} }
}
return EXIT_SUCCESS; return EXIT_SUCCESS;
} }