diff --git a/du3/program.c b/du3/program.c index eea3a0d..8b13789 100644 --- a/du3/program.c +++ b/du3/program.c @@ -1,95 +1 @@ -#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; -}