Reputation: 3060
I'm trying to make a calculator in Python 3 (just to learn). I want to be able to evaluate (just as an example) "5 * ( 2 + 1 )^2" from an input(). I would like to be able to detect if parenthesis are closed or contain another set of parenthesis. I also need to be able to isolate the information within so I can evaluate it in the proper order.
I realize that this could be a significant chunk of code, so if you could point me in the right direction I would be very grateful. I'm looking for links to documentation, function names, and any hints you could provide to help me.
Upvotes: 1
Views: 234
Reputation: 429
This is a classic stack data structure practice problem. There are two approaches you can use, one being converting from infix to post/prefix notation which is considerably easier to process but still require the additional step of converting, or you can go and directly evaluate the expression.
Here is a good starting point on the subject, a basic implementation of a stack and some more in-depth information about your subject. Starting from there, you should easily find your way, otherwise give me a comment and I'll try to help you out.
Upvotes: 1
Reputation: 1850
A stack based calculator is what you are looking for !
http://en.wikipedia.org/wiki/Reverse_Polish_notation
Upvotes: 1