Question: 1. A problem in elementary algebra is to decide if an expression containing several kinds of brackets, such as {}, [], is correctly bracketed. This
1. A problem in elementary algebra is to decide if an expression containing several kinds of brackets, such as {}, [], is correctly bracketed. This is the case if a) there are the same number of left and right brackets of each kind, and
b) when a right bracket appears, the most recent preceding left bracket should be of the same type. For example: (20 points)
[a + b - {c + d} x e] : correctly bracketed
[a + b - {c + d] x e] : not correct
Write a program that
a) Shows a message, Enter an algebraic expression: and lets the user type in an algebraic expression, ending with a carriage return, that
contains each kind of brackets as stated above.
b) As the expression is being typed in, the program evaluates each character. If the expression is typed correctly, the program displays
Expression is correct and the program is terminated. If at any point
the expression is incorrectly bracketed (mismatch between left and
right bracket), the program terminates and shows a message
Incorrect expression.
| Example 1 | Example 2 |
| Enter an algebraic expression: [a + b - {c + a] Incorrect expression. | Enter an algebraic expression: [a + {b - c} x f ] - h Expression is correct. |
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
