편집 기록

편집 기록
  • 프로필 엽토군님의 편집
    날짜2021.02.11

    파이썬 후위표기 계산기


    class Stack:
    
        def __init__(self):
            self.list = list()
    
        def push(self, data):
            self.list.append(data)
    
        def pop(self):
            return self.list.pop()
    
    class Calculator:
    
        def __init__(self):
            self.stack = Stack()
    
        def calculate(self, string):
            list = []
            for x in list:
                if x == '+':
                    a = Stack.pop()
                    b = Stack.pop()
                    Stack.push(a + b)
                elif x == '-':
                    a = Stack.pop()
                    b = Stack.pop()
                    Stack.push(a - b)
                elif x == '*':
                    a = Stack.pop()
                    b = Stack.pop()
                    Stack.push(a * b)
                elif x == '/':
                    a = Stack.pop()
                    b = Stack.pop()
                    Stack.push(a / b)
                else:
                    Stack.push()
            return Stack
    
    
    calc = Calculator()
    
    print(calc.calculate('4 6 * 2 / 2 +'))
    
    print(calc.calculate('2 5 + 3 * 6 - 5 *'))
    

    위 상황에서 실행하면 계산한 값이 나오는게 아니라

    class '__main__.Stack'

    라고 출력이 됩니다.

    값을 출력하기 위해서는 어느 부분을 수정해야 할까요

  • 프로필 초보자님의 편집
    날짜2021.02.11

    파이썬 후위표기 계산기


    class Stack:
    
        def __init__(self):
            self.list = list()
    
        def push(self, data):
            self.list.append(data)
    
        def pop(self):
            return self.list.pop()
    
    class Calculator:
    
        def __init__(self):
            self.stack = Stack()
    
        def calculate(self, string):
            list = []
            for x in list:
                if x == '+':
                    a = Stack.pop()
                    b = Stack.pop()
                    Stack.push(a + b)
                elif x == '-':
                    a = Stack.pop()
                    b = Stack.pop()
                    Stack.push(a - b)
                elif x == '*':
                    a = Stack.pop()
                    b = Stack.pop()
                    Stack.push(a * b)
                elif x == '/':
                    a = Stack.pop()
                    b = Stack.pop()
                    Stack.push(a / b)
                else:
                    Stack.push()
            return Stack
    
    
    calc = Calculator()
    
    print(calc.calculate('4 6 * 2 / 2 +'))
    
    print(calc.calculate('2 5 + 3 * 6 - 5 *'))
    

    위 상황에서 실행하면 계산한 값이 나오는게 아니라

    class 'main.Stack'

    라고 출력이 됩니다.

    값을 출력하기 위해서는 어느 부분을 수정해야 할까요

  • 프로필 알 수 없는 사용자님의 편집
    날짜2021.02.11

    파이썬 후위표기 계산기


    class Stack:

    def __init__(self):
        self.list = list()
    
    def push(self, data):
        self.list.append(data)
    
    def pop(self):
        return self.list.pop()
    

    class Calculator:

    def __init__(self):
        self.stack = Stack()
    
    def calculate(self, string):
        list = []
        for x in list:
            if x == '+':
                a = Stack.pop()
                b = Stack.pop()
                Stack.push(a + b)
            elif x == '-':
                a = Stack.pop()
                b = Stack.pop()
                Stack.push(a - b)
            elif x == '*':
                a = Stack.pop()
                b = Stack.pop()
                Stack.push(a * b)
            elif x == '/':
                a = Stack.pop()
                b = Stack.pop()
                Stack.push(a / b)
            else:
                Stack.push()
        return Stack
    

    calc = Calculator()

    print(calc.calculate('4 6 * 2 / 2 +'))

    print(calc.calculate('2 5 + 3 * 6 - 5 *'))

    위 상황에서 실행하면 계산한 값이 나오는게 아니라

    class 'main.Stack'

    라고 출력이 됩니다.

    값을 출력하기 위해서는 어느 부분을 수정해야 할까요