后缀表达式

来源:互联网 发布:淘宝卡首页搜索技术 编辑:程序博客网 时间:2024/05/21 07:14

/* 说明:

        1.明白中缀表达式 后缀表达式的含义。        2.理解中缀表达式>>>>>>>后缀表达式的过程。            符号几种情况:                ①  有 “()” 时,                    当遇到 )  时,我们会把栈元弹出,直到遇到 (                    代码解析:                        for( var i = 0; i < str.length; i++ ){                            if(')' == str[i]){                                while(true){                                    var top = stack.peek();                                    stack.pop();                                    if('(' != top){                                    outStack[outStack.length] = top;                                }else{                                    break;                                }                            }                   ②  + - 和 * /                    当str中某个元素是这种情况,compare栈元素的优先级:                        代码解析:                            if(['*','/'].indexOf(stack.peek()) > -1)                        如果栈顶元素含有* /符号,栈顶元素的优先级高                        弹出栈元素后,把当前符号在push栈中。                    当栈元素中无 * / 后,则是直接去把该元素符号push栈中即可                        代码解析:                        else if(['-','+'].indexOf(str[i]) > -1){                            if(['*','/'].indexOf(stack.peek()) > -1){                                while(['*','/'].indexOf(stack.peek()) > -1){                                    outStack[outStack.length] = stack.peek();                                    stack.pop();                                }                                outStack[outStack.length] = str[i];                            }else{                                stack.push(str[i]);                            }                        }else if(['(','*','/'].indexOf(str[i]) > -1){                            stack.push(str[i]);                ③  数字----直接那个啥                    代码: outStack[outStack.length] = str[i];        3.知道后缀表达式的计算的方式。            案例:var str = '6523+8*+3+*';                循环str                   当:是数字时,直接push栈中,当遇到IsNaN非数字的元素时,计算                        stack.push( eval(stack.pop() + str[i] + stack.pop()));                结果直接push栈中 即可                相信大家看到这里一定明白啦 这个点位 哈哈哈 有错误的地方还请大家指出来哈!!!!                本文引用:http://www.cnblogs.com/tylerdonet/p/5816464.html    */
这里写代码片    function Stack(){        this.top = 0;        this.dataStore = [];        this.pop = pop;        this.push = push;        this.length = length;        this.clear = clear;        this.peek = peek;        this.printElement = printElement;    }    function push(element){        this.dataStore[this.top++] = element;    }    function pop(){        return this.dataStore[--this.top];    }    function length(){        return this.top;    }    function clear(){        this.top = 0;    }    function peek(){        return this.dataStore[this.top - 1];    }    function printElement(){        while(this.top > 0){            document.write(this.pop())        }    }    document.write('<br><br>');    function suffixExpression(){        var str = 'a+b*c+(d*e+f)*g';        var stack = new Stack();        var outStack = new Array();        for( var i = 0; i < str.length; i++ ){            if(')' == str[i]){                while(true){                    var top = stack.peek();                    stack.pop();                    if('(' != top){                        outStack[outStack.length] = top;                    }else{                        break;                    }                }                           }else if(['-','+'].indexOf(str[i]) > -1){                if(['*','/'].indexOf(stack.peek()) > -1){                    while(['*','/'].indexOf(stack.peek()) > -1){                        outStack[outStack.length] = stack.peek();                        stack.pop();                    }                    outStack[outStack.length] = str[i];                }else{                    stack.push(str[i]);                }            }else if(['(','*','/'].indexOf(str[i]) > -1){                stack.push(str[i]);            }else{                outStack[outStack.length] = str[i];            }        }        for(var i = 0; i < outStack.length; i++){            document.write(outStack[i]);        }    }    suffixExpression();    document.write('<br><br>');    function coountSuffixExpression(){        var str = '6523+8*+3+*';        var stack = new Stack();        for( var i = 0; i < str.length; ++i){            if(isNaN(str[i])){                stack.push( eval(stack.pop() + str[i] + stack.pop()));            }else{                stack.push(str[i]);            }        }        document.write(stack.pop());    }    coountSuffixExpression();
原创粉丝点击