They are all notation for expressions, so they are also referred to as prefix notation, infix notation, and postfix notation. The difference between them is that the operator is relative to the position of the operand: the operator of the prefix expression precedes the operand associated with it, and the infix and suffix are the same.
Example:
(3 + 4) x5-6 is an infix expression.
-x+ 3 4 5 6 prefix expression
3 4 + 5x6-suffix expression
infix expression (infix notation)
Infix expression is a general arithmetic or logical formula representation method, the operator is in infix form in the middle of the operand. Infix expression is a commonly used arithmetic representation method.
Although the human brain is easy to understand and analyze infix expression, but to the computer infix expression is very complex, so when calculating the value of an expression, it is usually necessary to first convert infix expression to prefix or suffix expression, and then evaluate. Calculating the value of a prefix or suffix expression is straightforward for a computer.
prefix expression (prefix notation, Polish style)
The operator of the prefix expression precedes the operand.
computer evaluation of the prefix expression:
A right-to-left scan expression, when the number is encountered, the number is pressed into the stack, when the operator is encountered, the top two number of pop-up stack, with operators to do the corresponding calculation (stack top element op sub-top element), and the results into the stack;
For example, the prefix expression "-x+ 3 4 5 6":
(1) From right to left scanning, 6, 5, 4, 3 is pressed into the stack;
(2) encountered the + operator, so pop up 3 and 4 (3 is the top element of the stack, 4 is the second top element, pay attention to the suffix expression comparison), calculate the value of the 3+4, 7, and then 7 into the stack;
(3) Next is the x operator, so eject 7 and 5, calculate the 7x5=35, 35 into the stack;
(4) The last is the-operator, which calculates the value of 35-6, or 29, resulting in the final result.
As you can see, it is easy to compute the value of the prefix expression with a computer.
converts an infix expression to a prefix expression:
Follow these steps:
(1) Initialize two stacks: operator stack S1 and stack S2 for storing intermediate results;
(2) Scanning infix expression from right to left;
(3) When the operand is encountered, it is pressed into the S2;
(4) When an operator is encountered, compare its precedence with the top operator of the S1 stack:
(4-1) If the S1 is empty, or the top operator of the stack is the closing parenthesis ")", the operator is placed directly into the stack;
(4-2) Otherwise, if the priority is higher or equal than the top operator of the stack, the operator is also pressed into the S1;
(4-3) Otherwise, the S1 stack top operator pops up and presses into the S2, and again goes to (4-1) compared with the new stack top operator in S1;
(5) When the parentheses are encountered:
(5-1) If the closing parenthesis ")", then press directly into the S1;
(5-2) If the opening parenthesis "(", then pops up the operator at the top of the S1 stack, and presses into the S2 until the closing parenthesis is encountered, this pair of parentheses is discarded;
(6) Repeat steps (2) to (5) until the leftmost side of the expression;
(7) The remaining operators in the S1 are popped and pressed into the S2;
(8) The elements in the S2 pop-up and output, the result is the infix expression corresponding to the prefix expression.
For example, the process of converting infix expression "1+ ((2+3) x4)-5" to a prefix expression is as follows:
The element that is scanned |
S2 (Stack bottom, top of stack) |
S1 (Stack bottom, top of stack) |
Description |
5 |
5 |
Empty |
Numbers, directly into the stack |
- |
5 |
- |
S1 is empty, operator directly into stack |
) |
5 |
- ) |
Right parenthesis directly into the stack |
4 |
5 4 |
- ) |
Digital direct into the stack |
X |
5 4 |
-) x |
S1 stack top is the right parenthesis, directly into the stack |
) |
5 4 |
-) x) |
Right parenthesis directly into the stack |
3 |
5 4 3 |
-) x) |
Digital |
+ |
5 4 3 |
-) x) + |
S1 stack top is the right parenthesis, directly into the stack |
2 |
5 4 3 2 |
-) x) + |
Digital |
( |
5 4 3 2 + |
-) x |
Opening parenthesis, popup operator until a closing parenthesis is encountered |
( |
5 4 3 2 +x |
- |
Ditto |
+ |
5 4 3 2 +x |
- + |
Priority vs.-Same, in-stack |
1 |
5 4 3 2 +x1 |
- + |
Digital |
Reach the left End |
5 4 3 2 +x1 +- |
Empty |
The remaining operators in the S1 |
So the result is "-+ 1x+ 2 3 4 5".
Package Com.ztesoft.zsmart.datastruct.stack;import Java.util.stack;public class Infixtoprefixexp {public static Strin G Ops = "() */%+-"; public static void Main (string[] args) {System.out.println (Infix2prefix ("1+ (2+3)-5")); }/** * infix expression converted to prefix expression: <br> * * @author wang.jun<br> * @taskId <br> * @param exp * @return <br> */public static string Infix2prefix (String exp) {StringBuffer result = new Strin Gbuffer (); Put the expression into the stack stack<character> expstack = Exp2stack (exp); Initializes an operator stack of two stacks with an intermediate result stack stack<string> s1 = new stack<string> (); stack<string> s2 = new stack<string> (); Right-to-left scan expression while (!expstack.isempty ()) {String Exppart = read (Expstack); Determines whether the operand is an if (!isoperator (Exppart)) {S2.push (Exppart); } else {opinstack (S1, S2, Exppart); }}//The remaining operators in S1 into S2 while (!s1.isempty ()) {S2.push (S1.pop ()); }//Processing result while (!s2.isempty ()) {Result.append (S2.pop ()); } return result.tostring (); /** * Operator: <br> * * @author wang.jun<br> * @taskId <br> * @param s1 * @p Aram S2 * @param CH <br> * * public static void Opinstack (stack<string> s1, stack<string> S2, String ch) {//When encountering parentheses: if (". Equals (ch) | |") ". Equals (CH)) {//If the opening parenthesis is directly into S1 if (")". Equals (CH)) {S1.push (CH); } else {//if the closing parenthesis pops up the S1 top operator, and presses into the S2 until the closing parenthesis is met, the String ch1 = "" is discarded; do {ch1 = S1.pop (); if (")". Equals (CH1)) {break; } else {S2.push (CH1);}} while (true); }} else {//if S1 is empty or the top of the stack is a closing parenthesis) then directly into the operand stack if (s1.isempty () | | | s1.lastelement (). Equals (")" )) {S1.push (CH); } else {String s1top = s1.lastelement (); if (Compareoplevel (CH, s1top)) {//If the precedence is higher or equal than the top operator of the stack, the operator is also pressed into S1; S1.push (CH); } else {do {s1top = S1.pop (); S2.push (S1top); S1top = S1.lastelement (); if (Compareoplevel (CH, s1top)) {//If the precedence is higher or equal than the top operator of the stack, the operator is also pressed into S1; S1.push (CH); Break }} while (true); }}}}/** * read operand from stack or operator: <br> * Operand consists of one or more data composition operators consisting of one character * * @auth or wang.jun<br> * @taskId <br> * @param stack * @return <br> * * public static String read (stack<character> Stack) {StringBuffer sb = new StringBuffer (); Char ch; do {ch = stack.pop (); Sb.append (CH); if (!stack.isempty ()) {Char ch_next = stack.lastelement (); if (Isoperator (ch_next) | | isoperator (CH)) {break; }} else {break; }} while (!stack.isempty ()); return sb.tostring (); }/** * To determine if the current character is an operator: <br> * * @author wang.jun<br> * @taskId <br> * @param op * @return <br> */public static Boolean isoperator (Character ch) {return Ops.indexof (ch.tostring () ) >-1; }/** * To determine if the current character is an operator: <br> * * @author wang.jun<br> * @taskId <br> * @param op * @return <br> * * public static Boolean Isoperator (String exp) {return exp.length () = = 1 && ops.indexof (exp.tostring ()) & Gt -1; }/** * Put the expression in the stack: <br> * * @author wang.jun<br> * @taskId <br> * @param exp * @return <br> */public static stack<character> Exp2stack (String exp) {stack<character> St ACK = new stack<character> (); for (int i = 0; i < exp.length (); i++) {Stack.push (Exp.charat (i)); } return stack; }/** * Judgment operator precedence Description: <br> * * @author wang.jun<br> * @taskId <br> * @pa Ram OP1 * @param OP2 * @return <br> */public static Boolean Compareoplevel (string op1, String op2) { if (Ops.indexof (OP1) < Ops.indexof (OP2)) {return true; } return false; }}
suffix expression (suffix notation, inverse polish)
A suffix expression is similar to a prefix expression, except that the operator is behind an operand.
computer evaluation of suffix expression:
Similar to prefix expressions, except that the order is from left to right:
From left to right scan the expression, when the number is encountered, the number is pressed into the stack, when the operator is encountered, the top two number of pop-up stack, with operators to do the corresponding calculation (the top element of the OP stack top elements), and the results into the stack;
For example, the suffix expression "3 4 + 5x6-":
(1) scan from left to right, press 3 and 4 into the stack;
(2) encountered the + operator, so pop up 4 and 3 (4 is the top element of the stack, 3 is the second top element, note the comparison with the prefix expression), calculate the value of the 3+4, 7, and then 7 into the stack;
(3) 5 into the stack;
(4) Next is the x operator, so eject 5 and 7, calculate the 7x5=35, 35 into the stack;
(5) 6 into the stack;
(6) The last is the-operator, which calculates the value of 35-6, or 29, resulting in the final result.
converts an infix expression to a suffix expression:
Similar to converting to prefix expressions, follow these steps:
(1) Initialize two stacks: operator stack S1 and stack S2 for storing intermediate results;
(2) Scanning infix expression from left to right;
(3) When the operand is encountered, it is pressed into the S2;
(4) When an operator is encountered, compare its precedence with the top operator of the S1 stack:
(4-1) If the S1 is empty, or the top operator of the stack is an opening parenthesis "(", the operator is put directly into the stack;
(4-2) Otherwise, if the precedence is higher than the top of the stack operator, the operator is also pressed into the S1 (note that the conversion to a prefix expression is higher or the same priority, but this does not include the same situation);
(4-3) Otherwise, the S1 stack top operator pops up and presses into the S2, and again goes to (4-1) compared with the new stack top operator in S1;
(5) When the parentheses are encountered:
(5-1) If the opening parenthesis "(", then press directly into the S1;
(5-2) If the closing parenthesis ")", then pops the operator at the top of the S1 stack and presses it into the S2 until the opening parenthesis is encountered, discarding the parentheses at this time;
(6) Repeat steps (2) to (5) until the rightmost side of the expression;
(7) The remaining operators in the S1 are popped and pressed into the S2;
(8) Pop-up the elements in S2 and output, the reverse of the result is the infix expression corresponding to the suffix expression (converted to prefix expression without reverse order).
For example, the process of converting infix expression "1+ ((2+3) x4)-5" to a suffix expression is as follows:
The element that is scanned |
S2 (Stack bottom, top of stack) |
S1 (Stack bottom, top of stack) |
Description |
1 |
1 |
Empty |
Numbers, directly into the stack |
+ |
1 |
+ |
S1 is empty, operator directly into stack |
( |
1 |
+ ( |
Left parenthesis, directly into the stack |
( |
1 |
+ ( ( |
Ditto |
2 |
1 2 |
+ ( ( |
Digital |
+ |
1 2 |
+ ( ( + |
S1 Stack top is left parenthesis, operator directly into stack |
3 |
1 2 3 |
+ ( ( + |
Digital |
) |
1 2 3 + |
+ ( |
Closing parenthesis, popup operator until an opening parenthesis is encountered |
X |
1 2 3 + |
+ (x |
S1 Stack top is left parenthesis, operator directly into stack |
4 |
1 2 3 + 4 |
+ (x |
Digital |
) |
1 2 3 + 4x |
+ |
Closing parenthesis, popup operator until an opening parenthesis is encountered |
- |
1 2 3 + 4x+ |
- |
-Same as + priority, so eject +, press in- |
5 |
1 2 3 + 4x+ 5 |
- |
Digital |
Reach the right End |
1 2 3 + 4x+ 5- |
Empty |
The remaining operators in the S1 |
So the result is "1 2 3 + 4x+ 5-" (Note the need for reverse output).
Write a Java program to convert an infix expression to a prefix expression and a suffix expression, and evaluate the value of the expression. The Topolishnotation () method converts infix expressions to prefix expressions (Polish), Toreversepolishnotation () methods to convert infix expressions to suffix expressions (inverse polish):
prefix, infix, suffix expression