Created
May 19, 2014 09:18
-
-
Save sota1235/c22607baa1e0d75e47d8 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/python | |
| def rpn(stack, obj): | |
| if type(obj) == int: | |
| stack.append(obj) | |
| else: | |
| p = stack.pop() | |
| if obj == '+': | |
| stack.append(stack.pop() + p) | |
| elif obj == '-': | |
| stack.append(stack.pop() - p) | |
| elif obj == '*': | |
| stack.append(stack.pop() * p) | |
| elif obj == '/': | |
| stack.append(stack.pop() / p) | |
| return stack | |
| while True: | |
| stack = [] | |
| print("半角スペース区切りで問題を入力してください") | |
| formula = input("-> ").split() | |
| try: | |
| for f in formula: | |
| stack = rpn(stack, int(f) if f.isdigit() else f) | |
| print(stack[0]) | |
| except: | |
| print('Error: invalid input') | |
| break |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment