一个简单的表达式计算,有时候我们要在程序中对某些自定义的公式进行计算。
如定制化的一些条件,如根据某个变量来决定界面元素的颜色
我们只要对这个类稍微扩展即可,例如我们重载getObjValue方法,
然后定义一个变量池,就可以实现对诸如 calculateExp("50-Qty*((1+2)*3)+2")的计算。
getObjValue()中实现对于Qty可以从变量池获得数据。
更多的扩展可以参照后面的类,可以做一个自己的脚本语言解释器。
Polynomial dt =new Polynomial();
long begin =System.currentTimeMillis();
VariableFactory vf = new VariableFactory();
vf.setVariable("A","Select * from mis_rwe where a=");
vf.setVariable("B","150");
vf.setVariable("conf ","'5'");
dt.setDataFactory(vf);
dt.calcuPolyn("set A=IIF(obj(b)=150:add(obj(a)+obj(conf),str(\" order by desc\")),:空)");//strExp
dt.calcuPolyn("set A=str('新的变量值:')+obj(a)");//strExp
System.out.println(vf.getVariable("a"));
dt.calcuPolyn("set B=100");//strExp
dt.calcuPolyn("set A=IIF(obj(b)=100:str('(变更B变量后)')+obj(a),:空)");//strExp
System.out.println(vf.getVariable("a"));
dt.calcuPolyn("set A=replace(Obj(A),变更B变量后,str('替换函数操作后'))");//strExp
System.out.println(vf.getVariable("a"));
dt.calcuPolyn("set A=replace(\"测试 test\" + ' W J G ', test, newstr)");//strExp
System.out.println(vf.getVariable("a"));
for(int i=0; i<10;i++)
dt.calcuPolyn("set B=obj(b)+'100'");
System.out.println("B="+vf.getVariable("b"));
System.out.println("运行时间:"+(System.currentTimeMillis()-begin) +"毫秒");
/*--------------------------------------------------
1.实现表达式的计算;
2.实现简单的加、减、乘、除、括号运算。
3.例:calculateExp("50-10*((1+2)*3)+2")=-38
----------------------------------------------------*/
import java.util.Vector;
import java.util.Hashtable;
import java.util.Stack;
import java.util.regex.Pattern;
import java.util.regex.Matcher;
import com.jgwang.IDataFactory;
public class CalculatorA
{
protected IDataFactory varFactory; //当前语句的变量库
protected Hashtable localVarFact;
protected static Vector funct;
protected static Hashtable pri;
protected static String NOSOFunction="No so Function";
public static FastHashMap buildedStates;
static
{
funct=new Vector();
funct.addElement("+");
funct.addElement("-");
funct.addElement("*");
funct.addElement("/");
funct.addElement("(");
funct.addElement(")");
pri=new Hashtable();
pri.put("+","1");
pri.put("-","1");
pri.put("*","2");
pri.put("/","2");
pri.put("(","10");
pri.put(")","0");
buildedStates = new FastHashMap();
}
public CalculatorA()
{
varFactory = new VariableFactory();
}
public void setDataFactory(IDataFactory v)
{
this.varFactory = v;
}
public void setVariable(String param, String v)
{
if(this.varFactory==null)
varFactory = new VariableFactory();
varFactory.setVariable(param, v);
}
/*------------------------分析表达式---------------------------*/
public Vector analyseExp(String strExp)
{
boolean isStrBegin = false;
try
{
Vector strObjs = new Vector();
Vector obj = new Vector();
byte[] bExps = strExp.getBytes();
byte[] bExps1={'+'};
char qmark='\'';
if (strExp == null)
return null;
int isFunct = 0;
for(int i=0; i < bExps.length; i++)
{
int cPr=0;
bExps1[0]=bExps[i];
if((char)bExps1[0]=='"' || (char)bExps1[0]=='\'')
{
if(!isStrBegin) qmark = (char)bExps1[0];
obj.addElement(new Byte(bExps1[0]));
if(qmark == (char)bExps1[0])
isStrBegin = !isStrBegin;
continue;
}
else if(isStrBegin)
{
obj.addElement(new Byte(bExps1[0]));
continue;
}
if((char)bExps1[0]==' ') continue;
if (funct.indexOf(new String(bExps1))>=0)
{
if('('==(char)bExps1[0] && obj.size()>0)
{
obj.addElement(new Byte(bExps1[0]));