美文网首页
JavaScrpt-小练习(计算器)

JavaScrpt-小练习(计算器)

作者: 测试探索 | 来源:发表于2022-06-22 21:18 被阅读0次

一、目录结构

image.png

二、代码练习

index.html
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
    <link rel="stylesheet" type="text/css" href="index.css">
    <script type = "text/javascript" src="index.js"></script>
    <script type = "text/javascript" src="../js/mooc.js"></script>

</head>
<body onload="init(),init_mooc()">
    <div id = "div1">
        <div id = "div2">
            <input type="text" name="num" id="num">
        </div>

        <div id = "div3">
            <input type="button" value = "c" name="" id="">
            <input type="button" value = "<-" name="" id="">
            <input type="button" value = "+/-" name="" id="">
            <input type="button" value = "/" name="" id="">
            <input type="button" value = "1" name="" id="n1">
            <input type="button" value = "2" name="" id="">
            <input type="button" value = "3" name="" id="">
            <input type="button" value = "*" name="" id="">
            <input type="button" value = "4" name="" id="">
            <input type="button" value = "5" name="" id="">
            <input type="button" value = "6" name="" id="">
            <input type="button" value = "-" name="" id="">
            <input type="button" value = "7" name="" id="">
            <input type="button" value = "8" name="" id="">
            <input type="button" value = "9" name="" id="">
            <input type="button" value = "+" name="" id="">
            <input type="button" value = "0" name="" id="">
            <input type="button" value = "." name="" id="">
            <input type="button" value = "=" name="" id="">
            <input type="button" value = "m" name="" id="imooc">
        </div>
    </div>


</body>
</html>
index.css
*{
    margin: 0px;
    padding: 0px;
}

div{
    width: 170px;
}

#div1{
    top: 60px;
    left:100px;
    position: absolute;

}

input[type="button"]{
    width: 30px;
    margin-right: 4px;
}

input[type="text"]{
    width:147px ;
    text-align: right;
    background-color: #fff;
    border:1px solid;
    padding-right: 5px;
    box-sizing: border-box;

}

input[type="button"]:hover{
    background-color: yellow;
    border: 1px solid;

}
index.js
function init() {
    //显示器文本框默认显示为0,并且不可手动输入,只可以鼠标点击
    var num = document.getElementById("num");
    num.value = 0;
    num.disabled = "disabled";

    var oButton = document.getElementsByTagName("input");
    var btn_num1;
    var fh;
    for(var i = 0;i < oButton.length;i++){
        oButton[i].onclick = function () {
            if(isNumber(this.value)){
                if(isNull(num.value)){
                    num.value = this.value
                }else{
                    num.value = num.value + this.value;

                }
            }else{
                // alert("feishuzi");
                var btn_num = this.value;
                switch (btn_num) {
                    case "+":
                        btn_num1 = Number(num.value);
                        num.value = 0;
                        fh = "+";
                        break;
                    case "-":
                        btn_num1 = Number(num.value);
                        num.value = 0;
                        fh = "-";
                        break;
                    case "*":
                        btn_num1 = Number(num.value);
                        num.value = 0;
                        fh = "*";
                        break;
                    case "/":
                        btn_num1 = Number(num.value);
                        num.value = 0;
                        fh = "/";
                        break;
                    case ".":
                        num.value = dec_number(num.value);
                        break;
                    case "<-":
                        num.value = back(num.value);
                        break;
                    case "c":
                        num.value = 0;
                        break;
                    case "+/-":
                        num.value = sign(num.value);
                        break;
                    case "=":
                        switch (fh) {
                            case "+":
                                num.value = btn_num1 + Number(num.value);
                                break;
                            case "-":
                                num.value = btn_num1 - Number(num.value);
                                break;
                            case "*":
                                num.value = btn_num1 * Number(num.value);
                                break;
                            case "/":
                                if(Number(num.value)==0){
                                    alert("除数不应为0");
                                    num.value = 0;
                                }else{
                                    num.value = btn_num1 / Number(num.value);

                                }
                                break;

                        }
                        break;

                }

            }
        }
    }

}
/*正负号*/
function sign(n){
    if(n.indexOf("-") == -1){
        n = "-" + n;
    }else{
        n = n.substr(1,n.length);
    }
    return n
}
/*退位键*/
function back(n){
    n = n.substr(0,n.length-1);
    if(isNull(n)){
        n=0;
    }
    return n
}



/*小数点*/
function dec_number(n){
    if(n.indexOf(".") == -1){
        n = n + ".";
    }
    return n
}
//验证文本框是否为空,或者0
function isNull(n) {
    if(n*1 == 0 || n.length == 0){
        return true;
    }else{
        return false;
    }
}
function isNumber(n){
    // if(!isNaN(n)){
    //     return ture;//参数n是数字
    // }else{
    //     return false;//参数n不是数字
    // }
//  第二种表达方式
    return !isNaN(n);
}
js/mooc.js
function init_mooc(){
    document.getElementById("imooc").onclick = function () {
            window.location.href = "https://www.jianshu.com/u/c84a9d27d8c3";
    }
}

相关文章

  • JavaScrpt-小练习(计算器)

    一、目录结构 二、代码练习 index.html index.css index.js js/mooc.js

  • 前端(十三)

    1.小作业 2.if练习2 3.if练习3 4.switch练习1 5.switch练习2 6.计算器

  • 赚大钱

    老师看到小尚同学经常不爱学习,老爱练习操作电子计算器:别人专心听讲的时候,他在练习操作电子计算器;别人仔细做题的时...

  • js计算器小练习

    function calculator(){ var operator =arguments[argum...

  • 前端

    1.计算器 2. switch语句练习

  • 3.6 嵌入式

    今天结合lcd显示屏和触摸屏进行练习编写简易计算器。 上午练习在触摸屏画线,下午编写触摸屏实现计算器功能,可以实现...

  • 【精致Java教程】19:小练习2

    做个条件结构的的小练习:模拟一个计算器练习内容:在控制台接受两个数字(用double存储)和一个运算符,然后输出运...

  • 那些年出现的作业

    Java面向对象之类的练习之模拟计算器 demo Java面向对象之类的练习之ArrayBox封装 demo

  • typescript学习

    关于typescript的计算器小栗子:

  • 【小程序入门集锦】15,一个开源的小程序计算器

    这是一个小程序新手练习的项目(基于微信quick demo改写),做了一个计算器。有基本的UI,有基本的跳转及简单...

网友评论

      本文标题:JavaScrpt-小练习(计算器)

      本文链接:https://www.haomeiwen.com/subject/hmnkvrtx.html