Created
January 9, 2020 11:57
-
-
Save Defelo/7c8210ab094d54059bb16699cd847e5f to your computer and use it in GitHub Desktop.
This file contains 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
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