JAVA四则运算字符串解释器

最近学习到后缀表达式,于是基于后缀表达式的思想,写了一个四则运算解释器,输入字符串类型的四则运算表达式,可以直接得到结果,支持括号嵌套. 
实现时主要考虑以下两点:

  • 字符串中运算符和数字分离
  • 运算符优先级
  • 括号的嵌套

  1. 运算符和数字分离:可以考虑用字符串数组存储
  2. 关于运算符优先级,最开始的想法是将乘除法看作一类,加减法看作一类,乘除法的优先级大于加减法,相同类型的运算符按照从左到右顺序依次计算.
  3. 括号的嵌套:由于括号内部本身也是表达式,因此可以使用递归处理,但重点在于括号的配对,由于使用递归,所以希望获取最大的嵌套单元.

具体实现:

首先实现运算符识别的代码,判断一个字符是否为运算符:

public static boolean opjudge(char c) {
        if (c == '+' || c == '-' || c == '*' || c == '/' || c == '(' || c == ')')
            return true;
        else
            return false;
    }

然后实现字符串表达式转为字符串数组, 由于要求输入的字符串运算符和数字之间没有空格,所以直接拆分不方便,而且运算符和数字的数目不确定,因此先使用ArrayList存储,然后再转成字符串数组,主要方法是遍历字符串中每一个字符并判断是否为运算符,如果是运算符,则直接将运算符转为字符串加入ArrayList,如果不是,则从此位置继续查找到下一个运算符出现的位置,两个位置之间即为操作数,使用substring截取字符串存入Arraylist,具体实现如下:

复制代码

public static String[] convert(String s) {
        ArrayList<String> arrayList = new ArrayList<>();
        for (int i = 0; i < s.length(); i++) {
            if (!opjudge(s.charAt(i))) {
                int j = i;
                while ((i < s.length()) && !opjudge(s.charAt(i)))
                    i++;
                arrayList.add(s.substring(j, i));
                i--;
            } else
                arrayList.add(String.valueOf(s.charAt(i)));
        }

        return arrayList.toArray(new String[arrayList.size()]);

    }

复制代码

然后对运算符进行分类和分级,加减法视作一类,乘除法视作一类,乘除法优先级高于加减法:

复制代码

 public static int opvalue(String s) {
        switch (s) {
        case "+":
        case "-":
            return 1;
        case "*":
        case "/":
            return 2;
        default:
            return -1;
        }

    }

复制代码

运算符优先级的比较,左边大于右边则返回true,( 两个运算符相等返回false)

复制代码

 public static boolean opcompare(String s1, String s2) {
        if (opvalue(s1) > opvalue(s2))
            return true;
        else {
            return false;
        }

    }

复制代码

括号内字符串的获取: 
由于括号都是成对出现,要从左至右获取第一个最长的括号表达式,则从左至右遍历字符串时,当右括号 ‘)’ 出现的数目等于左括号’(’ 时,即为所求,此处使用计数器实现,返回的是字符串中从k位置(第k个元素,一个数字算一个元素 )开始第一个带完整括号的子串,比如输入bracketGet(“(8-(2+3))*2+(5+7)*3”,0),返回8-(2+3):

复制代码

public static String bracketGet(String s, int k) {
        int m=0;
        int i;
        String[] arr=convert(s);
        for(i=k;i<arr.length;i++){
            if(arr[i].equals("(")){
                m++;
                continue;
                }
            if(arr[i].equals(")")){
                m--;
                if(m==0)
                    break;
                else
                    continue;
            }

        }
        StringBuilder sb=new StringBuilder();
        for(int j=k+1;j<i;j++){
            sb.append(arr[j]);
        }
        return sb.toString();
    }

复制代码

以上都是需要用到的配件,下面将利用上面写好的函数实现四则运算字符串的解析和运算,基本思想是使用两个栈,一个存储数字,一个存储运算符,先考虑没有括号的简单情况: 
遍历字符串数组: 
1.当前元素为操作数时,入数字栈 
2.当前元素为运算符时:

  • 如果运算符栈为空,则入运算符栈
  • 如果运算符栈不空,则比较当前运算符和栈顶运算符优先级(按照之前定义的优先级,乘除大于加减,乘除相同,加减相同):如果当前运算符优先级大于栈顶运算符,则当前运算符入栈;反之,如果当前运算符优先级等于或小于栈顶运算符,则将栈顶运算符出栈,同时数字栈中出栈两个数字参与运算,结果压入数字栈中,然后当前运算符入栈;
  • 最后将运算符栈中所有元素依次出栈,数字栈中相应将数字出栈参与运算得到结果并压入栈中;
  • 最后返回数字栈中栈顶元素即为表达式结果.
  • 有括号时括号内的运算步骤同上,可以获取括号内部的字符串,使用递归计算; 
    代码如下:
  • 复制代码

    public static int caculate(String formula) {
            String[] arr = convert(formula);
            Stack<Integer> val = new Stack<>();
            Stack<String> op = new Stack<>();
    
            for (int i = 0; i < arr.length; i++) {
                if (arr[i].equals("(")) {
                    val.push(caculate(bracketGet(formula, i)));//递归计算括号内部的值,并将该值入栈
                    i = i + bracketGet(formula, i).length() + 1;//括号串所在的位置遍历过,i需要跳过该段
                } else {
                    if (arr[i].equals("+") || arr[i].equals("-") || arr[i].equals("*") || arr[i].equals("/")) {
                        if (op.isEmpty())
                            op.push(arr[i]);
                        else if (opcompare(arr[i], op.lastElement())) {
                            op.push(arr[i]);
                        } else {
    
                            switch (op.pop()) {
                            case "+":
                                val.push(val.pop() + val.pop());
                                break;
                            case "-":
                                int c = val.pop();
                                int d = val.pop();  
                                val.push(d-c);//减法,pop的顺序从右至左,所以需要先取出元素
                                break;
                            case "*":
                                val.push(val.pop() * val.pop());
                                break;
                            case "/":
                                int a = val.pop();
                                int b = val.pop();
                                val.push(b / a);//同减法
                                break;
                            default:
                                break;
                            }
                            op.push(arr[i]);
                        }
                    } else
                        val.push(Integer.parseInt(arr[i]));
                }
            }
            while (!op.isEmpty()) {
                switch (op.pop()) {
                case "+":
                    val.push(val.pop() + val.pop());
                    break;
                case "-":
                    int c = val.pop();
                    int d = val.pop();
                    val.push(d-c);
                    break;
                case "*":
                    val.push(val.pop() * val.pop());
                    break;
                case "/":
                    int a = val.pop();
                    int b = val.pop();
                    val.push(b / a);
                    break;
                default:
                    break;
                }
            }
    
            return val.pop();
        }
    System.out.println(caculate("(((8-(2+3))*2+(5+7)*3))"));
    输出:42 正确

    复制代码

    以上实现是使用int类型,除法可能会不准确,改为double会准确


    后面又发现上面的实现方法仍然有冗余之处,下面是改进的代码,主要是优化了运算符的优先级,依次为 除法 乘法 减法 加法 依次降低, 遍历到运算符时,优先级高才入栈, 否则将栈中运算符出栈参与运算,直到栈空或 栈顶运算符优先级低于遍历到的运算符,才将该运算符入栈. 
    主要改动的是opjudge(),opcompare(), caculate()中少量改动,其余代码同上; 
    具体代码如下:

  • 复制代码

    public static int opvalue2(String s) {
            switch (s) {
            case "+":
                return 1;
            case "-":
                return 2;
            case "*":
                return 3;
            case "/":
                return 4;
            default:
                return -1;
            }
        }

    复制代码

    复制代码

    public static boolean opcompare2(String s1, String s2) {
            if (opvalue2(s1) >= opvalue2(s2))
                return true;
            else {
                return false;
            }
        }

    复制代码

    复制代码

    public static double caculate2(String formula) {
            String[] arr = convert(formula);
            Stack<Double> val = new Stack<>();
            Stack<String> op = new Stack<>();
    
            for (int i = 0; i < arr.length; i++) {
                if (arr[i].equals("(")) {
                    val.push(caculate2(bracketGet(formula, i)));
                    i = i + bracketGet(formula, i).length() + 1;
                } else if (arr[i].equals("+") || arr[i].equals("-") || arr[i].equals("*") || arr[i].equals("/")) {
                            while (!op.isEmpty() && opcompare2(op.lastElement(), arr[i])) {
                                switch (op.pop()) {
                                case "+":
                                    val.push(val.pop() + val.pop());
                                    continue;
                                case "-":
                                    double c = val.pop();
                                    double d = val.pop();
                                    val.push(d - c);
                                    continue;
                                case "*":
                                    val.push(val.pop() * val.pop());
                                    continue;
                                case "/":
                                    double a = val.pop();
                                    double b = val.pop();
                                    val.push(b / a);
                                    continue;
                                default:
                                    break;
                                }
                            }
                            op.push(arr[i]);        
                    } 
                    else
                        val.push(Double.parseDouble(arr[i]));
            }
            while (!op.isEmpty()) {
                switch (op.pop()) {
                case "+":
                    val.push(val.pop() + val.pop());
                    break;
                case "-":
                    double c = val.pop();
                    double d = val.pop();
                    val.push(d - c);
                    break;
                case "*":
                    val.push(val.pop() * val.pop());
                    break;
                case "/":
                    double a = val.pop();
                    double b = val.pop();
                    val.push(b / a);
                    break;
                default:
                    break;
                }
            }
            return val.pop();
        }

    复制代码

    运算实例:

  • 复制代码

    public static void main(String[] args) {
            double a=(1+2*(3.0/2)/(2+8)-(2*6.0)/(1+7))-(21+3*(5-2-(7*(4-3))));
            System.out.println(a);
            System.out.println(caculate2("(1+2*(3/2)/(2+8)-(2*6)/(1+7))-(21+3*(5-2-(7*(4-3))))"));
        }
    //output:
    -9.2
    -9.2

    复制代码

    至此,完成了四则运算字符串解释器,还有很多可以完善, 比如异常的处理,以及代码的简洁性,作为一个初学者,如果能被看到的话,希望多提建议:

    完整版代码如下:

  • 复制代码

    import java.util.ArrayList;
    import java.util.Stack;
    
    public class Main {
    
        public static void main(String[] args) {
            double a=(1+2*(3.0/2)/(2+8)-(2*6.0)/(1+7))-(21+3*(5-2-(7*(4-3))));
            System.out.println(a);
            System.out.println(caculate2("(1+2*(3/2)/(2+8)-(2*6)/(1+7))-(21+3*(5-2-(7*(4-3))))"));
        }
    
        //对运算符优先级进一步排序  减法大于加法   除法大于乘法  
        public static double caculate2(String formula) {
            String[] arr = convert(formula);
            Stack<Double> val = new Stack<>();
            Stack<String> op = new Stack<>();
    
            for (int i = 0; i < arr.length; i++) {
                if (arr[i].equals("(")) {
                    val.push(caculate2(bracketGet(formula, i)));
                    i = i + bracketGet(formula, i).length() + 1;
                } else if (arr[i].equals("+") || arr[i].equals("-") || arr[i].equals("*") || arr[i].equals("/")) {
                            while (!op.isEmpty() && opcompare2(op.lastElement(), arr[i])) {
                                switch (op.pop()) {
                                case "+":
                                    val.push(val.pop() + val.pop());
                                    continue;
                                case "-":
                                    double c = val.pop();
                                    double d = val.pop();
                                    val.push(d - c);
                                    continue;
                                case "*":
                                    val.push(val.pop() * val.pop());
                                    continue;
                                case "/":
                                    double a = val.pop();
                                    double b = val.pop();
                                    val.push(b / a);
                                    continue;
                                default:
                                    break;
                                }
                            }
                            op.push(arr[i]);        
                    } 
                    else
                        val.push(Double.parseDouble(arr[i]));
            }
            while (!op.isEmpty()) {
                switch (op.pop()) {
                case "+":
                    val.push(val.pop() + val.pop());
                    break;
                case "-":
                    double c = val.pop();
                    double d = val.pop();
                    val.push(d - c);
                    break;
                case "*":
                    val.push(val.pop() * val.pop());
                    break;
                case "/":
                    double a = val.pop();
                    double b = val.pop();
                    val.push(b / a);
                    break;
                default:
                    break;
                }
            }
            return val.pop();
        }
    
    public static String bracketGet(String s, int k) {
            int m=0;
            int i;
            String[] arr=convert(s);
            for(i=k;i<arr.length;i++){
                if(arr[i].equals("(")){
                    m++;
                    continue;
                    }
                if(arr[i].equals(")")){
                    m--;
                    if(m==0)
                        break;
                    else
                        continue;
                }
            }
            StringBuilder sb=new StringBuilder();
            for(int j=k+1;j<i;j++){
                sb.append(arr[j]);
            }
            return sb.toString();
        }
    
    public static String[] convert(String s) {
            ArrayList<String> arrayList = new ArrayList<>();
            for (int i = 0; i < s.length(); i++) {
                if (!opjudge(s.charAt(i))) {
                    int j = i;
                    while ((i < s.length()) && !opjudge(s.charAt(i)))
                        i++;
                    arrayList.add(s.substring(j, i));
                    i--;
                } else
                    arrayList.add(String.valueOf(s.charAt(i)));
            }
            return arrayList.toArray(new String[arrayList.size()]);
        }
    
        public static boolean opjudge(char c) {
        if (c == '+' || c == '-' || c == '*' || c == '/' || c == '(' || c == ')')
            return true;
        else
            return false;
    }
    
        public static int opvalue2(String s) {
            switch (s) {
            case "+":
                return 1;
            case "-":
                return 2;
            case "*":
                return 3;
            case "/":
                return 4;
            default:
                return -1;
            }
        }
    
        public static boolean opcompare2(String s1, String s2) {
            if (opvalue2(s1) >= opvalue2(s2))
                return true;
            else {
                return false;
            }
        }

    复制代码

最近学习到后缀表达式,于是基于后缀表达式的思想,写了一个四则运算解释器,输入字符串类型的四则运算表达式,可以直接得到结果,支持括号嵌套. 
实现时主要考虑以下两点:

  • 字符串中运算符和数字分离
  • 运算符优先级
  • 括号的嵌套

  1. 运算符和数字分离:可以考虑用字符串数组存储
  2. 关于运算符优先级,最开始的想法是将乘除法看作一类,加减法看作一类,乘除法的优先级大于加减法,相同类型的运算符按照从左到右顺序依次计算.
  3. 括号的嵌套:由于括号内部本身也是表达式,因此可以使用递归处理,但重点在于括号的配对,由于使用递归,所以希望获取最大的嵌套单元.

具体实现:

首先实现运算符识别的代码,判断一个字符是否为运算符:

public static boolean opjudge(char c) {
        if (c == '+' || c == '-' || c == '*' || c == '/' || c == '(' || c == ')')
            return true;
        else
            return false;
    }

然后实现字符串表达式转为字符串数组, 由于要求输入的字符串运算符和数字之间没有空格,所以直接拆分不方便,而且运算符和数字的数目不确定,因此先使用ArrayList存储,然后再转成字符串数组,主要方法是遍历字符串中每一个字符并判断是否为运算符,如果是运算符,则直接将运算符转为字符串加入ArrayList,如果不是,则从此位置继续查找到下一个运算符出现的位置,两个位置之间即为操作数,使用substring截取字符串存入Arraylist,具体实现如下:

    public static String[] convert(String s) {
        ArrayList<String> arrayList = new ArrayList<>();
        for (int i = 0; i < s.length(); i++) {
            if (!opjudge(s.charAt(i))) {
                int j = i;
                while ((i < s.length()) && !opjudge(s.charAt(i)))
                    i++;
                arrayList.add(s.substring(j, i));
                i--;
            } else
                arrayList.add(String.valueOf(s.charAt(i)));
        }

        return arrayList.toArray(new String[arrayList.size()]);

   :
    public static void main(String[] args) {
        double a=(1+2*(3.0/2)/(2+8)-(2*6.0)/(1+7))-(21+3*(5-2-(7*(4-3))));
        System.out.println(a);
        System.out.println(caculate2("(1+2*(3/2)/(2+8)-(2*6)/(1+7))-(21+3*(5-2-(7*(4-3))))"));
    }
//output:
-9.2
-9    }

    //对运算符优先级进一步排序  减法大于加法   除法大于乘法  
    public static double caculate2(String formula) {
        String[] arr = convert(formula);
        Stack<Double> val = new Stack<>();
        Stack<String> op = new Stack<>();

        for (int i = 0; i < arr.length; i++) {
            if (arr[i].equals("(")) {
                val.push(caculate2(bracketGet(formula, i)));
                i = i + bracketGet(formula, i).length() + 1;
            } else if (arr[i].equals("+") || arr[i].equals("-") || arr[i].equals("*") || arr[i].equals("/")) {
                        while (!op.isEmpty() && opcompare2(op.lastElement(), arr[i])) {
                            switch (op.pop()) {
                            case "+":
                                val.push(val.pop() + val.pop());
                                continue;
                            case "-":
                                double c = val.pop();
                                double d = val.pop();
                                val.push(d - c);
                                continue;
                            case "*":
                                val.push(val.pop() * val.pop());
                                continue;
                            case "/":
                                double a = val.pop();
                                double b = val.pop();
                                val.push(b / a);
                                continue;
                            default:
                                break;
                            }
                        }
                        op.push(arr[i]);        
                } 
                else
                    val.push(Double.parseDouble(arr[i]));

        }
        while (!op.isEmpty()) {
            switch (op.pop()) {
            case "+":
                val.push(val.pop() + val.pop());
                break;
            case "-":
                double c = val.pop();
                double d = val.pop();
                val.push(d - c);
                break;
            case "*":
                val.push(val.pop() * val.pop());
                break;
            case "/":
                double a = val.pop();
                double b = val.pop();
                val.push(b / a);
                break;
            default:
                break;
            }
        }

        return val.pop();
    }

public static String bracketGet(String s, int k) {
        int m=0;
        int i;
        String[] arr=convert(s);
        for(i=k;i<arr.length;i++){
            if(arr[i].equals("(")){
                m++;
                continue;
                }
            if(arr[i].equals(")")){
                m--;
                if(m==0)
                    break;
                else
                    continue;
            }

        }
        StringBuilder sb=new StringBuilder();
        for(int j=k+1;j<i;j++){
            sb.append(arr[j]);
        }
        return sb.toString();
    }

public static String[] convert(String s) {
        ArrayList<String> arrayList = new ArrayList<>();
        for (int i = 0; i < s.length(); i++) {
            if (!opjudge(s.charAt(i))) {
                int j = i;
                while ((i < s.length()) && !opjudge(s.charAt(i)))
                    i++;
                arrayList.add(s.substring(j, i));
                i--;
            } else
                arrayList.add(String.valueOf(s.charAt(i)));
        }

        return arrayList.toArray(new String[arrayList.size()]);

    }

    public static boolean opjudge(char c) {
    if (c == '+' || c == '-' || c == '*' || c == '/' || c == '(' || c == ')')
        return true;
    else
        return false;
}

    public static int opvalue2(String s) {
        switch (s) {
        case "+":
            return 1;
        case "-":
            return 2;
        case "*":
            return 3;
        case "/":
            return 4;
        default:
            return -1;
        }

    }

    public static boolean opcompare2(String s1, String s2) {
        if (opvalue2(s1) >= opvalue2(s2))
            return true;
        else {
            return false;
        }

猜你喜欢

转载自blog.csdn.net/m0_37822534/article/details/83150010