def test_priority(self):
     """
     Test case to check priority of Operators
     """
     plus = OpFactory.get_op_by_symbol('+')
     minus = OpFactory.get_op_by_symbol('-')
     multiplier = OpFactory.get_op_by_symbol('*')
     divider = OpFactory.get_op_by_symbol('/')
     power = OpFactory.get_op_by_symbol('^')
     self.assertFalse(plus > minus)
     self.assertFalse(minus > plus)
     self.assertFalse(minus > multiplier)
     self.assertTrue(multiplier > minus)
     self.assertFalse(multiplier > divider)
     self.assertFalse(divider > multiplier)
     self.assertFalse(divider > power)
     self.assertTrue(power > divider)
    def convert(expression_in_infix_notation: str) -> ReversePolishNotation:
        """
        Цикл while
        Main method of the class.
        Convert an infix expression to reverse polish notation

        :return: ReversePolishNotation object
        """

        state = ReversePolishNotationConverterState(
            expression_in_infix_notation)

        while not state.expression_in_infix_notation.empty():

            symbol = state.expression_in_infix_notation.top()

            if ReversePolishNotationConverter.is_part_of_digit(symbol):
                digit = ReversePolishNotationConverter.read_digit(state)
                state.expression_in_postfix_notation.put(digit)
                continue

            operator = OpFactory.get_op_by_symbol(symbol)

            if ReversePolishNotationConverter.is_open_bracket(operator):
                state.stack.push(operator)
                # state.expression_in_infix_notation.get()
                continue

            if ReversePolishNotationConverter.is_close_bracket(operator):
                state.pop_from_stack_until_opening_bracket()
                # state.expression_in_infix_notation.get()
                continue

            if ReversePolishNotationConverter.is_binary_operation(operator):
                ReversePolishNotationConverter.pop_from_stack_until_prioritizing(
                    operator, state)
                # state.expression_in_infix_notation.get()
            else:
                raise Exception(symbol)

        while not state.stack.empty():
            state.expression_in_postfix_notation.put(state.stack.top())
            state.stack.pop()
        return state.expression_in_postfix_notation
Exemplo n.º 3
0
    def convert(self, expression_in_infix_notation: str) -> ReversePolishNotation:
        while not self._infix_notation.empty():
            character = self._infix_notation.get()
            if self.is_part_of_digit(character):
                digit = self.read_digit(character)
                self._postfix_notation.put(digit)
                continue
            elif self.is_opening_bracket(character):
                self.stack.push(character)
                continue
            elif self.is_closing_bracket(character):
                self.pop_from_stack_until_opening_bracket()
                continue

            operator = OpFactory.get_op_by_symbol(character)
            if self.is_binary_operation(operator):
                self.pop_from_stack_until_prioritizing(operator)
            else:
                raise Exception(character)
        while not self.stack.empty():
            self._postfix_notation.put(self.stack.top())
            self.stack.pop()
        return self._postfix_notation
 def test_create_power(self):
     """
     Test case to create Power operator
     """
     operator = '^'
     self.assertIsInstance(OpFactory.get_op_by_symbol(operator), Power)
 def test_create_divider(self):
     """
     Test case to create Divider operator
     """
     operator = '/'
     self.assertIsInstance(OpFactory.get_op_by_symbol(operator), Divider)
 def test_create_multiplier(self):
     """
     Test case to create Multiplier operator
     """
     operator = '*'
     self.assertIsInstance(OpFactory.get_op_by_symbol(operator), Multiplier)
 def test_create_minus(self):
     """
     Test case to create Minus operator
     """
     operator = '-'
     self.assertIsInstance(OpFactory.get_op_by_symbol(operator), Minus)
 def test_create_plus(self):
     """
     Test case to create Plus operator
     """
     operator = '+'
     self.assertIsInstance(OpFactory.get_op_by_symbol(operator), Plus)