Question: Write a python program. This program will involve arithmetic expressions in both inx format and postx format. Inx expressions are the ordinary arithmetic expressions we
Write a python program. This program will involve arithmetic expressions in both inx format and postx format. Inx expressions are the ordinary arithmetic expressions we are familiar with. The arithmetic operators are unary minus, unary plus, multiplication, division, modulus, addition, and subtraction. Unary plus and unary minus have just one operand, which follows the operator. Some examples are +1 and -3. The binary operators are surrounded by their operands. Some examples are 5 - 3 and 3 * 4. The precedence of these operators are the same as they are in C++ and in mathematics:
Operator Priority Associativity
unary -, unary + Highest Right to Left
*, /, % Next Highest Left to Right
binary +, binary - Lowest Left to Right
The rst part of this program will be to convert an inx expression into a postx expression. To do this, we will need to use a stack. The list type will do nicely. We can push an element onto the stack by simply using the + operator. we can pop an element from the stack by slicing: if (len(list) != 0):
top = list[len(list) - 1]
stack = stack[:-1]
return top As we scan the inx expression from left to right, when we encounter an operand, we will send it to the end of the postx output. Operators and the left parenthesis can be pushed onto the stack. Right parentheses will never go on the stack or to the output, but will cause an action.
Part 2: The second part of the program is to take an inx expression and evaluate it. This time, operands will be pushed on the stack, as we scan the string from left to right. An operator will cause popping of an operand or operands from the stack and the action of the operator will be applied to the operand(s). When the end of the postx expression is reached, the stack will have a single operand which is the answer. Program Requirements: This program will be menu driven and the menu choices will be:
1. Convert an inx expression to its equivalent postx expression form
2. Evaluate a postx expression
3. Quit the program
Use a loop to continuously prompt the user for choices 1 and 2 until the user decides to quit. All output will be displayed on the terminal screen. Your program should check for an unmatched parentheses errors.
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
