Javascript如何评估正确的括号?

时间:2014-01-28 03:52:24

标签: javascript jquery

我正在研究一个带有(5+4)等表达式的计算器,并通过将按下的按钮传递给数组来计算它,然后根据数组中的数据构建一个解析树。

有趣/奇怪的是,我的代码不会将右括号的值推送到数组。这是我的代码,有人可以帮助我吗?

console.log activeButton显示这是被按下的按钮的值,但即使我将calcArray.push()放在if语句之外它也不会推送到数组。

$(document).ready(function(){
        var calcArray = new Array();

        $("input").click(function(){
            var activeButton = this.value;
            console.log(activeButton);
            if(!isNaN(activeButton))
            {
                calcArray.push(parseInt(activeButton));
                console.log(calcArray);
            }
            else if(activeButton === "=")
            {
                evaluate(buildTree(calcArray));
                calcArray = [];
            }
            else
            {
                calcArray.push(activeButton);
            }
        });
    });

BuildTree代码:

function BinaryTree(root) {
    this.root = root;
    this.activeNode = root;
}

function Node(element){
    this.element = element;
    this.parent;
    this.rightChild;
    this.leftChild;

    this.setLeft = function(node){
        this.leftChild = node;
        node.parent = this;
    };

    this.setRight = function(node){
        this.rightChild = node;
        node.parent = this;
    };
}


//methods

var buildTree = function(array)
{
    var tree = new BinaryTree(new Node(null));
    for(var i = 0; i < array.length; i++)
    {
        var newNode = new Node(array[i]);
        if(array[i] == "(")
        {
            newNode.element = null;
            tree.activeNode.setLeft(newNode);
            tree.activeNode = newNode;
        }
        else if(array[i] == "+" || array[i] == "-" || array[i] == "/" || array[i] == "*")
        {
            tree.activeNode.element = newNode.element;
            tree.activeNode.setRight(new Node(null));
            tree.activeNode = tree.activeNode.rightChild;
        }
        else if(array[i] == ")")
        {
            if(tree.activeNode.parent == null)
            {
            ;
            }
            else
            {
                tree.activeNode = tree.activeNode.parent;
                tree.root = tree.activeNode;
            }
        }
        else
        {
            tree.activeNode.element = newNode.element;
            tree.activeNode = tree.activeNode.parent;
        }
    }
    return tree.activeNode;
}

var evaluate = function(node){
    var newNode1, newNode2;
    newNode1 = new Node(null);
    newNode1.parent = node;
    newNode2 = new Node(null);
    newNode2.parent = node;
    if(node.leftChild == null && node.rightChild == null)
        return node.element;
    else{
        newNode1.element = evaluate(node.leftChild);
        newNode2.element = evaluate(node.rightChild);
        if(newNode1.parent.element == "+")
        {
            return Number(newNode1.element) + Number(newNode2.element);
        }
        if(newNode1.parent.element == "-")
        {
            return newNode1.element - newNode2.element;
        }
        if(newNode1.parent.element == "*")
        {
            return newNode1.element * newNode2.element;
        }
        else
        {
            return newNode1.element / newNode2.element;
        }
    }
};

1 个答案:

答案 0 :(得分:3)

我只是尝试使用你的代码,并且它可以很好地将值作为字符串传递:

function pushButton (value) {
    var activeButton = value;
    console.log(activeButton);
    if(!isNaN(activeButton))
    {
        calcArray.push(parseInt(activeButton));
        console.log(calcArray);
    }
    else if(activeButton === "=")
    {
        evaluate(buildTree(calcArray));
        calcArray = [];
    }
    else
    {
        calcArray.push(activeButton);
    }
};

你不会在最后一种情况下打印出阵列(这是正确的paren会去的地方),所以你确定它不在数组上而你只是没有看到视觉反馈吗?

如果是这样,我们需要查看更多代码。尝试并设置一个jsfiddle。