如何计算数字和数学运算符的数组(或字符串) [英] How do I compute an array (or string) of numbers and mathematical operators

查看:73
本文介绍了如何计算数字和数学运算符的数组(或字符串)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作一个计算器,并推送/保存所有数字,并且运算符单击了一个数组和一个字符串.

I am making a calculator and pushing/saving all the numbers and operators clicked to an array and a string.

我想知道哪种方法 在这种情况下是最好的.从输入 OR 中创建字符串或数组是我想不到的更好的方法.

I want to know which approach is the best in this case. Making a string or an array from the input OR a better approach I can't think of.

我想计算数组或字符串.该字符串给出了错误的答案,我对如何计算数组一无所知.演示计算器在下面.

I want to compute the array or string. The string is giving wrong answers and I don't have a clue on how I can compute the array. The demo calculator is below.

$(document).ready(function(){
  var inputArr = [];
  var inputStr = '';
  
  $('span').click(function(){
  $('#input').append($(this).text());
  //push to inputArr
   inputArr.push($(this).text());
  //add to inputStr
   inputStr += $(this).text();
  
  });
  
  //Get result
  $('.equals').click(function(){
    $('#result').text(inputArr);
    $('#result').append("<br/>" + parseInt(inputStr));
  });
  
  $('.clear').click(function(){
   // clear everything
  $('#input').text('');
  inputArr = [];
  inputStr = '';
  
  });
});

span {
  background: #bbb;
  border-radius: 2px;
  margin: 5px;
  padding: .5em;
  cursor: pointer;
}
.equals{
  width: 30%;
  background: #bbb;
  border-radius: 2px;
  margin: 15px;
  padding: .5em;
  cursor: pointer;
  text-align: center;
  font-size: 1.5em;
}

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<span>1</span>  <span>2</span>  <span>3</span>  <span>4</span>  <span>5</span> 
<br/>
<br/>

<span>6</span>  <span>7</span>  <span>8</span>  <span>9</span>  <span>0</span> 
<br/><br/>
   

<span>+</span><span>-</span><span>*</span><span>/</span><span class='clear'>clear</span>

<p class="equals"> = </p>

<p id="input"></p>

<p id='result'></p>

我尝试使用parseInt(inputStr),但是给出了错误的答案.

I tried using parseInt(inputStr) but it gives wrong answers.

推荐答案

此函数采用字符串表达式或创建表达式的字符数组.

This function takes either a string expression, or an array of characters that create an expression.

function calculate(expression) {
  "strict mode";
  if (Array.isArray(expression))
    expression = expression.join("");
  var allowedChars = "1234567890%^*()-+/. ";
  for (var i = 0; i < expression.length; i++)
    if (allowedChars.indexOf(expression.charAt(i)) < 0)
      throw new Error("Invalid expression: unexpected '" + expression.charAt(i) + "' in '" + expression + "'");
  return eval(expression);
}

try {
  alert(calculate("4+4"));
  alert(calculate([7, "+", 9, "/", 34]));
  alert(calculate("45/3"));
  alert(calculate("100 * cheeseburger"));
} catch (e) {
  alert(e.message);
}

这就是为什么这样安全的原因:

Here's why this is safe:

  1. 不正确地使用eval会打开注入攻击的代码
    • strict mode eval无法将新变量引入周围的范围. (由于它是其自身功能的末尾,因此在该范围内什么也没发生.)
    • 如果存在不属于数学表达式的任何字符,则该函数将引发错误,从而无法进行注入.
  1. Improper use of eval opens up your code for injection attacks
    • In strict mode eval is not able to introduce new variables to the surrounding scope. (As it's at the end of it's own function, nothing else is happening in that scope anyway.)
    • The function will throw an error if any character is present which is not part of a mathematical expression, making injection impossible.
  • 在这种情况下,该函数将引发错误,其中包括行号. eval的代码只有一行,所以这无关紧要.
  • In this case the function throws an Error, which includes a line number. The eval'd code is only so a single line, so this is not relevant.
  • 在这种情况下,没有什么要编译/缓存的,所以这不是问题.

这篇关于如何计算数字和数学运算符的数组(或字符串)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆