What it does is, you enter an arithmetic expression and it calculates it.
Things to think of:
*Use spaces between everything!
*Write it like you do with hp calculators. What this means is:
It goes from left to right, whenever it finds an operator(like +) it takes the two closest numbers to the left and calculates them with the operator it found. It then repeats doing that until there's only one thing left and no operators.
So, for example. 7 + 3 is 7 3 +. (2 + 3) * 4 is 4 2 3 + *. This is good because there's no need for parenthesis or priority like * is handled before +
Yes, I was bored. I'm going to make it calculate equations later.
Code:
from time import sleep
def isfunction(x):
if x in ['+','-','*','/', '**']:
return True
else:
return False
def stack(string):
stack = string.split()
ostack = []
for z in stack:
if isfunction(z):
if len(ostack) >= 2:
arg1 = ostack.pop(-1)
arg2 = ostack.pop(-1)
ans = solve(arg1, arg2, z)
ostack.append(ans)
else:
print 'Error: Stack empty'
else:
ostack.append(z)
print
print 'The answer is: ', ans
print
print '_' * 80
def solve(arg1, arg2, z):
x = float(arg1)
y = float(arg2)
if z == '+':
return x + y
elif z == '-':
return y - x
elif z == '*':
return x * y
elif z == '/':
return y / x
elif z == '**':
return y ** x
def stringparse():
print
print 'Sting parsing testing'
print '-' * 20
string = raw_input("Enter string to parse: \n")
stack(string)
print
print 'Welcome to String parsing 1.0'
print
loop = 1
while loop == 1:
sleep(0.7)
print
print 'Please choose an option:'
print '1. Parse string'
print '2. exit'
option = input(">>>")
if option == 1:
stringparse()
elif option == 2:
loop = 0
else:
'Error: Invalid option'