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