javascript實現簡易計算機的代碼_javascript技巧

來源:互聯網
上載者:User

今天閑來無聊,想寫點什麼,突然想到用javascript寫一個計算機。程式還存在很多的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實現簡易計算機的代碼_雲棲社區</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; //儲存點擊運算子之前輸入框中的數值var operator; //儲存運算子var isPressEqualsKey = false; //記錄是否按下”=“鍵//數字鍵事件function connectionDigital(control){var txt = document.getElementById('txtScream');if(isPressEqualsKey){ txt.value = ""; //已進行過計算,則清空數值輸入框重新開始isPressEqualsKey = false;}//數值輸入已經存在小數點,則不允許再輸入小數點if(txt.value.indexOf('.') > -1 && control.value == '.')return false;txt.value += control.value; //將控制項值賦給數值輸入框中}//退格鍵事件function backspace(){var txt = document.getElementById('txtScream');txt.value = txt.value.substring(0,txt.value.length - 1);}//ce鍵事件:清空數字輸入框function clearAll(){document.getElementById('txtScream').value = "";result = "";operator = "";}// +、-、*、/ 事件function calculation(control){//將運算子儲存入全域變數中operator = control.value; var txt = document.getElementById('txtScream');if(txt.value == "")return false; //數值輸入框中沒有數字,則不能輸入運算子//將數值輸入框中的值儲存到計算運算式中result = txt.value; //清空輸入框,以待輸入操作值txt.value = ""; }//計算結果function getResult(){var opValue;//計算運算式中存在運算子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" border="1" align="center" cellpadding="0" cellspacing="0" bordercolor="#0066FF"><tr><td height="30" colspan="4" align="center"><input type="text" name="txtScream" id="txtScream" style="width:180px; border-style:none; text-align:right;" readonly="readonly" /> </td></tr><tr><td height="30" colspan="2"><input type="button" name="btnCE" id="btnCE" value="C E" style="width:80px;" align="right"; onclick="clearAll();" /></td><td height="30" colspan="2"><input type="button" name="btn10" id="btn10" value="Backspace" style="width:80px;" align="right"; 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實現簡易計算機的代碼就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支援雲棲社區。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.