From f875ace0782256ca34c5b2f7ff45c822471e1c3d Mon Sep 17 00:00:00 2001 From: VIliam Date: Thu, 31 Oct 2024 20:02:33 +0100 Subject: [PATCH] s --- a1/program.c | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/a1/program.c b/a1/program.c index 2b68a48..19e0a13 100644 --- a/a1/program.c +++ b/a1/program.c @@ -2,7 +2,8 @@ #include 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; -} \ No newline at end of file +}