Skip to content

Instantly share code, notes, and snippets.

@Defelo
Created January 9, 2020 11:57
Show Gist options
  • Save Defelo/7c8210ab094d54059bb16699cd847e5f to your computer and use it in GitHub Desktop.
Save Defelo/7c8210ab094d54059bb16699cd847e5f to your computer and use it in GitHub Desktop.
import math
#term = input(">> ").split()
stack = []
running = True
while running:
for t in input(">> ").split():
if t == "exit":
running = False
break
elif t == "stack":
pass
elif t == "clear":
stack.clear()
elif t == "pi":
stack.append(math.pi)
elif t == "e":
stack.append(math.e)
elif t in ["+", "-", "*", "/", "%", "^"]:
*stack, a, b = stack
if t == "^":
t = "**"
result = eval(str(a) + t + str(b))
stack.append(result)
elif t in ["!", "sin", "cos", "tan"]:
a = stack.pop()
if t == "!":
result = math.factorial(a)
elif t == "sin":
result = math.sin(a)
elif t == "cos":
result = math.cos(a)
elif t == "tan":
result = math.tan(a)
stack.append(result)
else:
a = float(t)
if a.is_integer():
a = int(a)
stack.append(a)
print("Stack:", stack)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment