电脑版
首页

搜索 繁体

116、骂战(2/2)

publicstaticStringcomputeString(Stringstring){

087

*/

093

try{

*@paramstring

096

}

072

095

}else{

084

083

}

079

069

070

*计算字符串四则运算表达式

string=computeStirngNoBracket(string);

Matchermatcher=null;

089

091

058

while(pattern.matcher(string).find()){

returne.getMessage();(未完待续。)

052

061

073

temp=matcher.group();

088

returns;

053

/**

071

at(obj);

StringregexCheck=“[\\(\\)\\d\\ \\-\\*/\\.]*“;//是否是合法的表达式

}

060

NumberFormatnumberFormat=NumberFormat.getInstance();

081

086

while(matcher.find()){

s=decimalFormat.format(obj);

065

054

067

//循环计算所有括号里的表达式

055

068

//最后计算总的表达式结果

}catch(Exceptione){

}else{

076

082

string=string.replaceAll(“\\s“,““);//去除空格

}

DecimalFormatdecimalFormat=newDecimalFormat(format);

Stringtemp=““;

*@return

}

}catch(NumberFormatExceptione){

092

Patternpattern=Patternpile(regex);

string.substring(index temp.length());

066

074

063

075

returnstring;

094

064

computeStirngNoBracket(temp)

intindex=-1;

097

059

string=string.substring(0,index)

Stringregex=“\\([\\d\\.\\ \\-\\*/] \\)“;//提取括号表达式

056

s=numberFormat.format(obj);

078

}

090

index=string.indexOf(temp);

matcher=pattern.matcher(string);

085

077

if(!Pattern.matches(regexCheck,string))

062

080

057

热门小说推荐

最近更新小说