From 8b9cc212e675d5d5fdc38aaeeda1ec6ea7e18ad8 Mon Sep 17 00:00:00 2001 From: Denys Sanchuk Date: Tue, 11 Mar 2025 13:32:59 +0000 Subject: [PATCH] Aktualizovat du3/program.c --- du3/program.c | 94 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 94 insertions(+) diff --git a/du3/program.c b/du3/program.c index 8b13789..80b91b0 100644 --- a/du3/program.c +++ b/du3/program.c @@ -1 +1,95 @@ +#include +#include +#include +#include +#include +void strip_spaces(char *str) { + char temp[1000]; + int j = 0; + int i = 0; + + while (str[i]) { + if (isspace(str[i]) && (j == 0 || isspace(temp[j - 1]) || temp[j - 1] == '=' || temp[j - 1] == '+' || temp[j - 1] == '-' || temp[j - 1] == '*' || temp[j - 1] == '/')) { + i++; + } else { + temp[j++] = str[i++]; + } + } + temp[j] = '\0'; + strcpy(str, temp); +} + +int parse_equation(char *line, double *num1, double *num2, char *op, double *result) { + strip_spaces(line); + + char *ptr = line; + char *endptr; + + *num1 = strtod(ptr, &endptr); + if (ptr == endptr) return 0; + ptr = endptr; + + while (isspace(*ptr)) ptr++; + if (*ptr != '+' && *ptr != '-' && *ptr != '*' && *ptr != '/') return 0; + *op = *ptr; + ptr++; + + while (isspace(*ptr)) ptr++; + *num2 = strtod(ptr, &endptr); + if (ptr == endptr) return 0; + ptr = endptr; + + while (isspace(*ptr)) ptr++; + if (*ptr != '=') return 0; + ptr++; + + while (isspace(*ptr)) ptr++; + *result = strtod(ptr, &endptr); + if (ptr == endptr) return 0; + while (isspace(*endptr)) endptr++; + if (*endptr != '\0' && *endptr != '\n') return 0; + + return 1; +} + +double calculate(double num1, double num2, char op) { + switch (op) { + case '+': return num1 + num2; + case '-': return num1 - num2; + case '*': return num1 * num2; + case '/': return num1 / num2; + default: return NAN; + } +} + +double round_to_2(double value) { + return round(value * 100) / 100; +} + +int main() { + char line[1000]; + + while (fgets(line, sizeof(line), stdin)) { + if (line[0] == '\n') break; + + double num1, num2, expected_result; + char op; + + if (!parse_equation(line, &num1, &num2, &op, &expected_result)) { + printf("CHYBA\n"); + continue; + } + + double actual_result = calculate(num1, num2, op); + actual_result = round_to_2(actual_result); + + if (actual_result == expected_result) { + printf("OK\n"); + } else { + printf("ZLE\n"); + } + } + + return 0; +} \ No newline at end of file