1:使用代码动态编译,产生零时文件,放弃后,考虑一个方法能够完成表达式的计算
2:可以使用JAVA5提供的JavaScript对象完成,
3.可以考虑使用Antlr或其它的这样的类库来完成表达式的求解
4:自己实现方法,本代码只考虑的整数,你完全可以使用<T>,来完成求解任何类型的表达式,而此段代码没有考虑含有()的表达式
代码如下:
package
JXDO.Data.Factory;
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/a41954a27d6ad96fa2c2cf816e677448.gif)
/** */
/**
* <p>通过表达式,得到结果,提供给数据分页配置文件使用</p>
* <p>保护类,在本包内调用</p>
* @author javasuki
* @since java5
* @version 1.0
*/
class
JDyValue
![](https://i-blog.csdnimg.cn/blog_migrate/a41954a27d6ad96fa2c2cf816e677448.gif)
...
{
private String evl;
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
/** *//**
* JDyValue .ctor()
* @param evl 表达式
*/
public JDyValue(String evl)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
this.evl = evl;
}
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
/** *//**
* 得到计算结果
* @return
*/
public int getValue() throws Exception
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
//- 2 * 3 - 7 + 5
String str = this.evl;
str = str.replaceAll(" ",""); //去掉 WhiteSpace -2*3-7+5
str = str.trim();
try
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
//表达式就是整数
int iVal = Integer.valueOf(str);
return iVal;
}
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
catch(Exception ex)...{}
boolean bFrtOper = false;
if(str.length()>1)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
//判断第一个字符是否为 [-] 负号
char ch = str.toCharArray()[0];
String stmp = String.valueOf(ch);
try
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
Integer.valueOf(stmp);
}
catch(Exception ex)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
//发生错误,则第一个字符不是 [-] 负号,然后去掉[-],因为下面要对运算符号进行处理
//[*2] [+2] [/2]都是没有意义的,只有[-2]表示是负数,则去掉无效字符
if(ch != '-')str = str.substring(1);
else bFrtOper = true;
}
}
else return 0;
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
//第一个char = '-'
if(bFrtOper)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
//在str中插入逗号 str 变为: -2,*,3,-,7,+,5
str = str.substring(1);
str = "-" + this.repOper(str);
}
else
str = this.repOper(str);
//表达式修改成有逗号分隔符后,奇数位是数字,偶数为是运算符
String[] sAry = str.split(",");
if(sAry.length < 3)throw new Exception("error:( " + this.evl + " )不是一个有效的算术表达式...");
int val = 0;
try
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
int idx = 1;
for(String s : sAry)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
if(idx == 3)
val = this.dyEvl(Integer.valueOf(sAry[0]), sAry[1], Integer.valueOf(sAry[2]));
else if(idx > 3 && idx % 2 == 1)//奇数位,上的为数值
val = this.dyEvl(val, sAry[idx-2], Integer.valueOf(s));
idx += 1;
}
}
catch(Exception ex)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
throw new Exception("error:( " + this.evl + " )表达式中含有非法字符...");
}
return val;
}
//运算符插入逗号分隔符
private String repOper(String str)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
str = str.replaceAll("/+", ",+,");
str = str.replaceAll("-", ",-,");
str = str.replaceAll("/*", ",*,");
str = str.replaceAll("/", ",/,");
str = str.replaceAll("%", ",%,");
return str;
}
private int dyEvl(int i1,String oper,int i2)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
int val = 0;
if(oper.compareTo("+")==0)val = i1 + i2;
else if(oper.compareTo("-")==0)val = i1 - i2;
else if(oper.compareTo("*")==0)val = i1 * i2;
else if(oper.compareTo("/")==0)val = Integer.valueOf(String.valueOf(i1 / i2));
else if(oper.compareTo("%")==0)val = i1 % i2;
return val;
}
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
/**//*public static void main(String[] args) throws Exception
{
int i = new JDyValue("- 2 * 3 - 7 + 5").getValue();
System.out.print(i);
}*/
}