将以下infix表达式转换为后缀形式(使用堆栈)

发布于 2025-01-28 09:44:12 字数 81 浏览 2 评论 0原文

伙计们请其应有的ASAPP

将以下infix表达式转换为后缀表单(使用堆栈)。 (a + 2) *(b -c + d * e) + f

guys please its due asapp

Transform the following infix expression to postfix form (using stacks).
(A + 2) * (B - C + D * E) + F

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

左耳近心 2025-02-04 09:44:12

PostFix的想法是,您将操作数放在操作员的左侧,而不是将操作数放在两侧。这很好,因为它消除了对操作顺序的歧义,而无需括号。从infix表达式开始时,我们要转换为后缀,始终首先完全括号表达式表达式。您的表达方式是:

(A + 2) * (B - C + D * E) + F

根据标准操作顺序,相应的完全父母的表达式是:

(((A + 2) * ((B - C) + (D * E))) + F)

这清楚地清楚每个infix操作员的级别。最右后缀运算符应为最小括号数量包含的Infix操作员:在我们的情况下, +。这意味着我们的后缀表达式必须以 +结尾,并在其左侧有两个子表达式:

r s +

s的表达式很容易:F。r的表达式应该是与infix + operator的另一侧相对应的后缀表达式, ((a + 2) *(((b -c) +(d * e)))。因此,我们可以递归应用相同的过程以获取R的表达式。我们发现 * *是这里的最外部运算符,因此我们获得

r' s' *

R'将是(a + 2)的后缀表达式;这很容易,是2 +。另一个((b -c) +(d * e))有点棘手。它的最外面的操作是 +,因此

r'' s'' +

r''是b -c的后缀表达式,即bc-,s''是d * e的后缀表达式,即de *。因此,我们对S'的表达方式是

B C - D E * +

替代的,我们得到了R的表达:

A 2 + B C - D E * + *

最后,原始infix表达式的后缀版本:

A 2 + B C - D E * + * F +

这都说,我不确定如何确切地解释“使用堆栈” ...也许是这样。建议您想要的是一块代码,该代码会自动执行此过程。从概念上讲,这并不比我们所做的要难得多,但是写出所有代码是一件琐事。也许很高的描述就足够了:

  1. 将表达式将表达式化成一系列由操作员在顶层(不是括号内)分隔的子表达式,
  2. 从而通过使用简单的符号使用简单的符号来递归使用操作顺序,以递归递归的infix infix表达式来生成后缀表达式表达式。 ,并在子表达式上递归调用该方法,

请注意,作为应用操作顺序的替代方案,或者作为其实现,您可以首先在鉴于操作顺序的情况下产生完全存在的infix表达式,然后简单地改变infix( r#s)至rs#,大大简化了转换。

The idea with postfix is that you put the operand(s) to the left of the operator rather than on either side. This is nice because it removes ambiguity about the order of operations without the need for parentheses. When starting with an infix expression we want to convert to postfix, always fully parenthesize the infix expression first. Your expression is this:

(A + 2) * (B - C + D * E) + F

According to standard rules of order of operations, a corresponding fully-parenthesized expression is this:

(((A + 2) * ((B - C) + (D * E))) + F)

This makes it clear at what level each infix operator exists. The rightmost postfix operator should be the infix operator that is enclosed by the smallest number of parentheses: in our case, +. That means our postfix expression must end with + and have two subexpressions to the left of it:

r s +

The expression for s is easy: it is F. The expression for r should be the postfix expression corresponding to the other side of the infix + operator, ((A + 2) * ((B - C) + (D * E))). So, we can apply the same procedure recursively to get an expression for r. We find that * is the outermost operator here, so we get

r' s' *

r' will be a postfix expression for (A + 2); this one's easy, it's A 2 +. The other one, ((B - C) + (D * E)), is a little trickier. Its outermost operation is +, so

r'' s'' +

r'' is the postfix expression for B - C, which is B C -, and s'' is the postfix expression for D * E, which is D E *. So, our expression for s' is

B C - D E * +

Substituting back, we get an expression for r:

A 2 + B C - D E * + *

Finally, the postfix version of the original infix expression:

A 2 + B C - D E * + * F +

All this said, I am not sure exactly how to interpret "using stacks"... perhaps this suggests what you want is a piece of code that does this procedure automatically for any infix expression. That's not conceptually much harder than what we've done, but writing out all the code for it is a bit of a chore. Maybe a high level description would suffice:

  1. Tokenize the expression into a series of subexpressions separated by operators at the top level (not inside parentheses)
  2. Generate the postfix expression for the tokenized infix expression recursively by applying order of operations, using simple symbols as they are, and calling the method recursively on subexpressions

Note that as an alternative to applying order of operations, or perhaps as an implementation of it, you could first produce the fully-parenthesized infix expression given the order of operations, and then simply transform the infix (r # s) to r s #, simplifying the conversion considerably.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文