我需要一些计算字符串中的数学表达式。
到目前为止,我的代码仅适用于正数。
我使用正则表达式将字符串拆分为两个单独的数组。我能够将所有数学符号拆分为一个数组,将所有数字拆分为另一个数组。但是我不确定如何处理负数。(我不明白正则表达式,我只是放了东西,它有效,但不适用于负数(
无论如何,这是我的代码,提前感谢!
boolean mybool = true;
String str = "1.0+2.0+3.0+4.0";
String[] numarray = str.split("[-+/%*]");
String[] signarray = str.split("[0123456789.]");
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
最终我希望能够计算这样的字符串字符串 str = "3.01+2.2/4.01*7.1%4.0--2.0";
但是我不知道如何从 Sting 获取负数并存储在 NUM 数组中。
感谢您的帮助!
理想情况下,您应该使用解析器而不是正则表达式。但是,鉴于您当前的需求,使用正面和负面的回溯很简单。
(1(. 运算符前面总是有一个十进制数。因此,我们通过匹配出现在十进制数之后的任何运算符(正回溯(来拆分参数。
String[] arguments = str.split("(?<=\d)[-+/%*]");
(2(. 参数可以以可选的减号开头,但前面不能有另一个十进制数。因此,我们通过匹配不在十进制数之后的参数来拆分运算符(负回溯(。
String[] operators = str.split("(?<!\d)-?[0-9.]+");
但请注意,数组的位置 0 处将有一个空运算符。如果你想避免这种情况,那么你可以使用许多不同的方法来代替String.split。