The specific steps are as follows:
1) Initialize two stacks: operator stack s1 and stack s2 for storing intermediate results;
2) Scan infix expression from left to right;
3) When encountering an operand, press it to s2;
4) When an operator is encountered, compare its priority with s1 stack top operator:
(1) If s1 is empty, or the stack top operator is an open bracket "("), this operator is directly put on the stack;
(2) Otherwise, if the priority is higher than that of the stack top operator, the operator is also pressed into s1;
(3) Otherwise, pop up the operator at the top of s1 stack and press it into s2, go to (4-1) again and compare it with the new operator at the top of s1;
5) When parentheses are encountered:
(1) If it is an open bracket "(", press s1 directly
(2) If it is the closing bracket ")", the operators at the top of s1 stack will pop up in turn and press s2 until the left bracket is encountered. At this time, this pair of brackets will be discarded
6) Repeat steps 2 through 5 until the rightmost part of the expression
7) Pop up the remaining operators in s1 and press them into s2
8) Pop up the elements in s2 and output them. The reverse order of the result is the suffix expression corresponding to the infix expression
Infix expression "1 + ((2 + 3) × 4) The process of converting "5" to suffix expression is as follows
Scanned elements | S2 (stack bottom - > stack top) | s1 (stack bottom - > stack top) | explain |
1 | 1 | empty | Number, direct stack |
+ | 1 | + | s1 is null, and the operator is directly put on the stack |
( | 1 | + ( | Left parenthesis, direct stack |
( | 1 | + ( ( | ditto |
2 | 1 2 | + ( ( | number |
+ | 1 2 | + ( ( + | s1 the top of the stack is left parenthesis, and the operator is directly put on the stack |
3 | 1 2 3 | + ( ( + | number |
) | 1 2 3 + | + ( | Right parenthesis, pop up the operator until the left parenthesis is encountered |
× | 1 2 3 + | + ( × | s1 the top of the stack is left parenthesis, and the operator is directly put on the stack |
4 | 1 2 3 + 4 | + ( × | number |
) | 1 2 3 + 4 × | + | Right parenthesis, pop up the operator until the left parenthesis is encountered |
- | 1 2 3 + 4 × + | - | -The priority is the same as + so pop up + and press in- |
5 | 1 2 3 + 4 × + 5 | - | number |
Reach the rightmost end | 1 2 3 + 4 × + 5 - | empty | Operators remaining in s1 |
The result is "1 2 3 + 4" × + 5 –"
package com.wang; import java.util.*; public class Test { public static void main(String[] args) { //Infix expression String exp="1+((2+3)*4)-5"; List<String> inlist=toExp(exp); System.out.println("Infix expression: "+inlist); //Corresponding suffix expression List<String> parseList = parseList(inlist); System.out.println("Corresponding suffix expression: "+parseList); /*String Poland="3 4 + 5 * 6 -"; //First, the expression is stored in Arralist, and the calculation is completed by traversing Arralist and matching stack List<String> getlist = getlist(Poland); System.out.println(getlist);*/ int res=compute(parseList); System.out.println("result: "+res); } public static List<String> getlist(String Poland) { //Divide the string by space and store it in the array String[] split = Poland.split(" "); ArrayList<String> arrayList = new ArrayList<String>(); //Traverse the array into the collection for (String i :split) { arrayList.add(i); } return arrayList; } public static int compute(List<String> list) { Stack<String> stack=new Stack<String>(); for (String i : list) { //Use regular expressions to extract multiple digits if (i.matches("\\d+")) { //Push stack.push(i); }else { //Take out two operations and put them on the stack int num2=Integer.parseInt(stack.pop()); int num1=Integer.parseInt(stack.pop()); int res=0; if(i.equals("+")) { res=num1+num2; }else if (i.equals("-")) { res=num1-num2; }else if (i.equals("*")) { res=num1*num2; } else if (i.equals("/")) { res=num1/num2; } stack.push(res+""); } } return Integer.parseInt(stack.pop()); } //Store infix expressions in the set, that is, convert them to 1, +, (, (, 2, +, 3,), *, 4,), -, 5 public static List<String> toExp(String s){ ArrayList<String> ls = new ArrayList<String>(); //Used to traverse infix strings int i=0; //Multi bit number for splicing String str; //Each bit is put into c char c; do { //If it is a character, add ls if((c=s.charAt(i))<48||(c=s.charAt(i))>59) { ls.add(""+c); i++; }else { //If it is a number, you need to consider whether there are multiple numbers //Reset str before each splice str=""; while (i<s.length()&&(c=s.charAt(i))>=48&&(c=s.charAt(i))<=59) { str+=c; i++; } ls.add(str); } } while (i<s.length()); return ls; } //The infix expression is divided into two parts //Stored in symbol stack and intermediate result stack public static List<String> parseList(List<String> ls){ //Symbol stack Stack<String> s1 = new Stack<String>(); //Intermediate result stack ArrayList<String> s2 = new ArrayList<String>(); for (String s : ls) { //If it is a number, add ls1 if (s.matches("\\d+")) { s2.add(s); }else if (s.equals("(")) { s1.push(s); }else if (s.equals(")")) { //If it is a right parenthesis, the operators at the top of s1 stack will pop up in turn and press s2 until the left parenthesis is is encountered while (!s1.peek().equals("(")) { s2.add(s1.pop()); } //Pop out parentheses s1.pop(); }else { while(s1.size()!=0&&Oper.getv(s1.peek())>=Oper.getv(s)) { s2.add(s1.pop()); } s1.push(s); } } //Pop up the remaining operators in s1 and add s2 while(s1.size() != 0 ) { s2.add(s1.pop()); } return s2; } } class Oper{ private static int ADD=1; private static int SUB=1; private static int MUL=1; private static int DIV=1; public static int getv(String oper) { int su=0; switch (oper) { case "+": su=ADD; break; case "-": su=SUB; break; case "*": su=MUL; break; case "/": su=DIV; break; } return su; } }