102 lines
2.1 KiB
C
102 lines
2.1 KiB
C
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include <stdbool.h>
|
|
#include <ctype.h>
|
|
#include <math.h>
|
|
|
|
bool check_spaces(const char* str) {
|
|
int len = strlen(str);
|
|
|
|
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] != ' ')
|
|
return false;
|
|
}
|
|
}
|
|
|
|
|
|
if (str[len - 2] == '+' || str[len - 2] == '-' || str[len - 2] == '*' || str[len - 2] == '/')
|
|
return false;
|
|
|
|
|
|
if (isdigit(str[len - 2]) && str[len - 1] == ' ')
|
|
return false;
|
|
|
|
return true;
|
|
}
|
|
|
|
char* check_math_problem(const char* problem) {
|
|
if (!check_spaces(problem)) {
|
|
return "CHYBA";
|
|
}
|
|
|
|
if (strlen(problem) == 0) {
|
|
return "CHYBA";
|
|
}
|
|
|
|
double n1, n2, res;
|
|
char sing, equel;
|
|
|
|
if(sscanf(problem, "%lf %c %lf %c %lf", &n1, &sing, &n2, &equel, &res) != 5) {
|
|
return "CHYBA";
|
|
}
|
|
|
|
if (equel != '=') {
|
|
return "CHYBA";
|
|
}
|
|
|
|
// Проверяем, что после результата нет пробела
|
|
if (strchr(problem, '=') - strchr(problem, ' ') == 2) {
|
|
return "CHYBA";
|
|
}
|
|
|
|
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) {
|
|
return "CHYBA";
|
|
}
|
|
result = n1 / n2;
|
|
break;
|
|
default:
|
|
return "CHYBA";
|
|
}
|
|
|
|
if (fabs(result - res) < 0.01) {
|
|
return "OK";
|
|
} else {
|
|
return "ZLE";
|
|
}
|
|
}
|
|
|
|
int main() {
|
|
char problem[100];
|
|
|
|
while (true) {
|
|
fgets(problem, sizeof(problem), stdin);
|
|
if (strlen(problem) <= 1) {
|
|
break;
|
|
}
|
|
printf("%s\n", check_math_problem(problem));
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
|
|
|
|
|