简体   繁体   English

使用C / C ++将后缀/前缀表达式显示为解析树

[英]Displaying the postfix/prefix expression as a parse tree using C/C++

I have successfully converted infix expression to postfix expression and was also able to evaluate the postfix expression but I am facing problem in generating a parse tree for the same with C/C++ 我已成功将中缀表达式转换为后缀表达式,并且还能够评估后缀表达式,但我在使用C / C ++生成解析树时面临问题

My output : 我的输出:

           enter the expression string a+b*c
           the expression is correct
           the postfix expression is - abc *+
           enter the value of a-1
           enter the value of b-2
           enter the value of c-3
           the postfix expression is -abc*+
           result= 7

I also require to display: Syntax tree 我还需要显示:语法树

               +          
             /   \                                
          *       a                               
         /   \                                               
        b     c       

Any feedback would be very helpful in my project. 任何反馈对我的项目都非常有帮助。

Thanks in Adv. 谢谢Adv。

@LD: Thanks for your consistent help. @LD:感谢您的一贯帮助。 I need the pseudocode in turbo C. I don't know Ruby. 我需要turbo C中的伪代码。我不知道Ruby。

It's much easier to "draw" them like the following: 如下所示“绘制”它们要容易得多:

+
  a
  *
    b
    c

Or, if you want to use simple character graphics (I've changed the + and * operators to Add and Mul , to avoid clashing with the graphics): 或者,如果你想使用简单的字符图形(我已经将+*运算符更改为AddMul ,以避免与图形冲突):

Add
+-- a
+-- Mul
    +-- b
    +-- c

The trick to doing this is possible draw a subtree in isolation (eg the mul tree), and then draw it with suitable prefixes when drawing the outer tree. 这样做的诀窍是可以单独绘制一个子树(例如mul树),然后在绘制外树时用合适的前缀绘制它。

In fact, if you are familiar with C++ stream buffers, you could create a prefixing stream buffer that handles the prefixes and simply print the inner tree. 实际上,如果您熟悉C ++流缓冲区,则可以创建一个前缀流缓冲区来处理前缀并简单地打印内部树。

The big difference compared to the style you suggested is that your style simply doesn't scale. 与您建议的风格相比,最大的区别在于您的风格根本无法扩展。 If, for example the top operator would have two big subtrees, they would have been drawn extremely far apart. 例如,如果顶级算子有两个大的子树,那么它们就会被画得相距甚远。

EDIT: A slightly more complex tree could be drawn like this: 编辑:可以像这样绘制一个稍微复杂的树:

Add
+---Sub
|    +---Div
|    |    +---p
|    |    +---q
|    +---y
+---Mul
     +---b
     +---c

EDIT: On request, here comes some pseudo-code (which, incidentally, is acceptable by a Ruby interpreter). 编辑:根据要求,这里有一些伪代码(顺便提一下,Ruby解释器可以接受)。 You must, however, use a suitable C++ data structure to represent the tree. 但是,您必须使用合适的C ++数据结构来表示树。

# Return the drawn tree as an array of lines.
#
# node ::= string
# node ::= [string, node, node]
def render_tree(node, prefix0 = "", prefix = "")
  if (node.is_a?(String))
    puts prefix0 + node         # Value
  else
    puts prefix0 + node[0]      # Operator
    render_tree(node[1], prefix  + "+---", prefix + "|    ")
    render_tree(node[2], prefix  + "+---", prefix + "     ")
  end
end
render_tree(["Add", ["Sub", ["Div", "p", "q"], "y"], ["Mul", "b", "c"]])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM