javascript实现简易计算器

 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">

<html>

    <head>

        <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />

        <title>Untitled Document</title>

        <script type="text/javascript">

         var a=/^/s*|/s*$/g;

   function b(){

    var ab=this.toString().replace(a,"");

    return ab;

   }

   String.prototype.trim=b;

   

   

            var num1 = 0;

            var num2 = "";

            var flag = 1;

            var fuhao = "";

            function shu(id){

                if (flag == 1) {

     if (num1 == 0 && id == 0) {

      num1 = 0;

     }

     else {

      num1 += id;
      result.value = parseFloat(num1);
     }
 
               }
                else {
                    if (num2 == "" || num2 == 0) {
                        result.value = "";
                        num2 = id;
                    }
                    else {
                        num2 += id;
                    }
                    result.value = num2;
                }
               
            }
           
            function jia(jiahao){
                flag++;
                if (num2 == "") {
                    fuhao1 = jiahao;
                }
                else {
                    result.value = suan(fuhao1);
                    num1 = result.value;
                    num2 = "";
                    fuhao1 = jiahao;
                }
            }
           
            function suan(fu){
                var result = 0;
                switch (fu) {
                    case "+":
                        result = parseFloat(num1) + parseFloat(num2);
                        break;
                    case "-":
                        result = num1 - num2;
                        break;
                    case "*":
                        result = num1 * num2;
                        break;
                    case "/":
                        result = num1 / num2;
                        break;
                }
                return result;
            }
           
            function dengyu(){
                if (num2 != "") {
                    result.value = suan(fuhao1);
                    num1 = result.value;
                    num2 = "";
                }
            }
           
            function c(){
                num1 = 0;
                num2 = "";
                flag = 1;
                fuhao = "";
    result.value=0;
            }
        </script>
    </head>
    <body>
        <table width="170" border="1">
            <tr>
                <td colspan="4">
                    <input class="ri" type="text" id="result" size="21" value="0">
                </td>
            </tr>
            <tr>
                <td>
                    <input οnclick="shu(this.id)" type="button" id="7" value="  7  ">
                </td>
                <td>
                    <input οnclick="shu(this.id)" type="button" id="8" value="  8  ">
                </td>
                <td>
                    <input οnclick="shu(this.id)" type="button" id="9" value="  9  ">
                </td>
                <td>
                    <input οnclick="jia(this.value.trim())" type="button" id="jia" value="  +  ">
                </td>
            </tr>
            <tr>
                <td>
                    <input οnclick="shu(this.id)" type="button" id="4" value="  4  ">
                </td>
                <td>
                    <input οnclick="shu(this.id)" type="button" id="5" value="  5  ">
                </td>
                <td>
                    <input οnclick="shu(this.id)" type="button" id="6" value="  6  ">
                </td>
                <td>
                    <input οnclick="jia(this.value.trim())" type="button" id="jian" value="   -  ">
                </td>
            </tr>
            <tr>
                <td>
                    <input οnclick="shu(this.id)" type="button" id="1" value="  1  ">
                </td>
                <td>
                    <input οnclick="shu(this.id)" type="button" id="2" value="  2  ">
                </td>
                <td>
                    <input οnclick="shu(this.id)" type="button" id="3" value="  3  ">
                </td>
                <td>
                    <input οnclick="jia(this.value.trim())" type="button" id="cheng" value="   *  ">
                </td>
            </tr>
            <tr>
                <td>
                    <input οnclick="shu(this.id)" type="button" id="0" value="  0  ">
                </td>
                <td>
                    <input οnclick="c()" type="button" id="cls" value="  c  ">
                </td>
                <td>
                    <input οnclick="jia(this.value.trim())" type="button" id="chu" value="   /  ">
                </td>
                <td>
                    <input οnclick="dengyu()" type="button" id="deng" value="  =  ">
                </td>
            </tr>
        </table>

    </body>

</html>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值