This commit is contained in:
Džubara 2024-10-31 20:02:33 +01:00
parent d8c2ae1115
commit f875ace078

View File

@ -2,7 +2,8 @@
#include <string.h>
int match(char open, char close) {
return (open == '{' && close == '}') || (open == '(' && close == ')');
return (open == '{' && close == '}') || (open == '[' && close == ']') ||
(open == '(' && close == ')');
}
void check_brackets(const char *code) {
@ -13,13 +14,15 @@ void check_brackets(const char *code) {
for (int i = 0; code[i] != '\0'; i++) {
char c = code[i];
if (c == '{' || c == '(') {
if (top < 99) {
stack[++top] = c;
if (c == '{' || c == '[' || c == '(') {
stack[++top] = c;
} else if (c == '}' || c == ']' || c == ')') {
if (top == -1) {
printf("Unexpected closing bracket %c at position %d\n", c, i);
return;
}
} else if (c == '}' || c == ')') {
if (top == -1 || !match(stack[top], c)) {
printf("Error at %d\n", i);
if (!match(stack[top], c)) {
printf("Crossed bracket %c in %d, expected %c\n", c, i, stack[top]);
return;
}
top--;
@ -28,7 +31,7 @@ void check_brackets(const char *code) {
if (top == -1) {
printf("All brackets OK\n");
} else {
printf("Unmatched brackets\n");
printf("Unmatched opening bracket %c\n", stack[top]);
}
}
@ -38,4 +41,4 @@ int main() {
fgets(code, 100, stdin);
check_brackets(code);
return 0;
}
}