This commit is contained in:
Sadchenko 2024-03-28 23:15:33 +01:00
parent 511967b3bf
commit 7cbecc2e83

View File

@ -2,49 +2,33 @@
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <stdbool.h> #include <stdbool.h>
#include <ctype.h>
#include <math.h> #include <math.h>
bool check_spaces(const char* str) { bool check_spaces(const char* str) {
int len = strlen(str); int len = strlen(str);
bool is_operator = false;
bool is_operand = false;
for (int i = 0; i < len; ++i) { for (int i = 0; i < len - 1; ++i) {
char current_char = str[i]; if (str[i] == '+' || str[i] == '-' || str[i] == '*' || str[i] == '/') {
if (i == 0 || str[i - 1] != ' ')
if (current_char == ' ') { return false;
if (!is_operator && !is_operand) {
if (str[i + 1] != ' ')
return false; 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 false;
}
return true; return true;
} }
char* check_math_problem(const char* problem) { char* check_math_problem(const char* problem) {
if (!check_spaces(problem)) { if (!check_spaces(problem)) {
return "ZLE"; return "CHYBA";
} }
if (strlen(problem) == 0) { if (strlen(problem) == 0) {
@ -75,7 +59,7 @@ char* check_math_problem(const char* problem) {
break; break;
case '/': case '/':
if (fabs(n2) < 1e-9) { if (fabs(n2) < 1e-9) {
return "ZLE"; return "CHYBA";
} }
result = n1 / n2; result = n1 / n2;
break; break;
@ -104,7 +88,3 @@ int main() {
return 0; return 0;
} }