目錄
1 問題描述
問題描述
輸入一個只包含加減乖除和括號的合法表達式,求表達式的值。其中除表示整除。
輸入格式
輸入一行,包含一個表達式。
輸出格式
輸出這個表達式的值。
樣例輸入
1-2+3*(4-5)
樣例輸出
-4
數據規模和約定
表達式長度不超過100,表達式運算合法且運算過程都在int內進行。
2 解決方案
具體代碼如下:
package com.liuzhen.systemExe; import java.util.Scanner; import java.util.Stack; public class Main{ //計算表達式的值 public void getExpressionValue(String A){ char[] arrayA = A.toCharArray(); Stack<Integer> Value = new Stack<Integer>(); //存放運算數字及表達式計算結果 Stack<Character> Operator = new Stack<Character>(); //存放運算符 for(int i = 0;i < A.length();i++){ int temp = 0; if(arrayA[i] >= '0' && arrayA[i] <= '9'){ temp = arrayA[i] - '0'; i = i + 1; while(i < A.length() && arrayA[i] >= '0' && arrayA[i] <= '9'){ temp = temp * 10 + (arrayA[i] - '0'); i++; } i--; //對應上面一句i = i+1;因為在for循環中有i++自增操作,若不執行此句,會導致i自增兩次 Value.push(temp); } else{ if(Operator.empty()){ Operator.push(arrayA[i]); } else{ char temp1 = Operator.pop(); //進棧前,存放運算符棧中棧頂存放字符 int judge = comparePriority(temp1,arrayA[i]); //比較當前字符與棧頂字符優先級 if(judge == 1){ //當前字符優先級小於棧頂字符 int tempA = Value.pop(); int tempB = Value.pop(); int result = computeNumber(tempB,tempA,temp1); Value.push(result); Operator.push(arrayA[i]); } if(judge == 0){ //當前字符優先級大於棧頂字符 Operator.push(temp1); Operator.push(arrayA[i]); } if(judge == 2){ //字符')'遇到'(',剛好使得'('出棧 System.out.println("'('剛好遇到')'"); //這種情況也應該不會出現,按照給定優先級,')'一般會先遇到+、-、*、/字符 } if(judge == 3){ //此時')'剛好准備進棧 while(temp1 != '('){ //')'字符要等到第一個'('出棧才能結束循環 //System.out.println(temp1); int tempA = Value.pop(); int tempB = Value.pop(); int result = computeNumber(tempB,tempA,temp1); Value.push(result); temp1 = Operator.pop(); } } if(judge == -1){ //此時,說明當前棧頂字符為')',這是不存在的,因為遇到')',按要求不讓進棧 System.out.println("出現棧頂有')'錯誤!!!"); } } } } while(!Operator.empty() && !Value.empty()){ //此時,字符棧中還存在運算符的情況 char temp1 = Operator.pop(); int tempA = Value.pop(); int tempB = Value.pop(); int result = computeNumber(tempB,tempA,temp1); Value.push(result); } System.out.println(Value.pop()); //此時運算符棧為空,數字棧中只存在表達式計算最終結果 } //計算a operator b的值,operator = {+,-,*,/} public int computeNumber(int a,int b,char operator){ int result; switch(operator){ case '+': result = a+b; break; case '-': result = a-b; break; case '*': result = a*b; break; case '/': result = a/b; break; default: result = 0; break; } return result; } //判斷運算符a和b的優先級 public int comparePriority(char a,char b){ //使用二維數組表達運算符之間的優先級,行用字符a表示,列用字符b表示 int[][] Value = {{1,1,0,0,0,3}, {1,1,0,0,0,3}, {1,1,1,1,0,3}, {1,1,1,1,0,3}, {0,0,0,0,0,2}, {-1,-1,-1,-1,-1,-1}}; int i = 0; int j = 0; if(a == '+') i = 0; if(a == '-') i = 1; if(a == '*') i = 2; if(a == '/') i = 3; if(a == '(') i = 4; if(a == ')') i = 5; if(b == '+') j = 0; if(b == '-') j = 1; if(b == '*') j = 2; if(b == '/') j = 3; if(b == '(') j = 4; if(b == ')') j = 5; return Value[i][j]; } public static void main(String[] args){ Main test = new Main(); Scanner in = new Scanner(System.in); System.out.println("請輸入一個算法表達式:"); String A = in.nextLine(); test.getExpressionValue(A); } }
運行結果:
請輸入一個算法表達式: 1-2+3*(4-5) -4 請輸入一個算法表達式: 1-2*((2+3)*2-(2+3)) -9 請輸入一個算法表達式: 1-2*((2+3)*(2+3)) -49