How to properly parse numbers in an arithmetic expression, differentiating positive and negative ones?

断了今生、忘了曾经 提交于 2019-12-02 00:13:21

That is the problem called "unary minus" and can in your case (no variables) get solved by substitution.

The operator - is an unary minus if it is

  • preceded by a left parenthesis
  • preceded by another operator
  • the first character of the input

Now instead of storing a - you store a different character like, say m and assign it a higher precedence than the other operators (or the same as the exponentiation operator if you have one).

Another tip: don't use spaces to indicate anything, an arithmetic expression must work without any spaces or it is not correct.

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