Home > Web Front-end > JS Tutorial > Simple calculator implementation code written in JS_javascript skills

Simple calculator implementation code written in JS_javascript skills

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-05-16 18:46:41
Original
1002 people have browsed it
2009-08-21_195533
1. Effect picture of this page
2009-08-21_195501
2. Effect after beautification

[Ctrl A select all Note: If you need to introduce external Js, you need to refresh to execute
]<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>
Related labels:
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Issues
Where is js written?
From 1970-01-01 08:00:00
0
0
0
js addClass not working
From 1970-01-01 08:00:00
0
0
0
js file code not found
From 1970-01-01 08:00:00
0
0
0
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template