简单计算器

 

<html>  
<head>  
    <title>科学计算器</title>  
    <meta content="text/html; charset=gb2312" http-equiv="Content-Type">  
    <!--written by GoldHuman li hai-->  
    <!--2000.8-->  
    <style>  
        .buttons table input {  
            width: 50px;  
        }  
    </style>  
  
    <script language="javascript">  
<!--  
    var endNumber = true  
    var mem = 0  
    var carry = 10  
    var hexnum = "0123456789abcdef"  
    var angle = "d"  
    var stack = ""  
    var level = "0"  
    var layer = 0  
  
  
    //数字键  
  
    function inputkey(key) {  
        var index = key.charCodeAt(0);  
        if ((carry == 2 && (index == 48 || index == 49))  
 || (carry == 8 && index >= 48 && index <= 55)  
 || (carry == 10 && (index >= 48 && index <= 57 || index == 46))  
 || (carry == 16 && ((index >= 48 && index <= 57) || (index >= 97 && index <= 102))))  
            if (endNumber) {  
                endNumber = false  
                document.calc.display.value = key  
            }  
            else if (document.calc.display.value == null || document.calc.display.value == "0")  
                document.calc.display.value = key  
            else  
                document.calc.display.value += key  
    }  
  
    function changeSign() {  
        if (document.calc.display.value != "0")  
            if (document.calc.display.value.substr(0, 1) == "-")  
                document.calc.display.value = document.calc.display.value.substr(1)  
            else  
                document.calc.display.value = "-" + document.calc.display.value  
    }  
  
    
  
    //运算符  
  
    function operation(join, newlevel) {  
        endNumber = true  
        var temp = stack.substr(stack.lastIndexOf("(") + 1) + document.calc.display.value  
        while (newlevel != 0 && (newlevel <= (level.charAt(level.length - 1)))) {  
            temp = parse(temp)  
            level = level.slice(0, -1)  
        }  
        if (temp.match(/^(.*\d[\+\-\*\/\%\^\&\|x])?([+-]?[0-9a-f\.]+)$/))  
            document.calc.display.value = RegExp.$2  
        stack = stack.substr(0, stack.lastIndexOf("(") + 1) + temp + join  
        document.calc.operator.value = " " + join + " "  
        level = level + newlevel  
  
    }   
  
    //等号  
  
    function result() {  
        endNumber = true  
        while (layer > 0)  
            disbracket()  
        var temp = stack + document.calc.display.value  
        while ((level.charAt(level.length - 1)) > 0) {  
            temp = parse(temp)  
            level = level.slice(0, -1)  
        }  
  
        document.calc.display.value = temp  
        document.calc.bracket.value = ""  
        document.calc.operator.value = ""  
        stack = ""  
        level = "0"  
    }  
    
  
    //十进制转换  
  
    function todec(num, oldcarry) {  
        if (oldcarry == 10 || num == 0) return (num)  
        var neg = (num.charAt(0) == "-")  
        if (neg) num = num.substr(1)  
        var newnum = 0  
        for (var index = 1; index <= num.length; index++)  
            newnum = newnum * oldcarry + hexnum.indexOf(num.charAt(index - 1))  
        if (neg)  
            newnum = -newnum  
        return (newnum)  
    }  
  
    function decto(num, newcarry) {  
        var neg = (num < 0)  
        if (newcarry == 10 || num == 0) return (num)  
        num = "" + Math.abs(num)  
        var newnum = ""  
        while (num != 0) {  
            newnum = hexnum.charAt(num % newcarry) + newnum  
            num = Math.floor(num / newcarry)  
        }  
        if (neg)  
            newnum = "-" + newnum  
        return (newnum)  
    }  
  
    //表达式解析  
  
    function parse(string) {  
        if (string.match(/^(.*\d[\+\-\*\/\%\^\&\|x\<])?([+-]?[0-9a-f\.]+)([\+\-\*\/\%\^\&\|x\<])([+-]?[0-9a-f\.]+)$/))  
            return (RegExp.$1 + cypher(RegExp.$2, RegExp.$3, RegExp.$4))  
        else  
            return (string)  
    }  
  
    //数学运算和位运算  
  
    function cypher(left, join, right) {  
        left = todec(left, carry)  
        right = todec(right, carry)  
        if (join == "+")  
            return (decto(parseFloat(left) + parseFloat(right), carry))  
        if (join == "-")  
            return (decto(left - right, carry))  
        if (join == "*")  
            return (decto(left * right, carry))  
        if (join == "/" && right != 0)  
            return (decto(left / right, carry))  
        if (join == "%")  
            return (decto(left % right, carry))  
        if (join == "&")  
            return (decto(left & right, carry))  
        if (join == "|")  
            return (decto(left | right, carry))  
        if (join == "^")  
            return (decto(Math.pow(left, right), carry))  
        if (join == "x")  
            return (decto(left ^ right, carry))  
        if (join == "<")  
            return (decto(left << right, carry))  
        alert("除数不能为零")  
        return (left)  
    }  
  
   
  
    //界面  
  
    //-->  
    </script>  
  
    <!--written by GoldHuman li hai-->  
    <!--2000.8-->  
    <meta name="GENERATOR" content="MSHTML 8.00.6001.18702">  
</head>  
<body>  
    <div align="center">  
        <form name="calc">  
            <table border="1" width="500" height="250">  
                <tbody>  
                    <tr>  
                        <td height="50">  
                            <table width="500">  
                                <tbody>  
                                    <tr>  
                                        <td></td>  
                                        <td>  
                                            <div align="center">  
                                                <input value="0" readonly size="40" name="display">  
                                            </div>  
                                        </td>  
                                    </tr>  
                                </tbody>  
                            </table>  
                        </td>  
                    </tr>  
                    <tr>  
                        <td>  
                          
                            <table width="500">  
                                <tbody>  
                                    <tr>  
                                         
                                        <td>  
                                             
                                            <input style="background-color: lightgrey" readonly size="3" name="operator">  
                                        </td>  
                                        <td width="183">  
                                            <input style="color: red" onClick="backspace()" value=" 退格 " type="button">  
                                            <input style="color: red" onClick="document.calc.display.value = 0 " value=" 清屏 "  
                                                type="button">  
                                           
                                        </td>  
                                    </tr>  
                                </tbody>  
                            </table>  
                           
                                         
                                            <table>  
                                                <tbody>  
                                                    <tr align="middle">  
                                                        <td>  
                                                            <input style="color: blue" onClick="inputkey('7')" value=" 7 " type="button" name="k7">  
                                                        </td>  
                                                        <td>  
                                                            <input style="color: blue" onClick="inputkey('8')" value=" 8 " type="button" name="k8">  
                                                        </td>  
                                                        <td>  
                                                            <input style="color: blue" onClick="inputkey('9')" value=" 9 " type="button" name="k9">  
                                                        </td>  
                                                        <td>  
                                                            <input style="color: red" onClick="operation('/', 6)" value=" / " type="button">  
                                                        </td>  
                                                       
                                                    </tr>  
                                                    <tr align="middle">  
                                                        <td>  
                                                            <input style="color: blue" onClick="inputkey('4')" value=" 4 " type="button" name="k4">  
                                                        </td>  
                                                        <td>  
                                                            <input style="color: blue" onClick="inputkey('5')" value=" 5 " type="button" name="k5">  
                                                        </td>  
                                                        <td>  
                                                            <input style="color: blue" onClick="inputkey('6')" value=" 6 " type="button" name="k6">  
                                                        </td>  
                                                        <td>  
                                                            <input style="color: red" onClick="operation('*', 6)" value=" * " type="button">  
                                                        </td>  
                                                     
                                                    </tr>  
                                                    <tr align="middle">  
                                                        <td>  
                                                            <input style="color: blue" onClick="inputkey('1')" value=" 1 " type="button">  
                                                        </td>  
                                                        <td>  
                                                            <input style="color: blue" onClick="inputkey('2')" value=" 2 " type="button" name="k2">  
                                                        </td>  
                                                        <td>  
                                                            <input style="color: blue" onClick="inputkey('3')" value=" 3 " type="button" name="k3">  
                                                        </td>  
                                                        <td>  
                                                            <input style="color: red" onClick="operation('-', 5)" value=" - " type="button">  
                                                        </td>  
                                                      
                                                        </td>  
                                                    </tr>  
                                                    <tr align="middle">  
                                                        <td>  
                                                            <input style="color: blue" onClick="inputkey('0')" value=" 0 " type="button">  
                                                        </td>  
                                                        <td>  
                                                            <input style="color: blue" onClick="changeSign()" value="+/-" type="button">  
                                                        </td>  
                                                        <td>  
                                                            <input style="color: blue" onClick="inputkey('.')" value=" . " type="button" name="kp">  
                                                        </td>  
                                                        <td>  
                                                            <input style="color: red" onClick="operation('+', 5)" value=" + " type="button">  
                                                        </td>  
                                                        <td>  
                                                            <input style="color: red" onClick="result()" value=" = " type="button">  
                                                        </td>  
                                             
                                                    </tr>  
                                                </tbody>  
                                            </table>  
                                        </td>  
                                    </tr>  
                                </tbody>  
                            </table>  
                        </td>  
                    </tr>  
                </tbody>  
            </table>  
        </form>  
    </div>  
</body>  
</html> 

 

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值