Reference no: EM132213276
Question :
Write a recursive MARS program that can compute the value of an expression in parentheses.
The program should accept an input string, sequentially examine each character, invoke the procedure calc_expr every time it sees a new open parenthesis, return from that procedure when the parenthesis is closed, and print the answer when the outermost parenthesis is closed.
We will only test with valid inputs including decimal integers, spaces, and +, -, *, / operators.
The program runs until the user enters a string starting with "e". Within parentheses, the expression is evaluated from left to right, i.e., (6/2+7/5) does 6/2 (which is 3), 3+7 (which is 10), 10/5 (which is 2). Here's an example run of the program:
Enter the expression you want evaluated:
((4+3)-((27/9)*2))
The answer is: 1
Enter the expression you want evaluated:
(1*(2 * (3 * (4 * 5))))
The answer is: 120
Enter the expression you want evaluated:
exit