diff --git a/a1/prog b/a1/prog index 6c5e973..521b61e 100755 Binary files a/a1/prog and b/a1/prog differ diff --git a/a1/program.c b/a1/program.c index 0c360ce..89ffff5 100644 --- a/a1/program.c +++ b/a1/program.c @@ -56,42 +56,47 @@ char getExpectedClosing(char open) { default: return '?'; } } - int main() { char input[MAX_SIZE]; if (fgets(input, MAX_SIZE, stdin) != NULL) { input[strcspn(input, "\n")] = '\0'; - printf("Read: %s\n", input); // Додано "Read: " + printf("Read: %s\n", input); int err = 0; if (is_valid_brackets(input, &err)) { - printf("All brackets OK\n"); // Змінено з "True" + printf("All brackets OK\n"); } else { char stack[MAX_SIZE]; int top = -1; - for (int i = 0; i < strlen(input) ; i++) { + + for (int i = 0; i < err; i++) { char c = input[i]; if (c == '(' || c == '[' || c == '{' || c == '<') { stack[++top] = c; } else if (c == ')' || c == ']' || c == '}' || c == '>') { - top--; + if (top >= 0 && c == getExpectedClosing(stack[top])) { + top--; + } else { + printf("Crossed bracket %c in %d, expected %c\n", + c, err, (top >= 0) ? getExpectedClosing(stack[top]) : '?'); + return 0; + } } } + char c = input[err - 1]; if (c == ')' || c == ']' || c == '}' || c == '>') { - if (top <-1) { - printf("Unexpected closing bracket %c in %d\n", c, err-1); - } else { - printf("Crossed bracket %c in %d, expected %c \n", c, err-1, getExpectedClosing(stack[err-2])); + if (top < -1) { + printf("Unexpected closing bracket %c in %d\n", c, err); } } else { - printf("Missing closing brackets: "); + printf("Missing closing brackets: "); for (int i = top; i > -1; i--) { printf("%c", getExpectedClosing(stack[i])); } printf("\n"); - } + } } } else { printf("Error reading input\n");