Prefix Notation

来源:互联网 发布:邪恶镰刀 数据 编辑:程序博客网 时间:2024/06/05 20:01
Polish Notation
1.Introduction
Polish notation, also known as Polish prefix notation or simply prefix notation, is a form of notation for logicarithmetic, and algebra. Its distinguishing feature is that it places operators to the left of their operands
For instance, the expression can be written in conventional infix notation as (5-6)*7,can be written in prefix as *-567. Next we will show how it changes from infix to prefix.
2.Algorithms
1)Create a stack to store operators or brackets
2) Scan the expression from the right to left. If the current character is the digit, then output it, and if it is the operator, then compare the priority and push it into the stack if it is high priority.
3) Continue the step 2, until all characters are scanned, and pop up all operators in the stack.
4) Output the result reversely.
3.Calculate
1) Scan from right to left, push the digits into a stack
2) If a operator occurs, then pop up the first both digits and calculate. After that, push the result into the stack.
3) Continue step 2 until it ends.
0 0
原创粉丝点击