[原创]表达式求值:经典算法

1.课件:表达式的三种表示形式及其规律

幻灯片45幻灯片46幻灯片47幻灯片48

 

2.后缀表达式求值以及如何实现

Knuth 将此概括为三个步骤:
  对中缀表达式进行语法分析
  中缀表达式到后缀表达式的转换
  对后缀表达式求值

幻灯片49幻灯片50幻灯片51

幻灯片52幻灯片53

 

C语言建议代码是实现:

幻灯片54幻灯片55

 

3.我的Java语言实现,利用了Java自身的优越性,可以更好的处理一些内容 (做这个是为了完成数据结构的课程设计,后期会有相应的文章介绍我的作品《算术24游戏》,谢谢关注)

    //测试的main方法

    public static void main(String arg[]) {

        String s = "2+6*7-5/1*7";

        ArrayList postfix = transform(s);

        for (int i = 0, len = postfix.size(); i < len; i++) {

            System.out.println(postfix.get(i));

        }

        calculate(postfix);

    }    



    //将中缀表达式转换成后缀表达式

    public static ArrayList transform(String prefix) {

        System.out.println("transform");

        int i, len = prefix.length();// 用字符数组保存前缀表达式

        prefix=prefix+ '#';// 让前缀表达式以'#'结尾

        Stack<Character> stack = new Stack<Character>();// 保存操作符的栈

        stack.push('#');// 首先让'#'入栈

        ArrayList postfix = new ArrayList();

        // 保存后缀表达式的列表,可能是数字,也可能是操作符,之前使用的是ArrayList

        for (i = 0; i < len + 1; i++) {

            System.out.println(i+" "+prefix.charAt(i));

            if (Character.isDigit(prefix.charAt(i))) {// 当前字符是一个数字

                if (Character.isDigit(prefix.charAt(i+1))) {// 当前字符的下一个字符也是数字(两位数)

                    postfix.add(10 * (prefix.charAt(i)-'0') + (prefix.charAt(i+1)-'0'));

                    i++;

                } else {// 当前字符的下一个字符不是数字(一位数)

                    postfix.add((prefix.charAt(i)-'0'));

                }

            } else {// 当前字符是一个操作符

                switch (prefix.charAt(i)) {

                case '(':// 如果是开括号

                    stack.push(prefix.charAt(i));// 开括号只是放入到栈中,不放入到后缀表达式中

                    break;

                case ')':// 如果是闭括号

                    while (stack.peek() != '(') {

                        postfix.add(stack.pop());// 闭括号是不入栈的

                    }

                    stack.pop();// 弹出'('

                    break;

                default:// 默认情况下:+ - * /

                    while (stack.peek() != '#'

                            && compare(stack.peek(), prefix.charAt(i))) {

                        postfix.add(stack.pop());// 不断弹栈,直到当前的操作符的优先级高于栈顶操作符

                    }

                    if (prefix.charAt(i) != '#') {// 如果当前的操作符不是'#'(结束符),那么入操作符栈

                        stack.push(prefix.charAt(i));// 最后的标识符'#'是不入栈的

                    }

                    break;

                }

            }

        }

        return postfix;

    }



    //比较运算符之间的优先级

    public static boolean compare(char peek, char cur) {// 如果是peek优先级高于cur,返回true,默认都是peek优先级要低

        if (peek == '*'

                && (cur == '+' || cur == '-' || cur == '/' || cur == '*')) {// 如果cur是'(',那么cur的优先级高,如果是')',是在上面处理

            return true;

        } else if (peek == '/'

                && (cur == '+' || cur == '-' || cur == '*' || cur == '/')) {

            return true;

        } else if (peek == '+' && (cur == '+' || cur == '-')) {

            return true;

        } else if (peek == '-' && (cur == '+' || cur == '-')) {

            return true;

        } else if (cur == '#') {// 这个很特别,这里说明到了中缀表达式的结尾,那么就要弹出操作符栈中的所有操作符到后缀表达式中

            return true;// 当cur为'#'时,cur的优先级算是最低的

        }

        return false;// 开括号是不用考虑的,它的优先级一定是最小的,cur一定是入栈

    }

    

    //计算后缀表达式

    public static boolean calculate(ArrayList postfix){//后缀表达式的运算顺序就是操作符出现的先后顺序

        System.out.println("calculate");

        int i,res=0,size=postfix.size();

        Stack<Integer> stack_num=new Stack<Integer>();

        for(i=0;i<size;i++){

            if(postfix.get(i).getClass()==Integer.class){//说明是操作数,这个很有用啊!

                stack_num.push((Integer)postfix.get(i));

                System.out.println("push"+" "+(Integer)postfix.get(i));

            }else{//如果是操作符

                System.out.println((Character)postfix.get(i));

                int a=stack_num.pop();

                int b=stack_num.pop();//注意运算时的前者和后者

                switch((Character)postfix.get(i)){

                case '+':

                    res=b+a;

                    System.out.println("+ "+a+" "+b);

                    break;

                case '-':

                    res=b-a;

                    System.out.println("- "+a+" "+b);

                    break;

                case '*':

                    res=b*a;

                    System.out.println("* "+a+" "+b);

                    break;

                case '/':

                    res=b/a;

                    System.out.println("/ "+a+" "+b);

                    break;

                }

                stack_num.push(res);

                System.out.println("push"+" "+res);

            }

        }

        res=stack_num.pop();

        System.out.println("res "+" "+res);

        if(res==24){

            return true;

        }

        return false;

    }

程序中的很多的输出是为了让我们更加清楚地看到整个的执行过程:想知道的话可以看看输出结果

transform
0 2
1 +
2 6
3 *
4 7
5 -
6 5
7 /
8 1
9 *
10 7
11 #
2
6
7
*
+
5
1
/
7
*
-
calculate
push 2
push 6
push 7
*
* 7 6
push 42
+
+ 42 2
push 44
push 5
push 1
/
/ 1 5
push 5
push 7
*
* 7 5
push 35
-
- 35 44
push 9
res  9

中缀表达式为 2+6*7-5/1*7 ,最终的结果是 9

中缀表达式为 (2+6)*7-5/1*7 ,最终的结果是 21

 

注:你可能觉得整个代码的结构不是很好,这个因为这个程序不是单纯的用于计算中缀表达式的,它是我做的《算术24游戏》的其中的一个模块,后期将会呈现,敬请期待

你可能感兴趣的:(java)