From d14a4c950872d5845dd10935f16ff3b4b1b44e44 Mon Sep 17 00:00:00 2001 From: VIliam Date: Fri, 1 Nov 2024 00:00:05 +0100 Subject: [PATCH] s --- a1/program.c | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/a1/program.c b/a1/program.c index 05fae37..ca09c6e 100644 --- a/a1/program.c +++ b/a1/program.c @@ -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"); }