繁体
096
071
}
Stringregex=“\\([\\d\\.\\ \\-\\*/] \\)“;//提取括号表达式
060
054
//循环计算所有括号里的表达式
s=numberFormat.format(obj);
095
080
081
本章已阅读完毕(请
击下一章继续阅读!)
}else{
}
085
068
083
057
Patternpattern=Patternpile(regex);
*/
*计算字符串四则运算表达式
091
string=string.substring(0,index)
Matchermatcher=null;
matcher=pattern.matcher(string);
093
058
NumberFormatnumberFormat=NumberFormat.getInstance();
088
090
intindex=-1;
065
temp=matcher.group();
082
077
StringregexCheck=“[\\(\\)\\d\\ \\-\\*/\\.]*“;//是否是合法的表达式
*@paramstring
059
}
DecimalFormatdecimalFormat=newDecimalFormat(format);
066
076
returns;
075
073
084
returnstring;
070
/**
069
}
while(matcher.find()){
string=computeStirngNoBracket(string);
publicstaticStringcomputeString(Stringstring){
computeStirngNoBracket(temp)
097
086
067
index=string.indexOf(temp);
Stringtemp=““;
string=string.replaceAll(“\\s“,““);//去除空格
052
*@return
087
returne.getMessage();(未完待续。)
078
}
072
094
//最后计算总的表达式结果
}
try{
056
074
string.substring(index temp.length());
064
055
if(!Pattern.matches(regexCheck,string))
}catch(Exceptione){
062
while(pattern.matcher(string).find()){
092
079
089
}catch(NumberFormatExceptione){
053
061
063
}else{
.format(obj);
s=decimalFormat.format(obj);