Update a1/program.c

This commit is contained in:
Marat Izmailov 2024-10-31 20:46:10 +00:00
parent e88adeb6e1
commit d54ea6c6c8

View File

@ -12,6 +12,14 @@ int isMatching(char opening, char closing) {
(opening == '<' && 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() { int main() {
char input[MAX_LENGTH + 1]; char input[MAX_LENGTH + 1];
char stack[MAX_LENGTH]; char stack[MAX_LENGTH];
@ -54,8 +62,7 @@ int main() {
if (!isMatching(stack[top--], current)) { if (!isMatching(stack[top--], current)) {
printf("Read: %s\n", input); printf("Read: %s\n", input);
printf("Crossed bracket %c in %d, expected %c.\n", current, position, printf("Crossed bracket %c in %d, expected %c.\n", current, position,
(current == '}') ? '{' : (current == ']') ? '[' : getExpectedClosingBracket(stack[top + 1]));
(current == ')') ? '(' : '<');
return 0; return 0;
} }
} }
@ -64,7 +71,7 @@ int main() {
// If there are unmatched opening brackets left in the stack // If there are unmatched opening brackets left in the stack
if (top != -1) { if (top != -1) {
printf("Read: %s\n", input); 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; return 0;
} }