diff --git a/a1/program.c b/a1/program.c index 28d47d3..0323371 100644 --- a/a1/program.c +++ b/a1/program.c @@ -2,49 +2,33 @@ #include #include #include +#include #include bool check_spaces(const char* str) { int len = strlen(str); - bool is_operator = false; - bool is_operand = false; - for (int i = 0; i < len; ++i) { - char current_char = str[i]; - - - if (current_char == ' ') { - if (!is_operator && !is_operand) { + for (int i = 0; i < len - 1; ++i) { + if (str[i] == '+' || str[i] == '-' || str[i] == '*' || str[i] == '/') { + + if (i == 0 || str[i - 1] != ' ') + return false; + + if (str[i + 1] != ' ') return false; - } - } - - else if (current_char >= '0' && current_char <= '9') { - is_operand = true; - is_operator = false; - } - - else if (current_char == '+' || current_char == '-' || current_char == '*' || current_char == '/') { - is_operator = true; - is_operand = false; - } - - else { - return false; } } - if (!is_operand) { + if (str[len - 2] == '+' || str[len - 2] == '-' || str[len - 2] == '*' || str[len - 2] == '/') return false; - } return true; } char* check_math_problem(const char* problem) { if (!check_spaces(problem)) { - return "ZLE"; + return "CHYBA"; } if (strlen(problem) == 0) { @@ -75,7 +59,7 @@ char* check_math_problem(const char* problem) { break; case '/': if (fabs(n2) < 1e-9) { - return "ZLE"; + return "CHYBA"; } result = n1 / n2; break; @@ -104,7 +88,3 @@ int main() { return 0; } - - - -