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 addList(x, y): cekX = st.size(x) cekY = st.size(y) hasil = st.stack() if cekX >= cekY: for i in range(cekX): if i < cekY: st.push(hasil, x[i] + y[i]) else: st.push(hasil, x[i]) else: for i in range(cekY): if i < cekX: st.push(hasil, x[i] + y[i]) else: st.push(hasil, y[i]) print(hasil)
def evaluatePost(strinfix): operatorr = "*:+-" dataaa = st.stack() bagii = strinfix.split() for k in bagii: if k not in operatorr: st.push(dataaa, int(k)) elif st.size(dataaa) > 1: r = st.pop(dataaa) t = st.pop(dataaa) if k == ":": k = "/" hasil = eval(" {}{}{} ".format(float(t), k, float(r))) st.push(dataaa, float(hasil)) return st.pop(dataaa)
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
if pilihan == 1: decimal = int(input("Masukkan Bilangan Decimal: ")) while decimal != 0: hasil = decimal % 2 st.push(stack, hasil) decimal = decimal // 2 for i in range(len(stack)): print(st.pop(stack), end="") elif pilihan == 2: octal = int(input("Masukkan Bilangan Decimal: ")) while octal > 0: hasil = octal % 8 st.push(stack, hasil) octal = octal // 8 for i in range(st.size(stack)): print(st.pop(stack), end="") elif pilihan == 3: hexa = int(input("Masukkan Bilangan Decimal: ")) while hexa > 0: hasil = hexa % 16 if hasil == 10: hasil = "A" elif hasil == 11: hasil = "B" elif hasil == 12: hasil = "C" elif hasil == 13: hasil = "D" elif hasil == 14: hasil = "E"