2017-02-13 89 views
0

我需要一些用於評估字符串中的數學表達式。使用正則表達式分割字符串評估字符串中的數學表達式

截至目前我的代碼只能用於正數。

我用正則表達式將字符串拆分成兩個單獨的數組。我能夠將一個數組中的所有數學符號和另一個數組中的所有數字分開。但我不確定如何爲負數做到這一點。 (我不明白的正則表達式我只是把東西和它的工作,但不是負數)

反正這裏是我的代碼,在此先感謝`

boolean mybool = true; 
    String str = "1.0+2.0+3.0+4.0"; 
    String[] numarray = str.split("[-+/%*]"); 
    String[] signarray = str.split("[.]"); 
    double result = 0.0; 
    double num1 = 0.0; 
    double num2 = 0.0; 

    String mystr = ""; 

    //Adds each element in sign array to mystr 
    for(String e : signarray){ 
     if(e != ""){ 
      mystr+=e; 
     } 
    } 

    //Assign signarray new size of length mystr 
    signarray = new String[mystr.length()]; 


    //Cycle through each element in str and add it to signarray 
    for(int i = 0; i < mystr.length(); i++){ 
     signarray[i] = mystr.charAt(i)+""; 
    } 

    //Print each element in num array 
    System.out.print("Print each element in number array: "); 
    for(String e : numarray){ 
     System.out.print(e+ " "); 
    } 
    System.out.println(); 

    System.out.print("Print each element in sign array: "); 
    //print each element in sign array 
    for(String e : signarray){ 
     System.out.print(e+ " "); 
    } 
    System.out.println(); 

    //Prints each element in sign array and element value 
    for(int i = 0; i < signarray.length; i++){ 
     System.out.println("SignArray[" + i + "] = " + signarray[i]); 
    } 


    for(int i = 2; i <= numarray.length; i++){ 

     //this will get the first two indexes of number array 
     //and store them in num1 and num1 then i use another if 
     //statement to go sign array to get a sign to evaluate the 
     //two nums and store the value in result. 
     //hopefully you understand my logic 
     if(mybool == true){ 


      num1 = Double.parseDouble(numarray[0]); 
      num2 = Double.parseDouble(numarray[1]); 
      System.out.println("num1 = " + num1); 
      System.out.println("num2 = " + num2); 


      if(signarray[0].equals("+")){ 
       result = num1 + num2; 
       System.out.println("Result = num1 + num2 = " + num1 + "+" + num2 + "= " + result); 
      } else if(signarray[0].equals("-")){ 
       result = num1 - num2; 
       System.out.println("Result = num1 - num2 = " + num1 + "-" + num2 + "= " + result); 
      } else if(signarray[0].equals("/")){ 
       result = num1/num2; 
       System.out.println("Result = num1/num2 = " + num1 + "/" + num2 + "= " + result); 
      } else if(signarray[0].equals("*")){ 
       result = num1 * num2; 
       System.out.println("Result = num1 * num2 = " + num1 + "*" + num2 + "= " + result); 
      } else if(signarray[0].equals("%")){ 
       result = num1 % num2; 
       System.out.println("Result = num1 % num2 = " + num1 + "%" + num2 + "= " + result); 
      } 

      mybool = false; 

     } else { 

      num2 = Double.parseDouble(numarray[i-1]); 
      System.out.println("Num2 = " + num2); 


      if(signarray[i-2].equals("+")){ 
       result = result + num2; 
       System.out.println("Result after math is : " + result); 

      } else if(signarray[i-2].equals("-")){ 
       result = result - num2; 
       System.out.println("Result after math is : " + result); 
      } else if(signarray[i-2].equals("/")){ 
       result = result/num2; 
       System.out.println("Result after math is : " + result); 
      } else if(signarray[i-2].equals("*")){ 
       result = result * num2; 
       System.out.println("Result after math is : " + result); 
      } else if(signarray[i-2].equals("%")){ 
       result = result % num2; 
       System.out.println("Result after math is : " + result); 
      } 

     } 



    }` 

輸出:

Print each element in number array: 1.0 2.0 3.0 4.0 
Print each element in sign array: + + + 
SignArray[0] = + 
SignArray[1] = + 
SignArray[2] = + 
num1 = 1.0 
num2 = 2.0 
Result = num1 + num2 = 1.0+2.0= 3.0 
Num2 = 3.0 
Result after math is : 6.0 
Num2 = 4.0 
Result after math is : 10.0 

最終我想能夠評估像這樣的字符串 // String str =「3.01 + 2.2/4.01 * 7.1%4.0--2.0」;

但我不知道如何從sting中獲得負數並存儲在num數組中。

感謝您的幫助!

+0

- 應該扔 - ( - 2)會很好 –

+1

我會調查正面和負面的lookbehinds爲您的正則表達式。或者更好的是,不要使用正則表達式使用解析器。 –

+0

如果你在標誌上拆分,你永遠不會得到標誌。此外,' - 2.0'是'-1 * -2.0' – sln

回答

0

理想情況下,您應該使用解析器而不是正則表達式。但是,考慮到您目前的要求,使用積極和消極的後臺服務會很簡單。 (1)

(1)。運算符總是以十進制數字開頭。所以我們通過匹配出現在十進制數之後的任何運算符(正向反序)來分割參數。

​​

(2)。參數可以以一個可選的減號開始,但不會在前面加上另一個十進制數。所以我們通過在十進制數之後匹配參數來分割運算符(否定向後)。

String[] operators = str.split("(?<!\\d)-?[0-9.]+"); 

但是請注意,在數組的位置0處會有一個空操作符。如果你想避免這種情況,你可以使用許多不同的方法來代替String.split。

+0

謝謝!這很好。只是好奇你將如何使用解析器來做到這一點? –

+0

@ j.doe如果你想要一個java數學表達式解析器,那麼許多已經寫好的並且可以免費使用的。我將開始在您最喜愛的搜索引擎上進行互聯網搜索:java數學表達式解析器 –

+0

感謝我在家的時候不好意思看看! –