Advertisement
Guest User

Untitled

a guest
Feb 23rd, 2020
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.05 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_operation.append(st[s-1])
  23.         if st[s] == '*':
  24.             stack_operation.append(st[s])
  25.             if st[s + 2] == '+' and priority.get('*') > priority.get('+'):
  26.                 stack_number.append(int(st[s-1]) * int(st[s+1]))
  27.         if st[s] == '-':
  28.             stack_number.append(st[s - 1])
  29.             stack_operation.append(st[s])
  30.         if st[s] == '/':
  31.             stack_number.append(st[s - 1])
  32.             stack_operation.append(st[s])
  33.         if st[s] == ';':
  34.             stack_number.append(st[s-1])
  35.     print(stack_number)
  36.     print(stack_operation)
  37.  
  38.  
  39. calculator("1 + 2 * 2 + 3 ;")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement