成功进化到程序猿快一年多了, 还没写过计算器, 正好今天比较闲,随手写了个计算器,最简单的实现,核心是eval()方法,把字符串作为JS代码处理,把输入的信息拼接成字符串,点等号执行代码得到结果,出异常弹窗提示

只用了几十行JS代码,感觉还可以精简 ,  我是一只追求简洁的程序猿

 @author beiguapipi 1//存储算式
 var all="";
 //添加字符串到式子
 function add(obj){
     if(document.getElementById("jieguo").innerHTML){
         window.location.reload();
     }else{
         all = all+obj.value;
         document.getElementById("jisuan").innerHTML=all;
     }
 }
 //删除一个最后的字符
 function del(){
     if(all.length>0){
         all= all.substr(0 ,all.length-1)
         document.getElementById("jisuan").innerHTML=all;
     }
 }
 //计算
 function jisuan(){
     try{
         //将字符串作为JS代码处理
         var num = eval(all);
         if(isNaN(num)){
             //如果出异常了,新建异常信息
             var e = new Error();
             e.message="输入式子不合法";
             throw e;
         }else{
             if(num=="Infinity"||num=="-Infinity"){
                 alert("运算中分母不能为零")
                 window.location.reload();
             }else{
                 document.getElementById("jieguo").innerHTML=num;
             }
         }
     }catch(e){
         //输出异常信息,刷新页面
         alert(e)
         window.location.reload();
     }
 }    
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>简单计算器</title>
<link type="text/css" rel="stylesheet" href="css.css">
<script type="text/javascript" src="control.js" charset="utf-8"></script>
</head>
<body>
<div id="body">
    <div id="title">
        <span id="sp">简单计算器</span>
    </div>
    <div id="area">
        <div id="top">
            <span id="jisuan"></span>
        </div>
        <div id="mid">
            计算结果:<span id="jieguo"></span>
        </div>
        <div id="button">
            <br>
            <input onclick="reflush()" id="clear"  class="but" type="button" value="清空">
            <input onclick="del()" id="del" class="but" type="button" value="退格">
            <input onclick="add(this)" class="but" type="button" value="/">
            <input onclick="add(this)" class="but" type="button" value="*"><br><br>
            <input onclick="add(this)" class="but" type="button" value="1">
            <input onclick="add(this)" class="but" type="button" value="2">
            <input onclick="add(this)" class="but" type="button" value="3">
            <input onclick="add(this)" class="but" type="button" value="-"><br><br>
            <input onclick="add(this)" class="but" type="button" value="4">
            <input onclick="add(this)" class="but" type="button" value="5">
            <input onclick="add(this)" class="but" type="button" value="6">
            <input onclick="add(this)" class="but" type="button" value="+"><br><br>
            <input onclick="add(this)" class="but" type="button" value="7">
            <input onclick="add(this)" class="but" type="button" value="8">
            <input onclick="add(this)" class="but" type="button" value="9"><br><br>
            <input onclick="add(this)" id="num_0" class="but" type="button" value="0">
            <input onclick="add(this)" class="but" type="button" value="."><br>
            <input onclick="jisuan()" id="num_deng" class="but" type="button" value="=">
            <input onclick="add(this)" class="but" type="button" value="(">
            <input onclick="add(this)" class="but" type="button" value=")">
        </div>
    </div>
</div>
</body>
</html>

CSS代码

@CHARSET "UTF-8";@CHARSET "UTF-8";
*{
    margin: 0px;
    padding: 0px;
}
#body{
    margin: auto;
    margin-top:10px;
    width: 450px;
    height: 600px;
    border: solid 5px rgb(241,241,241);
    background:rgb(255,255,225);
}
#title{
    width: 400px;
    height: 50px;
    position: relative;
    left:25%;
}

#sp{
    font-size: 1cm;
    font-style: oblique;
}
#area{
    width: 450px;
    height: 550px;
    border: solid 1px rgb(0,115,0);
    float: left;
}
#top{
    width: 400px;
    height: 35px;
    border: solid 3px rgb(200,225,225);
    float: left;
    position: relative;
    left:20px;
    top:15px;
    font-size: 25px;
    background-color: rgb(255,255,255)
}
#jisuan{
    float: right;
}
#mid{
    width: 400px;
    height: 35px;
    border: solid 3px rgb(200,225,225);
    float: left;
    position: relative;
    left:20px;
    top:30px;
    font-size: 25px;
    background-color: rgb(255,255,255)
}
#jieguo{
    width: 270px;
    height: 35px;
    border: solid 1px rgb(200,225,225);
    float: right;
}
#button{
    width: 400px;
    height: 400px;
    border: solid 3px rgb(200,225,225);
    float: left;
    position: relative;
    left:20px;
    top:50px;
}
.but{
    width: 90px;
    height: 45px;
    position: relative;
    left:10px;
}
#num_0{
    width: 187px;
}
#num_deng{
    height: 108px;
    position: relative;
    left:302px;
    top:-108px;
}
input{
    font-size: 25px;
}

JavaScript+HTML,简单的计算器实现的更多相关文章

  1. 用JavaScript制作简单的计算器

    <html > <head> <title>简单计算器</title> <style type="text/css"> ...

  2. jQuery/javascript实现简单网页计算器

    <html> <head> <meta charset="utf-8"> <title>jQuery实现</title> ...

  3. 利用css和javascript实现简单的计算器

    <!doctype html> <html> <head> <!--声明当前页面的编码集--> <meta http-equiv="Co ...

  4. JAVASCRIPT实现简单计算器

    最终效果如下图-2,有bug:就是整数后点击%号结果正确,如果小数后面点击%的话结果就错误!其他都正常,求指点:input的value是string类型的,在JS中改如何正确处理下图-1中的if部分? ...

  5. javascript 简单的计算器

    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx. ...

  6. js制作简单的计算器

    学着做了一个简单的计算器!记录记录!哈哈 <!DOCTYPE html> <html> <head> <title>简单的计算器</title&g ...

  7. JS实现一个简单的计算器

    使用JS完成一个简单的计算器功能.实现2个输入框中输入整数后,点击第三个输入框能给出2个整数的加减乘除.效果如上: 第一步: 创建构建运算函数count(). 第二步: 获取两个输入框中的值和获取选择 ...

  8. Java简单公式计算器

    最近给公司开发业务代码时,碰到一个场景,简单描述是这样的: 客户要向咱们公司定制一件产品,这个产品呢,有很多属性,那公司得根据这些属性报价呀,怎么报价呢?公司针对某种类型的产品有一个基准价,在同类产品 ...

  9. JS事件 编程练习-自制计算器 使用JS完成一个简单的计算器功能。实现2个输入框中输入整数后,点击第三个输入框能给出2个整数的加减乘除。

    编程练习 使用JS完成一个简单的计算器功能.实现2个输入框中输入整数后,点击第三个输入框能给出2个整数的加减乘除. 提示:获取元素的值设置和获取方法为:例:赋值:document.getElement ...

随机推荐

  1. C# MD5加密

    public static string Encrypt(string txt) { System.Security.Cryptography.MD5CryptoServiceProvider md5 ...

  2. C#枚举中的位运算权限分配浅谈

    常用的位运算主要有与(&), 或(|)和非(~), 比如: 1 & 0 = 0, 1 | 0 = 1, ~1 = 0 在设计权限时, 我们可以把权限管理操作转换为C#位运算来处理. 第 ...

  3. SSH项目(1)

    1.新建项目,添加jar包 tomcat jsp struts.hibernate.spring 2.配置 web.xml <?xml version="1.0" encod ...

  4. 写入标题使用依赖注入Title的setTitle方法

    1. 声明 Generator的声明方式类似一般的函数声明,只是多了个*号,并且一般可以在函数内看到yield关键字 function* showWords() { yield 'one'; yiel ...

  5. #Deep Learning回顾#之LeNet、AlexNet、GoogLeNet、VGG、ResNet

    CNN的发展史 上一篇回顾讲的是2006年Hinton他们的Science Paper,当时提到,2006年虽然Deep Learning的概念被提出来了,但是学术界的大家还是表示不服.当时有流传的段 ...

  6. Nginx配置单主机多域名

    http { include mime.types; default_type application/octet-stream; sendfile on; keepalive_timeout 65; ...

  7. 一个自定义控件的Demo

    里面包括Button.Checkbock.listview.popupwindow的自定义 import android.app.Activity; import android.content.In ...

  8. jquery.get()

    1.获取当前jquery对象匹配到的dom元素 2.语法: jqueryObject.get([index]) //jQueryObject[index]等价于jQueryObject.get(ind ...

  9. 查询SQLSERVER执行过的SQL记录

    SELECT TOP 1000 --创建时间 QS.creation_time, --查询语句 SUBSTRING(ST.text,(QS.statement_start_offset/2)+1, ( ...

  10. Enable rsh on MAC OS with command line

    1. Enable rsh on macos. 1). os version (10.0) Enabling the "Allow remote login" option tur ...