简体   繁体   中英

Parsing a parenthesis

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.

A stack based calculator is what you are looking for !

http://en.wikipedia.org/wiki/Reverse_Polish_notation

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM