如何获取number2中number1中的数字和symbol1 symbol2中的符号,如何设置条件,例如第一个输入不应该是运算符,或者如果多个运算符返回错误?我是第一次发布问题,因此,如果看起来不太干净,请原谅。
这是我的js: 这是我指定的onclicks
function clearCalculator()
{
document.querySelector("#result").value = '';
}
function printInteger(inputFromCalculator)
{
document.querySelector("#result").value += inputFromCalculator;
}
function printSymbol(inputFromCalculator)
{
document.querySelector("#result").value += inputFromCalculator;
}
function printFinalOutput()
{
var expression = document.querySelector("#result").value;
expression+="=";
//expression 22+32+22=
if(sanityCheck(expression)==true)
{
divideAndConquer(expression);
}
}
在这里,我尝试将整数放在var number1和number2中,将运算符放在var symbol1和symbol2中:
function divideAndConquer(expression)
{
var number1;
var number2;
var symbol1;
var symbol2;
var miniExpressionLength;
var partialOutput;
for(var i=0;i<expression.length;i++)
{
if(isNumber(expression[i])==true && number1==null)
{
number1=expression[i];
//logs number1 as single string
}
else
{
if(isNumber(expression[i])==true && number1!=null && symbol1==null)
{
number1 = number1 + expression[i];
}
}
if(symbol1!=null && number1!=null && isNumber(expression[i])==true && number2==null)
{
number2 = expression[i];
}
else
{
if(symbol1!=null && number1!=null && isNumber(expression[i])==true && number2!=null && symbol2==null)
{
number2 = number2 + expression[i];
}
}
if(isSymbol(expression[i])==true && symbol1==null)
{
symbol1=expression[i];
}
else
{
if(isSymbol(expression[i])==true && symbol1!=null)
{
symbol2=expression[i];
}
}if(symbol2!=null)
{
miniExpressionLength = i;
//console.log("length of mini expression ="+i)
break;
}
}
partialOutput = calculate(number1,number2,symbol1);
expression = partialOutput+""+expression.substring(miniExpressionLength,expression.length);
if(symbol1!=null)
{
divideAndConquer(expression);
}
else
{
console.log("expression ="+expression);
return expression;
}
}
在这里我指定了一些情况:
function sanityCheck(expression)
{
//case example +2+2
if(isNumber(expression[0])==false)
{
alert("invalid expression");
return false;
}
for(var i=0;i<expression.length;i++)
{
//case example 2++2
if(isSymbol(expression[i])==true && isSymbol(expression[i+1])==true)
{
alert("invalid expression");
return false;
}
}
return true;
}
答案 0 :(得分:1)
解析数学表达式不是一件容易的事,并且超出了答案的范围。有几个提供解析的库。 如果您出于学习目的而进行此操作,则应该看看:
例如,以下是一篇不错的博客文章,可帮助您入门: https://www.freecodecamp.org/news/parsing-math-expressions-with-javascript-7e8f5572276e/
答案 1 :(得分:0)
首先,请注意,在“ if语句”中,如果结果已经是布尔值,则不必使用“ ==”
Option Explicit
Function OnlineCurrency(current_country As String, to_country As String) As String
Dim HTTP As MSXML2.XMLHTTP60
Dim URL As String
Dim HTMLDoc As New HTMLDocument
URL = "http://www.google.com/search?q=HKD+to+USD"
Set HTTP = New MSXML2.XMLHTTP60
HTTP.Open "GET", URL, False
HTTP.send
Set HTMLDoc = New HTMLDocument
With HTMLDoc
.body.innerHTML = HTTP.responseText
OnlineCurrency = .getElementByClassName("dDoNo vk_bk").innerText
End With
End Function
您可以直接使用表达式:
if(isNumber(expression[0]) == false) {
请注意,句子开头的“!”(非门), 如果结果已经为假,则将通过。
我强烈建议您使用ES6阵列功能。 https://medium.com/poka-techblog/simplify-your-javascript-use-map-reduce-and-filter-bd02c593cc2d
sanityCheck条件:
因此您可以使用如下正则表达式:
if(!isNumber(expression[0])) {
(在这里https://regex101.com/自己测试)
注意:这仅适用于整数。
^\d+([+\-\/\*]\d+)+$
您可以对divideAndConquer函数执行以下操作:
function sanityCheck(expression)
{
const sanityExpresion = /^\d+([+\-\/\*]\d+)+$/
return sanityExpresion.test(expression);
}
如果仅使用评估,则相同:
function divideAndConquer(expresion) {
const symbolSeparation = /[+\-\/\*]/
const numberSeparation = /\d/
const numbers = expresion.split(symbolSeparation);
// filter where symbol !== “”
const symbols = expresion.split(numberSeparation).filter(s => s);
// So I get the first number and removed from the array
let result = numbers.shift();
// Here I use an array function
symbols.forEach(operator => {
const topNumber = numbers.shift();
result = eval(result + operator + topNumber);
})
return result;
}
但是我想您想在代码中多做一些练习,祝您好运。 XD
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/eval https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/shift https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/split https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions