package com.pro.stack;
import java.util.ArrayList;
import java.util.List;
import java.util.Stack;
public class ReversePolishNotation {
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
// 即"" => ArrayList[1, +,(,(,2, +,3,),*, 4,),-, 5 ]
//3.将得到的中缀表达式对应的List => 后缀表达式对应的List
// ArrayList[1, +, (, (, 2, +, 3, ), *, 4, ), -, 5] ==> [1, 2, 3, +, 4, *, +, 5, -]
String expression="1+((2+3)*4)-5";
List<String> infixExpressionList=toInfixExpressionList(expression);
System.out.println(infixExpressionList);
List<String> suffixExpressionList=parseSuffixExpressionList(infixExpressionList);
System.out.println("后缀表达式:"+suffixExpressionList);//[1, 2, 3, +, 4, *, +, 5, -]
int res = calculate(suffixExpressionList);
System.out.println("计算的结果是=" + res);
// 先定义一个逆波兰表达式
// (3+4)*5-6 => 3 4 + 5 * 6 -
// (30+4)*5-6 => 30 4 + 5 * 6 -
// 4*5-8+60+8 =>
/* String suffixExpression = "4 5 * 8 - 60 + 8 2 / +";
// 1.先将"3 4 + 5 * 6 - "==>放入ArrayList
// 2.将ArrayList 传递给 一个方法,遍历ArrayList配合栈完成计算
List<String> list = getListString(suffixExpression);
System.out.println("rpnList=" + list);
int res = calculate(list);
System.out.println("计算的结果是=" + res);
*/
}
// 将逆波兰式放入ArrayList中
public static List<String> getListString(String suffixExpression) {
// 将suffixExpression分割放入ArrayList
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>();
// 遍历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("运算符有误");
}
// 将结果入栈
stack.push("" + res);
}
}
// 将栈中的最后一个结果返回
return Integer.parseInt(stack.pop());
}
// 写一个方法将中缀表达式转成对应的List
public static List<String> toInfixExpressionList(String s) {
// 定义一个List,存放中缀表达式对应的内容
List<String> ls = new ArrayList<String>();
int i = 0;// 指针,用于遍历中缀表达式的字符串
String str;// 用于多位数的拼接
char c; // 存放字符
do {
// 如果c是一个非数字,加入到ls
if ((c = s.charAt(i)) < 48 || (c = s.charAt(i)) > 57) {
ls.add("" + c);
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;
}
//
public static List<String> parseSuffixExpressionList(List<String> ls) {
//定义两个栈
Stack<String> s1=new Stack<String>();//符号栈
//由于s2在整个转换过程中,没有pop操作.而且我们还需要逆序输出,
//因此我们使用List<String>代替
List<String> s2=new ArrayList<String>();
//遍历ls
for (String item : ls) {
//如果是一个数,加入s2
if(item.matches("\\d+")){
s2.add(item);
}else if(item.equals("(")){
s1.push(item);
}else if(item.equals(")")){
//如果是右括号")" 则依次弹出栈顶的运算符,并压入s2,直到遇到左括号为止,此时将这一对括号丢弃
while(!s1.peek().equals("(")){
s2.add(s1.pop());
}
s1.pop();//将对应的"("弹出s1栈,消除小括号
}else{
//当item的小于等于s1栈顶运算符,将s1栈顶的运算符弹出并加入到s2中,再次转到(4.1)与s1中新的栈顶运算符相比较
//缺少一个比较优先级高低的方法
while(s1.size()!=0&&Operation.getValue(s1.peek().charAt(0))>=Operation.getValue(item.charAt(0)) ){
s2.add(s1.pop());
}
//还需要把item压入栈
s1.push(item);
}
}
//将s1中剩余的运算符依次弹出并加入s2
while (s1.size()!=0) {
s2.add(s1.pop());
}
return s2;//注意由于存放到List中,因此安顺序输出就是对应的后缀表达式
}
}
//编写一个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(char operation) {
int result=0;
switch(operation) {
case '+':
result=ADD;
break;
case '-':
result=SUB;
break;
case '/':
result=DIV;
break;
case '*':
result=MUL;
break;
default:
System.out.println("不存在该运算符");
break;
}
return result;
}
}