Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def calculator(st):
- st = st.split(' ')
- token_numbers = []
- token_operators = []
- priority = {
- '+': 1,
- '-': 1,
- '*': 2,
- '/': 2
- }
- stack_number = []
- stack_operation = []
- for s in st:
- if s.isdigit():
- token_numbers.append(int(s))
- else:
- token_operators.append(s)
- for s in range(len(st)):
- if st[s] == '+':
- stack_number.append(st[s-1])
- stack_operation.append(st[s])
- if st[s] == '*':
- stack_operation.append(st[s])
- if st[s + 2] == '+' and priority.get('*') > priority.get('+'):
- stack_number.append(int(st[s-1]) * int(st[s+1]))
- if st[s] == '-':
- stack_number.append(st[s - 1])
- stack_operation.append(st[s])
- if st[s] == '/':
- stack_number.append(st[s - 1])
- stack_operation.append(st[s])
- if st[s] == ';':
- stack_number.append(st[s-1])
- print(stack_number)
- print(stack_operation)
- calculator("1 + 2 * 2 + 3 ;")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement