diff --git a/a1/program.c b/a1/program.c index 7147ee0..9fa8e48 100644 --- a/a1/program.c +++ b/a1/program.c @@ -10,9 +10,14 @@ int is_matching_pair(char open, char close) { (open == '<' && close == '>'); } +char matching_close(char open) { + return open == '(' ? ')' : + open == '{' ? '}' : + open == '[' ? ']' : '>'; +} + void check_brackets(const char *code) { char stack[MAX_LEN]; - int positions[MAX_LEN]; int top = -1; printf("Read: %s\n", code); @@ -23,7 +28,6 @@ void check_brackets(const char *code) { if (ch == '(' || ch == '{' || ch == '[' || ch == '<') { if (top < MAX_LEN - 1) { stack[++top] = ch; - positions[top] = i; } else { printf("Stack overflow at position %d\n", i); return; @@ -35,10 +39,7 @@ void check_brackets(const char *code) { return; } if (!is_matching_pair(stack[top], ch)) { - printf("Crossed bracket %c in %d, expected %c\n", ch, i, - stack[top] == '(' ? ')' : - stack[top] == '{' ? '}' : - stack[top] == '[' ? ']' : '>'); + printf("Crossed bracket %c in %d, expected %c\n", ch, i, matching_close(stack[top])); return; } top--; @@ -46,10 +47,11 @@ void check_brackets(const char *code) { } if (top != -1) { - printf("Missing closing brackets: %c\n", - stack[top] == '(' ? ')' : - stack[top] == '{' ? '}' : - stack[top] == '[' ? ']' : '>'); + printf("Missing closing brackets: "); + while (top != -1) { + printf("%c", matching_close(stack[top--])); + } + printf("\n"); } else { printf("All brackets OK\n"); }