JS中的后缀算法评估

Postfix Algorithm Evaluation in JS

这就是我们的开始:'7.7+7' 函数的输入是一个字符串数组(原始输入转换为后缀):7.7,7,+

然后会反馈到另一个函数,以便在我的 html 页面中显示。此函数嵌套在另一个父函数中以使用 class 对象调用。

this.postfixEval = function(postfixArray){
        var stack = new Stack();

        for( element of postfixArray){
            console.log("element: " + element);

            if(isNaN(element)){
                var x = stack.pop();
                var y = stack.pop();
                console.log("var x/y: " + x + " " + y + " element: " + element) ;
                if (element == "+"){
                    result = (y+x);
                    console.log("Expected Result: " + result)
                    stack.push(y + x);
                } else if (element == '-'){
                    stack.push(y - x);
                } else if (element == '*'){
                    stack.push(y * x);
                } else if (element == '/'){
                    stack.push(y / x);
                }
            } else {
                stack.push( parseFloat(element) );
            }
        }
        //final check for non numbers within the stack
        var returnValue = null;
        while( !stack.isEmpty() ){
            stack.print();
            var element = stack.pop();  
            if(isNaN(element)){
                continue;
            } else{
                returnValue = element;
            }
        }
        return returnValue;
    }

我不确定我做错了什么?输出仅为 7.7。这是尝试调试的日志示例:

scripts.js:75 postFix: 7.7,7,+ 
scripts.js:145 undefined
scripts.js:175 element: 7.7
scripts.js:175 element: 7
scripts.js:175 element: + 
scripts.js:180 var x/y: 7 7.7 element: + 
scripts.js:76 result: null

感谢您的宝贵时间和提前帮助。

如果我用一个简单的数组替换您的 new Stack,并进行必要的调整以适应该更改,我会得到正确的答案...

function postfixEval( postfixArray ) {
        var stack = [];

        for( element of postfixArray){
            console.log("element: " + element);

            if(isNaN(element)){
                var x = stack.pop();
                var y = stack.pop();
                console.log("var x/y: " + x + " " + y + " element: " + element) ;
                if (element == "+"){
                    result = (y+x);
                    console.log("Expected Result: " + result)
                    stack.push(y + x);
                } else if (element == '-'){
                    stack.push(y - x);
                } else if (element == '*'){
                    stack.push(y * x);
                } else if (element == '/'){
                    stack.push(y / x);
                }
            } else {
                stack.push( parseFloat(element) );
            }
        }
        //final check for non numbers within the stack
        var returnValue = null;
        while( stack.length > 0 ){
            console.log( stack );
            var element = stack.pop();  
            if(isNaN(element)){
                continue;
            } else{
                returnValue = element;
            }
        }
        return returnValue;
    }
    
postfixEval(['7.7','7','+']);
    

这是我的方法:

const value = '2 3 * 1 - 5 /';

function postfixEval( expr ) {
    let stack = [];
    const arr = expr.split('').filter((el)=>el !== " " )
    console.log( arr );
    for( element of arr){

        console.log("element: " + element);

        if(isNaN(element)){
            var y = stack.pop();
            var x = stack.pop();
            const result = eval(x+element+y)
            stack.push(result)
            console.log( result );  
        } else {
            stack.push( parseFloat(element) );
        }
    }
        console.log( typeof stack[0] );
    return stack[0];
}

Lee SEE 我的方法

函数 evaluatePostfix(exp) {

    let stack=[];
    
    
    for(let i=0;i<exp.length;i++)
    {
        let c=exp[i];
        
        
        if(! isNaN( parseInt(c) ))
        stack.push(c.charCodeAt(0) - '0'.charCodeAt(0));
        
        
        else
        {
            let val1 = stack.pop();
            let val2 = stack.pop();
            
            switch(c)
            {
                case '+':
                stack.push(val2+val1);
                break;
                
                case '-':
                stack.push(val2- val1);
                break;
                
                case '/':
                stack.push(val2/val1);
                break;
                
                case '*':
                stack.push(val2*val1);
                break;
        }
        }
    }
    return stack.pop();

}

let exp="121*+2-"; document.write("后缀评估:"+evaluatePostfix(exp));