This commit is contained in:
Nataliia Kobryn 2025-10-20 19:37:43 +02:00
parent 8d9bc08d2f
commit a4d2f90f18
2 changed files with 16 additions and 11 deletions

BIN
a1/prog

Binary file not shown.

View File

@ -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");