gpt4 book ai didi

javascript - Javascript 如何评估右括号?

转载 作者:行者123 更新时间:2023-11-28 20:02:41 25 4
gpt4 key购买 nike

我正在开发一个计算器,它采用诸如 (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;
}
}
};

最佳答案

我刚刚使用您的代码尝试了这一点,并且将值作为字符串传递得很好:

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);
}
};

在最后一种情况下,您不会打印出数组(这是右括号所在的位置),所以您确定它不在数组上,并且您只是看不到视觉反馈吗?

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

关于javascript - Javascript 如何评估右括号?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21396484/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com