後綴表達式
//中綴表達式轉後綴表達式
public class InfixToSuffix {
public static void main(String[] args) {
String infixExpression = "(20-40/2*3/8+20)/20+8/2*3-(10*4/5-6)/2";
String suffixExpression = infixToSuffix(infixExpression);
System.out.println(suffixExpression);
}
private static String infixToSuffix(String infixExpression) {
ArrayStack<String> opStack = new ArrayStack<>();
ArrayList<String> suffixList = new ArrayList<>();
infixExpression = insertBlanks(infixExpression);
String[] tokens = infixExpression.split(" ");
System.out.println(Arrays.toString(tokens));
for (String token:tokens) {
if(token.length() == 0) {
continue;
}
if (isOperator(token)) { //如果是操作符
while (true) {
//棧空 或 棧頂是( 直接進
if(opStack.isEmpty() || "(".equals(opStack.peek())) {
opStack.push(token);
break;
}
//當前操作符的優先級 > 棧頂操作符的優先級 直接進
if (priority(token) > priority(opStack.peek())) {
opStack.push(token);
break;
}
//如果棧不爲空 且 棧頂不是( 且 棧頂操作符的優先級>= 當前的操作符
suffixList.add(opStack.pop());
}
} else if (isNumber(token)) { //如果是數字
suffixList.add(token);
} else if ("(".equals(token)) { //如果是(
opStack.push(token);
} else if (")".equals(token)) { //如果是)
while (true) {
if ("(".equals(opStack.peek())) {
opStack.pop();
break;
} else {
suffixList.add(opStack.pop());
}
}
} else {
throw new IllegalArgumentException("wrong identifer " + token);
}
}
while (!opStack.isEmpty()) {
suffixList.add(opStack.pop());
}
StringBuilder sb = new StringBuilder();
//10 20 2 * / +
for (int i = 0; i < suffixList.size() ; i++) {
sb.append(suffixList.get(i) + " ");
}
return sb.toString();
}
private static int priority(String token) {
if ("*".equals(token) || "/".equals(token)) {
return 1;
}
if ("+".equals(token) || "-".equals(token)) {
return 0;
}
return -1;
}
private static boolean isNumber(String token) {
return token.matches("\\d+"); // \\d+ 匹配的是表示多個數字
}
private static boolean isOperator(String token) {
return "+".equals(token) || "-".equals(token) || "*".equals(token) || "/".equals(token);
}
private static String insertBlanks(String expression) {
StringBuilder sb = new StringBuilder();
//遍歷表達式
char c;
for (int i = 0; i < expression.length(); i++) {
c = expression.charAt(i);
//如果遇到符號 則將符號的前後都加上空格 添加在sb
if (c == '(' || c == ')' || c == '+' || c == '-' || c == '*' || c == '/') {
sb.append(' ');
sb.append(c);
sb.append(' ');
} else {
//如果遇到數字 則原封不動添加在sb
sb.append(c);
}
}
return sb.toString();
}
}
//後綴表達式計算器
public class SuffixCalculator {
public static void main(String[] args) {
String expression = "20 40 2 / 3 * 8 / - 20 + 20 / 8 2 / 3 * + 10 4 * 5 / 6 - 2 / -";
ArrayStack<Integer> stack = new ArrayStack<>();
String[] tokens = expression.split(" ");
for (String token:tokens) {
//如果是數字直接進
if (isNumber(token)) {
stack.push(new Integer(token));
} else {
//如果是操作符 則彈出兩個數字 進行計算 將新的結果入棧
processAnOperator(stack,token);
}
}
System.out.println(stack.pop());
}
private static void processAnOperator(ArrayStack<Integer> stack, String token) {
int num1 = stack.pop();
int num2 = stack.pop();
switch (token) {
case "+":
stack.push(num2 + num1);
break;
case "-":
stack.push(num2 - num1);
break;
case "*":
stack.push(num2 * num1);
break;
case "/":
stack.push(num2 / num1);
break;
}
}
private static boolean isNumber(String token) {
return token.matches("\\d+"); // \\d+ 匹配的是表示多個數字
}
}
執行結果
12