<> Infix expression to suffix expression

Analysis of the thinking steps of infix expression to suffix expression .

* Initializing a stack and a queue , Operator stack S1 And queues that store intermediate results Q1
* Scan infix expression from left to right
* Put on queue when an operand is encountered Q1 in
* When an operator is encountered , Compare it with S1 Priority of stack top operators
* If S1 Empty , Or stack top operator is left bracket ( , The operator is directly pushed into the stack
* otherwise , If the priority is higher than the top of stack operator , Also press operators into S1
* otherwise , take S1 The operator at the top of the stack pops up and adds to the Q1 in , Go to again (4.1) And S1 Comparison of new stack top operators in
* When parentheses are encountered
* If it's left parenthesis ( , Then press in directly S1
* If it's right bracket ) , Then pop up one by one S1 Operators at the top of the stack , And add to Q1, Until left parenthesis is encountered , Discard this pair of parentheses
* Repeat step 2 to 5, Up to the far right of the expression
* take S1 The remaining operators in pop-up and add to Q1
* Take out one by one Q1 Element in and output , The result is the suffix expression corresponding to the infix expression .
<> Implementation code
package com.codestd.study.stack; import org.apache.commons.lang3.StringUtils;
import java.util.ArrayDeque; import java.util.Queue; import java.util.Stack;
/** * Postfix Expression * * @author jaune * @since 1.0.0 */ public class SuffixExpression {
/** * Infix expression to suffix expression * * @param expression expression */ public String toSuffix(String
expression) { String[] expItems = StringUtils.split(expression, ' '); Stack<
String> operator = new Stack<>(); Queue<String> exp = new ArrayDeque<>(); for (
int i = 0; i < expItems.length; i++) { String expItem = expItems[i]; if (this.
isNumber(expItem)) { exp.add(expItem); } else if (this.isOperator(expItem)) {
this.operatorHandle(operator, exp, expItem); } else if (this.isBracket(expItem))
{ if (this.isOpeningBracket(expItem)) { operator.push(expItem); } else { while (
!operator.peek().equals("(")) { exp.add(operator.pop()); } operator.pop(); } } }
while (!operator.isEmpty()) { exp.add(operator.pop()); } return StringUtils.join
(exp, " "); } private void operatorHandle(Stack<String> operator, Queue<String>
exp, String s) { if (operator.isEmpty() || this.isOpeningBracket(operator.peek()
)) { operator.push(s); } else if (this.getPriority(s) > this.getPriority(
operator.peek())) { operator.add(s); } else { exp.add(operator.pop()); this.
operatorHandle(operator, exp, s); } } private boolean isNumber(String s) {
return s.matches("\\d+"); } private boolean isOperator(String s) { return "+".
equals(s) || "-".equals(s) || "*".equals(s) || "/".equals(s); } private boolean
isBracket(String s) { return "(".equals(s) || ")".equals(s); } private boolean
isOpeningBracket(String s) { return "(".equals(s); } private int getPriority(
String s) { if ("+".equals(s) || "-".equals(s)) { return 1; } else if ("*".
equals(s) || "/".equals(s)) { return 2; } else { throw new RuntimeException(
" This symbol is not recognized "); } } }
<> Suffix expression evaluation

Suffix expressions are very suitable for computer evaluation .1 + ( ( 2 + 3 ) * 4 ) - 5 The corresponding suffix expression is 1 2 3 + 4 * + 5 -.
The calculation method is very simple , Create a stack S1, Scan expressions from left to right , When a value is encountered, it is placed in the stack ; Operator encountered , From the stack S1 Two values pop up in , And evaluates the expression , Then re stack .

Suffixes are easy to evaluate , There's no code here . Interested students can realize the following by themselves .

Technology
©2019-2020 Toolsou All rights reserved,
Science fiction comes true !“ Trisomy ” Found out Linux File name validity detection Obviously post Why does the request display parameters in the address bar ?( Essence )2020 year 7 month 13 day Wechat applet Inter communication page Vue Development tips Centos7 lower mysql8.0 To install and change the initial password ;springboot Multi profile configuration Note 14 : The second biggest obstacle to motivating others in R & D management ELementUI select Multi select drop-down box to get all properties of the selected item ( Essence 2020 year 6 month 2 Daily update ) TypeScript Function explanation