diff --git a/a1/program.c b/a1/program.c index 08056c0..9e51074 100644 --- a/a1/program.c +++ b/a1/program.c @@ -12,6 +12,14 @@ int isMatching(char opening, char closing) { (opening == '<' && closing == '>'); } +// Function to get the expected closing bracket for a given opening bracket +char getExpectedClosingBracket(char opening) { + return (opening == '{') ? '}' : + (opening == '[') ? ']' : + (opening == '(') ? ')' : + (opening == '<') ? '>' : '\0'; +} + int main() { char input[MAX_LENGTH + 1]; char stack[MAX_LENGTH]; @@ -54,8 +62,7 @@ int main() { if (!isMatching(stack[top--], current)) { printf("Read: %s\n", input); printf("Crossed bracket %c in %d, expected %c.\n", current, position, - (current == '}') ? '{' : (current == ']') ? '[' : - (current == ')') ? '(' : '<'); + getExpectedClosingBracket(stack[top + 1])); return 0; } } @@ -64,7 +71,7 @@ int main() { // If there are unmatched opening brackets left in the stack if (top != -1) { printf("Read: %s\n", input); - printf("Unexpected end of input, expected closing bracket for %c.\n", stack[top]); + printf("Missing closing brackets: %c\n", getExpectedClosingBracket(stack[top])); return 0; }