1.分析

2.代码
package com.atguigu.stack;
import java.util.ArrayList;
import java.util.List;
import java.util.Stack;
public class PolandNotation {
public static void main(String[] args) {
String expression = "1+((2+3)*4)-5";
List<String> infixExpressionList = toInfixExpressionList(expression);
System.out.println(infixExpressionList);
List<String> sufffixExpressionList = parseSuffixExpressionList(infixExpressionList);
System.out.println(sufffixExpressionList);
}
public static List<String> toInfixExpressionList(String s){
List<String> ls = new ArrayList<String>();
int i = 0;
String str;
char c;
do {
if (((c=s.charAt(i)) < 48) || ((c=s.charAt(i)) > 57)){
ls.add(""+c);
i++;
}else {
str = "";
while ((i < s.length()) && ((c=s.charAt(i)) >= 48) && ((c=s.charAt(i)) <= 57)){
str+=c;
i++;
}
ls.add(str);
}
}while (i<s.length());
return ls;
}
public static List<String> parseSuffixExpressionList(List<String> ls){
Stack<String> s1 = new Stack<String>();
List<String> s2 = new ArrayList<String>();
for (String item:ls){
if (item.matches("\\d+")){
s2.add(item);
}else if (item.equals("(")){
s1.push(item);
}else if (item.equals(")")){
while (!s1.peek().equals("(")){
s2.add(s1.pop());
}
s1.pop();
}else {
while (s1.size() != 0 && Operation.getValue(s1.peek()) >= Operation.getValue(item)){
s2.add(s1.pop());
}
s1.push(item);
}
}
while (s1.size() != 0){
s2.add(s1.pop());
}
return s2;
}
}
class Operation{
private static int ADD = 1;
private static int SUB = 1;
private static int MUL = 2;
private static int DIV = 1;
public static int getValue(String operation){
int result = 0;
switch (operation){
case "+":
result = ADD;
break;
case "-":
result = SUB;
break;
case "*":
result = MUL;
break;
case "/":
result = DIV;
break;
default:
System.out.println("运算符错误");
break;
}
return result;
}
}