百行以內實現複雜數學表示式計算

陳能豆發表於2020-05-12

一改以前

本次先上程式碼

package good;
//Evaluate complex expressions
import java.util.Scanner;

public class Example {

  public static void main(String[] arg) {//test

    Scanner in = new Scanner(System.in);
//    String one = in.next();
    try {
      System.out.println("(1.5+1)+(1+1)=" + RemoveBrackets(new StringBuffer("(1.5+1)+(1+1)")));
    }
    catch (Exception e){
      System.out.println("Please check if the expression is valid if there is an error");
    }
  }

  public static double RemoveBrackets(StringBuffer a) {//Bracket removal method

    while (a.toString().indexOf('(') != -1) {
      int i = a.toString().indexOf('(');
      int j = GetBracketPosition(a.toString(), i + 1);
      String to = a.toString().substring(i + 1, j);
      a.replace(i, j + 1, String.valueOf(RemoveBrackets(new StringBuffer(to))));
    }
    return OnlyMultiplication(a.toString());//計算除乘法以外的算式
  }


  //A method for calculating addition, subtraction, multiplication, and division without parentheses
  private static double OnlyMultiplication(String a) {//To eliminate the formula other than multiplication

    char[] b = a.toCharArray();
    for (int i = 0; i < a.length(); i++) {/*I'm going to check the addition and subtraction because I'm going to multiply and divide and then I'm going to add and subtract*/

      if (b[i] == '+') {
        return OnlyMultiplication(a.substring(0, i)) + OnlyMultiplication(a.substring(i + 1));//Prevent double parenthesis
      }

      if (b[i] == '-') {
        return OnlyMultiplication(a.substring(0, i)) - OnlyMultiplication(a.substring(i + 1));
      }
    }//So let's go over here and do the subtraction and the subtraction
    //That means there's no plus or minus sign
    //A separate multiplication or division
    String[] sum1 = a.split("/");//The second half of this method is to remove the division sign

    double too = Double.valueOf(CalculateMultiplication(sum1[0]));
    for (int i = 1; i < sum1.length; i++) {
      too = too * 1.0 / Double.valueOf(sum1[i]);
    }
    return too;
  }

  //So the only way to multiply is to multiply
  private static double CalculateMultiplication(String aaa) {//Remove the multiplication sign
    double to = 1.0;
    if (aaa.indexOf('*') == -1) {//Without a multiplication sign
      return Double.valueOf(aaa);
    }
    String[] too = aaa.split("\\*");

    for (int i = 0; i < too.length; i++) {
      to = to * Double.valueOf(too[i]);
    }
    return to;
  }

  private static int GetBracketPosition(String a, int b) {//Find the corresponding close parenthesis position
    int count1 = 1;
    int count2 = 0;
    char[] aa = a.toCharArray();
    for (int i = b; i < a.length(); i++) {
      if (aa[i] == '(') {//Prevent double parenthesis
        count1++;
      }
      if (aa[i] == ')') {
        count2++;
        if (count2 == count1) {
          return i;
        }
      }
    }
    return 0;
  }
}

 

 

下面是執行結果

 

 

OK  程式碼可以先看一下     相信肯定有人能夠不靠解析   直接看懂

下面我來具體的一行一行的解釋我的程式碼

先說一下具體思路

我們先考慮括號的問題     因為括號內容優先計算

當我們吧所有括號裡面的內容處理好之後     我們用計算的結果替換掉括號以及裡面的表示式

我們下面會將那種不帶括號的表示式是怎樣計算的

當然  必須要提示的一點    不排除會有雙層括號情況的出現   所以我們要對拆分出來的括號裡面分表示式在進行一次括號檢測     當檢測到沒有括號的時候   我們才呼叫無括號表示式計算方法   計算表示式的最終結果

 

OK  下面我們進入無括號表示式計算方法的解釋環節

現在強調一下  我們已經完成括號處理  

我們的表示式裡只有加減乘除的簡單運算

 

我們就以加號和減號為分界線    

這樣就成功吧式子拆成了要麼是乘除運算  要麼是單獨是數    這樣處理起來是不是就方便的太多太多了

哈哈哈哈   香不香

 

OK  我們的解析就到這

當然 方法肯定不止一種    比我這個方法好的方法也肯定多了去了       

不喜勿噴

 

相關文章