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