Advertisement
Guest User

Untitled

a guest
Feb 23rd, 2020
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.09 KB | None | 0 0
  1. def calculator(st):
  2.     st = st.split(' ')
  3.     token_numbers = []
  4.     token_operators = []
  5.     priority = {
  6.         '+': 1,
  7.         '-': 1,
  8.         '*': 2,
  9.         '/': 2
  10.     }
  11.     stack_number = []
  12.     stack_operation = []
  13.  
  14.     for s in st:
  15.         if s.isdigit():
  16.             token_numbers.append(int(s))
  17.         else:
  18.             token_operators.append(s)
  19.  
  20.     for s in range(len(st)):
  21.         if st[s] == '+':
  22.             stack_number.append(st[s-1])
  23.             stack_operation.append(st[s])
  24.         if st[s] == '*':
  25.             stack_operation.append(st[s])
  26.             if st[s + 2] == '+' and priority.get('*') > priority.get('+'):
  27.                 stack_number.append(int(st[s-1]) * int(st[s+1]))
  28.         if st[s] == '-':
  29.             stack_number.append(st[s - 1])
  30.             stack_operation.append(st[s])
  31.         if st[s] == '/':
  32.             stack_number.append(st[s - 1])
  33.             stack_operation.append(st[s])
  34.         if st[s] == ';':
  35.             stack_number.append(st[s-1])
  36.     print(stack_number)
  37.     print(stack_operation)
  38.  
  39.  
  40. calculator("1 + 2 * 2 + 3 ;")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement