java 字符串 表达式_java求字符串型逻辑表达式的bool值

packagenet.rubyeye.codelib.util;/*** 

类说明:用于表达式与实际值的比较

注意事项:


 

创建日期:Aug 6, 2007 10:18:58 AM

文件名:ExpressionParser.java

*@author:庄晓丹

*@version$Id:$*/publicclassExpressionParser {privatestaticfinalbooleanDEBUG=true;privatestaticExpressionParser parser=newExpressionParser();privateExpressionParser() {

}publicstaticExpressionParser getInstance() {returnparser;

}publicbooleanfireRule(String expression,doublefact) {

traceCalculate("\nexpression:"+expression);

expression=expression.replace("\n|\r","").trim();char[] chars=expression.toCharArray();returnparseExpression(fact, chars);

}/***@paramfact

*@paramoperatorsStack

*@paramoperandsStack

*@paramchars

*@paramoperand

*@paramoperator

*@return*/privatebooleanparseExpression(doublefact,char[] chars) {booleanresult=true;

String operand="";

String operator="";

Stack operatorsStack=newStack();

Stack operandsStack=newStack();for(inti=0; i

traceCalculate("token:"+token);if(Character.isDigit(token)||token=='.') {if(!operator.equals("")) {

traceCalculate("push operator:"+operator);//将操作符放入操作符号栈operatorsStack.push(operator);

operator="";

}

operand+=token;

result=checkTail(fact, operatorsStack, operandsStack,

chars.length, operand, result, i);continue;

}elseif(Character.isLetter(token)) {if(!operator.equals("")) {

traceCalculate("push operator:"+operator);//将操作符放入操作符号栈operatorsStack.push(operator);

operator="";

}

operand=String.valueOf(token);

result=checkTail(fact, operatorsStack, operandsStack,

chars.length, operand, result, i);//将操作数放入操作数栈operandsStack.push(operand);

traceCalculate("push operand:"+token);

operand="";continue;

}else{if(!operatorsStack.isEmpty()&&!operandsStack.isEmpty()) {//当前操作数是字母(变量),已存入栈,因此需要取出if(operand.equals("")) {

operand=(String) operandsStack.pop();

result=result&&calculatePerfomance(operatorsStack,

operandsStack, operand, fact);//当前操作数是数字}else{

result=result&&calculatePerfomance(operatorsStack,

operandsStack, operand, fact);

}

}if(!operand.equals("")) {

result=checkTail(fact, operatorsStack, operandsStack,

chars.length, operand, result, i);//将操作数放入操作数栈operandsStack.push(operand);

traceCalculate("push2 operand:"+operand);

operand="";

}

operator+=token;continue;

}

}returnresult;

}/*** 判断是否已经到表达式尾端,如果是,计算

*@paramfact

*@paramoperatorsStack

*@paramoperandsStack

*@paramchars

*@paramoperand

*@paramresult

*@parami

*@return*/privatebooleancheckTail(doublefact, Stack operatorsStack,

Stack operandsStack,intchars_length, String operand,booleanresult,inti) {if(i==chars_length-1) {

result=result&&calculatePerfomance(operatorsStack, operandsStack,

operand, fact);

}returnresult;

}privatevoiddisplayStack(String name,Stack stack) {if(DEBUG) {for(inti=0; i

System.out.println(name+stack.pool.get(i));

}

}privatebooleancalculatePerfomance(Stack operatorsStack,

Stack operandsStack, String currentOperand,doublefact) {

traceCalculate("开始计算");

displayStack("operators stack:",operatorsStack);

displayStack("operands stack:",operandsStack);

traceCalculate("currentOperand="+currentOperand);

String operator=(String) operatorsStack.pop();doublelastOperand=coverOperandToDouble((String) operandsStack.pop(),

fact);doublenextOperand=coverOperandToDouble(currentOperand, fact);booleanresult=true;if(operator.equals("=="))returnlastOperand==nextOperand;

if (operator.indexOf("=") >= 0)

hasEqual = true;char[] operators=operator.toCharArray();for(inti=0; i

result=result&&(lastOperand

result=(lastOperand==nextOperand);break;case'>':

result=result&&(lastOperand>nextOperand);break;

}

}

if ((!result) && hasEqual)

result = lastOperand == nextOperand;returnresult;

}/*** 用于debug*/privatevoidtraceCalculate(String info) {if(DEBUG)

System.out.println(info);

}privatedoublecoverOperandToDouble(String operand,doublefact) {//如果是字母,也就是变量,返回fact变量if(Character.isLetter(operand.toCharArray()[0]))returnfact;elsereturnDouble.parseDouble(operand);

}

}

表情包
插入表情
评论将由博主筛选后显示,对所有人可见 | 还能输入1000个字符
相关推荐
<div style="color:rgba(0,0,0,.75);"> <span style="color:#4d4d4d;"> </span> <div style="color:rgba(0,0,0,.75);"> <span style="color:#4d4d4d;"> </span> <div style="color:rgba(0,0,0,.75);"> <div style="color:rgba(0,0,0,.75);"> <span style="color:#4d4d4d;">当前课程中商城项目的实战源码是我发布在 GitHub 上的开源项目 newbee-mall (新蜂商城),目前已有 6300 多个 star,</span><span style="color:#4d4d4d;">本课程是一个 Spring Boot 技术栈的实战类课程,课程共分为 3 大部分,前面两个部分为基础环境准备和相关概念介绍,第三个部分是 Spring Boot 商城项目功能的讲解,让大家实际操作并实践上手一个大的线上商城项目,并学习到一定的开发经验以及其中的开发技巧。<br /> 商城项目所涉及的功能结构图整理如下:<br /> </span> </div> <div style="color:rgba(0,0,0,.75);">   </div> <div style="color:rgba(0,0,0,.75);"> <p style="color:#4d4d4d;"> <img alt="modules" src="https://imgconvert.csdnimg.cn/aHR0cHM6Ly9uZXdiZWUtbWFsbC5vc3MtY24tYmVpamluZy5hbGl5dW5jcy5jb20vcG9zdGVyL3N0b3JlL25ld2JlZS1tYWxsLXMucG5n?x-oss-process=image/format,png" /> </p> </div> <p style="color:rgba(0,0,0,.75);"> <strong><span style="color:#e53333;">课程特色</span></strong> </p> <p style="color:rgba(0,0,0,.75);">   </p> <div style="color:rgba(0,0,0,.75);">   </div> <div style="color:rgba(0,0,0,.75);"> <ul> <li> 对新手开发者十分友好,无需复杂的操作步骤,仅需 2 秒就可以启动这个完整的商城项目 </li> <li> 最终的实战项目是一个企业级别的 Spring Boot 大项目,对于各个阶段的 Java 开发者都是极佳的选择 </li> <li> 实践项目页面美观且实用,交互效果完美 </li> <li> 教程详细开发教程详细完整、文档资源齐全 </li> <li> 代码+讲解+演示网站全方位保证,向 Hello World 教程说拜拜 </li> <li> 技术栈新颖且知识点丰富,学习后可以提升大家对于知识的理解和掌握,可以进一步提升你的市场竞争力 </li> </ul> </div> <p style="color:rgba(0,0,0,.75);">   </p> <p style="color:rgba(0,0,0,.75);"> <span style="color:#e53333;">课程预览</span> </p> <p style="color:rgba(0,0,0,.75);">   </p> <div style="color:rgba(0,0,0,.75);">   </div> <div style="color:rgba(0,0,0,.75);"> <p style="color:#4d4d4d;"> 以下为商城项目的页面和功能展示,分别为: </p> </div> <div style="color:rgba(0,0,0,.75);"> <ul> <li> 商城首页 1<br /> <img alt="" src="https://img-bss.csdnimg.cn/202103050347585499.gif" /> </li> <li> 商城首页 2<br /> <img alt="" src="https://img-bss.csdn.net/202005181054413605.png" /> </li> <li>   </li> <li> 购物车<br /> <img alt="cart" src="https://imgconvert.csdnimg.cn/aHR0cHM6Ly9uZXdiZWUtbWFsbC5vc3MtY24tYmVpamluZy5hbGl5dW5jcy5jb20vcG9zdGVyL3Byb2R1Y3QvY2FydC5wbmc?x-oss-process=image/format,png" /> </li> <li> 订单结算<br /> <img alt="settle" src="https://imgconvert.csdnimg.cn/aHR0cHM6Ly9uZXdiZWUtbWFsbC5vc3MtY24tYmVpamluZy5hbGl5dW5jcy5jb20vcG9zdGVyL3Byb2R1Y3Qvc2V0dGxlLnBuZw?x-oss-process=image/format,png" /> </li> <li> 订单列表<br /> <img alt="orders" src="https://imgconvert.csdnimg.cn/aHR0cHM6Ly9uZXdiZWUtbWFsbC5vc3MtY24tYmVpamluZy5hbGl5dW5jcy5jb20vcG9zdGVyL3Byb2R1Y3Qvb3JkZXJzLnBuZw?x-oss-process=image/format,png" /> </li> <li> 支付页面<br /> <img alt="" src="https://img-bss.csdn.net/201909280301493716.jpg" /> </li> <li> 后台管理系统登录页<br /> <img alt="login" src="https://imgconvert.csdnimg.cn/aHR0cHM6Ly9uZXdiZWUtbWFsbC5vc3MtY24tYmVpamluZy5hbGl5dW5jcy5jb20vcG9zdGVyL3Byb2R1Y3QvbWFuYWdlLWxvZ2luLnBuZw?x-oss-process=image/format,png" /> </li> <li> 商品管理<br /> <img alt="goods" src="https://imgconvert.csdnimg.cn/aHR0cHM6Ly9uZXdiZWUtbWFsbC5vc3MtY24tYmVpamluZy5hbGl5dW5jcy5jb20vcG9zdGVyL3Byb2R1Y3QvbWFuYWdlLWdvb2RzLnBuZw?x-oss-process=image/format,png" /> </li> <li> 商品编辑<br /> <img alt="" src="https://img-bss.csdnimg.cn/202103050348242799.png" /> </li> </ul> </div> </div> </div> </div>
©️2020 CSDN 皮肤主题: 游动-白 设计师:白松林 返回首页