|
@@ -523,7 +523,7 @@ let cbAnalyzer = {
|
|
|
},
|
|
|
//输入合法性
|
|
|
inputLegal: function (exp) {
|
|
|
- let ilegalRex = /[^0-9,\u4e00-\u9fa5,\+,\-,\/,\*,\(,\),.,{,},F]/g;
|
|
|
+ let ilegalRex = /[^0-9,\u4e00-\u9fa5,\+,\-,\/,\*,\(,\),.,{,},F,%]/g;
|
|
|
return !ilegalRex.test(exp);
|
|
|
},
|
|
|
//基数合法性、存在性
|
|
@@ -622,8 +622,10 @@ let cbAnalyzer = {
|
|
|
let ilegalRex = /[\+,\-,\*,\/]{2}/g;
|
|
|
let rex2 = /[{]{2}/g;
|
|
|
let rex3 = /[}]{2}/g;
|
|
|
- let rex4 = /[F]{2}/g
|
|
|
- return !ilegalRex.test(exp) && !rex2.test(exp) && !rex3.test(exp) && !rex4.test(exp);
|
|
|
+ let rex4 = /[F]{2}/g;
|
|
|
+ let rex5 = /[.]{2}/g;
|
|
|
+ let rex6 = /[%]{2}/g;
|
|
|
+ return !ilegalRex.test(exp) && !rex2.test(exp) && !rex3.test(exp) && !rex4.test(exp) && !rex5.test(exp) && !rex6.test(exp);
|
|
|
},
|
|
|
//
|
|
|
legalExp: function (node) {
|
|
@@ -719,6 +721,21 @@ let cbParser = {
|
|
|
}
|
|
|
return rst;
|
|
|
},
|
|
|
+ //表达式中的百分数转换成小数
|
|
|
+ percentToNum: function (exp) {
|
|
|
+ let rex = /\d+(\.\d+)?%/g;
|
|
|
+ let percents = exp.match(rex);
|
|
|
+ let numRex = /\d+(\.\d+)?/g;
|
|
|
+ if(cbTools.isDef(percents)){
|
|
|
+ for(let i = 0, len = percents.length; i < len; i++){
|
|
|
+ let percentNum = percents[i].match(numRex);
|
|
|
+ if(cbTools.isDef(percentNum) && percentNum.length === 1){
|
|
|
+ exp = exp.replace(new RegExp(percents[i], 'g'), percentNum[0]/100);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return exp;
|
|
|
+ },
|
|
|
//将行引用转换成ID引用
|
|
|
toIDExpr: function (exp) {
|
|
|
let exps = [];
|
|
@@ -878,7 +895,10 @@ let calcBase = {
|
|
|
//输入式转换表达式
|
|
|
let compileExp = $CBP.toCompileExpr(exp);
|
|
|
//计算
|
|
|
- let calcBaseValue = eval(compileExp);
|
|
|
+ console.log(compileExp);
|
|
|
+ let calcExp = $CBP.percentToNum(compileExp);
|
|
|
+ console.log(calcExp);
|
|
|
+ let calcBaseValue = eval(calcExp);
|
|
|
if(!cbTools.isNum(calcBaseValue)){
|
|
|
throw '表达式不正确';
|
|
|
}
|