代码实现
package com.atguigu.stack;
import java.util.ArrayList;
import java.util.List;
import java.util.Stack;
/**
* @author WHT
* @create 2021-06-11 9:22
*/
public class PolandNotation {
public static void main(String[] args) {
// 完成将一个中缀表达式转成后缀表达式的功能。
// 说明:
// 1. 1+((2+3)×4)-5 => 转成 1 2 3 + 4 × + 5 –
// 2. 因为直接对str 进行操作,不方便,因此 先将 "1+((2+3)×4)-5" =》 中缀的表达式对应的List
// 即 "1+((2+3)×4)-5" => ArrayList [1,+,(,(,2,+,3,),*,4,),-,5]
// 3. 将得到的中缀表达式对应的List => 后缀表达式对应的List
// 即 ArrayList [1,+,(,(,2,+,3,),*,4,),-,5] =》 ArrayList [1,2,3,+,4,*,+,5,–]
String expression = "1+((2+3)*4)-5";
List<String> infixExpressionList = toInfixExpressionList(expression);
System.out.println("中缀表达式对应的List=" + infixExpressionList); // ArrayList [1,+,(,(,2,+,3,),*,4,),-,5]
List<String> suffixExpressionList = parseSuffixExpressionList(infixExpressionList);
System.out.println("后缀表达式对应的List=" + suffixExpressionList); // ArrayList [1,2,3,+,4,*,+,5,–]
System.out.printf("expression=%d", calculate(suffixExpressionList));
}
// 完成对逆波兰表达式的运算。
/*
* 1)从左至右扫描,将3和4压入堆栈;
2)遇到+运算符,因此弹出4和3(4为栈顶元素,3为次顶元素),计算出3+4的值,得7,再将7入栈;
3)将5入栈;
4)接下来是×运算符,因此弹出5和7,计算出7×5=35,将35入栈;
5)将6入栈;
6)最后是-运算符,计算出35-6的值,即29,由此得出最终结果。
*/
public static int calculate(List<String> ls) {
// 创建栈, 只需要一个栈即可。
Stack<String> stack = new Stack<String>();
// 遍历 ls。
for (String item: ls) {
// 这里使用正则表达式来取出数。
if (item.matches("\\d+")) { // 匹配的是多位数。
// 入栈。
stack.push(item);
}
else {
// pop出两个数,并运算, 再入栈。
int num2 = Integer.parseInt(stack.pop());
int num1 = Integer.parseInt(stack.pop());
int res = 0;
if (item.equals("+")) {
res = num1 + num2;
}
else if (item.equals("-")) {
res = num1 - num2;
}
else if (item.equals("*")) {
res = num1 * num2;
}
else if (item.equals("/")) {
res = num1 / num2;
}
else {
throw new RuntimeException("运算符有误~~");
}
// 把 res 入栈。
stack.push("" + res);
}
}
// 最后留在stack中的数据是运算结果。
return Integer.parseInt(stack.pop());
}
// 将 中缀表达式 转成对应的 List 。
public static List<String> toInfixExpressionList(String s) {
// 定义一个List,存放 中缀表达式 对应的内容。
List<String> ls = new ArrayList<String>();
int index = 0; // 用于遍历中缀表达式字符串。
String str; // 对多位数的拼接。
char c; // 每遍历到一个字符,就放入到c。
while (true) {
if (index > s.length() - 1) {
break;
}
else {
c = s.charAt(index);
// 如果c是一个非数字,则需要加入到ls。
if (c < 48 || c > 57) {
ls.add("" + c);
index++; // 遍历。
}
// 如果是一个数,则需要考虑多位数。
else {
str = ""; // str 清空。
while (index < s.length() && s.charAt(index) >= 48 && s.charAt(index) <= 57) {
str += s.charAt(index); // 拼接。
index++; // 遍历。
}
ls.add(str);
}
}
}
return ls; // 返回转换成的List。
}
//将得到的中缀表达式对应的List => 后缀表达式对应的List。
//即 ArrayList [1,+,(,(,2,+,3,),*,4,),-,5] =》 ArrayList [1,2,3,+,4,*,+,5,–]
public static List<String> parseSuffixExpressionList(List<String> ls) {
Stack<String> s1 = new Stack<String>(); // 符号栈。
List<String> s2 = new ArrayList<String>(); // 结果List。
// 遍历ls
for (String item: ls) {
// 如果是一个数,加入s2。
if (item.matches("\\d+")) {
s2.add(item);
}
else if (item.equals("(")) {
s1.push(item);
}
// 如果是右括号“)”,则依次弹出s1栈顶的运算符,并压入s2,直到遇到左括号为止,此时将这一对括号丢弃。
else if (item.equals(")")) {
String temp = s1.pop();
while (true) {
if (temp.equals("(")) {
break;
}
else {
s2.add(temp);
temp = s1.pop();
}
}
}
// 当item的优先级小于等于s1栈顶运算符, 将s1栈顶的运算符弹出并加入到s2中。
else {
while (s1.size() != 0 && Operation.getValue(item) <= Operation.getValue(s1.peek()) ) {
s2.add(s1.pop());
}
// 还需要将item压入栈。
s1.push(item);
}
}
// 将s1中剩余的运算符依次弹出并加入s2。
while (s1.size() != 0) {
s2.add(s1.pop());
}
return s2; // 因为是存放到List, 因此按顺序输出就是对应的后缀表达式对应的List。
}
// 返回运算符的优先级,优先级是程序员来确定, 优先级使用数字表示,数字越大,则优先级就越高。
// 假定目前的表达式只有 +, -, *, /。
public int priority(int oper) {
if (oper == '*' || oper == '/') {
return 1;
}
else if (oper == '+' || oper == '-') {
return 0;
}
else {
return -1;
}
}
}
// 编写一个类 Operation 可以返回一个运算符对应的优先级。
class Operation {
private static int ADD = 1;
private static int SUB = 1;
private static int MUL = 2;
private static int DIV = 2;
// 写一个方法,返回对应的优先级数字。
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;
}
}
运行结果
D:\java\jdk-11.0.11\bin\java.exe "-javaagent:D:\IntelliJ IDEA 2020.1.1\lib\idea_rt.jar=1593:D:\IntelliJ IDEA 2020.1.1\bin" -Dfile.encoding=UTF-8 -classpath E:\WHT\Project\Java\DataStructures\out\production\DataStructures com.atguigu.stack.PolandNotation
中缀表达式对应的List=[1, +, (, (, 2, +, 3, ), *, 4, ), -, 5]
不存在该运算符~~
不存在该运算符~~
后缀表达式对应的List=[1, 2, 3, +, 4, *, +, 5, -]
expression=16
Process finished with exit code 0