백준 [ALGORITHM] - 계산기 프로그램 (5613)
              
          2024. 11. 21. 11:57ㆍ코딩/백준 [ALGORITHM]
반응형
    
    
    
  import operator
def calc_operator(operators, values):
    op = operators.pop()
    b = values.pop()
    a = values.pop()
    values.append(ops[op](a, b))
ops = {
    '+' : operator.add,
    '-' : operator.sub,
    '*' : operator.mul,
    '/' : lambda x, y: x // y, 
}
calc_data = []
while True:
    input_data = input()
    
    if input_data == '=':
        try:
            
            values = []
            operators = []
            for token in calc_data:
                if token.isdigit():
                    values.append(int(token))
                elif token in ops: 
                    if len(values) >= 2:
                        calc_operator(operators, values)
                    operators.append(token)
                else:
                    break
            if operators: 
                calc_operator(operators, values)
            print(values[0])
        except Exception as e:
            pass
        break
    else:
        calc_data.append(input_data)반응형
    
    
    
  '코딩 > 백준 [ALGORITHM]' 카테고리의 다른 글
| 백준 [ALGORITHM] - 수들의 합2 (2003) (0) | 2024.12.05 | 
|---|---|
| 백준 [ALGORITHM] - 2차원 배열의 합 (2167) (0) | 2024.12.05 | 
| 백준 [ALGORITHM] - ROT13 (4446) (0) | 2024.09.09 | 
| 백준[ALGORITHM] - 럭비 클럽 (2083) (0) | 2024.08.04 | 
| 백준[ALGORITHM] - 좌표 정렬하기 (11650) (0) | 2024.07.18 |