#include #include #include #define MAX_SIZE 1000 char getExpectedClosing(char open) { switch (open) { case '(': return ')'; case '[': return ']'; case '{': return '}'; case '<': return '>'; default: return '?'; } } bool isOpening(char c) { return c == '(' || c == '[' || c == '{' || c == '<'; } bool isClosing(char c) { return c == ')' || c == ']' || c == '}' || c == '>'; } char getExpectedOpening(char close) { switch (close) { case ')': return '('; case ']': return '['; case '}': return '{'; case '>': return '<'; default: return '?'; } } int main() { char input[MAX_SIZE]; if (fgets(input, MAX_SIZE, stdin) != NULL) { input[strcspn(input, "\n")] = '\0'; printf("Read: %s\n", input); char stack[MAX_SIZE]; int top = -1; for (int i = 0; input[i] != '\0'; i++) { char c = input[i]; if (isOpening(c)) { stack[++top] = c; } else if (isClosing(c)) { if (top == -1) { printf("Unexpected closing bracket %c in %d\n", c, i); return 0; } char last = stack[top--]; if (getExpectedClosing(last) != c) { printf("Crossed bracket %c in %d, expected %c \n", c, i , getExpectedClosing(last)); return 0; } } } if (top != -1) { printf("Missing closing brackets: "); for (int i = top; i >= 0; i--) { printf("%c", getExpectedClosing(stack[i])); } printf("\n"); } else { printf("All brackets OK\n"); } } else { printf("Error reading input\n"); } return 0; }