中缀到前缀的转换
我正在准备一项考试,其中我无法理解以下表达式的中缀表示法到抛光表示法的转换:
(a–b)/c*(d + e – f / g)
任何人都可以逐步告诉如何将给定表达式转换为前缀吗?
I am preparing for an exam where i couldn't understand the convertion of infix notation to polish notation for the below expression:
(a–b)/c*(d + e – f / g)
Can any one tell step by step how the given expression will be converted to prefix?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(14)
(a–b)/c*(d + e – f / g)
前缀表示法(逆波兰操作符在最后,不清楚你指的是哪一个,但原理是完全相同的) ):
(/ fg)
(+ de)
(- (+ de) (/ fg))
(- ab)< /code>
(/ (- ab) c)
(* (/ (- ab) c) (- (+ de) (/ fg)))
(a–b)/c*(d + e – f / g)
Prefix notation (reverse polish has the operator last, it is unclear which one you meant, but the principle will be exactly the same):
(/ f g)
(+ d e)
(- (+ d e) (/ f g))
(- a b)
(/ (- a b) c)
(* (/ (- a b) c) (- (+ d e) (/ f g)))
如果您不太理解中缀和前缀的含义,我强烈建议您重读教科书的该部分。如果你得出了这个问题的正确答案,但仍然不理解这个概念,那么你对自己没有任何好处。
从算法角度来看,它非常简单。你自己的行为有点像一台计算机。首先按照计算顺序在每个计算周围放置括号。然后(再次按照从第一个计算到最后一个计算的顺序)只需将运算符移到其左侧表达式的前面即可。之后,您可以通过删除括号来简化。
If there's something about what infix and prefix mean that you don't quite understand, I'd highly suggest you reread that section of your textbook. You aren't doing yourself any favors if you come out of this with the right answer for this one problem, but still don't understand the concept.
Algorithm-wise, its pretty darn simple. You just act like a computer yourself a bit. Start by puting parens around every calculation in the order it would be calculated. Then (again in order from first calculation to last) just move the operator in front of the expression on its left hand side. After that, you can simplify by removing parens.
步骤 1:
(ab)/c*(d+e- /fg))
步骤 2:
(ab)/c*(+de - /fg)
步骤 3 :
(ab)/c * -+de/fg
步骤 4:
-ab/c * -+de/fg
步骤 5:
/-abc * - +de/fg
步骤 6:
*/-abc-+de/fg
这是前缀表示法。
step 1:
(a-b)/c*(d+e- /fg))
step 2:
(a-b)/c*(+de - /fg)
step 3:
(a-b)/c * -+de/fg
Step 4:
-ab/c * -+de/fg
step 5:
/-abc * -+de/fg
step 6:
*/-abc-+de/fg
This is prefix notation.
我在 youtube 上看到了这个方法,因此发布在这里。
给定中缀表达式:(a–b)/c*(d + e – f / g)
将其反转:
从左到右读取字符。
维护一组操作员
积分:youtube
I saw this method on youtube hence posting here.
given infix expression : (a–b)/c*(d + e – f / g)
reverse it :
read characters from left to right.
maintain one stack for operators
credits : youtube
(a–b)/c*(d + e – f / g)
记住从最左边到最右边扫描表达式
从括号内的术语开始
遵循先到先得的规则...
*、/、% 处于同一水平并高于
+ 和 -...所以
(ab) = -bc 前缀
(ab) = bc- 用于后缀
另一个带括号的术语:
(d + e - f / g) = 首先移动 /
然后先加“+”,然后再减“-”(记住它们在同一水平上..)
(d + e - f / g)
移动/先
(d + e - (/fg)) = 前缀
(d + e - (fg/)) = 后缀
然后是 + 然后 -
((+de) - (/fg)) = 前缀
((de+) - (fg/)) = 后缀
(-(+de)(/fg)) = 前缀,因此新表达式现在为 -+de/fg (1)
((de+)(fg/)-) = 后缀,因此新表达式现在为 de+fg/- (2)
(a–b)/c* 因此
(ab)/c*(d + e – f /g) = -bc 前缀[-ab]/c*[-+de/fg] --->摘自(1)
/c * 暂时不要动
所以 '/' 首先出现在 '*' 之前,因为它们在同一级别,移动 '/'
最右边:/[-ab]c * [-+de/fg]
然后将“*”移动到最右边
(ab)/c*(d + e – f / g) = bc- 后缀 [ab-]/c*[de+fg/-]--->摘自(2)
所以 '/' 首先出现在 '' 之前,因为它们在同一级别,移动 '/'
最左边:[ab-]c[de+fg/-]/
然后将“”移动到最左边
[ab-] c [de+fg/-]/ = 删除分组符号= ab - cde + fg / - / * -->后缀
(a–b)/c*(d + e – f / g)
remember scanning the expression from leftmost to right most
start on parenthesized terms
follow the WHICH COMES FIRST rule...
*, /, % are on the same level and higher than
+ and -.... so
(a-b) = -bc prefix
(a-b) = bc- for postfix
another parenthesized term:
(d + e - f / g) = do move the / first
then plus '+' comes first before minus sigh '-' (remember they are on the same level..)
(d + e - f / g)
move / first
(d + e - (/fg)) = prefix
(d + e - (fg/)) = postfix
followed by + then -
((+de) - (/fg)) = prefix
((de+) - (fg/)) = postfix
(-(+de)(/fg)) = prefix so the new expression is now -+de/fg (1)
((de+)(fg/)-) = postfix so the new expression is now de+fg/- (2)
(a–b)/c* hence
(a-b)/c*(d + e – f / g) = -bc prefix [-ab]/c*[-+de/fg] ---> taken from (1)
/ c * do not move yet
so '/' comes first before '*' because they on the same level, move '/'
to the rightmost : /[-ab]c * [-+de/fg]
then move '*' to the rightmost
(a-b)/c*(d + e – f / g) = bc- for postfix [ab-]/c*[de+fg/-]---> taken from (2)
so '/' comes first before '' because they on the same level, move '/'
to the leftmost: [ab-]c[de+fg/-]/
then move '' to the leftmost
[ab-] c [de+fg/-]/ = remove the grouping symbols= a b - c d e + f g / - / * --> Postfix
简单的谷歌搜索就得到了这个。怀疑任何人都可以更简单地解释这一点。但我想在编辑之后,我可以尝试提出这些概念,以便您可以回答您自己的问题。
提示:
为了考试而学习,你必须努力。预测你,成绩会变高,我会的:D
解释:
这都是关于操作与操作数关联的方式。每种符号类型都有自己的规则。您只需要分解并记住这些规则即可。如果我告诉你我把 (2*2)/3 写为 [* /] (2,2,3),你所需要做的就是学习如何将后一个符号转换为前一个符号。
我的自定义表示法表示,将前两个操作数相乘,然后将所得操作数除以第三个操作数。得到它 ?他们试图教你三件事。
simple google search came up with this. Doubt anyone can explain this any simpler. But I guess after an edit, I can try to bring forward the concepts so that you can answer your own question.
Hint :
Study for exam, hard, you must. Predict you, grade get high, I do :D
Explanation :
It's all about the way operations are associated with operands. each notation type has its own rules. You just need to break down and remember these rules. If I told you I wrote (2*2)/3 as [* /] (2,2,3) all you need to do is learn how to turn the latter notation in the former notation.
My custom notation says that take the first two operands and multiple them, then the resulting operand should be divided by the third. Get it ? They are trying to teach you three things.
这个算法将帮助你更好地理解。
步骤 1. 将“)”推入 STACK,并在 A 末尾添加“(”。
步骤 2. 从右向左扫描 A,对 A 的每个元素重复步骤 3 至 6,直到 STACK 为空。
步骤 3.如果遇到操作数,则将其添加到 B。
步骤 4。 如果遇到右括号,则将其推入 STACK
步骤 5。 如果遇到运算符,则:
一个。重复从 STACK 中弹出并添加到 B 每个具有相同的操作符(在 STACK 的顶部)
或比运算符更高的优先级。
b.将运算符添加到堆栈。
步骤 6. 如果遇到左括号则
一个。反复从STACK中弹出并添加到B(每个运算符都在堆栈顶部,直到遇到左括号)
b.删除左括号。
步骤 7. 退出
This algorithm will help you for better understanding .
Step 1. Push “)” onto STACK, and add “(“ to end of the A.
Step 2. Scan A from right to left and repeat step 3 to 6 for each element of A until the STACK is empty.
Step 3. If an operand is encountered add it to B.
Step 4. If a right parenthesis is encountered push it onto STACK.
Step 5. If an operator is encountered then:
a. Repeatedly pop from STACK and add to B each operator (on the top of STACK) which has same
or higher precedence than the operator.
b. Add operator to STACK.
Step 6. If left parenthesis is encontered then
a. Repeatedly pop from the STACK and add to B (each operator on top of stack until a left parenthesis is encounterd)
b. Remove the left parenthesis.
Step 7. Exit
https://en.wikipedia.org/wiki/Shunting-yard_algorithm
步骤:
https://en.wikipedia.org/wiki/Shunting-yard_algorithm
step through:
这是一个用于将中缀转换为前缀并评估前缀表达式的java实现(基于rajdip的算法)
here is an java implementation for convert infix to prefix and evaluate prefix expression (based on rajdip's algorithm)
在前缀表达式中,首先是运算符,然后是操作数: +ab[ oprator ab ]
中缀:
(a–b)/c*(d + e – f / g)
步骤 1:
(a - b) = (- ab)
[ '(' 具有最高优先级 ]步骤 2:
(d + e - f / g) = ( d + e - / fg)
[ '/' 具有最高优先级 ]步骤 3:
(-ab )/ c * (- + de / fg)
=/ - abc * (- + de / fg)
前缀:
* / - abc - + de / fg
In Prefix expression operators comes first then operands : +ab[ oprator ab ]
Infix :
(a–b)/c*(d + e – f / g)
Step 1:
(a - b) = (- ab)
[ '(' has highest priority ]step 2:
(d + e - f / g) = (d + e - / fg)
[ '/' has highest priority ]Step 3:
(-ab )/ c * (- + de / fg)
=/ - abc * (- + de / fg)
Prefix :
* / - abc - + de / fg
使用堆栈对 PostFix 进行中缀:
Infix to PostFix using Stack:
也许您正在谈论逆波兰表示法?如果是,您可以在维基百科上找到非常详细的转换步骤示例;如果不是,我不知道你在问什么:(
你可能还想阅读我对另一个问题的回答,我在其中提供了这样的实现:C++ 简单操作(+、-、/、*)评估类
Maybe you're talking about the Reverse Polish Notation? If yes you can find on wikipedia a very detailed step-to-step example for the conversion; if not I have no idea what you're asking :(
You might also want to read my answer to another question where I provided such an implementation: C++ simple operations (+,-,/,*) evaluation class
这是使用堆栈的算法。
只需遵循这些简单的步骤即可。
1.反转给定的中缀表达式。
2.将反转表达式中的“(”替换为“)”,将“)”替换为“(”。
3.现在将标准中缀应用于后缀子例程。
4.将建立的后缀表达式反转,得到所需的前缀表达式。
如果您发现第 3 步有困难,请参阅 http://scanftree.com/Data_Structure/infix-to-prefix
其中还给出了一个可行的例子。
This is the algorithm using stack.
Just follow these simple steps.
1.Reverse the given infix expression.
2.Replace '(' with ')' and ')' with '(' in the reversed expression.
3.Now apply standard infix to postfix subroutine.
4.Reverse the founded postfix expression, this will give required prefix expression.
In case you find step 3 difficult consult http://scanftree.com/Data_Structure/infix-to-prefix
where a worked out example is also given.