fork download
  1. #! /usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3.  
  4. def main():
  5. print('> ', end='')
  6. cal = input().split()
  7. stack = []
  8. for c in cal:
  9. if c.isdigit() == True:
  10. stack.append(int(c))
  11. elif len(stack) >= 2:
  12. b = stack.pop()
  13. a = stack.pop()
  14. if c == '+':
  15. stack.append(a + b)
  16. elif c == '-':
  17. stack.append(a - b)
  18. elif c == '*':
  19. stack.append(a * b)
  20. elif c == '/':
  21. if b == 0:
  22. print('division by zero')
  23. break
  24. stack.append(a / b)
  25. if len(stack) == 1:
  26. print(stack.pop())
  27. else:
  28. print('error')
  29.  
  30. if __name__ == '__main__':
  31. main()
  32.  
Success #stdin #stdout 0.02s 28384KB
stdin
1 2 + 4 5 + *
stdout
> 27