js版本计算器第三版
2012-07-19 09:20
204 查看
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <title>计算器</title> <style type="text/css"> input { width:50px; } #Backspace { width:120px; } #CE { width:110px; } #result { width:300px; } </style> <script type="text/javascript"> function Calculator() { this.value = ''; this.current = ''; this.operator = ''; this.process = function (code) { switch (code.toString()) { case '.' : case '0' : case '1' : case '2' : case '3' : case '4' : case '5' : case '6' : case '7' : case '8' : case '9' : this.setNum(code); break; case '+/-' : this.setSymbol(); break; case 'CE' : this.clearResult(); break; case 'C' : this.clearAll(); break; case 'Backspace' : this.deleteOne(); break; case 'sqrt' : case '+' : case '-' : case '*' : case '/' : case '=' : this.calculator(code); break; } } // 设置数字 this.setNum = function(num) { var result = document.getElementById("result"); var pointIndex = this.current.toString().indexOf('.'); if (pointIndex >= 0 && num == '.') {// 己经包含小数点,不再添加 return; } var maxLength = 32; if (pointIndex) {// 包含小数点时,最大位数为33 maxLength = 33; } // 超出最大位数限制 if (this.current.toString().length >= maxLength) { return; } if (this.current.toString() == '0' && num != '.') {// 输入的数非小数,去掉前面的0 this.current = num; } else { this.current += "" + num; } result.value = this.current; } // 设置符号位 this.setSymbol = function() { var result = document.getElementById("result"); var num = new Number(result.value); var tmp = -num.valueOf(); result.value = tmp; } // 清除结果,恢复为0 this.clearResult = function() { var result = document.getElementById("result"); result.value = '0'; this.current = ''; } // 清除结果,恢复为0,不清除符号 this.clearAll = function() { var result = document.getElementById("result"); result.value = '0'; this.value = ''; this.current = ''; this.operator = ''; } // 删除一位数 this.deleteOne = function() { var result = document.getElementById("result"); var tmp = result.value.length > 1 ? result.value.substr(0, result.value.length - 1) : 0; tmp = tmp == '-' ? 0 : tmp; result.value = tmp; this.current = tmp; } // 计算 this.calculator = function(operator) { var result = document.getElementById("result"); if (operator == 'sqrt') { if (result.value != "") { this.value = Math.sqrt(result.value); result.value = this.value; this.current = result.value; } } else { if (this.value != "" && this.current != "" && this.operator != '') { var firstNum = new Number(this.value).valueOf(); var secondNum = new Number(this.current).valueOf(); var value = 0; switch (this.operator) { case '+' : value = firstNum + secondNum; break; case '-' : value = firstNum - secondNum; break; case '*' : value = firstNum * secondNum; break; case '/' : value = firstNum / secondNum; break; } this.value = value; result.value = this.value.toString(); } else { this.value = this.current == '' ? this.value : this.current; } if (operator == "=") { result.value = this.value == '' ? '0' : this.value; this.operator = ''; } else { this.operator = operator; } this.current = ''; } } } var c = new Calculator(); document.onkeyup = function(e) { var evt = (typeof e == "undefined") ? window.event : e; if ((evt.keyCode >= 48 && evt.keyCode <= 57 && !evt.shiftKey) || (evt.keyCode >= 96 && evt.keyCode <= 105 && !evt.shiftKey)) { if (evt.keyCode > 57) { c.process(evt.keyCode - 96); } else { c.process(evt.keyCode - 48); } } else if ((evt.keyCode == 107 && !evt.shiftKey) || (evt.keyCode == 61 && evt.shiftKey) || (evt.keyCode == 187 && evt.shiftKey)) { c.process('+'); } else if ((evt.keyCode == 109 && !evt.shiftKey) || (evt.keyCode == 189 && !evt.shiftKey)) { c.process('-'); } else if ((evt.keyCode == 106 && !evt.shiftKey) || (evt.keyCode == 56 && evt.shiftKey)) { c.process('*'); } else if ((evt.keyCode == 111 && !evt.shiftKey) || (evt.keyCode == 191 && !evt.shiftKey)) { c.process('/'); } else if (evt.keyCode == 13 || (evt.keyCode == 61 && !evt.shiftKey) || (evt.keyCode == 187 && !evt.shiftKey)) { c.process('='); } else if ((evt.keyCode == 110 && !evt.shiftKey) || (evt.keyCode == 190 && !evt.shiftKey)) { c.process('.'); } else if (evt.keyCode == 27) { c.process('C'); } else if (evt.keyCode == 8) { c.process('Backspace'); } return false; //alert(evt.keyCode); } </script> </head> <body > <table border="0" width="300px"> <tr> <td colspan="5"><input type="text" id="result" value="0" style="text-align:right;" readonly="readonly" /></td> </tr> <tr> <td colspan="2"><input id="Backspace" type="button" value="Backspace" onclick="c.process(this.value)" /></td> <td colspan="2"><input id="CE" type="button" value="CE" onclick="c.process(this.value)" /></td> <td><input type="button" value="C" onclick="c.process(this.value)" /></td> </tr> <tr> <td><input type="button" value="7" onclick="c.process(this.value)" /></td> <td><input type="button" value="8" onclick="c.process(this.value)" /></td> <td><input type="button" value="9" onclick="c.process(this.value)" /></td> <td><input type="button" value="/" onclick="c.process(this.value)" /></td> <td><input type="button" value="sqrt" onclick="c.process(this.value)" /></td> </tr> <tr> <td><input type="button" value="4" onclick="c.process(this.value)" /></td> <td><input type="button" value="5" onclick="c.process(this.value)" /></td> <td><input type="button" value="6" onclick="c.process(this.value)" /></td> <td><input type="button" value="*" onclick="c.process(this.value)" /></td> <td><input type="button" value="%" onclick="c.process(this.value)" /></td> </tr> <tr> <td><input type="button" value="1" onclick="c.process(this.value)" /></td> <td><input type="button" value="2" onclick="c.process(this.value)" /></td> <td><input type="button" value="3" onclick="c.process(this.value)" /></td> <td><input type="button" value="-" onclick="c.process(this.value)" /></td> <td><input type="button" value="1/x" onclick="c.process(this.value)" /></td> </tr> <tr> <td><input type="button" value="0" onclick="c.process(this.value)" /></td> <td><input type="button" value="+/-" onclick="c.process(this.value)" /></td> <td><input type="button" value="." onclick="c.process(this.value)" /></td> <td><input type="button" value="+" onclick="c.process(this.value)" /></td> <td><input type="button" value="=" onclick="c.process(this.value)" /></td> </tr> </table> </body> </html>
相关文章推荐
- Web 基础学习之 用 JS 实现网页版本 计算器
- js版本计算器
- JS和JAVA版本的工作日计算器
- js版本计算器第二版
- 4.2以上版本的sdk使用webview js和java代码交互必须加注解才能生效
- JS根据userAgent值来判断浏览器的类型及版本
- Datatables v1.10服务端模式启动js-java版本
- 一行命令搞定node.js 版本升级
- js 写的计算器
- js高级程序设计(第三版)总结--函数的详解及return的用法
- 用JS判断浏览器的版本
- h5+css3+js 算数运算计算器小程序
- 用JS判断IE版本的代码 超管用!
- 简单的js计算器
- js入门小练习:网页计算器
- js笔记——浏览器及版本判断
- js做的计算器
- 使用jquery版本的viewer.js图片更新的问题
- android webview js交互 调取本地相册(兼容各个版本)并且上传到服务器端。
- 做出的一个图片的自动展示效果(JS版本)