简单的计算器制作

制作简单计算器

刚入门用js实现了一个小小的计算器跟大家分享一下

  • HTML文件
  • CSS文件
  • JS文件

整体效果展示
整体效果展示

HTML文件

<!DOCTYPE html>
<html lang = "en" >
    <head>
        <meta charset = "UTF-8">
        <title> Simple Calculator </title>
        <link rel = "stylesheet" href = "./cal.css">
        <script type="text/javascript" src = "./cal.js"></script>
    </head>
    <body>
        <div id = "the_title">Simple Calculator </div> 
        <div id = "calculator_container">
            <div id = "calculator">
                <input id = "result">
                <div id = "first_row">
                    <button id = "seven" class = "button" >7</button>
                    <button id = "eight" class = "button" >8</button>
                    <button id = "nine" class = "button" >9</button> 
                    <button id = "divide" class = "button" >/</button> 
                </div>
                <div id = "second_row">
                    <button id = "four" class = "button" >4</button>
                    <button id = "five" class = "button" >5</button>
                    <button id = "six" class = "button" >6</button>
                    <button id = "multiply" class = "button" >*</button>
                </div>
                <div id = "third_row">
                    <button id = "one" class = "button" >1</button>
                    <button id = "two" class = "button" >2</button>
                    <button id = "three" class = "button" >3</button>
                    <button id = "substract" class = "button" >-</button>
                </div>
                <div id = "fourth_row">
                    <button id = "zero" class = "button">0</button>
                    <button id = "point" class = "button" >.</button>
                    <button id = "delete_one" class = "button" >←</button>
                    <button id = "plus" class = "button" >+</button>
                </div>
                <div id = "fifth_row">
                    <button id = "left_bracket" class = "button" >(</button>
                    <button id = "right_bracket" class = "button" >)</button>
                    <button id = "delete_all" class = "button" >CE</button>
                    <button id = "equal" class = "button" >=</button>
                </div>
            </div>
        </div>
    </body>
</html>                

CSS文件

body
{
    background-color: pink ; 
    width: 100% ;
    height: 100% ;
    margin: 0px;
    padding: 0px;
    overflow : hidden;
    background:url(https://timgsa.baidu.com/timg?image&quality=80&size=b10000_10000&sec=1508567142&di=f077307d708e1eb41bf7524fc3c1442c&src=http%3A%2F%2Fpic1.16pic.com%2F00%2F14%2F57%2F16pic_1457961_b.jpg);
    background-size: 100% ,100% ;
    position: absolute; 
    top : 50px ;

}

#the_title
{
    position: absolute ;
    top : 50px ;
    left : calc(50% - 120px);
    font-size:  20px ;
    font-weight: bold ;
    color : white;

}

#the_photo
{
    background-size:  100% , 100% ;
}

#calculator
{
    border-radius: 15px ;
    border:  5px solid white ;
    position: absolute; 
    top : 100px;
    left: calc( 50% - 200px) ;
    width: 350px ;
    height: 471px ;
    background-color: #FFCCCC;
}

#claculator_container
{
    position: relative ;
    border: 1px solid black ;
    width: 1000px ;
    height: 100% ;
    margin-left: auto ;
    margin-right: auto ;
}

#result
{
    width: 295px ;
    height: 45px ;
    margin-left: 24px ;
    margin-top: 12px ;
    margin-bottom: 12px ; 
    text-align: center ;

}

#result
{
    background-color: #9999CC ;
    border : 3px solid black ;
    color : #80CAAC;
    font-size: 30px ;
}

.button 
{
    width: 70px;
    height: 70px;
    font-size: 20px;
    color: white;
    margin-left:3px;
    margin-top: 5px;
    background-color: #FFCC99;
    opacity: 0.8;
}

.button:hover
{
    background-color : #FF99FF;
}


#first_row,
#second_row,
#third_row,
#fourth_row,
#fifth_row
{
    margin-left: 20px;
}

#equal 
{
    background-color:#FFCC33 ;
}

#delete_all,#delete_one 
{
    background-color:#FFCC33 ;
}

#equal:hover 
{
    background-color:#FF99FF ;
}

#delete_all:hover,#delete_one:hover 
{
    background-color:#FF99FF ;
}

#plus,#substract,#multiply,#divide 
{
    background-color: #FF66CC ;
}

#plus:hover,#substract:hover,#multiply:hover,#divide:hover 
{
    background-color: #FF99FF ;
}

JS文件

//最蠢的eval实现方法但是搞定了0.1+0.2=0.30000000000004的问题
//看了网上的博客然后解决问题可以说的上是客观一个小进步


var display = ""; 
var check = 0;


window.onload = function() 
{
    document.getElementById("zero").onclick=function() 
    {
        if (check == 1) 
        {
            display = "";
            document.getElementById("result").value = display;
            check = 0;
        }
        display += "0";
        document.getElementById("result").value = display;
    }

    document.getElementById("one").onclick=function() 
    {
        if (check == 1) 
        {
            display = "";
            document.getElementById("result").value = display;
            check = 0;
        }
        display += "1";
        document.getElementById("result").value = display;
    }

    document.getElementById("two").onclick=function() 
    {
        if (check == 1) 
        {
            display = "";
            document.getElementById("result").value = display;
            check = 0;
        }
        display += "2";
        document.getElementById("result").value = display;
    }

    document.getElementById("three").onclick=function() 
    {
        if (check == 1) 
        {
            display = "";
            document.getElementById("result").value = display;
            check = 0;
        }
        display += "3";
        document.getElementById("result").value = display;
    }

    document.getElementById("four").onclick=function() 
    {
        if (check == 1) 
        {
            display = "";
            document.getElementById("result").value = display;
            check = 0;
        }
        display += "4";
        document.getElementById("result").value = display;
    }

    document.getElementById("five").onclick=function() 
    {
        if (check == 1) 
        {
            display = "";
            document.getElementById("result").value = display;
            check = 0;
        }
        display += "5";
        document.getElementById("result").value = display;
    }

    document.getElementById("six").onclick=function()
     {
        if (check == 1) 
        {
            display = "";
            document.getElementById("result").value = display;
            check = 0;
        }
        display += "6";
        document.getElementById("result").value = display;
    }

    document.getElementById("seven").onclick=function() 
    {
        if (check == 1) 
        {
            display = "";
            document.getElementById("result").value = display;
            check = 0;
        }
        display += "7";
        document.getElementById("result").value = display;
    }

    document.getElementById("eight").onclick=function() 
    {
        if (check == 1) 
        {
            display = "";
            document.getElementById("result").value = display;
            check = 0;
        }
        display += "8";
        document.getElementById("result").value = display;
    }

    document.getElementById("nine").onclick=function() 
    {
        if (check == 1) 
        {
            display = "";
            document.getElementById("result").value = display;
            check = 0;
        }
        display += "9";
        document.getElementById("result").value = display;
    }

    document.getElementById("plus").onclick=function() 
    {
        if (check == 1) 
        {
            check = 0;
        }
        if (display[display.length-1] == '+') return;
        display += "+";
        document.getElementById("result").value = display;
    }

    document.getElementById("substract").onclick=function() 
    {
        if (check == 1) 
        {
            check = 0;
        }
        if (display[display.length-1] == '-') return;
        display += "-";
        document.getElementById("result").value = display;
    }

    document.getElementById("multiply").onclick=function() 
    {
        if (check == 1) 
        {
            check = 0;
        }
        if (display[display.length-1] == '*') return;
        display += "*";
        document.getElementById("result").value = display;
    }

    document.getElementById("divide").onclick=function() 
    {
        if (check == 1) 
        {
            check = 0;
        }
        if (display[display.length-1] == '/') return;
        display += "/";
        document.getElementById("result").value = display;
    }

    document.getElementById("point").onclick=function() 
    {
        if (check == 1) 
        {
            display = "";
            document.getElementById("result").value = display;
            check = 0;
        }
        display += ".";
        document.getElementById("result").value = display;
    }

    document.getElementById("delete_one").onclick=function() 
    {
        if (check == 1)
        {
            return;
        }
        display = display.substring(0, display.length-1);
        document.getElementById("result").value = display;
    }

    document.getElementById("delete_all").onclick=function() 
    {
        display = "";
        document.getElementById("result").value = display;
    }

    document.getElementById("left_bracket").onclick=function() 
    {
        if (check == 1) 
        {
            display = "";
            document.getElementById("result").value = display;
            check = 0;
        }
        display += "(";
        document.getElementById("result").value = display;
    }

    document.getElementById("right_bracket").onclick=function() 
    {
        if (check == 1) 
        {
            display = "";
            document.getElementById("result").value = display;
            check = 0;
        }
        display += ")";
        document.getElementById("result").value = display;
    }

    document.getElementById("equal").onclick=function() 
    {

        try 
        {
            //这里处理0123 = 83 这个js认为数字前面有0就把原数字从八进制转化为十进制数字的bug
            while(display[0] == "0")
            {
                display = display.substr(1,(display.length-1)) ;
            }
            //这里处理 12 + 0123 这种表达式中出现0的bug
            for( var i = 0 ; i < display.length ; i ++ )
            {
                if( display[i] == "0" )
                {
                    if( display[i] == 0 && ( display[i-1] == "+" || display[i-1] == "-" || display[i-1] == "*" || display[i-1] == "/"))
                    {
                        display = display.replace("0","") ;
                    }
                }
            }
            display = eval(display);
        }

        catch(exception) 
        {
            alert("Enter Error!");
        }
        //这里处理浮点值如0.1+0.2 = 0.30000004的问题
        display = parseFloat( display ) ;
        display = parseFloat( display.toFixed(12) ) ;
        document.getElementById("result").value = display;
        check = 1 ;
    }

}

希望大家有什么意见可以多多在下面留言,一起进步。

  • 3
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值