|
@@ -25,10 +25,10 @@ public class ComputeFormulaUtil {
|
|
|
// 获取计算结果,使用方法其实就是 new Calc("100 + 20 * 5 + (1 + 2)").getResult();就可以得到结果了
|
|
|
public BigDecimal getResult() {
|
|
|
BigDecimal nextValue = nextValue(BigDecimal.ZERO, '+');
|
|
|
- if(val!=null&&val[0]=='['&&val[len-1]==']'){
|
|
|
- return new BigDecimal(0.0-nextValue.doubleValue());
|
|
|
+ if (val != null && val[0] == '[' && val[len - 1] == ']') {
|
|
|
+ return nextValue.abs();
|
|
|
}
|
|
|
- return nextValue(BigDecimal.ZERO, '+');
|
|
|
+ return nextValue;
|
|
|
}
|
|
|
|
|
|
// 为什么要获取下一个值,加法、减法、和左小阔号,都需要获取下一个值,因为加法、减法如果碰到乘法、除法,那么运算优先权在右侧,如果碰到左侧小括号,优先权也在右侧,所以要先把右边的值算出来
|
|
@@ -108,9 +108,8 @@ public class ComputeFormulaUtil {
|
|
|
}
|
|
|
|
|
|
public static void main(String[] args) {
|
|
|
- ComputeFormulaUtil computeUtil = new ComputeFormulaUtil("[0-1]");
|
|
|
+ ComputeFormulaUtil computeUtil = new ComputeFormulaUtil("106.61");
|
|
|
BigDecimal result = computeUtil.getResult();
|
|
|
System.out.println(result);
|
|
|
}
|
|
|
-
|
|
|
}
|