Question
Design a recursive algorithm to evaluate arithmetic expressions that consist of integer numbers, opening and closing parentheses and the operators +, -, * and /.
Design a recursive algorithm to evaluate arithmetic expressions that consist of integer numbers, opening and closing parentheses and the operators +, -, * and /. Here / is integer division. A well-defined arithmetic expression is defined as: a digit d {0, 1, 2, 3, 4, 5, 6, 7, 8, 9} is a well-formed arithmetic expression. If X and Y are well-formed arithmetic expressions, then the four expressions (X + Y), (X - Y), (X * Y) and (X / Y) are also well-formed arithmetic expressions. Examples of well-formed arithmetic expressions are "((8+7)*3)", "(4-(9-2))" and "0". However, the expressions "3+) 2 (())", "(9 + ())", "-4", "5-8", "108" or "(8)" are not well-formed arithmetic expressions. Your algorithm should check whether an input string is a well-formed arithmetic expression. If it is not well-formed then your algorithm must return a constant NOTWELLFORMED, otherwise it must evaluate the expression and return the integer result. Your algorithm does not have to deal with division by 0. You may assume that the input string is an array or list of n characters.
Your algorithm must be written in unambiguous pseudocode
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started