基于javascript实现简单计算器功能

 更新时间:2016年01月03日 11:07:34   投稿:lijiao  
这篇文章主要介绍了基于javascript实现简单计算器功能的相关资料,需要的朋友可以参考下

本文实例为大家介绍javascript实现简单计算器功能的详细代码,分享给大家供大家参考,具体内容如下

效果图:

实现代码:

<html> 
  <head> 
    <script> 
      function calc(event){ 
        // test  
        //window.alert(event.value); 
        var val = new String(event.value); 
        // clear space 
        val = val.trim(); 
        var res = document.getElementById("res"); 
        // clear 
        if(val == "clear"){ 
          res.value = ""; 
        } 
 
        // back 
        if(val == "back"){ 
          res.value = res.value.substring(0, res.value.length - 1); 
        } 
 
        // power 
        if(val == "power"){ 
          val = "p"; 
        } 
        // add val to text 
        if(val.length == 1 && val != "="){ 
          res.value = res.value + val; 
        } 
  
        // calc result 
        if(val == "="){ 
          var arr; 
          var result; 
          // power 
          if(res.value.indexOf("p") != -1){ 
            arr = res.value.split("p"); 
            //window.alert(arr); 
             result = Math.pow(parseFloat(arr[0]) ,parseFloat(arr[1])); 
            //window.alert(res); 
            res.value = result; 
          }       
          // plus 
          if(res.value.indexOf("+") != -1){ 
            arr = res.value.split("+"); 
            //window.alert(arr); 
             result = parseFloat(arr[0]) + parseFloat(arr[1]); 
            //window.alert(res); 
            res.value = result; 
          } else if(res.value.indexOf("-") != -1){ 
            // minus 
            arr = res.value.split("-"); 
            //window.alert(arr); 
            result = parseFloat(arr[0]) - parseFloat(arr[1]); 
            //window.alert(res); 
            res.value = result; 
          } else if(res.value.indexOf("*") != -1){ 
            // multiply 
            arr = res.value.split("*"); 
            //window.alert(arr); 
            result = parseFloat(arr[0]) * parseFloat(arr[1]); 
            //window.alert(res); 
            res.value = result; 
          } else if(res.value.indexOf("/") != -1){ 
            // division 
            arr = res.value.split("/"); 
            //window.alert(arr); 
            result = parseFloat(arr[0]) / parseFloat(arr[1]); 
            //window.alert(res); 
            res.value = result; 
          } else if(res.value.indexOf("%") != -1){ 
            // module 
            arr = res.value.split("%"); 
            //window.alert(arr); 
            result = parseFloat(arr[0]) % parseFloat(arr[1]); 
            //window.alert(res); 
            res.value = result; 
          } 
        }   
      } 
    </script> 
  </head> 
  <body> 
    <table border="1px" cellpadding="10px" cellspacing="5px" align="center"> 
      <tr align="center"> 
        <td colspan="4"><input type="text" id="res" size="35px" value="" style="text-align:right;"/></td> 
      </tr> 
      <tr align="center"> 
        <td><input type="button" value="power" onclick="calc(this)"/></td> 
        <td><input type="button" value="clear" onclick="calc(this)"/></td> 
        <td colspan="2"><input type="button" value="   back   " onclick="calc(this)"/></td> 
      </tr> 
      <tr align="center"> 
        <td><input type="button" value="  1  " onclick="calc(this)"/></td> 
        <td><input type="button" value="  2  " onclick="calc(this)"/></td> 
        <td><input type="button" value="  3  " onclick="calc(this)"/></td> 
        <td><input type="button" value="  +  " onclick="calc(this)"/></td> 
      </tr> 
      <tr align="center"> 
        <td><input type="button" value="  4  " onclick="calc(this)"/></td> 
        <td><input type="button" value="  5  " onclick="calc(this)"/></td> 
        <td><input type="button" value="  6  " onclick="calc(this)"/></td> 
        <td><input type="button" value="  -  " onclick="calc(this)"/></td> 
      </tr> 
      <tr align="center"> 
        <td><input type="button" value="  7  " onclick="calc(this)"/></td> 
        <td><input type="button" value="  8  " onclick="calc(this)"/></td> 
        <td><input type="button" value="  9  " onclick="calc(this)"/></td> 
        <td><input type="button" value="  *  " onclick="calc(this)"/></td> 
      </tr> 
      <tr align="center"> 
        <td><input type="button" value="  0  " onclick="calc(this)"/></td> 
        <td><input type="button" value="  =  " onclick="calc(this)"/></td> 
        <td><input type="button" value="  %  " onclick="calc(this)"/></td> 
        <td><input type="button" value="  /  " onclick="calc(this)"/></td> 
      </tr> 
    </table> 
  </body> 
</html> 

希望本文所述对大家学习javascript程序设计有所帮助。

相关文章

  • JS数组array元素的添加和删除方法代码实例

    JS数组array元素的添加和删除方法代码实例

    这篇文章主要介绍了JS数组array元素的添加和删除方法代码实例,本文直接给出操作代码实例,需要的朋友可以参考下
    2015-06-06
  • JS 面向对象之神奇的prototype

    JS 面向对象之神奇的prototype

    对于初学 JavaScript 的人来说 prototype 是一种很神奇的特性,而事实上,prototype 对于 JavaScript 的意义重大,prototype 不仅仅是一种管理对象继承的机制,更是一种出色的设计思想。
    2011-02-02
  • 详解Webpack实战之构建 Electron 应用

    详解Webpack实战之构建 Electron 应用

    本篇文章主要介绍了Webpack实战之构建 Electron 应用,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-12-12
  • JavaScript简单实现鼠标移动切换图片的方法

    JavaScript简单实现鼠标移动切换图片的方法

    这篇文章主要介绍了JavaScript简单实现鼠标移动切换图片的方法,涉及JavaScript针对鼠标事件的响应及页面元素的动态变换技巧,需要的朋友可以参考下
    2016-02-02
  • angular之ng-template模板加载

    angular之ng-template模板加载

    本篇文章主要介绍了angular之ng-template模板加载,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-11-11
  • IScroll5实现下拉刷新上拉加载的功能实例

    IScroll5实现下拉刷新上拉加载的功能实例

    本篇文章主要介绍了IScroll5实现下拉刷新上拉加载的功能实例,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-08-08
  • js实现无需数据库的县级以上联动行政区域下拉控件

    js实现无需数据库的县级以上联动行政区域下拉控件

    县级以上联动行政区域下拉控件,想必大家对此也有所熟悉,本文为大家介绍下使用js实现无需数据库的联动下拉控件,感兴趣的朋友可以参考下,希望对大家有所帮助
    2013-08-08
  • JavaScript文档碎片操作实例分析

    JavaScript文档碎片操作实例分析

    这篇文章主要介绍了JavaScript文档碎片操作技巧,结合实例形式分析了JavaScript中使用文档碎片优化dom操作的技巧,需要的朋友可以参考下
    2015-12-12
  • 详解uniapp无痛刷新token方法

    详解uniapp无痛刷新token方法

    为了给用户一个流畅的体验,token过期后需要重新请求新的token替换过期的token。本文将详细介绍uniapp无痛刷新token方法。
    2021-06-06
  • JS判断用户用的哪个浏览器实例详解

    JS判断用户用的哪个浏览器实例详解

    这篇文章主要介绍了JS判断用户用的哪个浏览器的实例代码,非常不错,具有一定的参考借鉴价值,需要的朋友可以参考下
    2018-10-10

最新评论