java计算无括号算式

该博客介绍了如何在Java中实现一个计算无括号算式的函数,如1+2*5-8,不使用栈或递归。代码通过遍历字符串处理运算符和数字,处理了除数为0和其他可能的错误情况。异常用于捕获表达式错误,遵循阿里巴巴开发手册的建议,不推荐用异常处理业务逻辑。
摘要由CSDN通过智能技术生成

 一个循环,没有栈,没有递归。逻辑有点绕,所以没什么注释。(带括号算式

(阿里开发手册提到:不要用异常来处理业务,异常抛出耗时的问题。这里为了拿到错误信息就用了异常,不需要异常信息返回null即可)

/**
     * 计算无括号算式,如 1+2*5-8 。只返回null或double。算式异常时返回null
     * @author: 一只拖拉机
     * @param a
     * @exception ExpressionException :表达式错误异常
     * @exception NumberFormatException 除数0、数字转化异常
     * @return
     * @version
     */
    private static Double computeNumber(String a) {
    	if(a == null)return null;
    	char[] chars = a.toCharArray();
    	Double tem = null,m1=null;
    	int f=0;
    	char ty = 0,f1=0,f2=0; // 1+ 2- 3 * 4/
    	for(int i=0;i<chars.length;i++) {
    		char t = chars[i];
    		if(t<42 || t== 44 || t > 57)throw new ExpressionException("出现了不应该出现的字符:"+t);; // 只允许的符号   0至9 + - . / * 
    		if(t < 48 && t != 46 && i > 0 && chars[i-1] > 47 && chars[i-1] < 58) { // + - * /
    			if(t=='-' || t=='+') {
    				if(m1 != null) {
    					Double ddd= Double.valueOf(String.valueOf(chars, f, i-f));
        				if(f1 > 0) {
        					tem =  computeNormal(f1,tem,computeNormal(f2,m1,ddd));
        				}else {
        					tem = computeNormal(f2,m1,ddd);
        				}
        				m1 = null;
        			}else if(f1=='-') {
        				tem =tem - Double.valueOf(String.valueOf(chars, f, i-f));
        			}else if (f1 == '+') {
        				tem = tem + Double.valueOf(String.valueOf(chars, f, i-f));
        			}else { // 首次
        				tem = Double.valueOf(String.valueOf(chars, f, i-f));
        			}
        			f1 = t;
    			}else {
    				if(m1 != null) {
    					m1 = computeNormal(f2,m1,Double.valueOf(String.valueOf(chars, f, i-f)));
        			}else {
        				m1 = Double.valueOf(String.valueOf(chars, f, i-f));
        			}
    				f2 = t;
    			}
    			ty = t;
    			f = i + 1;
    		}
    		if(i == chars.length-1) {
    			if(i-f+1 == 0) throw new ExpressionException("错误的表达式:"+chars[i]); // 表示最后一个是字符
    			Double ddd= Double.valueOf(String.valueOf(chars, f,  i-f+1));
    			if(f == 0)return ddd;
    			if(m1 != null) {
    				if(f1 > 0) {
    					return computeNormal(f1,tem,computeNormal(f2,m1,ddd));
    				}
    				return computeNormal(f2,m1,ddd);
    			}
    			return computeNormal(ty,tem,ddd);
    		}
    	}
    	throw new ExpressionException("错误的表达式");
    }
      
    
    private static Double computeNormal(char sign,Double d1,Double d2) {
    	if(d1==null || d2== null)return null;
    	switch(sign) {
    	    case '-':
    	    	return d1 - d2;
    	    case '+':
    	    	return d1 + d2;
    	    case '*':
    	    	return d1 * d2;
			case '/':
				if(d2 == 0)throw new NumberFormatException("除数不能为0");
				return d1 / d2;
			default :
				return null;
    	}
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值