Question: how would i make my program, be able to input an equation because its only giving me an awnser? Python help please! def infixToPostfix(infixexpr): prec

how would i make my program, be able to input an equation because its only giving me an awnser? Python help please!

def infixToPostfix(infixexpr):

prec = {} prec["*"] = 3 prec["/"] = 3 prec["+"] = 2 prec["-"] = 2 prec["("] = 1 opStack = Stack() postfixList = [] tokenList = infixexpr.split()

for token in tokenList: if token in "ABCDEFGHIJKLMNOPQRSTUVWXYZ" or token in "0123456789": postfixList.append(token) elif token == '(': opStack.push(token) elif token == ')': topToken = opStack.pop() while topToken != '(': postfixList.append(topToken) topToken = opStack.pop() else: while (not opStack.isEmpty()) and \ (prec[opStack.peek()] >= prec[token]): postfixList.append(opStack.pop()) opStack.push(token)

while not opStack.isEmpty(): postfixList.append(opStack.pop())

print(infixToPostfix("A * B + C * D"))

def postfix_evaluation(s): s=s.split() n=len(s) stack =[] for i in range(n): if s[i].isdigit(): stack.append(int(s[i])) elif s[i]=="+": a=stack.pop() b=stack.pop() stack.append(int(a)+int(b)) elif s[i]=="*": a=stack.pop() b=stack.pop() stack.append(int(a)*int(b)) elif s[i]=="/": a=stack.pop() b=stack.pop() stack.append(int(b)/int(a)) elif s[i]=="-": a=stack.pop() b=stack.pop() stack.append(int(b)-int(a))

return stack.pop() return " ".join(postfixList)

s="10 2 8 * + 3 -" val=postfix_evaluation(s) print(val)

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!