Example #1
0
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")
Example #2
0
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)