用JS寫的簡單的計算機實現代碼_javascript技巧

來源:互聯網
上載者:User
  
1.本頁效果圖片
 
   2.美化後的效果
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <html> <head> <script> Array.prototype.remove=function(index) { if(isNaN(index)||index>this.length){return false;} for(var i=0,n=0;i<this.length;i++) { if(this[i]!=this[index]) { this[n++]=this[i] } } this.length-=1 } function commonMath(){ //類初始化 this.init = function(){ this.tmp = ''; this.debug = 0; this.output = ''; this.method = ''; this.sign = 1;//0:-;1:+ this.register = Array();//寄存器 } //設定報錯資訊 this.showmsg = function(msg){ if(this.debug == 1) alert(msg); } //設定運算子 this.setMethod = function(_method){ this.method = _method; _length = this.register.length; if(_length == 0){ if(this.tmp == '') return; this.register[0] = this.tmp; this.register[1] = _method; this.tmp = ''; return; } if(_length = 2 || this.tmp == ''){ this.register[1] = _method; } if(_length == 2 && this.tmp != ''){ this.register[2] = this.tmp; this.run(1); } if(_length == 3) this.run(1); } //設定顯示值 this.setValue = function(_value){ var tmp = parseInt(eval(this.tmp+'+"'+_value+'"')); max = /^-?\d{1,9}$/i; if( max.test(tmp) == false){ return; } this.tmp = tmp; this.output.value = this.tmp; } //設定符號 this.setSign = function(){ var del = 0; var sign = Array('-','+'); this.sign = this.sign ^ 1; _sign = sign[this.sign]; if(/\d/i.test(this.tmp)== false && this.register.length > 0){ del = 1; this.tmp = this.register[0]; } if(_sign == '-'){ this.tmp = -this.tmp; }else{ this.tmp = Math.abs(this.tmp); } this.output.value = this.tmp; if(del == 1){ this.register[0] = this.tmp; this.tmp = ''; } } //擷取按鍵 this.getValue = function(input){ var _in = input; var inputList = document.getElementsByTagName("input"); this.output = inputList[0]; regMethod = /^[\+|\-|\*|\/]$/i; if(regMethod.test(_in)){ this.setMethod(_in); return; } regNum = /^\d$/i; if(regNum.test(_in)){ this.setValue(_in); return; } regSign = /^\+\/\-$/i; if(regSign.test(_in)){ this.setSign(_in); return; } regResult = /^=$/; if(regResult.test(_in)){ this.run(); return; } } //計算結果 this.run = function(type){ if(this.register.length < 2) return this.showmsg(1); if(this.register.length == 2 && (this.tmp == '')) return this.showmsg(2); if(this.register.length == 2 && this.tmp != ''){ this.register[2] = this.tmp; this.run(); } this.showmsg(this.register.join(' ')); var _exp = parseInt(eval(this.register.join(' '))); this.output.value = _exp; for(i=0;i<3;i++){ this.register.remove(i); } this.register[0] = _exp; this.tmp = ''; if(type) this.register[1] = this.method; return; } } var commonMath = new commonMath(); commonMath.init(); </script> <style> #main{ width:100%; height:100%; } #inputs{ position:relative; top:50%; left:50%; } .inbutton{ width:30px; } .outbutton{ width:145px; text-align:right; } .tcenter{ text-align:center; } </style> </head> <body> <div id="main"> <div> <span class="outbutton tcenter">etongchina@gmail.com</span> </div> <div> <span><input id="output" type="text" class="outbutton" maxlength="9" value="." /></span> </div> <div> <span><input type="button" class="inbutton" value="7" onclick="commonMath.getValue(this.value)" /></span> <span><input type="button" class="inbutton" value="8" onclick="commonMath.getValue(this.value)" /></span> <span><input type="button" class="inbutton" value="9" onclick="commonMath.getValue(this.value)" /></span> <span><input type="button" class="inbutton" value="+" onclick="commonMath.getValue(this.value)" /></span> </div> <div> <span><input type="button" class="inbutton" value="4" onclick="commonMath.getValue(this.value);" /></span> <span><input type="button" class="inbutton" value="5" onclick="commonMath.getValue(this.value);" /></span> <span><input type="button" class="inbutton" value="6" onclick="commonMath.getValue(this.value);" /></span> <span><input type="button" class="inbutton" value="-" onclick="commonMath.getValue(this.value);" /></span> </div> <div> <span><input type="button" class="inbutton" value="1" onclick="commonMath.getValue(this.value);" /></span> <span><input type="button" class="inbutton" value="2" onclick="commonMath.getValue(this.value);" /></span> <span><input type="button" class="inbutton" value="3" onclick="commonMath.getValue(this.value);" /></span> <span><input type="button" class="inbutton" value="*" onclick="commonMath.getValue(this.value);" /></span> </div> <div> <span><input type="button" class="inbutton" value="0" onclick="commonMath.getValue(this.value);" /></span> <span><input type="button" class="inbutton" value="+/-" onclick="commonMath.getValue(this.value);" /></span> <span><input type="button" class="inbutton" value="." onclick="commonMath.getValue(this.value);" /></span> <span><input type="button" class="inbutton" value="/" onclick="commonMath.getValue(this.value);" /></span> </div> <div> <span><input type="button" class="outbutton tcenter" value="計算" onclick="commonMath.getValue('=');" /></span> </div> </div> </body> </html>
[Ctrl+A 全選 注:如需引入外部Js需重新整理才能執行]

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.