分享一下:
http://www.ixueyun.com/community/home-space-uid-11902-do-blog-id-1923.html
http://www.4ucode.com/Study/Topic/450127
public class Eval {
public Object eval2(String s) throws ScriptException, NoSuchMethodException{
if (s.replaceAll("[\\d+-\\\\*/()]","").equals("")) {
ScriptEngineManager manager = new ScriptEngineManager();
ScriptEngine engine = manager.getEngineByName("JavaScript");
engine.eval("function t() {return " + s + ";}");
Invocable invocable = (Invocable) engine;
Object o = invocable.invokeFunction("t");
return o;
}
return null;
}
/**
* 计算字符串四则运算表达式
* @param string
* @return
* @author ZYWANG 2009-8-31
*/
public String eval(String string) {
String regexCheck = "[\\(\\)\\d\\+\\-\\*/\\.]*";// 是否是合法的表达式
if (!Pattern.matches(regexCheck, string))
return string;
Matcher matcher = null;
String temp = "";
int index = -1;
String regex = "\\([\\d\\.\\+\\-\\*/]+\\)";// 提取括号表达式
string = string.replaceAll("\\s", "");// 去除空格
try {
Pattern pattern = Pattern.compile(regex);
// 循环计算所有括号里的表达式
while (pattern.matcher(string).find()) {
matcher = pattern.matcher(string);
while (matcher.find()) {
temp = matcher.group();
index = string.indexOf(temp);
string = string.substring(0, index)
+ computeStirngNoBracket(temp)
+ string.substring(index + temp.length());
}
}
// 最后计算总的表达式结果
string = computeStirngNoBracket(string);
} catch (NumberFormatException e) {
return e.getMessage();
}
return string;
}
/**
* 计算不包含括号的表达式
* @param string
* @return
* @author ZYWANG 2009-8-31
*/
private String computeStirngNoBracket(String string) {
string = string.replaceAll("(^\\()|(\\)$)", "");
String regexMultiAndDivision = "[\\d\\.]+(\\*|\\/)[\\d\\.]+";
String regexAdditionAndSubtraction = "(^\\-)?[\\d\\.]+(\\+|\\-)[\\d\\.]+";
String temp = "";
int index = -1;
// 解析乘除法
Pattern pattern = Pattern.compile(regexMultiAndDivision);
Matcher matcher = null;
while (pattern.matcher(string).find()) {
matcher = pattern.matcher(string);
if (matcher.find()) {
temp = matcher.group();
index = string.indexOf(temp);
string = string.substring(0, index) + doMultiAndDivision(temp)
+ string.substring(index + temp.length());
}
}
// 解析加减法
pattern = Pattern.compile(regexAdditionAndSubtraction);
while (pattern.matcher(string).find()) {
matcher = pattern.matcher(string);
if (matcher.find()) {
temp = matcher.group();
index = string.indexOf(temp);
if (temp.startsWith("-")) {
string = string.substring(0, index)
+ doNegativeOperation(temp)
+ string.substring(index + temp.length());
} else {
string = string.substring(0, index)
+ doAdditionAndSubtraction(temp)
+ string.substring(index + temp.length());
}
}
}
return string;
}
/**
* 执行乘除法
* @param string
* @return
* @author ZYWANG 2009-8-31
*/
private String doMultiAndDivision(String string) {
String value = "";
double d1 = 0;
double d2 = 0;
String[] temp = null;
if (string.contains("*")) {
temp = string.split("\\*");
} else {
temp = string.split("/");
}
if (temp.length < 2)
return string;
d1 = Double.valueOf(temp[0]);
d2 = Double.valueOf(temp[1]);
if (string.contains("*")) {
value = String.valueOf(d1 * d2);
} else {
value = String.valueOf(d1 / d2);
}
return value;
}
/**
* 执行加减法
* @param string
* @return
* @author ZYWANG 2009-8-31
*/
private String doAdditionAndSubtraction(String string) {
double d1 = 0;
double d2 = 0;
String[] temp = null;
String value = "";
if (string.contains("+")) {
temp = string.split("\\+");
} else {
temp = string.split("\\-");
}
if (temp.length < 2)
return string;
d1 = Double.valueOf(temp[0]);
d2 = Double.valueOf(temp[1]);
if (string.contains("+")) {
value = String.valueOf(d1 + d2);
} else {
value = String.valueOf(d1 - d2);
}
return value;
}
/**
* 执行负数运算
* @param string
* @return
* @author ZYWANG 2010-11-8
*/
private String doNegativeOperation(String string) {
String temp = string.substring(1);
if (temp.contains("+")) {
temp = temp.replace("+", "-");
} else {
temp = temp.replace("-", "+");
}
temp = doAdditionAndSubtraction(temp);
if (temp.startsWith("-")) {
temp = temp.substring(1);
} else {
temp = "-" + temp;
}
return temp;
}
}
Eval eval = new Eval();
System.out.println(eval.eval("(1+(2+3)*2/5)*2/7+(3*7-3)*5")); 运行时间:0.007s
System.out.println(eval.eval2("(1+(2+3)*2/5)*2/7+(3*7-3)*5"));运行时间:0.118s 需要jdk6或者以上