1, the definition of infix expression and why to convert infix expression to a 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. For a computer, it is simpler to calculate the value of a prefix or suffix expression than an infix expression.
For example, the computer computes the suffix expression as follows---- the computer evaluation of the suffix expression:
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 the operator to do the corresponding calculation (the top element of the second stack op stack top element), 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.
2, the infix expression is converted to the suffix-expression algorithm:
Here, only one stack is used to hold the operators that are encountered when scanning infix expressions. The operand of the operation during the scan is append directly to the end of the output expression
What are the conditions under which the operator presses into the stack?
If the currently scanned operator has precedence over the top of the stack operator, the stack is entered.
If the priority of the currently scanned operator is the same as the precedence of the top of the stack operator, you need to determine the binding direction of the current scan operator operation, if the binding direction is from left to right, it does not need to be in the stack, if the binding direction is from right to left, then stack. Where the addition , subtraction, multiplication, and addition operators are bound from left to right, while the exponentiation operator is in the right-to-left direction. Since the optimal level of the exponentiation operator is the highest and its binding direction is from right to left, the scan will be placed directly into the stack when the exponentiation operator is encountered.
? For the processing of parentheses in infix expressions
The left parenthesis is always pressed into the stack. Once the opening parenthesis is in the stack, it is treated as the operator with the lowest precedence, i.e. any subsequent operator will be pressed into the stack. When a closing parenthesis is encountered, the operators are popped from the stack and added to the end of the output expression until an opening parenthesis is popped (there are no parentheses in the suffix expression, and the parentheses do not need to be added to the output expression). Then, the algorithm continues ....
In the process of processing infix expressions from left to right, the following actions are performed according to the symbols encountered:
① operands each operand is added to the end of the output expression (the output expression is the result of the resulting suffix expression)
The ② operator ^ (exponentiation) ^ presses the stack (because the highest priority is given to exponentiation in all operators (plus, minus, multiply, divide, exponentiation), and the combination of exponentiation is right-to-left)
The ③ operator +-*/pops the operator from the stack and adds them to the end of the output expression until the stack is empty or the top of the stack is lower than the new operator and then presses the new operator into the stack
④ left bracket (pressed into stack
⑤ the Right parenthesis) pops the operator from the stack, adds them to the end of the output expression, until an opening parenthesis is popped, discarding the two parentheses
3, the specific infix expression to postfix expression of the Java Code implementation
Note: The stack used to hold the operator in the program is not the stack of the Java.util package in the JDK, but the stack that is implemented by itself. Reference: Using Java arrays to implement sequential stacks
1 Importlist. Sequencestack;2 Importlist. Stack;3 4 Public classPostfix {5 /*6 * @Task: Converting infix expressions to suffix expressions7 * @param: infix valid infix expression string8 * @return: Suffix expression string equivalent to Infix9 */Ten Public Staticstring Convert2postfix (String infix) { OneStringBuffer postfix =NewStringBuffer ();//Initializes a string buffer to hold the suffix expression generated during the conversion AStack<character> Operatorstack =NewSequencestack<character>(); - intCharactercount =infix.length (); - CharTopcharactor; the - for(intindex = 0; Index < Charactercount; index++){ - BooleanDone =false; - CharNextcharacter =Infix.charat (index); + if(Isvariable (nextcharacter)) -Postfix =postfix.append (nextcharacter); + Else{ A Switch(Nextcharacter) at { - Case^: - Operatorstack.push (nextcharacter); - Break; - Case' + ': Case‘-‘: Case‘*‘: Case‘/‘: - while(!done &&!)Operatorstack.empty ()) { inTopcharactor =Operatorstack.peek (); - if(Getprecedence (Nextcharacter) <=getprecedence (Topcharactor)) { toPostfix =postfix.append (topcharactor); + Operatorstack.pop (); - } the Else *Done =true;//when the top element of the stack pops, the priority of the nextcharacter is greater than the top of the stack $}//End WhilePanax NotoginsengOperatorstack.push (Nextcharacter);//when the Nextcharacter priority is greater than the top of the stack, then the nextcharacter push into the stack - Break; the Case‘(‘: + Operatorstack.push (nextcharacter); A Break; the Case‘)‘: +Topcharactor =Operatorstack.pop (); - while(Topcharactor! = ' ('){ $Postfix =postfix.append (topcharactor); $Topcharactor =Operatorstack.pop (); - } - Break; the default: Break; -}//End SwitchWuyi } the}//End for - Wu while(!Operatorstack.empty ()) { -Topcharactor =Operatorstack.pop (); AboutPostfix =postfix.append (topcharactor); $ } - returnpostfix.tostring (); - } - A Private Static intGetprecedence (Charoperator) { + Switch(operator) the { - Case‘(‘: Case‘)‘:return0;//actually only +-*/only need to call the function compare priority $ Case' + ': Case‘-‘:return1;//+-Priority level is 1 the Case‘*‘: Case‘/‘:return2;//*/priority is 2 the Case' ^ ':return3; the } the return-1; - } in the //operands are denoted by letters by default the Private Static BooleanIsvariable (Charcharactor) { About returnCharacter.isletter (charactor); the } the the //For test Purpose + Public Static voidMain (string[] args) { -String postfix = Convert2postfix ("a/b* (c + (D-E))"); the System.out.println (postfix);Bayi } the}
References: prefixes, infix, suffix expressions
Algorithm detailed explanation reference "data structure and algorithm Java language Description Second Edition"--frank M.carrano. 21st Chapter
Application of the stack in the prefix expression to the suffix expression