中缀转后缀的思路
一、思路
1、中缀–>顺序Arraylist集合 2、顺序ArrayList集合–>逆缀ArrayList形式
3、逆缀ArrayList形式–>计算步骤
二、源代码
代码如下(示例):
package Stack;
import java.util.ArrayList;
import java.util.List;
import java.util.Stack;
//采用Arraylist和Stack实现逆波兰表达式
public class PolandNotation {
public static void main(String[] args) {
/* String Expression = "4 5 * 8 - 60 + 8 2 / +";
List<String> stringlist = getStringlist(Expression);
System.out.println(stringlist);
int calculate = calculate(stringlist);
System.out.println("========");
System.out.println(calculate);*/
String InfixExpression = "1+((2+3)*4)-5";
List<String> list = toInfixExpressionList(InfixExpression);
System.out.println(list);
List<String> list1 = parseSuffixExpressionList(list);
System.out.println(list1);
System.out.println("========");
int result = calculate(list1);
System.out.println(result);
}
public static List<String> getStringlist(String Expression) {
List<String> list = new ArrayList<>();
String[] split = Expression.split(" "); //返回是一个String数组
//遍历数组,将其中的元素取出来依次加入到list中
for (String item : split) {
list.add(item);
}
return list;
}
//计算公式
public static int calculate(List<String> list) {
Stack<String> stack = new Stack<>();
//注意这里item是字符串的形式
for (String item : list) {
if (item.matches("\\d+")) {
stack.push(item);
} else {
int num1 = Integer.parseInt(stack.pop());
int num2 = Integer.parseInt(stack.pop());
int result = 0;
if (item.equals("+")) {
result = num1 + num2;
stack.push(String.valueOf(result));
}
if (item.equals("-")) {
result = num2 - num1;
stack.push(String.valueOf(result));
}
if (item.equals("*")) {
stack.push(String.valueOf(num1 * num2));
}
if (item.equals("/")) {
stack.push(String.valueOf(num2 / num1));
}
}
}
return Integer.parseInt(stack.pop());
}
//将中缀表达式转换成顺序ArrayList的行式
public static List<String> toInfixExpressionList(String string) {
List<String> list = new ArrayList<>();
int i = 0;
String s;
char c;
while (i < string.length()) {
//如果是符号,不是数字,就直接加入到数组中
if ((c = string.charAt(i)) < 48 || (c = string.charAt(i)) > 57) {
list.add("" + c);
i++;
} else {
//如果是数字的话,就需要考虑多位数得情况
s = "";
while (i < string.length() && (c = string.charAt(i)) >= 48 && (c = string.charAt(i)) <= 57) {
s = s + c;
i++;
}
list.add(s);
}
}
return list;
}
//将顺序Arraylist装换成逆缀ArrayList形式
public static List<String> parseSuffixExpressionList(List<String> arrayList) {
//步骤中其实S2没有取出的过程,于是可以用ArrayList来代替S2栈
Stack<String> s1 = new Stack<>();
List<String> s2 = new ArrayList<>();
for (String item : arrayList) {
if (item.matches("\\d+")) {
s2.add(item);
} else if (item.equals("(")) {
s1.add(item);
} else if (item.equals(")")) {
while (!s1.peek().equals("(")) {
s2.add(s1.pop());
}
s1.pop();
} else {
while (s1.size() > 0 && getpriority(item) <= getpriority(s1.peek())) {
s2.add(s1.pop());
}
s1.push(item);
}
}
while (s1.size() != 0) {
s2.add(s1.pop());
}
return s2;
}
//比较运算符的优先级
public static int getpriority(String s) {
int result = 0;
switch (s) {
case "+":
result = 1;
break;
case "-":
result = 1;
break;
case "*":
result = 2;
break;
case "/":
result = 2;
break;
}
return result;
}
}
总结
思路搞清楚,逻辑很重要。