def isValidate(strInfix): stack = st.stack() con = 0 for cek in strInfix: if cek in kurung.values(): st.push(stack, cek) elif cek in kurung.keys(): if st.isEmpty(stack): print("Kelebihan Kurung Tutup") con = 1 break elif not st.dataCheck(kurung,st.peek(stack),cek): st.pop(stack) else: print(st.peek(stack), cek, "Kesalahan Kurung Tutup") con = 1 break if st.isEmpty(stack) and con == 0: return True elif not st.isEmpty(stack) and con == 0: print("Kelebihan Kurung Buka")
def in2Post(strinfix): operator = {"+": 1, "-": 2, "*": 3, ":": 4} kurungg = {")": "(", "}": "{", "]": "["} dataa = st.stack() output = "" bagi = [] temp = "" for j in strinfix: if j == " ": continue elif j not in operator.keys() and j not in kurungg.keys( ) and j not in kurungg.values(): temp += j else: if temp != "": bagi.append(temp) temp = "" bagi.append(j) if temp != "": bagi.append(temp) for y in bagi: if y == "": continue elif y in kurungg.values(): st.push(dataa, y) elif y in operator.keys(): if st.size(dataa) != 0: p = st.peek(dataa) if p not in operator.keys(): st.push(dataa, y) elif p in operator.keys(): if operator[y] > operator[a]: st.push(dataa, y) elif operator[y] < operator[a]: output += st.pop(dataa) st.push(dataa, y) else: st.push(dataa, y) elif y in kurungg.keys(): while not st.isEmpty(dataa): q = st.pop(dataa) if q in operator.keys(): output += " " + q else: output += " " + y if st.size(dataa) > 0: while not st.isEmpty(dataa): output += " " + st.pop(dataa) return output
def isValidate(strinfix): data = st.stack() kurung = {")": "(", "}": "{", "]": "["} for i in strinfix: if i in kurung.values(): st.push(data, i) elif i in kurung.keys(): if st.isEmpty(data): return "Kelebihan kurung tutup" else: a = st.peek(data) if a == kurung[i]: st.pop(data) else: return "Kurung tutup tidak sama" if st.size(data) > 0: print("Kelebihan kurung buka") return False else: return True
def in2Post(strInfix): stack = st.stack() result = "" # Loop each strInfix for i in strInfix: # to skip iteration if found space if i == " ": continue # check if i is a number elif i.isdigit(): result += i # check if i is kurung buka elif i in kurung.values(): st.push(stack,i) # check if i operator elif i in operator.keys(): # to separate each number result += " " # loop if stack is not empty and check level of operator while not st.isEmpty(stack) and st.compareOperator(operator, stack, i): # pop operator if current operator more higher level result += st.pop(stack)+" " # to push current operator after poping lower level operator st.push(stack,i) # to catch kurung tutup else: # loop if stack is not empty and check if kurung buka == kurung tutup while not st.isEmpty(stack) and st.dataCheck(kurung,st.peek(stack), i): # poping everything in the kurung :v result += " "+st.pop(stack) # last, pop kurung buka st.pop(stack) # check if stack is not empty while not st.isEmpty(stack): # pop everything remaining from the stack result += " "+st.pop(stack) return(result)
elif hasil == 15: hasil = "F" st.push(stack, hasil) hexa = hexa // 16 for i in range(st.size(stack)): print(st.pop(stack), end="") elif pilihan == 4: biner = input("Masukkan Bilangan Biner: ") lenBiner = int(len(biner)) result = 0 for j in biner: lenBiner = lenBiner - 1 hasil = int(j) * (2**(lenBiner)) st.push(stack, hasil) for i in range(st.size(stack)): result = result + st.peek(stack) print(st.pop(stack), end=" ") print(" = ", result) elif pilihan == 5: octal = input("Masukkan Bilangan Octal: ") lenOctal = len(octal) result = 0 for i in octal: lenOctal = lenOctal - 1 hasil = int(i) * (8**(lenOctal)) st.push(stack, hasil) for j in range(st.size(stack)): result = result + st.peek(stack) print(st.pop(stack), end=" ") print("=", result) elif pilihan == 6:
import Module.Stack as st kata = "{[23+56]*[12-2]:[10+20]]" stack = st.stack() con = 0 for cek in kata: if cek == "(" or cek == "{" or cek == "[": st.push(stack, cek) elif cek == ")" or cek == "}" or cek == "]": if st.isEmpty(stack): print("Kelebihan Kurung Tutup") con = 1 break elif st.peek(stack) == "(" and cek == ")" or st.peek( stack) == "{" and cek == "}" or st.peek( stack) == "[" and cek == "]": st.pop(stack) else: print(st.peek(stack), cek, "Kesalahan Kurung Tutup") con = 1 break if st.isEmpty(stack) and con == 0: print("Ntaps, Wes Bener") elif not st.isEmpty(stack) and con == 0: print("Kelebihan Kurung Buka")