如何为简单的字符串方程式创建自己的简单eval方法?

I am trying to write a simple method to parse a string equation like "5*3-3+13" and calculate the result without using eval(). I have a problem with how to set the operator precedence. My initial idea is to use recursion, but I have trouble when there is continuous multiplication or division in the equation. Another idea is to just loop through the equation to find the multiplication and division first, but that takes too much time. Is there an idea that I can utilize?

评论