c – 如何为“(1 3 *(5/4))”等字符串编写求值器,并获得数字结果

这是一个面试问题
我对它的解决方案感到困惑,我想我需要堆栈来推送和弹出这些操作符和操作数,
但是我需要两个堆栈,一个用于运算符,一个用于操作数?或者只是一个堆栈?我认为我们需要两个堆栈,但是有什么办法可以解决使用一个堆栈?

我也有点困惑,这将如何工作,每次我得到一个运算符,我会弹出两个我最高的操作数,并推动结果在操作数堆栈

首选的是括号,然后是分,多,最后的减法,然后加法

但是如何检查何时弹出两个操作数并进行必要的操作操作?

解决方法

看看 shunting yard algorithm.

The shunting yard algorithm is a method for parsing mathematical equations specified in infix notation. It can be used to produce output in Reverse Polish notation (rpn) or as an abstract Syntax tree (AST). The algorithm was invented by Edsger Dijkstra and named the “shunting yard” algorithm because its operation resembles that of a railroad shunting yard.

相关文章

本程序的编译和运行环境如下(如果有运行方面的问题欢迎在评...
水了一学期的院选修,万万没想到期末考试还有比较硬核的编程...
补充一下,先前文章末尾给出的下载链接的完整代码含有部分C&...
思路如标题所说采用模N取余法,难点是这个除法过程如何实现。...
本篇博客有更新!!!更新后效果图如下: 文章末尾的完整代码...
刚开始学习模块化程序设计时,估计大家都被形参和实参搞迷糊...