Sin descripción

taschenrechner.py 841B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. def Grundrechenarten(c,a,d):
  2. if a == '+':
  3. result = float(c)+float(d)
  4. elif a == '-':
  5. result = float(c)-float(d)
  6. elif a == '*':
  7. result = float(c)*float(d)
  8. elif a == '/':
  9. result = float(c)/float(d)
  10. if result == int(result):
  11. result = int(result)
  12. return result
  13. def toList(s):
  14. L = []
  15. for i in s:
  16. if i != '\n':
  17. L.append(i)
  18. return L
  19. def f(i,s,S = None):
  20. if not S:
  21. S = []
  22. if i == len(s):
  23. return S[0]
  24. a = s[i]
  25. if a.isdigit():
  26. S.append(a)
  27. return f(i+1,s,S)
  28. else:
  29. c = S.pop()
  30. d = S.pop()
  31. S.append(Grundrechenarten(c,a,d))
  32. return f(i+1,s,S)
  33. x = raw_input()
  34. s = ""
  35. while x != '=\n':
  36. s += x
  37. x = raw_input() + '\n'
  38. print f(0,toList(s))