This commit is contained in:
Džubara 2024-11-01 00:00:05 +01:00
parent 9d79f9d77b
commit d14a4c9508

View File

@ -6,11 +6,19 @@ int match(char open, char close) {
(open == '(' && close == ')') || (open == '<' && close == '>');
}
char expected_closing(char open) {
if (open == '{') return '}';
if (open == '[') return ']';
if (open == '(') return ')';
if (open == '<') return '>';
return '\0';
}
void check_brackets(const char *code) {
char stack[100];
int top = -1;
printf("Read: %s\n", code);
printf("Read: %s\n", code);
for (int i = 0; code[i] != '\0'; i++) {
char c = code[i];
@ -22,18 +30,19 @@ void check_brackets(const char *code) {
return;
}
if (!match(stack[top], c)) {
printf("Crossed bracket %c in %d, expected %c\n", c, i, stack[top]);
printf("Crossed bracket %c in %d, expected %c\n", c, i, expected_closing(stack[top]));
return;
}
top--;
}
}
if (top == -1) {
printf("All brackets OK\n");
} else {
printf("Missing closing brackets: ");
while (top >= 0) {
printf("%c", stack[top--]);
printf("%c", expected_closing(stack[top--]));
}
printf("\n");
}