package stack;
import java.util.ArrayList;
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<String> infixExpressionList = toInfixExpressionList(expression);
System.out.println(infixExpressionList);
List<String> parseSuffixExpressionList = parseSuffixExpressionList(infixExpressionList);
System.out.println(parseSuffixExpressionList);
System.out.printf("expression=%d",calculate(parseSuffixExpressionList));
// 先定义逆波兰表达式
// (3+4)*5-6--》3 4 + 5 * 6 -
// String suffixExpression = "4 5 * 8 - 60 + 8 2 / +";
// 先将suffixExpression放入ArrayList中
// 将ArrayList传递给一个方法,配合栈完成计算
// List<String> rpnList = getListString(suffixExpression);
// int res=calculate(rpnList);
// System.out.println(res);
}
public static List<String> parseSuffixExpressionList(List<String> ls) {
Stack<String> s1 = new Stack<>();// 符号栈
List<String> s2 = new ArrayList<>();// 中间结果
// 遍历ls
for (String item : ls) {
// 如果是一个数,加入到s2
if (item.matches("\\d+")) {
s2.add(item);
} else if (item.equals("(")) {
s1.push(item);
} else if (item.equals(")")) {
while (!s1.peek().equals("(")) {
s2.add(s1.pop());
}
s1.pop();// 将左括号弹出s1
} else {
// 当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
public static List<String> toInfixExpressionList(String s) {
// 定义一个List,存放中缀表达式对应的内容
List<String> ls = new ArrayList<>();
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 = "";
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> getListString(String 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>();
// 遍历list
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;
}
// 把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;
case "(":
break;
default:
System.out.println("不存在该运算符");
break;
}
return result;
}
}
中缀转后缀、简单计算器的实现
最新推荐文章于 2022-03-21 21:05:34 发布