dongjinwang 2015-08-09 02:29 采纳率: 16.7%
浏览 3313

急JS编写的计算器 如何实现 连续加减乘除四则运算 求大神指点 急帮我再看看小数点的问题

<html>
<head>
    <title>JiSuanQi</title>
    <!-- 移动层外部JS文件 -->
    <script src="src/js/yidong.js"></script>
    <link rel="stylesheet" href="src/css/jsq.css">
    <script>
        var oper = ""
        var isClickOper = false//是否点击了运算符
        var num = 0;
        var isClickPoint = false;//是否点击了小数点
        //输入数字 
        function addNum(n){
            var nowNum = myform.num.value;
            if(nowNum == "0"||isClickOper){//直接赋值
                myform.num.value = n;
                isClickOper = false;
            }else if(myform.num.value.length<9){//限制文本框按键输入数字长度最多9个
                //myform.num.value = myform.num.value + n;//连接赋值
                myform.num.value += n  //也可以这样写连接赋值
            }
        }
        //添加运算符
        function setOper(op){
            oper = op;
            num = myform.num.value*1;
            isClickOper = true;
        }

        //得到结果
        function getCount(){
            if(oper != ""){
                if(oper == "+"){
                    num = num + myform.num.value*1;
                }else if(oper == "-"){
                    num = num - myform.num.value*1;
                }else if(oper == "*"){
                    num = num * myform.num.value*1
                }else if(oper == "/"){
                            if(myform.num.value == 0){
                            num ="除数不能为0"}else{
                            num = num / myform.num.value*1
                            }
                }else if(oper == "%"){
                    num = num % myform.num.value*1
                }
                myform.num.value = num;
            }
        }

        //添加小数点
    function addPoint(){
        if(!isClickPoint){//判断是否点击过小数点
            myform.num.value += ".";
                isClickPoint = true;
            }
        }



        //清楚
        function cc(){
            myform.num.value = "0";
            isClickOper = false;
            isClickPoint = false;
            num = 0;
            oper="0";   
        }
        //删除
        function del(){
            var nowNum = myform.num.value;
            if(nowNum.length==1){
                myform.num.value = "0";
            }else{
                myform.num.value = nowNum.substr(0,nowNum.length-1)
            }
        }

    </script>
    <!--鼠标进入、默认、按下、放开样式-->
    <style>
    #ceng{width:440px;height:500px;background:#141414;}
    .jr{background-Image:url('src/img/jr.png')}
    .mr{background-Image:url('src/img/mr.png')}
    .ax{background-Image:url('src/img/ax.png')}
    .fk{background-Image:url('src/img/fk.png')}
    table{position:relative;top:30px;}
    #daceng{background-image:url('src/img/1.jpg')}

    </style>
</head>
<body bgcolor="green">
    <center>
    <form id="myform">
    <div onmousemove="mouseMove()" id="daceng" style="width:100%;height:800px">

    <div onmousedown="mouseDown()" onmouseup="mouseUp()" id="ceng" style="top:80px;left:300px;position:absolute;"><font size="7" color="#99ff99">简易计算器</font>
        <table border="1" width="422px" height="360px" align="center">
            <tr>
                <td colspan="4">
                    <input id="num" type="text" onfocus="blur()" value="0"/>
                </td>
            </tr>
            <tr>
                <td>
                    <input id="bt1" type="button" value="1" onclick="addNum(1)"/>
                </td>
                <td>
                    <input id="bt2" type="button" value="2" onclick="addNum(2)"/>
                </td>
                <td>
                    <input id="bt3" type="button" value="3" onclick="addNum(3)"/>
                </td>
                <td>
                    <input id="bt10" type="button" value="+" onclick="setOper('+')"/>
                </td>
            </tr>
            <tr>
                <td>
                    <input id="bt4" type="button" value="4" onclick="addNum(4)"/>
                </td>
                <td>
                    <input id="bt5" type="button" value="5" onclick="addNum(5)"/>
                </td>
                <td>
                    <input id="bt6" type="button" value="6" onclick="addNum(6)"/>
                </td>
                <td>
                    <input id="bt11" type="button" value="-" onclick="setOper('-')"/>
                </td>
            </tr>
            <tr>
                <td>
                    <input id="bt7" type="button" value="7" onclick="addNum(7)"/>
                </td>
                <td>
                    <input id="bt8" type="button" value="8" onclick="addNum(8)"/>
                </td>
                <td>
                    <input id="bt9" type="button" value="9" onclick="addNum(9)"/>
                </td>
                <td>
                    <input id="bt12" type="button" value="*" onclick="setOper('*')"/>
                </td>
            </tr>
            <tr>
                <td>
                    <input id="bt0" type="button" value="0" onclick="addNum(0)"/>
                </td>
                <td>
                    <input id="bt13" type="button" value="." onclick="addPoint()"/>
                </td>
                <td>
                    <input id="bt14" type="button" value="%" onclick="setOper('%')"/>
                </td>
                <td>
                    <input id="bt15" type="button" value="/" onclick="setOper('/')"/>
                </td>
            </tr>
            <tr>
                <td>
                    <input id="bt16" type="button" value="C" onclick="cc()"/>
                </td>
                <td>
                    <input id="bt17" type="button" value="←" onclick="del()"/>
                </td>
                <td colspan="2">
                    <input id="bt18" type="button" value="=" onclick="getCount()"/>
                </td>

            </tr>
        </table>
    </div>
    </div>
    </form>
    </center>
    <!-- 打开关闭计算器 -->
    <input type="button" value="打开计算器"style="font-size:20px;color:black;background:lime" onclick="ceng.style.display='block'">
    <input type="button" style="font-size:20px;color:black;background:lime" value="关闭计算器" onclick="ceng.style.display='none'">

</body>

<script src="src/js/jsq.js"></script>

  • 写回答

2条回答 默认 最新

  • 勤奋的重剑 2015-08-09 07:52
    关注

    function setOper(op){
    oper = op;
    num = myform.num.value*1;
    isClickOper = false;
    }
    将这段代码中true改成false

                function del(){
            var nowNum = myform.num.value;
            if(nowNum.length==1){
                myform.num.value = "0";
                                isClickOper = false;//加上这段
            }else{
                myform.num.value = nowNum.substr(0,nowNum.length-1)
            }
    
        }
                其他由于mouseDown()等函数不存在无法知道什么错误
    
    评论

报告相同问题?

悬赏问题

  • ¥15 Arcgis相交分析无法绘制一个或多个图形
  • ¥15 seatunnel-web使用SQL组件时候后台报错,无法找到表格
  • ¥15 fpga自动售货机数码管(相关搜索:数字时钟)
  • ¥15 用前端向数据库插入数据,通过debug发现数据能走到后端,但是放行之后就会提示错误
  • ¥30 3天&7天&&15天&销量如何统计同一行
  • ¥30 帮我写一段可以读取LD2450数据并计算距离的Arduino代码
  • ¥15 飞机曲面部件如机翼,壁板等具体的孔位模型
  • ¥15 vs2019中数据导出问题
  • ¥20 云服务Linux系统TCP-MSS值修改?
  • ¥20 关于#单片机#的问题:项目:使用模拟iic与ov2640通讯环境:F407问题:读取的ID号总是0xff,自己调了调发现在读从机数据时,SDA线上并未有信号变化(语言-c语言)