package com.atguigu.stack;
import com.sun.org.apache.xerces.internal.impl.xpath.XPath;
import org.w3c.dom.ls.LSException;
import java.util.ArrayList;
import java.util.Calendar;
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 infixExpressionList = toInfixExpressionList(expression);
System.out.println("中缀表达式对应的List="+infixExpressionList );//ArrayList[1,+,(,(,2,+,3,),*,4,),-,5]
List suffixExpressionList = parseSuffixExpressionList(infixExpressionList);
System.out.println("后缀表达式对应的List="+suffixExpressionList);//ArrayList[1,2,3,+,4,*,+,5,-]
System.out.printf("expression=%d",calculate(suffixExpressionList));
}
//方法:将得到的中缀表达式对应的List=》后缀表达式对应的List
//即:ArrayList[1,+,(,(,2,+,3,),*,4,),-,5]=》ArrayList[1,2,3,+,4,*,+,5,-]
public static List parseSuffixExpressionList(List list) {
//定义两个栈
Stack s1 = new Stack<>();//符号栈
//说明:由于s2这个栈没有pop操作,且后续需要逆序输出,所以直接用List去替换
//Stack s2 = new Stack<>();//存放中间结果的栈
List s2 = new ArrayList<>();//存放中间结果的List s2
//遍历list
for (String s : list) {
if (s.matches("\d+")) {//遇到数,直接入s2
s2.add(s);
} else if (s.equals("(")) {//遇到(,直接入s1
s1.push(s);
} else if (s.equals(")")) {
//如果遇到右括号,从s1弹出运算符,压入s2,直到遇到左括号为止
while (!s1.peek().equals("(")) {
s2.add(s1.pop());
}
s1.pop();//将“(”弹出
} else {//遇到运算符
//当s的优先级小于或等于s1栈顶运算符优先级时,将s1栈顶运算符弹出并压入s2,再次与s1栈中新的栈顶运算符比较
//问题:缺少一个比较运算符优先级大小的方法
while (s1.size() != 0 && Operation.getValue(s1.peek())>=Operation.getValue(s)){
s2.add(s1.pop());
}
//还需要将s压入栈中
s1.push(s);
}
}
//将s1中剩余的运算符加入到s2中
while (s1.size()!=0){
s2.add(s1.pop());
}
return s2;//因为是存放到List中,因此按顺序输出就是对应的后缀表达式对应的List
}
//方法:将中缀表达式转为对应的List
public static List toInfixExpressionList(String str) {
List list = new ArrayList<>();
int i = 0;
String s = "";//对多位数的拼接
char c;//遍历到一个字符就放到c中
do { //0->48 9->57
if ((c = str.charAt(i)) < 48 || (c = str.charAt(i)) > 57) {//非数
list.add(c + "");
i++;
} else {//数字,考虑多位数的问题
s = "";//先清空s
while (i < str.length() && (c = str.charAt(i)) >= 48 && (c = str.charAt(i)) <= 57) {
s += c;
i++;
}
list.add(s);
}
} while (i < str.length());
return list;
}
//将一个逆波兰表达式依次将数据和运算符放入到ArrayList中
public static List getListString(String suffixExpression) {
String[] split = suffixExpression.split(" ");
List list = new ArrayList<>();
for (String ele : split) {
list.add(ele);
}
return list;
}
//完成对逆波兰表达式的计算
public static int calculate(List ls) {
Stack stack = new Stack<>();
for (String str : ls) {
//使用正则表达式来取数
if (str.matches("\d+")) {//匹配的是多位数
stack.push(str);
} else {
int num2 = Integer.parseInt(stack.pop());
int num1 = Integer.parseInt(stack.pop());
int res = 0;
if (str.equals("+")) {
res = num1 + num2;
} else if (str.equals("-")) {
res = num1 - num2;
} else if (str.equals("*")) {
res = num1 * num2;
} else if (str.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("不存在该运算符");
break;
}
return result;
}
}