我需要一些用於評估字符串中的數學表達式。使用正則表達式分割字符串評估字符串中的數學表達式
截至目前我的代碼只能用於正數。
我用正則表達式將字符串拆分成兩個單獨的數組。我能夠將一個數組中的所有數學符號和另一個數組中的所有數字分開。但我不確定如何爲負數做到這一點。 (我不明白的正則表達式我只是把東西和它的工作,但不是負數)
反正這裏是我的代碼,在此先感謝`
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數組中。
感謝您的幫助!
- 應該扔 - ( - 2)會很好 –
我會調查正面和負面的lookbehinds爲您的正則表達式。或者更好的是,不要使用正則表達式使用解析器。 –
如果你在標誌上拆分,你永遠不會得到標誌。此外,' - 2.0'是'-1 * -2.0' – sln