一个表达式计算工具

可以计算: 加减乘除,余数,括号 , 结果为整数,操作数全部为整数。
中间应该有考虑不周的地方,请给我留言。附上计算表达式。
转载,请注明本地址
测试:

public static void main(String[] args) throws Exception {
        String exp = "-1*((24+8)/(6+2)*5-10)/2*2+10/2+200";
        // exp = "(9/2-1)/2*2-1+9-8%2";
       //  exp = "(120-4/2/1*20)/8-1";
       //  exp="100-(1+100)";
       //  exp="-20-20*4/(2+2)*1+100/4";
      //  exp="-100/2*100";
       // exp="-10+100/2";
        exp="-2*(3-4)";
       
        int v = Jisuanqi.cal(exp);
        System.out.println(exp + "=" + v);
    }


/**
*
* @author jeffay.jiang
*/
public class Jisuanqi {

    public static String getFirstNumber(String exp, int pos,boolean bSub) {
        String v = "";
        if(bSub && exp.charAt(pos)=='-'){
            pos++;
            v="-";
        }
        char c = exp.charAt(pos);
        while (c >= '0' && c <= '9' || c == '.') {
            v = v + c;
            pos++;
            if (pos >= exp.length()) {
                break;
            }
            c = exp.charAt(pos);
        }
        return v;
    }

    public static String getKuhaoExp(String exp, int pos) {
        String v = "";
        char c;
        int k = 1;
        int to = pos;
        for (int i = pos; i < exp.length(); i++) {
            if (exp.charAt(i) == '(') {
                k++;
            }
            if (exp.charAt(i) == ')') {
                k--;
            }
            if (k == 0) {
                to = i;
                break;
            }
        }
        v = exp.substring(pos, to);
        //System.out.println("---exp=" + exp + ",Pos=" + pos + ",One=" + v);

        return v;
    }

    public static int cal(String exp) throws Exception {

//        System.out.println("=================" + exp);
       
        if (exp == null || exp.length() == 0) {
            return 0;
        }

        boolean bSub = false;
        String ops = "+-"; //计算加号

        int index = 0;
        while (index < exp.length()) {
            if (exp.charAt(index) == '-') {
                if (bSub) {
                    bSub = false;
                } else {
                    bSub = true;
                }
            }
            if (ops.indexOf(exp.charAt(index)) >= 0) {
                exp = exp.substring(1); //去掉前面的 +- 符号
            } else {
                break;
            }
        }

        int v = 0;
        String kha = "(";
        int k = exp.indexOf(kha);
        if (k >= 0) { //先处理括号
            String exp1 = exp.substring(0, k);
            String exp2 = getKuhaoExp(exp, k + 1);//从k+1起,到匹配的 )
            String exp3 = exp.substring(k + exp2.length() + 2);
            int vp = cal(exp2);
            if (bSub) {
                if(exp1.length()==0){
                    vp=(-1)*vp;
                }else{
                    exp1 = "-" + exp1;
                }
            }
          //  System.out.println("&&&&&&&&&&&&&&&&&& exp1="+exp1 +",vp="+vp+", exp2="+exp2+",v="+vp+", sub="+bSub);           
            String expn=exp1 + String.valueOf(vp) + exp3;
         //   System.out.println("::::::::::::::::::::::::::new exp="+expn +", have-sub="+bSub);
            v = cal(expn);
            return v;
        }

        ops = "*/%";    //先算乘除
        index = 0;
        while (index < exp.length()) {
            if (ops.indexOf(exp.charAt(index)) >= 0) {
                exp = exp.substring(1); //去掉前面的 */% 符号
            } else {
                break;
            }
        }
        for (int i = 0; i < exp.length(); i++) {
            if (ops.indexOf(exp.charAt(i)) >= 0) { //有 乘除符号
                String exp1 = exp.substring(0, i);
                char op = exp.charAt(i);
                String exp2 = exp.substring(i + 1);
                if (bSub) {
                    exp1 = "-" + exp1;
                }
                return chengchu(exp1, op, exp2);
            }
        }

        ops = "+-"; //计算加号 , 在算加减
        for (int i = 0; i < exp.length(); i++) {
            if (ops.indexOf(exp.charAt(i)) >= 0) { //有 乘除符号
                String exp1 = exp.substring(0, i);
                char op = exp.charAt(i);
                String exp2 = exp.substring(i + 1);
                if (bSub) {
                    exp1 = "-" + exp1;
                }
                return jiajian(exp1, op, exp2);
            }
        }


        v = Integer.parseInt(exp);
        if(bSub){
            v=(-1)*v;
        }
        return v;
    }

    private static int chengchu(String exp1, char op, String exp2) throws Exception {
       
      //  System.out.println("**********chengchu*******" + exp1 + "," + op + "," + exp2+"]");
       
        if (exp2.length() == 0) {
            return Integer.parseInt(exp1);
        }
       
        String last = getLastNumber(exp1);
        String next = getFirstNumber(exp2, 0,true);
        String left = exp2.substring(next.length());
        
        exp1 = exp1.substring(0, exp1.length() - last.length());
       
      //  System.out.println("          ******pre=" + exp1 + ", last=" + last + ", next=" + next + ", left=" + left);

        int v = 0;
        if (op == '*') {
            v = Integer.parseInt(last) * Integer.parseInt(next);
        }
        if (op == '/') {
            v = Integer.parseInt(last) / Integer.parseInt(next);
        }
        if (op == '%') {
            v = Integer.parseInt(last) % Integer.parseInt(next);
        }

        if (left.length() > 0 || exp1.length() > 0) {
      //      System.out.println("          ******pre=" + exp1 + ", v=" + v + ", left=" + left);
            v = cal(exp1 + String.valueOf(v) + left);
        }
       
        return v;
    }

    public static String getLastNumber(String exp) {
        String s = "";
        int i = exp.length();
        char c;
        while (i > 0) {
            c = exp.charAt(i - 1);
            i--;
            if (c >= '0' && c <= '9') {
                s = c + s;
            } else {
                break;
            }
        }
        return s;
    }

    private static int jiajian(String exp1, char op, String exp2) throws Exception {
       
       // System.out.println("+++++++++++++++++" + exp1 + "," + op + "," + exp2);

        if (exp2.length() == 0) {
            return Integer.parseInt(exp1);
        }
        String next = getFirstNumber(exp2, 0,false);
        String left = exp2.substring(next.length());
        int v = 0;
        if (op == '+') {
            v = Integer.parseInt(exp1) + Integer.parseInt(next);
        }
        if (op == '-') {
            v = Integer.parseInt(exp1) - Integer.parseInt(next);
        }

        if (left.length() > 0) {
            v = cal(String.valueOf(v) + left);
        }

        return v;
    }
}

你可能感兴趣的:(表达式)