/**
* 中缀表达式转后缀表达式
* 1+((2+3)*4)-5 -> 1 2 3 + 4 * + 5 *
* <p>
* 1)初始化两个栈:运算栈s1和储存中间结果的栈s2;
* 2)从左至右扫描中缀表达式;
* 3)遇到操作数时,将其压入s2;
* 4)遇到运算符时,比较其与s1栈顶运算符的优先级;
* <p>
* 1.如果s1为空,或栈顶运算符为左括号“(”,则直接将次运算符入栈;
* 2.否则,若优先级比栈顶运算符的高,也将运算符压入s1;
* 3.否则,将s1栈顶的运算符弹出并压入到s2中,再次转到(4.1)与s1中新的栈顶运算符相比较;
* <p>
* 5)遇到括号时:
* <p>
* (1)如果是左括号“(”,则直接压入s1;
* (2)如果是右括号“)”,则依次弹出s1栈顶的运算符,并压入s2,直到遇到左括号为止,此时将着一对括号丢弃
* <p>
* 6)重复步骤2至5,直到表达式的最右边;
* 7)将s1中剩余的运算符一次弹出并压入s2;
* 8)依次弹出s2中的元素并输出,结果的逆序为中缀表达式对应的后缀表达式;
*/
public class PolandNotation2 {
public static void main(String[] args) {
String expresssion = "1+((2+3)*4)-5";
List<String> infixExpressionList = toInfixExpressionList(expresssion);
System.out.println("中缀表达式对应的List:" + infixExpressionList);
List<String> list = parseSuffixExpression(infixExpressionList);
System.out.println("后缀表达式对应的List:" + list);
System.out.println("expresssion=" + Operation.calcuate(list));
}
//中缀表达式对应的List转后缀表达式对应的List
//1+((2+3)*4)-5 -> 1 2 3 + 4 * + 5 *
public static List<String> parseSuffixExpression(List<String> ls) {
Stack<String> s1 = new Stack<>();//符号栈
List<String> s2 = new ArrayList<>();//储存中间结果的List
//遍历ls
for (String item : ls) {
//如果是一个数,加入到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();//此时将着一对括号丢弃
} else {
//当item的优先级小于等于s1栈顶运算符,将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
}
//中缀表达式对应的List
public static List<String> toInfixExpressionList(String s) {
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) {
str += c;
i++;
}
ls.add(str);
}
} while (i < s.length());
return ls;
}
}
//返回运算符对应的优先级
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)从左至右扫描,将3和4压入堆栈
* 2)遇到+运算符,因此弹出4和3,计算3+4,得7,再将7入栈
* 3)将5入栈
* 4)接下来时*运算符,因此弹出5和7,计算7*5=35,将35入栈
* 5)6入栈
* 6)最后时-运算符,计算35-6的值,即39
*/
public static int calcuate(List<String> ls) {
Stack<String> stack = new Stack<>();
//遍历ls
for (String l : ls) {
//正则表达式取出数
if (l.matches("\\d+")) {//匹配多位数
stack.push(l);
} else {
//pop出两个数,运算
int num2 = Integer.parseInt(stack.pop());
int num1 = Integer.parseInt(stack.pop());
int res = 0;
if (l.equals("+")) {
res = num1 + num2;
} else if (l.equals("-")) {
res = num1 - num2;
} else if (l.equals("*")) {
res = num1 * num2;
} else if (l.equals("/")) {
res = num1 / num2;
} else {
throw new RuntimeException("运算符有误");
}
stack.push(res + "");
}
}
return Integer.parseInt(stack.pop());
}
}
中缀表达式转后缀表达式
最新推荐文章于 2024-06-20 19:31:44 发布