Convert a string with mathematical expressions into a float

牧云@^-^@ 提交于 2019-12-01 14:27:22

One solution that doesn't rely on external libraries is to first convert the string into a sequence of tokens, then evaluate the expression using the shunting-yard algorithm chained onto a simple stack machine.

You need to parse this input using a parser then evaluate it. I have heard of people having success using muParser for this, to save you writing it all yourself. Check it out. http://muparser.sourceforge.net/

Yes. Read up on "expression evaluation", or use a ready-made script interpreter. JavaScript is available to all iOS programs as a part of WebView.

Yes, of course it's possible.

You should look into infix to postfix conversion using a stack: http://scriptasylum.com/tutorials/infix_postfix/algorithms/infix-postfix/index.htm

And then look into how to evaluate a postix expression, also using a stack: http://scriptasylum.com/tutorials/infix_postfix/algorithms/postfix-evaluation/index.htm

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!