欢迎来到天天文库
浏览记录
ID:37239678
大小:78.50 KB
页数:11页
时间:2019-05-20
《JAVA实现Eval和计算24点》由会员上传分享,免费在线阅读,更多相关内容在行业资料-天天文库。
1、Java中eval的实现及引用本文主要在Java中实现类似JavaScript中eval()函数相似的功能,同时使用实现的evel实现4个数计算24点的应用。1、JavaEval实现Java实现Eval的代码如下:packagecom.parddu.eval;importjava.util.ArrayList;importjava.util.List;importjava.util.Stack;/***计算字符串表达式,类似JavaScript中Eval()函数*@project*@authorPard
2、du*@datetime2012-11-17下午1:17:03*@version1.0*/publicclassEval{privateStringexp;//表达式privateCnoderoot;//运算二叉树根元素/***获取表达式值*@return*/publicdoublegetValue(){returnthis.calculate(this.root);}/***字符串表达式运算*@paramexp表达式*/publicEval(Stringexp){this.exp=exp;this.r
3、oot=newCnode(this.exp,null,null,null,0,'');this.init(this.root);}/***将表达式分解为运算二叉树*@paramn根节点*/privatevoidinit(Cnoden){if(!this.isNumber(n.getExp())){intindex=-1;//运算符位置Stringep=n.getExp();//如果表达式的首字母和末字母是一对括号,则去除括号//保存各个括号的索引栈Stackxl=newStack4、teger>();Stackxr=newStack();Stackdl=newStack();Stackdr=newStack();for(intj=0;j5、h(j);}elseif(ep.charAt(j)==']'){dr.push(j);}//小括号去除if(xl.size()!=0&&xl.size()==xr.size()){if(xl.get(0)==0&&xr.pop()==ep.length()-1){ep=ep.substring(1,ep.length()-1);}}//大括号去除if(dl.size()!=0&&dl.size()==dr.size()){if(dl.get(0)==0&&dr.pop()==ep.length()-1)6、{ep=ep.substring(1,ep.length()-1);}}}//用于记录表达式的括号区间Listxlist=newArrayList();Listdlist=newArrayList();//保存各个括号的索引栈Stackxleft=newStack();Stackxri7、ght=newStack();Stackdleft=newStack();Stackdright=newStack();for(intj=0;j8、}elseif(ep.charAt(j)==']'){dright.push(j);}//小括号位置if(xleft.size()!=0&&xright.size()!=0){ParenthesesIntervaltemp=newParenthesesInterval(xleft.pop(),xright.pop());xlist.add(temp);}//大括号位置if(dleft.size()!=0&&dleft.size()!=0){
4、teger>();Stackxr=newStack();Stackdl=newStack();Stackdr=newStack();for(intj=0;j5、h(j);}elseif(ep.charAt(j)==']'){dr.push(j);}//小括号去除if(xl.size()!=0&&xl.size()==xr.size()){if(xl.get(0)==0&&xr.pop()==ep.length()-1){ep=ep.substring(1,ep.length()-1);}}//大括号去除if(dl.size()!=0&&dl.size()==dr.size()){if(dl.get(0)==0&&dr.pop()==ep.length()-1)6、{ep=ep.substring(1,ep.length()-1);}}}//用于记录表达式的括号区间Listxlist=newArrayList();Listdlist=newArrayList();//保存各个括号的索引栈Stackxleft=newStack();Stackxri7、ght=newStack();Stackdleft=newStack();Stackdright=newStack();for(intj=0;j8、}elseif(ep.charAt(j)==']'){dright.push(j);}//小括号位置if(xleft.size()!=0&&xright.size()!=0){ParenthesesIntervaltemp=newParenthesesInterval(xleft.pop(),xright.pop());xlist.add(temp);}//大括号位置if(dleft.size()!=0&&dleft.size()!=0){
5、h(j);}elseif(ep.charAt(j)==']'){dr.push(j);}//小括号去除if(xl.size()!=0&&xl.size()==xr.size()){if(xl.get(0)==0&&xr.pop()==ep.length()-1){ep=ep.substring(1,ep.length()-1);}}//大括号去除if(dl.size()!=0&&dl.size()==dr.size()){if(dl.get(0)==0&&dr.pop()==ep.length()-1)
6、{ep=ep.substring(1,ep.length()-1);}}}//用于记录表达式的括号区间Listxlist=newArrayList();Listdlist=newArrayList();//保存各个括号的索引栈Stackxleft=newStack();Stackxri
7、ght=newStack();Stackdleft=newStack();Stackdright=newStack();for(intj=0;j8、}elseif(ep.charAt(j)==']'){dright.push(j);}//小括号位置if(xleft.size()!=0&&xright.size()!=0){ParenthesesIntervaltemp=newParenthesesInterval(xleft.pop(),xright.pop());xlist.add(temp);}//大括号位置if(dleft.size()!=0&&dleft.size()!=0){
8、}elseif(ep.charAt(j)==']'){dright.push(j);}//小括号位置if(xleft.size()!=0&&xright.size()!=0){ParenthesesIntervaltemp=newParenthesesInterval(xleft.pop(),xright.pop());xlist.add(temp);}//大括号位置if(dleft.size()!=0&&dleft.size()!=0){
此文档下载收益归作者所有