Skip to content
Snippets Groups Projects
Commit 96424f52 authored by ys554's avatar ys554
Browse files

assn2

parent a20b8c07
No related branches found
No related tags found
No related merge requests found
The program is written python 3!
Thank you :)
#!/usr/bin/env python3
#Yegeon Seo
#CS265 Assn2
import fileinput
# Stack class and it's ADTs
class Stack:
def __init__(self):
self.items = []
self.precedence = {'+':1, '-':1, '*':2, '/':2, '%':2}
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)
# Method to check if a token is an operator
def isOperator(exp):
operator = ["+", "-", "*", "/", "%"]
if exp in operator:
return True
else:
return False
# Method to check if a token is an operand
def isOperand(exp):
if isOperator(exp) or exp == "(" or exp == ")":
return False
else:
return True
# Method to check and compare the values of precedence
def notGreater(stack, i):
try:
a = stack.precedence[i]
b = stack.precedence[stack.peek()]
return True if a <= b else False
except KeyError:
return False
# Method that converts an infix expression to a postfix expression
def infix2postfix(infixList):
stack = Stack()
postfixList = []
# Append a right parenthesis to the end of the expression
# and push a new left parenthesis to the stack
infixList.append(")")
stack.push("(")
for token in infixList:
# If the token is an operand, append it to the postfix expression
if isOperand(token):
postfixList.append(token)
# If the token is a left parenthesis, push it to the stack
elif token == "(":
stack.push(token)
# If the token is a right parenthesis,
# pop operators from the stack and append
# to the postfix expression, until a left
# parenthesis is encountered on the stack.
# Then remove and discard the left parenthesis
elif token == ")":
while( (not stack.isEmpty()) and stack.peek() != "("):
a = stack.pop()
postfixList.append(a)
if (not stack.isEmpty() and stack.peek() != "("):
return False
else:
stack.pop()
# If the token is an operator, then pop operators
# from the stack and append to the postfix expression
# while the operators have equal or higher precedence
# than the current token. Then push current operator to stack
else:
while (not stack.isEmpty() and notGreater(stack, token)):
postfixList.append(stack.pop())
stack.push(token)
# Clear out the stack if it's not empty after all procedures
# (This should not run if all the inputs are correct)
while not stack.isEmpty():
postfixList.append(stack.pop())
# Take the output and pass it to the evalPostfix function
evalPostfix(postfixList)
def evalPostfix(postfix):
stack = Stack()
for token in postfix:
# If the token is an operand, push it to the stack
if isOperand(token):
stack.push(token)
# if the token is an operator, assign x and y as top 2 values in the stack
# and do the math
else:
y = int(stack.pop())
x = int(stack.pop())
if token == "+":
result = x + y
elif token == "-":
result = x - y
elif token == "*":
result = x * y
elif token == "/":
result = x / y
else:
result = x % y
stack.push(result)
# Print the output in postfix expression = result format
print(" ".join(postfix), "=", stack.peek())
# Driver program that reads in a file and pass it to infix2postfix function above
for line in fileinput.input():
infixInput = []
newLine = line.split()
infix2postfix(newLine)
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment