中缀到后缀转换(使用堆栈)

时间:2017-03-16 02:39:39

标签: c++ algorithm

我编写了一个程序,使用c ++中的堆栈将中缀转换为后缀操作。但是在运行这个时我遇到了Segmantation故障。 请帮我弄清楚我的错误。我无法知道。

我使用了以下算法:

Num

代码是:

1. Scan the infix expression from left to right.
2. If the scanned character is an operand, output it.
3. Else,
…..3.1 If the precedence of the scanned operator is greater than the precedence of the operator in the stack(or the stack is empty), push it.
…..3.2 Else, Pop the operator from the stack until the precedence of the scanned operator is less-equal to the precedence of the operator residing on the top of the stack. Push the scanned operator to the stack.
4. If the scanned character is an ‘(‘, push it to the stack.
5. If the scanned character is an ‘)’, pop and output from the stack until an ‘(‘ is encountered.
6. Repeat steps 2-6 until infix expression is scanned.
7. Pop and output from the stack until it is not empty.

0 个答案:

没有答案