package Stack.后缀表达式转中缀表达式;
import java.util.ArrayList;
import java.util.List;
import java.util.Stack;
/**
* @author 郑道炫
* @version 1.0
*/
public class PolandNotation {
public static void main(String[] args) {
//完成将一个中缀表达式转成后缀表达式的功能
//说明
//1.1+((2+3)*4)-5=>转成1 2 3 + 4* + 5 -
//即1+((2+3)*4)-5=>ArrayList[1,+,(,2,+,3,)*4,),-,5]
String expression = "1+((2+3)*4)-5";
List<String> list = toInfixExpressionList(expression);
System.out.println(list);//Arraylist
//将得到的中缀表达式对应的List=>后缀表达式对应的List
//即ArrayList[[1, +, (, (, 2, +, 3, ), *, 4, ), -, 5]]
//转成 [1,2,3,+,4,*,+,5,-]
List<String> strings = parseSuffixExpressionList(list);
System.out.println("后缀表达式list"+strings);
System.out.printf("expression=%d",calculate(strings));
// 中缀表达式list[1, 2, 3, +, 4, *, +, 5, -]
}
//将得到的中缀表达式对应的List=>后缀表达式对应的List
//即ArrayList[[1, +, (, (, 2, +, 3, ), *, 4, ), -, 5]]
//转成 [1,2,3,+,4,*,+,5,-]
public static List<String> parseSuffixExpressionList(List<String> list){
//定义两个栈
Stack<String>s1=new Stack<String>();//符号栈
//说明:因为s2这个栈,在整个转换过程中,没有pop操作,而且后面我们还需要逆序输出
//因此比较麻烦,这里我们就不用Stack<String>直接使用List<String>s2
List<String>s2=new ArrayList<>();//存放中间结果的栈时
//遍历list
for (String item :list) {
//如果是一个数,就加入s2
if (item.matches("\\d+")){
s2.add(item);
}else if (item.equals("(")){
s1.push(item);
}else if (item.equals(")")){
//如果是有括号")",则依次弹出s1栈顶的运算符,并压入s2,直接遇到左括号为止,此时将这一对括号丢弃
while (!s1.peek().equals("(")){
s2.add(s1.pop());
}
s1.pop();//将这一个(弹出s1栈,消除小括号
}else {
//当item的优先级小于等于栈顶运算符的优先级,将s1栈顶的运算符弹出并压入到s2中,再次转到(4.1)与s1中新的栈顶
//问题:我们缺一个比较优先级高低的方法
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,因此按顺序输出就是对应的逆波兰表达式
}
//编写一个方法,将中缀表达式转成对应的List
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++;
} else {
//如果是一个数,需要考虑多位数
str = "";//先将str 制程""
while (i < s.length() && (c = s.charAt(i)) >= 48 && (c = s.charAt(i)) <= 57) {
//0 [48]~9[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> lists = new ArrayList<>();
for (String string : split) {
lists.add(string);
}
return lists;
}
//1.从左到右进行扫描,将3和4放入堆栈
//2.遇到+运算符,因此弹出4和3,计算出3+4的值,得7,再将7入栈,
//3.将5入栈
//4.接下来是x运算符,因此弹出5和7,计算出7*5=35,将35入栈;
//5.将6入栈,
//6.最后是-运算符,计算出35-6的值,即29,由此得出最终结果
//
//
public static int calculate(List<String> ls) {
//创建栈,只需要一个栈即可
Stack<String> stack = new Stack<>();
//遍历 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;
switch (item) {
case "+":
res = num1 + num2;
break;
case "-":
res = num1 - num2;
break;
case "/":
res = num1 / num2;
break;
case "*":
res = num1 * num2;
break;
default:
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;
}
}
结果:
中缀表达式:[1, +, (, (, 2, +, 3, ), *, 4, ), -, 5]
不存在该运算符
不存在该运算符
后缀表达式list[1, 2, 3, +, 4, *, +, 5, -]
expression=16