写在前面
栈的三种表达式
前缀表达式
流程
(3+4)*5-6
-> - * + 3 4 5 6
- 从右至左扫描,将
6 5 4 3
压入栈
- 遇到
+
运算符弹出3 4
,3为栈顶,4为次顶,计算得7
,压入栈
- 接下来
*
,弹出7 5
,计算得35
,再入栈
- 最后
-
,弹出35 6
,最终结果29
中缀表达式
- 常见的运算方式,但是对于计算机来说并不好操作
(3+4)*5-6
后缀表达式
- 逆波兰表达式,运算符位于操作数之后
(3+4)*5-6
-> 3 4 + 5 * 6 -
流程
- 从左至右扫描,将
3 4
压入栈
- 遇到
+
,弹出4 3
,4为栈顶,3为次顶,得7
入栈
5
入栈
- 遇到
*
,弹出5 7
得35
,入栈
- 最后
-
,弹出6 35
,但是栈底35
减去6
,得29
逆波兰计算器
//`(30+4)*5-6`
String suffixExpression = "30 4 + 5 * 6 -"
//先将表达式放入list中
System.out.println(calculate(getListString(suffixExpression)))
public static List<String> getListString(String suffixExpression) {
String[] split = suffixExpression.split(" ");
ArrayList<String> list = new ArrayList<>();
for (String s : split) {
list.add(s);
}
return list;
}
public static int calculate(List<String> list) {
Stack<String> stack = new Stack<>();
for (String s : list) {
if (s.matches("\d+")) {
stack.push(s);
} else {
int num2 = Integer.parseInt(stack.pop());
int num1 = Integer.parseInt(stack.pop());
int res = 0;
if (s.equals("+")) {
res = num1 + num2;
} else if (s.equals("-")) {
res = num1 - num2;
} else if (s.equals("*")) {
res = num1 * num2;
} else if (s.equals("/")) {
res = num1 / num2;
} else {
throw new RuntimeException("error");
}
stack.push("" + res);
}
}
return Integer.parseInt(stack.pop());
}
中缀表达式转换成后缀表达式
- 扫描完毕后执行7,s2栈的逆序为需要的后缀表达式

代码实现
- 小数的处理,通过正则表达式进行筛选
s.matches("\d+\.\d+") || s.matches("\d+")
- 表达式中的空格处理
expression = expression.replace(" ", "");
- 将中缀表达式转换成list
public static List<String> toInfixExpressionList(String expression) {
List<String> list = new ArrayList<>();
int i = 0;
String str;
char c;
do {
if (((c = expression.charAt(i)) < 48) || ((c = expression.charAt(i)) > 57)) {
list.add("" + c);
i ++;
} else {
str = "";
while (i < expression.length()
&& ((((c = expression.charAt(i)) >= 48) && ((c = expression.charAt(i)) <= 57))
|| ((c = expression.charAt(i)) == 46))) {
str += c;
i++;
}
list.add(str);
}
} while (i < expression.length());
return list;
}
public static List<String> parseSuffixExpressionList(List<String> list) {
Stack<String> stack = new Stack<>();
List<String> resList = new ArrayList<>();
for (String s : list) {
if (s.matches("\d+\.\d+") || s.matches("\d+")) {
resList.add(s);
} else if (s.equals("(")) {
stack.push(s);
} else if(s.equals(")")) {
while (!stack.peek().equals("(")) {
resList.add(stack.pop());
}
stack.pop();
} else {
while (stack.size() != 0 && Operation.getValue(stack.peek()) >= Operation.getValue(s)) {
resList.add(stack.pop());
}
stack.push(s);
}
}
while (stack.size() != 0){
resList.add(stack.pop());
}
return resList;
}
class Operation{
private static int ADD = 1;
private static int SUB = 1;
private static int MUL = 2;
private static int DIV = 2;
private static int LEFTPARE = 0;
public static int getValue(String operation) {
int res = 0;
switch (operation) {
case "+":
res = ADD;
break;
case "-":
res = SUB;
break;
case "*":
res = MUL;
break;
case "/":
res = DIV;
break;
case "(":
res = LEFTPARE;
break;
default:
System.out.println("Operation is error.");
break;
}
return res;
}
}
public static double calculate(List<String> list) {
Stack<String> stack = new Stack<>();
for (String s : list) {
if (s.matches("\d+\.\d+") || s.matches("\d+")) {
stack.push(s);
} else {
double num2 = Double.parseDouble(stack.pop());
double num1 = Double.parseDouble(stack.pop());
double res = 0;
if (s.equals("+")) {
res = num1 + num2;
} else if (s.equals("-")) {
res = num1 - num2;
} else if (s.equals("*")) {
res = num1 * num2;
} else if (s.equals("/")) {
res = num1 / num2;
} else {
throw new RuntimeException("error");
}
stack.push("" + res);
}
}
return Double.parseDouble(stack.pop());
}