js版本计算器

来源:互联网 发布:mac里的照片导出到u盘 编辑:程序博客网 时间:2024/05/18 01:34
<!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.prevNum = '0';this.operator = '';this.clear = false;this.process = function (element) {switch (element.value) {case '.' :case '0' :case '1' :case '2' :case '3' :case '4' :case '5' :case '6' :case '7' :case '8' :case '9' :this.setNum(element.value);break;case '+/-' :this.setSymbol();break;case 'CE' :case 'C' :this.clearResult();break;case 'Backspace' : this.deleleOne();break;case '+' :case '-' :case '*' :case '/' :this.setOperator(element.value);break;case '=' :this.calculator();break;}}// 设置数字this.setNum = function(num) {var result = document.getElementById("result");var pointIndex = result.value.indexOf('.');if (pointIndex >= 0 && num == '.') {// 己经包含小数点,不再添加return;}var maxLength = 32;if (pointIndex) {// 包含小数点时,最大位数为33maxLength = 33;}// 超出最大位数限制if (result.value.length >= maxLength) {return;}/*if (result.value == '0' && num != '.') {// 输入的数非小数,去掉前面的0result.value = num;} else {result.value = result.value + num;}*/alert('operator: ' + this.operator.length + " clear: " + this.clear);if (this.operator != "" && this.clear) {this.prevNum = result.value;result.value = '';this.clear = false;}if (result.value == '0' && num != '.') {// 输入的数非小数,去掉前面的0result.value = num;} else {result.value = result.value + num;}}// 设置符号位this.setSymbol = function() {var result = document.getElementById("result");var num = new Number(result.value);var tmp = -num.valueOf();result.value = tmp;}// 清除结果,恢复为0this.clearResult = function() {var result = document.getElementById("result");result.value = 0;this.prevNum = '';this.operator = '';}// 删除一位数this.deleteOne = function() {if (result.value.length > 1) {result.value =  result.value.substr(0, result.value.length - 1);} else {result.value = 0;}}this.setOperator = function(operator) {this.operator = operator;alert(this.operator);//this.prevNum = document.getElementById("result").value;this.clear = true;this.calculator();}// 计算this.calculator = function() {var result = document.getElementById("result");var currentNum = result.value;if (this.prevNum.length && currentNum.length) {var data = 0;switch (this.operator) {case '+' :var firstNum = new Number(this.prevNum).valueOf();var secondNum = new Number(currentNum).valueOf();data = firstNum + secondNum;break;case '-' :data = this.prevNum - currentNum;break;case '*' :data = this.prevNum * currentNum;break;case '/' :data = this.prevNum / currentNum;break;}result.value = data;this.prevNum = data;this.operator = '';} else {//result.value = this.prevNum;}}}var c = new Calculator();  </script> </head><body ><table border="0" width="300px">   <tr><td colspan="5"><input type="text" id="result" value="0" style="text-align:right;" /></td></tr><tr><td colspan="2"><input id="Backspace" type="button" value="Backspace" onclick="c.process(this)" /></td><td colspan="2"><input id="CE" type="button" value="CE" onclick="c.process(this)" /></td><td><input type="button" value="C" onclick="c.process(this)" /></td></tr><tr><td><input type="button" value="7" onclick="c.process(this)" /></td><td><input type="button" value="8" onclick="c.process(this)" /></td><td><input type="button" value="9" onclick="c.process(this)" /></td><td><input type="button" value="/" onclick="c.process(this)" /></td><td><input type="button" value="sqrt" onclick="c.process(this)" /></td></tr><tr><td><input type="button" value="4" onclick="c.process(this)" /></td><td><input type="button" value="5" onclick="c.process(this)" /></td><td><input type="button" value="6" onclick="c.process(this)" /></td><td><input type="button" value="*" onclick="c.process(this)" /></td><td><input type="button" value="%" onclick="c.process(this)" /></td></tr><tr><td><input type="button" value="1" onclick="c.process(this)" /></td><td><input type="button" value="2" onclick="c.process(this)" /></td><td><input type="button" value="3" onclick="c.process(this)" /></td><td><input type="button" value="-" onclick="c.process(this)" /></td><td><input type="button" value="1/x" onclick="c.process(this)" /></td></tr><tr><td><input type="button" value="0" onclick="c.process(this)" /></td><td><input type="button" value="+/-" onclick="c.process(this)" /></td><td><input type="button" value="." onclick="c.process(this)" /></td><td><input type="button" value="+" onclick="c.process(this)" /></td><td><input type="button" value="=" onclick="c.process(this)" /></td></tr></table></body></html>
未完成
原创粉丝点击