2024-03-20 16:11:09 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <math.h>
|
|
|
|
|
|
|
|
#define BUFFER_SIZE 100
|
|
|
|
|
|
|
|
// Funkcia na vyhodnotenie výrazu
|
|
|
|
char* evaluate_expression(char* expression) {
|
|
|
|
double num1, num2, expected_result, result;
|
|
|
|
char operator;
|
|
|
|
|
|
|
|
// Načítanie čísel a operátora zo vstupného reťazca
|
|
|
|
if (sscanf(expression, "%lf %c %lf = %lf", &num1, &operator, &num2, &expected_result) != 4) {
|
|
|
|
return "CHYBA";
|
|
|
|
}
|
|
|
|
|
2024-03-20 16:14:16 +00:00
|
|
|
// Kontrola delenia nulou
|
2024-03-20 16:22:05 +00:00
|
|
|
if ((operator == '/') && (fabs(num2) < 0.000001)) {
|
2024-03-20 16:14:16 +00:00
|
|
|
return "ZLE";
|
|
|
|
}
|
|
|
|
|
2024-03-20 16:11:09 +00:00
|
|
|
// Vykonanie operácie a porovnanie s očakávaným výsledkom
|
|
|
|
switch (operator) {
|
|
|
|
case '+':
|
|
|
|
result = num1 + num2;
|
|
|
|
break;
|
|
|
|
case '-':
|
|
|
|
result = num1 - num2;
|
|
|
|
break;
|
|
|
|
case '*':
|
|
|
|
result = num1 * num2;
|
|
|
|
break;
|
|
|
|
case '/':
|
|
|
|
result = num1 / num2;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return "CHYBA";
|
|
|
|
}
|
|
|
|
|
2024-03-20 16:18:08 +00:00
|
|
|
// Zaokrúhlenie výsledku na dve desatinné miesta
|
2024-03-20 16:22:05 +00:00
|
|
|
result = round(result * 100.0) / 100.0;
|
2024-03-20 16:18:08 +00:00
|
|
|
|
2024-03-20 16:22:05 +00:00
|
|
|
// Porovnanie výsledku so skutočným výsledkom
|
|
|
|
if (result == expected_result) {
|
2024-03-20 16:11:09 +00:00
|
|
|
return "OK";
|
|
|
|
} else {
|
|
|
|
return "ZLE";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int main() {
|
|
|
|
char buffer[BUFFER_SIZE];
|
|
|
|
|
|
|
|
// Načítanie vstupných úloh a ich vyhodnotenie
|
|
|
|
while (fgets(buffer, BUFFER_SIZE, stdin)) {
|
|
|
|
// Odstránenie znaku nového riadku
|
|
|
|
buffer[strcspn(buffer, "\n")] = '\0';
|
|
|
|
|
|
|
|
// Ukončenie načítavania, ak je riadok prázdny
|
|
|
|
if (strlen(buffer) == 0) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
printf("%s\n", evaluate_expression(buffer));
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|