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 <string.h>
#include <stdbool.h>
#include <ctype.h>
#include <math.h>
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];
for (int i = 0; i < len - 1; ++i) {
if (str[i] == '+' || str[i] == '-' || str[i] == '*' || str[i] == '/') {
if (current_char == ' ') {
if (!is_operator && !is_operand) {
if (i == 0 || 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 {
if (str[i + 1] != ' ')
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;
}