This commit is contained in:
Sadchenko 2024-03-28 22:45:01 +01:00
parent e8dc682eeb
commit 4ce91bfee7

View File

@ -4,48 +4,17 @@
#include <stdbool.h> #include <stdbool.h>
#include <math.h> #include <math.h>
char* check_math_problem(const char* problem) {
char* check_math_problem(const char* problem) if (strlen(problem) == 0) {
{
//char* copy;
char* copy = (char*)malloc((strlen(problem) + 1) * sizeof(char));
strcpy(copy, problem);
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"; return "CHYBA";
} }
double n1, n2, res; double n1, n2, res;
char sing, equel; char sing, equel;
if(sscanf(copy, "%lf%c%lf%c%lf", &n1, &sing, &n2, &equel, &res) !=5) if(sscanf(problem, "%lf %c %lf %c %lf", &n1, &sing, &n2, &equel, &res) != 5) {
{
free(copy);
return "CHYBA"; return "CHYBA";
} }
// printf("%lf%c%lf%c%lf", n1, sing, n2, equel, res);
//return "1234";
double result; double result;
switch (sing) { switch (sing) {
@ -60,34 +29,27 @@ char* check_math_problem(const char* problem)
break; break;
case '/': case '/':
if (fabs(n2) < 1e-9) { if (fabs(n2) < 1e-9) {
free(copy);
return "ZLE"; return "ZLE";
} }
result = n1 / n2; result = n1 / n2;
break; break;
default: default:
free(copy);
return "CHYBA"; return "CHYBA";
} }
free(copy);
if (fabs(result - res) < 0.01) { if (fabs(result - res) < 0.01) {
return "OK"; return "OK";
} else { } else {
return "ZLE"; return "ZLE";
} }
return "0";
} }
int main() { int main() {
char problem[100]; char problem[100];
while (true) { while (true) {
fgets(problem, sizeof(problem), stdin); fgets(problem, sizeof(problem), stdin);
if (strlen(problem) == 1) { if (strlen(problem) <= 1) {
break; break;
} }
printf("%s\n", check_math_problem(problem)); printf("%s\n", check_math_problem(problem));