85 lines
2.0 KiB
C
85 lines
2.0 KiB
C
#include <stdio.h>
|
|
#include <string.h>
|
|
#include <stdlib.h>
|
|
#include <math.h>
|
|
|
|
int main() {
|
|
char priklad[300];
|
|
int count = 0;
|
|
|
|
// Read input line by line
|
|
while (fgets(priklad, sizeof(priklad), stdin) != NULL) {
|
|
count++;
|
|
|
|
// Parse expression and evaluate it
|
|
char *start = priklad;
|
|
char *end = NULL;
|
|
int valid = 1;
|
|
float num1 = strtof(start, &end);
|
|
if (end == start) {
|
|
continue;
|
|
}
|
|
start = end;
|
|
|
|
// Find the operation symbol
|
|
char op = *start;
|
|
if (op != '+' && op != '-' && op != '*' && op != '/') {
|
|
printf("CHYBA\n");
|
|
valid = 0;
|
|
}
|
|
start++;
|
|
|
|
// Parse second number
|
|
float num2 = strtof(start, &end);
|
|
if (end == start) {
|
|
valid = 0;
|
|
}
|
|
start = end;
|
|
|
|
// Check for extra characters
|
|
while (*start != '\0') {
|
|
if (*start != '\n' && (*start < '0' || *start > '9') && *start != '.' && *start != ' ') {
|
|
valid = 0;
|
|
break;
|
|
}
|
|
start++;
|
|
}
|
|
|
|
float result=0;
|
|
if (valid) {
|
|
switch (op) {
|
|
case '+':
|
|
result = num1 + num2;
|
|
break;
|
|
case '-':
|
|
result = num1 - num2;
|
|
break;
|
|
case '*':
|
|
result = num1 * num2;
|
|
break;
|
|
case '/':
|
|
result = num1 / num2;
|
|
break;
|
|
}
|
|
}
|
|
|
|
// 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;
|
|
}
|
|
|
|
|
|
|