前言:
由于前面中缀表达式运算起来代码量太大,比较复杂,我们考虑转成后缀表达式进行计算。
思路分析:
1.
2.
3.
直接上一波代码
package cn.itjy.Stack;
import java.util.ArrayList;
import java.util.List;
import java.util.Stack;
/**
* 中缀表达式转后缀表达式的代码
*
* @author dell
*
*/
public class PloandNotation {
public static void main(String[] args) {
// 完成将一个中缀表达式转成后缀表达式的功能
// 说明
/*
* 1.1+((2+3)x4)-5 => 转成123 + 4 x + 5 - 2.先将直接对字符串进行操作,不太方便 ,因此先将 1+((2+3)x4)-5
* ==> 转成中缀表达式对应的list 即 1+((2+3)x4)-5 ==> ArrayList[1,+,(,(,2,+,3,),*,4,),-,5]
*
*/
String expression = "1+((2+3)*4)-5";
List<String> list = toInfixExpressionList(expression);
System.out.println("Infix: "+list);
List<String> expression2 = parseSuffixExpression(list);
System.out.println("suffix: "+expression2);
System.out.println(calculate(expression2));
/*
* // 先定义一个逆波兰表达式 // (3+4)*5-6 ==> 3 4 + 5 * 6 - // 说明:为了方便,逆波兰表达式的数字和符号使用空格隔开
* String suffixExpression = "3 4 + 5 * 6 -"; // 思路
*
* 1.先将 "3 4 + 5 * 6 -" ==> 放到一个ArrayList中 2.将ArrayList 传递给一个方法,遍历ArrayList 配合栈
* 完成计算
*
* List<String> rpnList = getListString(suffixExpression); int res =
* calculate(rpnList); System.out.println(res);
*/
}
// ArrayList[1,+,(,(,2,+,3,),*,4,),-,5] => ArrayList[1,2,3,+,4,*,+,5,-]
// 将中缀表达式转成对应的list
public static List<String> parseSuffixExpression(List<String> ls) {
// 定义两个栈
Stack<String> s1 = new Stack<>(); // 符号栈
// 因为s2这个栈,在整个转换过程中,没有pop操作,而且后面我们还需要逆序输出
// 因此比较麻烦,这里我们就不用Stack<String>直接使用List<String> s2
// Stack<String> s2 = new Stack<>(); //存储中间结果的栈s2
List<String> s2 = new ArrayList<String>();
// 遍历ls
for (String s3 : ls) {
// 如果是一个数,加s2
if (s3.matches("\\d+")) {
s2.add(s3);
} else if (s3.equals("(")) {
s1.push(s3);
} else if (s3.equals(")")) {
// 如果是右括号)依次弹出s1栈顶的运算符,并压入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()) >= Operation.getValue(s3)) {
s2.add(s1.pop());
}
// 还需要将item压入栈
s1.push(s3);
}
}
// 將s1剩余的运算符加入s2中
while(s1.size() !=0) {
s2.add(s1.pop());
}
// 因为是存放到List中,因此按顺序输出就是对应的后缀表达式的输出
return s2;
}
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去
// 小于1大于9判定为括号和+-*/
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;
}
}
// 将逆波兰表达式,依次将数据和运算符饭放入到 ArrayList中
public static List<String> getListString(String suffixExpression) {
// 将suffixExpression 分割
String[] strings = suffixExpression.split(" ");
List<String> list = new ArrayList<String>();
for (String string : strings) {
list.add(string);
}
return list;
}
// 完成对逆波兰表达式的运算
/*
* 1.从右到做扫描,将3和4压入堆栈; 2.遇到运算符,因此弹出4和3(4为堆顶元素,3为次顶元素),计算3和4的值 3.将5入栈
* 4.接下来是x的运算符,因此弹出5和7,计算7 x 5 的值,将35入栈 将6入栈 最后是-运算符 ,计算出35-6的值,即29,由此得到最终的结果
*/
public static int calculate(List<String> list) {
// 创建一个栈,只需要一个栈即可
Stack<String> stack = new Stack<String>();
// 遍历 list
for (String item : list) {
// 这里使用正则表达式来取出数
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());
}
}
// 编写一个类 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 OperExpression) {
int result = 0;
switch (OperExpression) {
case "+":
result = ADD;
break;
case "-":
result = SUB;
break;
case "*":
result = MUL;
break;
case "/":
result = DIV;
break;
default:
System.out.println("不存在该运算符");
break;
}
return result;
}
}
总结:这次的代码还是比较费脑细胞的。代码还是比较有难度的。