A formula expression analysis implemented by myself

When playing games recently, there is a need to parse a formula into a sequence of pre-order traversal, and then store this sequence string in the database. In the game server, read this sequence and perform evaluation. The work of parsing this formula is placed in the game editor and I am responsible for it.

The requirements of the formula are as follows: the formula only supports + - * /. ( ) and keywords in the game world, such as role.curlv, which represent the current level of the character. These keywords are finally translated into something like this [0.12], where 0 means role and 12 means curlv attribute.

A simple formula looks like this: role.curlv * 10 / 100 + 50.

At first I thought this problem was not difficult, but when I actually started it, I found that it was very difficult. According to the characteristics of the formula, I spent 2 days to barely realize it with the stack. However, due to the length of the code, there are too many judgment conditions. It is regarded as wild by cto. At the same time, he gave me a blog and let me see his professional writing style. After reading that blog, I realized that it is a pity that I did not learn the principles of compilation in college (there is no such course in college). I can't understand the grammar derivation inside. In desperation, I googled it myself. I went to the library to borrow books and spent several days learning the basics of compilation principles. According to the method in the book, I finally wrote a class.

The following is the source code:

 

 

 

Instructions:

Compile and pass under vs2005. In vs2010, due to the upgrade of sting in vs2010, it will lead to out of bounds.

operation result:

 

Guess you like

Origin blog.csdn.net/suzhijie325/article/details/6032727