:中缀表达式 转 后缀表达式
package com.atguigu.stack;
//后缀表达式
import java.util.List;
import java.util.Stack;
import java.lang.management.OperatingSystemMXBean;
import java.util.ArrayList;
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" => 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";
System.out.println("原运算表达式: 1+((2+3)*4)-5");
List<String> infixExpressionList = toInfixExpressionList(expression);
System.out.println("中缀表达式对应List: " + infixExpressionList);// [1,+,(,(,2,+,3,),*,4,),-,5]
List<String> suffixExpressionList = parseSuffixExpressionList(infixExpressionList);
System.out.println("后缀表达式对应List: " + suffixExpressionList);// [1,+,(,(,2,+,3,),*,4,),-,5]
System.out.printf("结果 expression = %d", calculate(suffixExpressionList));
// 先定义逆波兰表达式
// (3+4)*5-6 -> 3 4 + 5 * 6 - =29
// (30+4)*5-6 -> 30 4 + 5 * 6 - =164
// 4*5-8+60+8/2->=76 -->4 5 * 8 - 60 + 8 2 / +
// String suffixExpression = "4 5 * 8 - 60 + 8 2 / +";
/*
* 思路 1.先将"3 4 + 5 * 6 -"放在ArrayList中 2.将ArrayList传递给一个方法,遍历ArrrayList配合栈完成计算
*/
/*
* List<String> rpnList = getListString(suffixExpression);
* System.out.println("rpnList="+rpnList); int result = calculate(rpnList);
* System.out.println("计算的结果是:"+ result);
*/
}
// 即ArrayList[1,+,(,(,2,+,3,),*,4,),-,5]-》ArrayList[1,2,3,+,4,*,+,5,-]
// 方法 将得到的中缀表达式对应的List-》后缀表达式对应的List
public static List<String> parseSuffixExpressionList(List<String> ls) {
// 定义两个栈
Stack<String> s1 = new Stack<String>();// 符号栈
/*
* 说明:因为s2这个栈,在整个转换过程,没有pop操作,且后面需要逆序输出 比较麻烦,因此这里不用Stack<String> 直接使用
* List<String> s2
*/
// Stack<String> s2 = new Stack<String>();//储存中间结果的栈s2
List<String> s2 = new ArrayList<String>();// 储存中间结果的List s2
// 遍历
for (String item : ls) {
// 如果是数字,加入 s2
if (item.matches("\\d+")) {
s2.add(item);
} else if (item.equals("(")) {
s1.push(item);
} else if (item.equals(")")) {
// 如果遇到 “)” 则依次弹出s1栈顶的运算符,直到遇到”(“,然后将这对括号丢弃
while (!s1.peek().equals("(")) {
s2.add(s1.pop());
}
s1.pop();// !!! 弹出s1栈的左括号,消除了一对括号
} else {
// 当item的优先级<=s1栈顶运算符,将s1弹出加入到s2,
// 问题:缺少一个比较运算符优先级的方法
while (s1.size() != 0 && Operation.getValue(s1.peek()) >= Operation.getValue(item)) {
s2.add(s1.pop());
}
// 还需要将item压入栈
s1.push(item);
}
}
// 将s1中剩余的运算符依次弹出并加入到s2
while (s1.size() != 0) {
s2.add(s1.pop());
}
return s2;// 存放在List,无需再逆序输出
}
// 方法:将中缀表达式转换成后缀表达式
// s="1+((2+3)*4)-5";
public static List<String> toInfixExpressionList(String s) {
// 定义一个List,存放中缀表达式对应的内容
List<String> ls = new ArrayList<String>();
int i = 0;// 指针,用于遍历中缀表达式字符串
String str;// 多位数的拼接
char c;// 每遍历到一个字符,就放入到c
do {// 如果c是非数字,加入到ls
if ((c = s.charAt(i)) < 48 || (c = s.charAt(i)) > 57) {
ls.add("" + c);
i++;// i后移
} else {// 如果是一个数,需要考虑多位数
str = "";// 先将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;
}
// 将一个逆波兰表达式,依次将数据和运算符放入到ArrayList中
public static List<String> getListString(String suffixExpression) {
// 将suffixExpression分割
String[] split = suffixExpression.split(" ");
List<String> list = new ArrayList<String>();
for (String ele : split) {
list.add(ele);
}
return list;
}
// 完成对逆波兰表达式的计算
public static int calculate(List<String> ls) {
// 创建栈,只需一个栈
Stack<String> stack = new Stack<String>();
// 遍历
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());
}
}
//编写类 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("不存在该运算符 " + '"' + operation + '"');
break;
}
return result;
}
}
运行结果: