gpt4 book ai didi

javascript - Chrome 应用程序 eval()

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

我正在尝试为 Chrome 创建一个简单的计算器。我需要使用 eval() 来计算表达式,但它被 chrome 禁止。有什么解决方法吗?这里是js。谁能给出解决方案吗?我到处都找过了。

var text = document.getElementById("tBox");
var x = 0;
var y = 0;
var op = "";
var resetFlag = 0;

document.getElementById("buttonbackspace").addEventListener("click",function(){backspace()});

document.getElementById("buttonone").addEventListener("click",function(){addDigit(1)});

document.getElementById("buttontwo").addEventListener("click",function(){addDigit(2)});

document.getElementById("buttonthree").addEventListener("click",function(){addDigit(3)});

document.getElementById("buttonfour").addEventListener("click",function(){addDigit(4)});

document.getElementById("buttonfive").addEventListener("click",function(){addDigit(5)});

document.getElementById("buttonsix").addEventListener("click",function(){addDigit(6)});

document.getElementById("buttonseven").addEventListener("click",function(){addDigit(7)});

document.getElementById("buttoneight").addEventListener("click",function(){addDigit(8)});

document.getElementById("buttonnine").addEventListener("click",function(){addDigit(9)});

document.getElementById("buttonzero").addEventListener("click",function(){addDigit(0)});

document.getElementById("buttondecimal").addEventListener("click",function(){addDigit(".")});

document.getElementById("buttonaddition").addEventListener("click",function(){add()});

document.getElementById("buttonsubtraction").addEventListener("click",function(){subtract()});

document.getElementById("buttonmultiplication").addEventListener("click",function(){multiply()});

document.getElementById("buttondivision").addEventListener("click",function(){divide()});

var backspace = function() {
tBox.value = tBox.value.slice(0, -1);
};
var addDigit = function(x) {
tBox.value = tBox.value + x;
}
var add = function() {
if (x === 0) {
x = tBox.value;
tBox.value = "";
op = "+";
} else if (y === 0) {
y = tBox.value;
equals();
}
};
var subtract = function() {
if (x === 0) {
x = tBox.value;
tBox.value = "";
op = "-";
} else if (y === 0) {
y = tBox.value;
equals();
}
};
var multiply = function() {
if (x === 0) {
x = tBox.value;
tBox.value = "";
op = "*";
} else if (y === 0) {
y = tBox.value;
equals();
}
};
var divide = function() {
if (x === 0) {
x = tBox.value;
tBox.value = "";
op = "/";
} else if (y === 0) {
y = tBox.value;
equals();
}
};
var equals = function() {
y = tBox.value;
tBox.value = eval(x + op + y); //Eval over here
x = 0;
y = 0;
resetFlag = 1;
};

最佳答案

我无事可做……好吧,我给你做了一个函数来评估你的数学:

var eval = function( maths ){
// Lets start

var math_object = [];
var number = 0;
var pattern;

while( maths.length>0 ){
// get front number
number = parseFloat( maths );
// create regexp pattern
pattern = new RegExp('^'+((number>=0)?'\\+*':'')+number.toString(), 'i');
// check for error
if( isNaN(number) || !maths.match(pattern) ) return "Expression Error";
// cut number from string
maths = maths.replace(pattern,'');
// push number into array
math_object.push(number);
// Check if next has action
if( maths.length==0 ) break;
// get math action
if( !maths[0].match(/[\/\*+-]/i) ) return "Expression Error";
// save acton
if( maths[0].match(/[+-]/i) ){
math_object.push('+');
}else{
math_object.push(maths[0]);
maths = maths.substr(1);
}
}

if( math_object.length%2==0 ) return "Expression Error";
if( math_object.length==1 ) return math_object[0];

//console.log(math_object);

var math_object_structed = [];

var i = 0;
while( i<math_object.length ){
if( i+1==math_object.length ){
math_object_structed.push(math_object[i]);
break;
}
if( math_object[i+1]=='+' ){
math_object_structed.push(math_object[i]);
math_object_structed.push('+');
i+=2;
}else{
var array = [
math_object[i],
math_object[i+1],
math_object[i+2]
];
math_object.splice(i, 2);
math_object[i] = array;
}
}

//console.log(math_object_structed);

var doMath = function( math_array ){
while( math_array.length>1 ){
var first = math_array.shift();
if( typeof first != "number" )
first = doMath( first );

var action = math_array.shift();

var second = math_array.shift();
if( typeof second != "number" )
second = doMath( second );

var result;
if( action=='+' )
result = first + second;
else if( action=='*' )
result = first * second;
else if( action=='/' )
result = first / second;
else
return "Expression Error";

math_array.unshift( result );
}

if( typeof math_array[0] != "number" )
return doMath( math_array[0] );
else
return math_array[0];
}

return doMath( math_object_structed );
}

现在 1+2+3/5+14*3*4*5+16 的结果是 859.6我的函数 eval("1+2+3/5+14*3*4*5+16") 的结果是 859.6

祝你编码愉快。

关于javascript - Chrome 应用程序 eval(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25196121/

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