예제 #1
0
    def calculate(self):

        prefix_notation = self.split_prefix_notation(self.prefix_notation)
        #prefix_notation.print_to_console()
        #print('\n')

        stack = Stack()
        list_elem = prefix_notation.tail

        for _ in range(prefix_notation.get_size(), 0, -1):

            if list_elem.value == 'pi' or list_elem.value == 'e' or list_elem.value[
                    0].isdigit(
                    ) or list_elem.value == '-pi' or list_elem.value == '-e' or (
                        list_elem.value[0] == '-' and len(list_elem.value) > 1
                        and not (str(self.functions.search(
                            list_elem.value)).isdigit())):

                if list_elem.value == 'pi':
                    stack.push(pi)
                elif list_elem.value == 'e':
                    stack.push(e)
                elif list_elem.value == '-pi':
                    stack.push(-pi)
                elif list_elem.value == '-e':
                    stack.push(-e)
                else:
                    stack.push(list_elem.value)

            else:

                if (str(self.functions.search(list_elem.value)).isdigit()):

                    digit = stack.get_top()
                    stack.pop()
                    stack.push(self.function(list_elem.value, float(digit)))

                elif str(self.operators.search(list_elem.value)).isdigit():

                    digit_1 = stack.get_top()
                    stack.pop()
                    digit_2 = stack.get_top()
                    stack.pop()

                    stack.push(
                        self.operation(float(digit_1), list_elem.value,
                                       float(digit_2)))

            list_elem = list_elem.prev

        return stack.get_top()
예제 #2
0
class IteratorDFT:
    def __init__(self, heap_size, heap_list):
        self.current_elem = 0
        self.heap_list = heap_list
        self.end = heap_size - 1
        self.count = 0
        self.stack = Stack()

    def __iter__(self):
        return self

    def has_left_child(self, index):
        if (2 * index) + 1 <= self.end:
            return True
        return False

    def has_right_child(self, index):
        if (2 * index) + 2 <= self.end:
            return True
        return False

    def __next__(self):
        if self.count <= self.end:
            current_elem = self.current_elem

            if self.has_right_child(self.current_elem):
                self.stack.push((2 * self.current_elem) + 2)

            if self.has_left_child(self.current_elem):
                self.current_elem = (2 * self.current_elem) + 1
            else:
                if self.count != self.end:
                    self.current_elem = self.stack.get_top()
                    self.stack.pop()

            self.count += 1

            return self.heap_list[current_elem]
        else:
            raise StopIteration
예제 #3
0
    def prefix_conversion(self, formula_list):
        """
      Преобразование в префиксную форму записи 
      """
        prefix_str = ''
        stack = Stack()
        list_elem = formula_list.tail

        for _ in range(formula_list.get_size(), 0, -1):

            if list_elem.value == ')':

                stack.push(list_elem.value)

            elif list_elem.value == 'pi' or list_elem.value == 'e' or list_elem.value[
                    0].isdigit(
                    ) or list_elem.value == '-pi' or list_elem.value == '-e' or (
                        list_elem.value[0] == '-'
                        and len(list_elem.value) > 1):

                if prefix_str == '':
                    prefix_str = list_elem.value + prefix_str
                else:
                    prefix_str = list_elem.value + ' ' + prefix_str

            elif list_elem.value == '(':

                while stack.get_top() != ')':

                    if prefix_str == '':
                        prefix_str = stack.get_top() + prefix_str
                    else:
                        prefix_str = stack.get_top() + ' ' + prefix_str

                    stack.pop()

                stack.pop()

            elif str(self.operators.search(list_elem.value)).isdigit() or str(
                    self.functions.search(list_elem.value)).isdigit():

                if stack.stack_list.is_empty():

                    stack.push(list_elem.value)

                elif self.get_priority(list_elem.value) > self.get_priority(
                        stack.get_top()):

                    stack.push(list_elem.value)

                else:

                    while self.get_priority(
                            stack.get_top()) > self.get_priority(
                                list_elem.value):

                        if prefix_str == '':
                            prefix_str = stack.get_top() + prefix_str
                        else:
                            prefix_str = stack.get_top() + ' ' + prefix_str

                        stack.pop()

                        if stack.stack_list.is_empty(): break

                    stack.push(list_elem.value)

            list_elem = list_elem.prev

        while not stack.stack_list.is_empty():

            prefix_str = stack.get_top() + ' ' + prefix_str
            stack.pop()

        return prefix_str