前綴表達式
- 前綴表達式又稱波蘭式,前綴表達式的運算符位於操作數之前。
- 舉例:(3+4)*5-6的前綴表達式為
- * + 3 4 5 6
前綴表達式計算機求值
從右至左掃描表達式,遇到數字時,將數字壓入堆棧,遇到運算符時,彈出棧頂的兩個數,用運算符對他們做相應的計算(棧頂元素和次頂元素),並將結果入棧;重復上述過程直到表達式最左端,最后運算得出的值即為表達式的結果。
舉例:
求前綴表達式:- * + 3 4 5 6的值:
1. 從右至左掃描,將6、5、4、3壓入堆棧;
2. 遇到 + 運算符,因此彈出3和4(3為棧頂元素,4為次頂元素),計算 3+4 的值,得7,再將7入棧;
3. 接下來是 * 運算符,因此彈出7和5,計算 7*5=35 ,將35入棧。
4. 最后是 - 運算符,計算 35-6 的值,即29,得最終結果29.
中綴表達式
- 中綴表達式就是常見得運算表達式,如
(3+4)*5-6
。 - 中綴表達式求值是我們人熟悉的,但是對於計算機來說卻不好操作,因此,在計算結果時,往往會將中綴表達式轉成其他表達式來操作。
后綴表達式
- 后綴表達式又稱逆波蘭表達式,與前綴表達式相似,只是運算符位於操作數之后。
- 舉例: (3+4)*5-6的后綴表達式為
3 4 + 5 * 6 -
后綴表達式計算機求值
從左至右掃描表達式,遇到數字時,將數字壓入堆棧,遇到運算符時,彈出棧頂的兩個數,用運算符對它們做相應的計算。並將結果入棧;重復上述過程直到表達式最右端,最后運算出的值即為表達式的結果。
舉例: 求 (3+4)*5-6 的后綴表達式 (3 4 + 5 * 6 -)
1. 從左至右掃描,將3和4入棧;
2. 遇到運算符 + ,因此彈出4和3,計算 3+4 的值,再將7入棧;
3. 將5入棧;
4. 接下來是 * 運算符,因此彈出5和7,計算出 7*5=35,將35入棧;
5. 將6入棧;
6. 最后是 - 運算符,計算 35-6 的值,即29,由此得出最終結果。
中綴表達式轉換為后綴表達式
1. 初始化兩個棧:運算符棧s1和儲存中間結果棧s2;
2. 從左至右掃描中綴表達式;
3. 遇到操作數時,將其壓入s2;
4. 遇到運算符時,比較其與s1棧頂運算符的優先級:
(1)如果s1為空,或棧頂運算符為左括號“(” ,則直接將此運算符入棧;
(2)否則,若優先級比棧頂運算符高,也將運算符壓入是s1;
(3)否則,將s1棧頂的運算符彈出並壓入到s2中,再次轉到4.1步驟與s1中新的棧頂運算符相比較;
5. 遇到括號時:
(1)如果時左括號“(” ,則直接壓入s1;
(2)如果是右括號“)” ,則依次彈出s1棧頂的運算符,並壓入s2,直到遇到左括號為止,此時將這一對括號丟棄;
6. 重復步驟2至5,直到表達式的最右邊;
7. 將s1中剩余元素依次彈出並壓入s2;
8. 依次彈出s2中的元素並輸出,結果的逆序即為中綴表達式對應的后綴表達式。
逆波蘭計算器:
目標:可以完成整數加減乘除(包含括號)的計算器。
package com.stack;
import java.util.ArrayList;
import java.util.List;
import java.util.Stack;
public class SimpleCalculator {
public static void main(String[] args) {
String s = "1+(2*3)" +
"";
List<String> infix = toInfixExpression(s);
List<String> postfix = toPostfixExpression(infix);
System.out.println(calculate(postfix));
}
//1. 將字符串轉化為中綴表達式,並存入到List中
public static List<String> toInfixExpression(String str){
ArrayList<String> infix = new ArrayList<>();
int i = 0;
char c;
String s;
do{
if((c = str.charAt(i)) < 48 || (c = str.charAt(i)) >57){
infix.add(""+c);
i++;
}else{
s = "";
while(i < str.length() && (c = str.charAt(i)) >= 48 && (c = str.charAt(i)) <= 57){
s += c;
i++;
}
infix.add(s);
}
}while(i < str.length());
return infix;
}
//2. 設置運算符優先級
public static int getPriority(String s){
final int ADDITION=1;
final int SUBTRACTION=1;
final int MULTIPLICATION=2;
final int DIVISION=2;
int result;
switch (s){
case "+":
result = ADDITION;
break;
case "-":
result = SUBTRACTION;
break;
case "*":
result = MULTIPLICATION;
break;
case "/":
result = DIVISION;
break;
default:
System.out.println("不存在該運算符!");
result = -1;
}
return result;
}
//3. 將中綴表達式轉換為后綴表達式,並存入到List中
public static List<String> toPostfixExpression(List<String> infix){
Stack<String> s1 = new Stack<>();
ArrayList<String> postfix = new ArrayList<>();
for (String s : infix) {
if(s.matches("\\d+")){
postfix.add(s);
}else if(s.equals("(")){
s1.push(s);
}else if(s.equals(")")){
while(!s1.peek().equals("(")){
postfix.add(s1.pop());
}
s1.pop();
}else{
while(s1.size() != 0 && getPriority(s1.peek()) >= getPriority(s)){
postfix.add(s1.pop());
}
s1.push(s);
}
}
while(s1.size() != 0){
postfix.add(s1.pop());
}
return postfix;
}
//4. 計算后綴表達式
public static int calculate(List<String> postfix){
Stack<String> stack = new Stack<>();
for (String s : postfix) {
if(s.matches("\\d+")){
stack.push(s);
}else{
int a = Integer.parseInt(stack.pop());
int b = Integer.parseInt(stack.pop());
int result = 0;
if(s.equals("+")){
result = a + b;
}else if(s.equals("-")){
result = b - a;
}else if(s.equals("*")){
result = a * b;
}else if(s.equals("/")){
result = b / a;
}else{
throw new RuntimeException("運算符異常!");
}
stack.push(""+result);
}
}
return Integer.parseInt(stack.peek());
}
}