2024-03-27 11:55:03 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdbool.h>
|
2024-03-28 22:15:33 +00:00
|
|
|
#include <ctype.h>
|
2024-03-27 11:55:03 +00:00
|
|
|
#include <math.h>
|
|
|
|
|
2024-03-28 22:03:39 +00:00
|
|
|
bool check_spaces(const char* str) {
|
|
|
|
int len = strlen(str);
|
|
|
|
|
2024-03-28 22:15:33 +00:00
|
|
|
for (int i = 0; i < len - 1; ++i) {
|
|
|
|
if (str[i] == '+' || str[i] == '-' || str[i] == '*' || str[i] == '/') {
|
|
|
|
|
|
|
|
if (i == 0 || str[i - 1] != ' ')
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (str[i + 1] != ' ')
|
2024-03-28 22:03:39 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2024-03-28 22:15:33 +00:00
|
|
|
if (str[len - 2] == '+' || str[len - 2] == '-' || str[len - 2] == '*' || str[len - 2] == '/')
|
2024-03-28 22:03:39 +00:00
|
|
|
return false;
|
|
|
|
|
2024-03-28 22:30:40 +00:00
|
|
|
|
|
|
|
if (isdigit(str[len - 2]) && str[len - 1] == ' ')
|
|
|
|
return false;
|
|
|
|
|
2024-03-28 22:03:39 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2024-03-28 21:45:01 +00:00
|
|
|
char* check_math_problem(const char* problem) {
|
2024-03-28 22:03:39 +00:00
|
|
|
if (!check_spaces(problem)) {
|
2024-03-28 22:15:33 +00:00
|
|
|
return "CHYBA";
|
2024-03-28 22:03:39 +00:00
|
|
|
}
|
|
|
|
|
2024-03-28 21:45:01 +00:00
|
|
|
if (strlen(problem) == 0) {
|
2024-03-27 11:55:03 +00:00
|
|
|
return "CHYBA";
|
|
|
|
}
|
2024-03-28 21:45:01 +00:00
|
|
|
|
2024-03-27 11:55:03 +00:00
|
|
|
double n1, n2, res;
|
|
|
|
char sing, equel;
|
2024-03-28 21:45:01 +00:00
|
|
|
|
|
|
|
if(sscanf(problem, "%lf %c %lf %c %lf", &n1, &sing, &n2, &equel, &res) != 5) {
|
2024-03-27 11:55:03 +00:00
|
|
|
return "CHYBA";
|
|
|
|
}
|
|
|
|
|
2024-03-28 22:03:39 +00:00
|
|
|
if (equel != '=') {
|
|
|
|
return "CHYBA";
|
|
|
|
}
|
|
|
|
|
2024-03-28 22:30:40 +00:00
|
|
|
// Проверяем, что после результата нет пробела
|
|
|
|
if (strchr(problem, '=') - strchr(problem, ' ') == 2) {
|
|
|
|
return "CHYBA";
|
|
|
|
}
|
|
|
|
|
2024-03-27 11:55:03 +00:00
|
|
|
double result;
|
|
|
|
switch (sing) {
|
|
|
|
case '+':
|
|
|
|
result = n1 + n2;
|
|
|
|
break;
|
|
|
|
case '-':
|
|
|
|
result = n1 - n2;
|
|
|
|
break;
|
|
|
|
case '*':
|
|
|
|
result = n1 * n2;
|
|
|
|
break;
|
|
|
|
case '/':
|
|
|
|
if (fabs(n2) < 1e-9) {
|
2024-03-28 22:15:33 +00:00
|
|
|
return "CHYBA";
|
2024-03-27 11:55:03 +00:00
|
|
|
}
|
|
|
|
result = n1 / n2;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return "CHYBA";
|
|
|
|
}
|
2024-03-28 21:45:01 +00:00
|
|
|
|
2024-03-27 11:55:03 +00:00
|
|
|
if (fabs(result - res) < 0.01) {
|
2024-03-27 12:04:36 +00:00
|
|
|
return "OK";
|
2024-03-27 11:55:03 +00:00
|
|
|
} else {
|
2024-03-27 12:04:36 +00:00
|
|
|
return "ZLE";
|
2024-03-27 11:55:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int main() {
|
|
|
|
char problem[100];
|
|
|
|
|
|
|
|
while (true) {
|
|
|
|
fgets(problem, sizeof(problem), stdin);
|
2024-03-28 21:45:01 +00:00
|
|
|
if (strlen(problem) <= 1) {
|
2024-03-27 11:55:03 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
printf("%s\n", check_math_problem(problem));
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2024-03-28 22:03:39 +00:00
|
|
|
|
2024-03-28 22:30:40 +00:00
|
|
|
|
2024-03-28 22:43:04 +00:00
|
|
|
|