Hasil belajar
HASIL BELAJAR BAHASA PEMOGRAMAN PYTHON
MOH.SABHAN - TEKNIK INFORMATIKA - NIM: 160411100078
INFIX TO POSTVIX
def Stack():
opStack=[]
return opStack
def push(opStack,data):
opStack.append(data)
def pop(opStack):
data = opStack.pop()
return data
def peek(opStack):
return opStack[len(opStack)-1]
def isEmpty(opStack):
return opStack == []
def size(opStack):
return len(opStack)
def infixToPostfix(infixexpr):
prec = {}
prec["*"] = 3
prec["/"] = 3
prec["+"] = 2
prec["-"] = 2
prec["("] = 1
opStack = Stack()
postfixList = []
tokenList = infixexpr.split()
hasil=' '
step = 0
for token in tokenList:
if token in "ABCDEFGHIJKLMNOPQRSTUVWXYZ" or token in "0123456789":
postfixList.append(token)
elif token == '(':
push(opStack,token)
elif token == ')':
topToken = pop(opStack)
while topToken != '(':
postfixList.append(topToken)
topToken = opStack.pop()
else:
while (not isEmpty(opStack)) and \
(prec[peek(opStack)] >= prec[token]):
postfixList.append(opStack.pop())
push(opStack,token)
while not isEmpty(opStack):
postfixList.append(opStack.pop())
return "Hasil = "+hasil.join(postfixList)
print(infixToPostfix("( A + B ) * C - ( D - E ) * ( F + G )"))
print("==============================================")
print(infixToPostfix("A * B + C * D "))
Komentar
Posting Komentar