ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

网页计算器

2021-11-28 23:02:02  阅读:200  来源: 互联网

标签:index 网页 calculate self value content substring 计算器


<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<style>
    * {
        margin: 0;
        padding: 0;
    }
    
    .box {
        width: 380px;
        height: 500px;
        margin: 0 auto;
        margin-top: 100px;
        border-radius: 5px;
        background: rgba(177, 165, 165, 0.58);
        border: 3px solid rgba(0, 0, 0, 0.20);
    }
    
    table {
        margin-top: 20px;
        margin-left: 30px;
        border-spacing: 20px
    }
    
    table td {
        width: 50px;
        height: 40px;
        border: 1px solid rgb(0, 0, 0);
        border-radius: 5px;
        text-align: center;
    }
    
    table input {
        border-radius: 5px;
        width: 50px;
        height: 40px;
        outline: none;
    }
    
    .top {
        width: 330px;
        height: 50px;
        margin-left: 15px;
        margin-top: 50px;
        border-radius: 10px;
        text-align: right;
        background-color: rgb(167, 166, 161);
    }
</style>

<body>
    <div class="box">
        <div>
            <input type="text" id="content" disabled="disabled" class="top">
        </div>
        <div class="contain">
            <table>

                <!--add, subtract, multiply divide delete 加 减 乘 除 删除-->
                <tr>
                    <td><input type="button" value="CE" id="clear" οnclick="appContent(this)"></td>
                    <td><input type="button" value="+" id="add" οnclick="appContent(this)"></td>
                    <td><input type="button" value="-" id="subtract" οnclick="appContent(this)"></td>
                    <td><input type="button" value="DEL" id="delect" οnclick="appContent(this)"></td>
                </tr>
                <tr>
                    <td><input type="button" value="7" id="number7" οnclick="appContent(this)"></td>
                    <td><input type="button" value="8" id="number8" οnclick="appContent(this)"></td>
                    <td><input type="button" value="9" id="number9" οnclick="appContent(this)"></td>
                    <td><input type="button" value="*" id="multiply" οnclick="appContent(this)"></td>
                </tr>
                <tr>
                    <td><input type="button" value="4" id="number4" οnclick="appContent(this)"></td>
                    <td><input type="button" value="5" id="number5" οnclick="appContent(this)"></td>
                    <td><input type="button" value="6" id="number6" οnclick="appContent(this)"></td>
                    <td><input type="button" value="/" id="divide" οnclick="appContent(this)"></td>
                </tr>
                <tr>
                    <td><input type="button" value="1" id="number1" οnclick="appContent(this)"></td>
                    <td><input type="button" value="2" id="number2" οnclick="appContent(this)"></td>
                    <td><input type="button" value="3" id="number3" οnclick="appContent(this)"></td>
                    <td><input type="button" value="." id="point" οnclick="appContent(this)"></td>
                </tr>
                <tr>
                    <td><input type="button" value="(" id="leftbracket" οnclick="appContent(this)"></td>
                    <td><input type="button" value="0" id="number0" οnclick="appContent(this)"></td>
                    <td><input type="button" value=")" id="rightbracket" οnclick="appContent(this)"></td>
                    <td><input type="button" value="=" id="equal" οnclick="appContent(this)"></td>

                </tr>
            </table>
        </div>
    </div>
</body>

<script>
    function appContent(self) {
        var content = document.getElementById('content');
        if (self.value != "CE" && self.value != "DEL" && self.value != "=") {
            content.value += self.value;
        } else if (self.value == "CE") {
            //清空
            content.value = "";
        } else if (self.value == "DEL") {
            //删除:截取除最后一个字符串 substring
            content.value = content.value.substring(0, content.value.length - 1);
        } else if (self.value == "=") {
            //判等
            var resultText = calculate(content.value);
            content.value = content.value + "=" + resultText;
        }
    }

    function calculate(content) {
        var index = content.lastIndexOf("(");
        if (index > -1) {
            var endIndex = content.indexOf(")", index);
            if (endIndex > -1) {
                var result = calculate(content.substring(index + 1, endIndex));
                return calculate(content.substring(0, index) + ("" + result) + content.substring(endIndex + 1))
            }
        }
        index = content.indexOf("+");
        if (index > -1) {
            return calculate(content.substring(0, index)) + calculate(content.substring(index + 1));
        }
        index = content.lastIndexOf("-");
        if (index > -1) {
            return calculate(content.substring(0, index)) - calculate(content.substring(index + 1));
        }
        index = content.lastIndexOf("*");
        if (index > -1) {
            return calculate(content.substring(0, index)) * calculate(content.substring(index + 1));
        }
        index = content.lastIndexOf("/");
        if (index > -1) {
            return calculate(content.substring(0, index)) / calculate(content.substring(index + 1));
        }
        if ("" == content) {
            return 0;
        } else {
            return content - 1 + 1;
        }
    }
</script>

</html>

标签:index,网页,calculate,self,value,content,substring,计算器
来源: https://blog.csdn.net/m0_51443493/article/details/121599787

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有