From 56532263e37a6ab672a27ccef0e2ff5e136a6113 Mon Sep 17 00:00:00 2001 From: Mykyta Sadchenko Date: Wed, 27 Mar 2024 12:55:03 +0100 Subject: [PATCH] funguje --- a1/program.c | 102 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 102 insertions(+) create mode 100644 a1/program.c diff --git a/a1/program.c b/a1/program.c new file mode 100644 index 0000000..9ac68c5 --- /dev/null +++ b/a1/program.c @@ -0,0 +1,102 @@ +#include +#include +#include +#include +#include + + +char* check_math_problem(const char* problem) { + char* result_str = (char*)malloc(10 * sizeof(char)); + if (result_str == NULL) { + fprintf(stderr, "Error with memory\n"); + exit(EXIT_FAILURE); + } + + + //char* copy = strcmp(problem); + 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"; + } + + + 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"; + } + + + if (fabs(result - res) < 0.01) { + strcpy(result_str, "OK"); + } else { + strcpy(result_str, "ZLE"); + } + + free(copy); + return result_str; +} + +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; +}