Possible Duplicate:
I’m making a PEMDAS solver and don’t know what to write
I’m making a pemdas solver program, and I can’t seem to make it solve an equation that the user writes into the program. My current code is:
def pemdas():
print("type in your pemdas problem.")
prob = int(input())
solve(prob)
print(str(prob))
input()
pemdas()
This is actually a tougher problem than you might think, and the question is pretty hard to answer in its current form. So here are some suggested approaches. You should look into these and give one or more of them a try, and then come back with a more specific question when you run into trouble.
Probably your best option is to convert the expression to postfix using the shunting-yard algorithm and then use a stack to evaluate the expression.
Another option is to use PyParsing. PyParsing lets you specify parsing rules for a string and assign functions to certain tokens; this will let you calculate your result during the parse stage. This will be a much more abstract approach. (Also, there may be some good examples of this sort of thing on their examples page. Do take a good look!)