In: Computer Science
1. Please program the following in Python 3 code.
2. Please share your code.
3. Please show all outputs.
Instructions:
Run Python code List as Stack and verify the following calculations; submit screen shots in a single file.
Postfix Expression Result
4 5 7 2 + - * = -16
3 4 + 2 * 7 / = 2
5 7 + 6 2 - * = 48
4 2 3 5 1 - + * + = 18
List as Stack Code:
""" File: pyStackPostfix.py Author: JD """ # 5 7 + 6 2 - * = 48 print("Postfix Calculator\n") stack = [] # Empty stack y = int(0) z = int(0) w = int(0) while True: x = input("Enter a postfix expression one by one:") if x >= '0' and x<= '9': x = int(x) stack.append(x) # Push on top of stack elif x == '+': # Got an operator if len(stack) >= 2: y = stack.pop() # Pop from top of stack z = stack.pop() w = y + z stack.append(w) # Push result back else: print("Stack error") # Not enough operhand A + B break elif x == '-': if len(stack) >= 2: y = stack.pop() z = stack.pop() w = y - z stack.append(w) else: print("Stack error") break elif x == '*': if len(stack) >= 2: y = stack.pop() z = stack.pop() w = y * z stack.append(w) else: print("Stack error") break elif x == '/': if len(stack) >= 2: y = stack.pop() z = stack.pop() w = y / z stack.append(w) else: print("Stack error") break elif x == '=': # Equal operator if len(stack) == 1: z = stack.pop() # Pop the result print (z) break else: print("Stack error") break
Postfix Calculator Enter a postfix expression one by one:5 Enter a postfix expression one by one:7 Enter a postfix expression one by one:+ Enter a postfix expression one by one:6 Enter a postfix expression one by one:2 Enter a postfix expression one by one:- Enter a postfix expression one by one:* Enter a postfix expression one by one:= 48 Press any key to continue . . .
# Class to convert the expression
class Evaluate:
# Constructor to initialize the class variables
def __init__(self, capacity):
self.top = -1 #declare top of stack
self.capacity = capacity
# This array is used a stack
self.array = []
# check if the stack is empty
def isEmpty(self):
return True if self.top == -1 else False
# Return the value of the top of the stack
def peek(self):
return self.array[-1]
# Pop the element from the stack
def pop(self):
if not self.isEmpty():
self.top -= 1
return self.array.pop()
else:
return "$"
# Push the element to the stack
def push(self, op):
self.top += 1
self.array.append(op)
# The main function that converts given infix expression
# to postfix expression
def evaluatePostfix(self, exp):
# Iterate over the expression for conversion
for i in exp:
# If the scanned character is an operand
# (number here) push it to the stack
if i.isdigit():
self.push(i)
# If the scanned character is an operator,
# pop two elements from stack and apply it.
else:
val1 = self.pop()
val2 = self.pop()
self.push(str(eval(val2 + i + val1)))
return int(self.pop())
# Driver program to test above function
exp=[] #temporary list to store expression
ele=input('Enter element of postfix expression: ') #asking for
first element of expression
while ele is not '=': #check if input is '=' if yes then stop
asking for next element
exp.append(ele)
ele=input('Enter element of postfix expression: ')
obj = Evaluate(len(exp)) #evaluate expression
print("postfix evaluation: ",(obj.evaluatePostfix(exp))) #print the
result
Save the above program using .py extension and run it using python 3
screenshots of your expressions are attached below
Thank you!