From d54ea6c6c804b872cede6fb159a78724d54ce3ae Mon Sep 17 00:00:00 2001 From: Marat Izmailov Date: Thu, 31 Oct 2024 20:46:10 +0000 Subject: [PATCH] Update a1/program.c --- a1/program.c | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) 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; }