javascript實(shí)現(xiàn)簡(jiǎn)易計(jì)算器的代碼

字號(hào):


    下面小編就為大家?guī)硪黄猨avascript實(shí)現(xiàn)簡(jiǎn)易計(jì)算器的代碼小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。
    今天閑來無聊,想寫點(diǎn)什么,突然想到用javascript寫一個(gè)計(jì)算器。程序還存在很多的Bug,先在這里記錄一下,以后慢慢更正。
    名單
    代碼如下:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>javascript實(shí)現(xiàn)簡(jiǎn)易計(jì)算器的代碼_腳本之家</title>
    <style type="text/css">
    input{
    width:30px;
    height:20px;
    text-align:center;
    }
    #tbCalculator td
    {
    text-align:center;
    vertical-align:middle;
    }
    </style>
    <script type="text/javascript">
    var result; //保存點(diǎn)擊運(yùn)算符之前輸入框中的數(shù)值
    var operator; //保存運(yùn)算符
    var isPressEqualsKey = false; //記錄是否按下”=“鍵
    //數(shù)字鍵事件
    function connectionDigital(control)
    {
    var txt = document.getElementById('txtScream');
    if(isPressEqualsKey)
    { 
    txt.value = ""; //已進(jìn)行過計(jì)算,則清空數(shù)值輸入框重新開始
    isPressEqualsKey = false;
    }
    //數(shù)值輸入已經(jīng)存在小數(shù)點(diǎn),則不允許再輸入小數(shù)點(diǎn)
    if(txt.value.indexOf('.') > -1 && control.value == '.')
    return false;
    txt.value += control.value; //將控件值賦給數(shù)值輸入框中
    }
    //退格鍵事件
    function backspace()
    {
    var txt = document.getElementById('txtScream');
    txt.value = txt.value.substring(0,txt.value.length - 1);
    }
    //ce鍵事件:清空數(shù)字輸入框
    function clearAll()
    {
    document.getElementById('txtScream').value = "";
    result = "";
    operator = "";
    }
    // +、-、*、/ 事件
    function calculation(control)
    {
    //將運(yùn)算符保存入全局變量中
    operator = control.value; 
    var txt = document.getElementById('txtScream');
    if(txt.value == "")return false; //數(shù)值輸入框中沒有數(shù)字,則不能輸入運(yùn)算符
    //將數(shù)值輸入框中的值保存到計(jì)算表達(dá)式中
    result = txt.value; 
    //清空輸入框,以待輸入操作值
    txt.value = ""; 
    }
    //計(jì)算結(jié)果
    function getResult()
    {
    var opValue;
    //計(jì)算表達(dá)式中存在運(yùn)算符
    var sourseValue = parseFloat(result);
    var txt = document.getElementById('txtScream');
    if(operator == '*')
    opValue = sourseValue * parseFloat(txt.value);
    else if(operator == '/')
    opValue = sourseValue / parseFloat(txt.value);
    else if(operator == '+')
    opValue = sourseValue + parseFloat(txt.value);
    else if(operator == '-')
    opValue = sourseValue - parseFloat(txt.value);
    txt.value = opValue;
    isPressEqualsKey = true;
    result = "";
    opValue = "";
    }
    </script>
    </head>
    <body>
    <table id="tbCalculator" width="200" cellpadding="0" cellspacing="0" bordercolor="#0066FF">
    <tr>
    <td height="30" colspan="4">
    <input type="text" name="txtScream" id="txtScream" readonly="readonly" /> </td>
    </tr>
    <tr>
    <td height="30" colspan="2">
    <input type="button" name="btnCE" id="btnCE" value="C E"; onclick="clearAll();" /></td>
    <td height="30" colspan="2">
    <input type="button" name="btn10" id="btn10" value="Backspace"; onclick="backspace();" /></td>
    </tr>
    <tr>
    <td height="30"><input type="button" name="btn7" id="btn7" value="7" onclick="connectionDigital(this);" /></td>
    <td><input type="button" name="btn8" id="btn8" value="8" onclick="connectionDigital(this);"/></td>
    <td><input type="button" name="btn9" id="btn9" value="9" onclick="connectionDigital(this);" /></td>
    <td><input type="button" name="btn6" id="btn6" value="/" onclick="calculation(this);" /></td>
    </tr>
    <tr>
    <td height="30">
    <input type="button" name="btn4" id="btn4" value="4" onclick="connectionDigital(this);"/></td>
    <td><input type="button" name="btn5" id="btn5" value="5" onclick="connectionDigital(this);"/></td>
    <td><input type="button" name="btn6" id="btn6" value="6" onclick="connectionDigital(this);"/></td>
    <td><input type="button" name="btn13" id="btn13" value="*" onclick="calculation(this);" /></td>
    </tr>
    <tr>
    <td height="30">
    <input type="button" name="btn1" id="btn1" value="1" onclick="connectionDigital(this);"/></td>
    <td><input type="button" name="btn2" id="btn2" value="2" onclick="connectionDigital(this);"/></td>
    <td><input type="button" name="btn3" id="btn3" value="3" onclick="connectionDigital(this);"/></td>
    <td><input type="button" name="btn18" id="btn18" value="-" onclick="calculation(this);" /></td>
    </tr>
    <tr>
    <td height="30"><input type="button" name="btn0" id="btn0" value="0" onclick="connectionDigital(this);"/></td>
    <td><input type="button" name="btndot" id="btndot" value="." onclick="connectionDigital(this);" /></td>
    <td><input name="btn22" type="button" id="btn22" value="=" onclick="getResult();" /></td>
    <td><input type="button" name="btn23" id="btn23" value="+" onclick="calculation(this);" /></td>
    </tr>
    </table>
    </body>
    </html>
    以上這篇javascript實(shí)現(xiàn)簡(jiǎn)易計(jì)算器的代碼就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考