2024-03-27 11:55:03 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdbool.h>
|
|
|
|
#include <math.h>
|
|
|
|
|
|
|
|
|
2024-03-27 12:04:36 +00:00
|
|
|
char* check_math_problem(const char* problem)
|
|
|
|
{
|
2024-03-27 11:55:03 +00:00
|
|
|
|
2024-03-27 12:04:36 +00:00
|
|
|
//char* copy;
|
2024-03-27 11:55:03 +00:00
|
|
|
char* copy = (char*)malloc((strlen(problem) + 1) * sizeof(char));
|
2024-03-27 12:04:36 +00:00
|
|
|
strcpy(copy, problem);
|
2024-03-27 11:55:03 +00:00
|
|
|
if (copy == NULL) {
|
|
|
|
fprintf(stderr, "Error with memory\n");
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
char* src = copy;
|
|
|
|
char* dst = copy;
|
|
|
|
while (*src != '\0') {
|
|
|
|
if (*src != ' ') {
|
|
|
|
*dst++ = *src;
|
|
|
|
}
|
|
|
|
src++;
|
|
|
|
}
|
|
|
|
*dst = '\0';
|
|
|
|
|
|
|
|
|
|
|
|
if (strlen(copy) == 0) {
|
|
|
|
free(copy);
|
|
|
|
return "CHYBA";
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
double n1, n2, res;
|
|
|
|
char sing, equel;
|
|
|
|
|
|
|
|
if(sscanf(copy, "%lf%c%lf%c%lf", &n1, &sing, &n2, &equel, &res) !=5)
|
|
|
|
{
|
|
|
|
free(copy);
|
|
|
|
return "CHYBA";
|
|
|
|
}
|
|
|
|
// printf("%lf%c%lf%c%lf", n1, sing, n2, equel, res);
|
|
|
|
//return "1234";
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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) {
|
|
|
|
free(copy);
|
|
|
|
return "ZLE";
|
|
|
|
}
|
|
|
|
result = n1 / n2;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
free(copy);
|
|
|
|
return "CHYBA";
|
|
|
|
}
|
2024-03-27 12:04:36 +00:00
|
|
|
free(copy);
|
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
|
|
|
}
|
|
|
|
|
2024-03-27 12:04:36 +00:00
|
|
|
return "0";
|
|
|
|
|
2024-03-27 11:55:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|