Esempio n. 1
0
def shuntingYard(operandStack, operatorStack, expression):
    if not sI.isAlphaInStr(expression):  # If there are alphabet in str, return the call
        # expression = sI.addSpaceBetweenCharacters(expression)  # Add space between character for splitting
        expressionList = expression.split()
        expressionListLength = len(expressionList)
        listIndex = 0
        previousToken = None
        endOfOperationFlag = False
        isTokenPushedFlag = False

    while not isOperationCompleted(expressionListLength, endOfOperationFlag):
        if expressionListLength != 0:   # Create the token and set the flag
            token = createToken(listIndex, expressionList, previousToken)
            isTokenPushedFlag = False
        else: token.tokenType = "END_OF_OPERATION_TOKEN"  # Change the token type to stop the operation

        if token.tokenType == "OPERAND_TOKEN":  # Directly push into the operand stack
            pushTokenToStack(token, operandStack, operatorStack)
            previousToken = token
            isTokenPushedFlag = True

        elif token.tokenType == "OPERATOR_TOKEN": # If the token type is operator
            while isStacksReadyForOperation(operandStack, operatorStack, token) == "YES":
                calculateAnsAndPushToOperandStack(operandStack, operatorStack)

            while isStacksReadyForOperation(operandStack, operatorStack, token) == "SAME_PRECEDENCE_AND_READY":
                previousToken = handleSameAssociativityAndReturnToken(operandStack, operatorStack, token)
                isTokenPushedFlag = True

            if not isTokenPushedFlag and isStacksReadyForOperation(operandStack, operatorStack, token) == "SAME_PRECEDENCE_BUT_XREADY":
                if len(operatorStack) > 1 and token.tokenId != operatorStack[len(operatorStack)-1].tokenId:
                    previousToken = token
                    token = createToken(listIndex, expressionList, previousToken)


            if not isTokenPushedFlag:
                pushTokenToStack(token, operandStack, operatorStack)
                previousToken = token
        else:
            calculateAnsAndPushToOperandStack(operandStack, operatorStack)
            expressionListLength += 1  # To offset the decrement later
        listIndex += 1
        expressionListLength -= 1

        if expressionListLength == 0 and len(operatorStack) == 0:
            endOfOperationFlag = True

    return st.popStack(operandStack)
 def test_d_isAlphaInStr_given_12point12_expect_false(self):
     # isAlphaInStr(expression)
     self.assertEqual(False, sI.isAlphaInStr("12.12"))
 def test_c_isAlphaInStr_given_111_expect_false(self):
     # isAlphaInStr(expression)
     self.assertEqual(False, sI.isAlphaInStr("111"))
 def test_b_isAlphaInStr_given_avc1234_expect_true(self):
     # isAlphaInStr(expression)
     self.assertEqual(True, sI.isAlphaInStr("avc1234"))
 def test_a_isAlphaInStr_given_abcdqef_expect_true(self):
     # isAlphaInStr(expression)
     self.assertEqual(True, sI.isAlphaInStr("abcdqef"))
Esempio n. 6
0
def isExpressionValid(expression):
    if sI.isAlphaInStr(expression):
        return False
    else:
        return True