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