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