Write a python program. This program will involve arithmetic expressions in both
ID: 3803436 • Letter: W
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 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 RightThe 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. 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.
Explanation / Answer
class Stack:
def __init__(self):
self.items = []
def isEmpty(self):
return self.items == []
def push(self, item):
self.items.insert(0,item)
def pop(self):
return self.items.pop(0)
def peek(self):
return self.items[0]
def size(self):
return len(self.items)
def infixToPostfix(Infix):
precedence = {}
precedence["^"] = 4
precedence["*"] = 3
precedence["/"] = 3
precedence["+"] = 2
precedence["-"] = 2
precedence["("] = 1
ObjStack = Stack()
PostfixString = []
tList = Infix.split()
for t in tList:
if t in "ABCDEFGHIJKLMNOPQRSTUVWXYZ" or t in "0123456789":
PostfixString.append(t)
elif t == '(':
ObjStack.push(t)
elif t == ')':
topt = ObjStack.pop()
while topt != '(':
PostfixString.append(topt)
topt = ObjStack.pop()
else:
while (not ObjStack.isEmpty()) and
(precedence[ObjStack.peek()] >= precedence[t]):
PostfixString.append(ObjStack.pop())
ObjStack.push(t)
while not ObjStack.isEmpty():
PostfixString.append(ObjStack.pop())
return " ".join(PostfixString)
def postfixEval(Postfix):
operandStack = Stack()
tList = Postfix.split()
for t in tList:
if t in "0123456789":
operandStack.push(int(t))
else:
operand2 = operandStack.pop()
operand1 = operandStack.pop()
result = Arithmatic(t,operand1,operand2)
operandStack.push(result)
return operandStack.pop()
def Arithmatic(op, op1, op2):
if op == "^":
return op1 ** op2
if op == "*":
return op1 * op2
elif op == "/":
return op1 / op2
elif op == "+":
return op1 + op2
else:
return op1 - op2
while True:
Infi=raw_input("Enter Infix expression")
stt=infixToPostfix(Infi)
print "Postfix of Infix is ",stt
print "Postfix evaluation is",postfixEval(stt)
=========================================================
output:
python ak.py
Enter Infix expression 5 + 3
Postfix of Infix is 5 3 +
Postfix evaluation is 8
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.