javascript - 计算器 : String Into Math Operation (JavaScript)

标签 javascript html

如何将字符串转换为数学运算?我不能使用评估。有什么建议么? 这是我到目前为止所拥有的。我将字符串拆分为一个数组,但如何将“=”、“-”等转换为实际的数学运算。另外,我知道我可以使用 parseInt 将“数字”转换为实际数字,但在代码中看起来如何?不确定如何开始。

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
  "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="Content-Type" content="application/xhtml+xml; charset=utf-8"/>
    <title>JavaScript HW3</title>
    <script type="text/javascript">
      function display(num) {
        var node = document.getElementById("display");  
        node.value+=num;    
      }

      function result(){
        document.getElementById("display").value = (document.getElementById("display").value).split("");
      }
    </script>
  </head>
  <body> 
    <h1>Simple calculator</h1>


    <form action="" method="get">
      <p>
        <input type="text" id="display"/>       
      </p>
    </form>

    <form action=""  method="get">
      <div class="button">
        <input type="button" class="btn" name="b7" value="7" onclick='display("7")'/>
        <input type="button" class="btn" name="b8" value="8" onclick='display("8")'/>
        <input type="button" class="btn" name="b9" value="9" onclick='display("9")'/>
        <input type="button" class="btn" name="db" value="/" onclick='display("/")'/>
        <br/>
        <input type="button" class="btn" name="b4" value="6" onclick='display("6")'/>
        <input type="button" class="btn" name="b5" value="5" onclick='display("5")'/>
        <input type="button" class="btn" name="b6" value="4" onclick='display("4")'/>
        <input type="button" class="btn" name="mb" value="*" onclick='display("*")'/>
        <br/>    
        <input type="button" class="btn" name="b1" value="3" onclick='display("3")'/>
        <input type="button" class="btn" name="b2" value="2" onclick='display("2")'/>
        <input type="button" class="btn" name="b3" value="1" onclick='display("1")'/>
        <input type="button" class="btn" name="mib" value="-" onclick='display("-")'/>
        <br></br>

        <input type="button" class="btn" name="db" value="." onclick='display(".")'/>
        <input type="button" class="btn" name="b0" value="0" onclick='display("0")'/>
        <input type="button" class="btn" name="eb" value="=" onclick='result("")'/>
        <input type="button" class="btn" name="ab" value="+" onclick='display("+")'/>
        <br/>
      </div>
    </form>

    <!-- W3C XHTML Validation logo-->
    <p>
      <a href="http://validator.w3.org/check?uri=referer"><img
        src="http://www.w3.org/Icons/valid-xhtml11"
        alt="Valid XHTML 1.1" height="31" width="88" /></a>
    </p>
  </body>
</html>

最佳答案

我不想写完整的东西,但这是您可以的一个方向。您可以将字符串转换为以字符串为键、以函数为值的对象的操作。考虑一个看起来像这样的对象:

var operations = {
    current: 0,
    '=': function(){
        return this.current
    },
    '+': function(n){
        this.current += Number(n)
        return this
    }
}
operations['+'](100)
operations['+'](50)
var total = operations['=']()
console.log("total: ", total )

这可能不是整个项目的最佳方式,但可能会给你一个想法,不会导致 if/elseswitch 的困惑声明。

关于javascript - 计算器 : String Into Math Operation (JavaScript),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51371546/

相关文章:

javascript - 遍历嵌套对象/数组

JavaScript 函数级作用域

javascript - 如何在不知道数据的情况下渲染动态表格?使用 HTML 和 JS

html - bootstrap 4中的全宽div

Python BeautifulSoup 选择 div 类

javascript - 从 vb.net 关闭脚本弹出窗口

javascript - 未捕获的语法错误 : Unexpected token &

html - 如何使 svg 图像适合父容器

html - 在移动设备上将按钮更改为卡片

javascript - 注销/删除dojo中div标签的所有子节点