11求一个简单 的js 代码 写的计算器 有加减乘除等号 还有mc mr ms m+m-就行了
2个回答
2013-07-08
展开全部
<HTML>
<HEAD>
<TITLE></TITLE>
<style type="text/css">
p {font-size: 12pt}
.red {color: red;width:34}
.redl{color:red;width:51}
.blue {color: blue;width:34}
</style>
<SCRIPT LANGUAGE="JavaScript">
var Memory = 0;
var Number1 = "";
var Number2 = "";
var NewNumber = "blank";
var opvalue = "";
function Display(displaynumber) {
document.calculator.answer.value = displaynumber;
}
function MemoryClear() {
Memory = 0;
document.calculator.mem.value = "";
}
function MemoryRecall(answer) {
if(NewNumber != "blank") {
Number2 += answer;
} else {
Number1 = answer;
}
NewNumber = "blank";
Display(answer);
}
function MemorySubtract(answer) {
Memory = Memory - eval(answer);
}
function MemoryAdd(answer) {
Memory = Memory + eval(answer);
document.calculator.mem.value = " M ";
NewNumber = "blank";
}
function ClearCalc() {
Number1 = "";
Number2 = "";
NewNumber = "blank";
Display("");
}
function Backspace(answer) {
answerlength = answer.length;
answer = answer.substring(0, answerlength - 1);
if (Number2 != "") {
Number2 = answer.toString();
Display(Number2);
} else {
Number1 = answer.toString();
Display(Number1);
}
}
function CECalc() {
Number2 = "";
NewNumber = "yes";
Display("");
}
function CheckNumber(answer) {
if(answer == ".") {
Number = document.calculator.answer.value;
if(Number.indexOf(".") != -1) {
answer = "";
}
}
if(NewNumber == "yes") {
Number2 += answer;
Display(Number2);
}
else {
if(NewNumber == "blank") {
Number1 = answer;
Number2 = "";
NewNumber = "no";
}
else {
Number1 += answer;
}
Display(Number1);
}
}
function AddButton(x) {
if(x == 1) EqualButton();
if(Number2 != "") {
Number1 = parseFloat(Number1) + parseFloat(Number2);
}
NewNumber = "yes";
opvalue = '+';
Display(Number1);
}
function SubButton(x) {
if(x == 1) EqualButton();
if(Number2 != "") {
Number1 = parseFloat(Number1) - parseFloat(Number2);
}
NewNumber = "yes";
opvalue = '-';
Display(Number1);
}
function MultButton(x) {
if(x == 1) EqualButton();
if(Number2 != "") {
Number1 = parseFloat(Number1) * parseFloat(Number2);
}
NewNumber = "yes";
opvalue = '*';
Display(Number1);
}
function DivButton(x) {
if(x == 1) EqualButton();
if(Number2 != "") {
Number1 = parseFloat(Number1) / parseFloat(Number2);
}
NewNumber = "yes";
opvalue = '/';
Display(Number1);
}
function SqrtButton() {
Number1 = Math.sqrt(Number1);
NewNumber = "blank";
Display(Number1);
}
function PercentButton() {
if(NewNumber != "blank") {
Number2 *= .01;
NewNumber = "blank";
Display(Number2);
}
}
function RecipButton() {
Number1 = 1/Number1;
NewNumber = "blank";
Display(Number1);
}
function NegateButton() {
Number1 = parseFloat(-Number1);
NewNumber = "no";
Display(Number1);
}
function EqualButton() {
if(opvalue == '+') AddButton(0);
if(opvalue == '-') SubButton(0);
if(opvalue == '*') MultButton(0);
if(opvalue == '/') DivButton(0);
Number2 = "";
opvalue = "";
}
</script>
</HEAD>
<BODY>
<center>
<form name="calculator">
<table bgcolor="#aaaaaa" width=230>
<tr><td>
<table bgcolor="#cccccc" border=1>
<tr><td>
<table border=0 cellpadding=0>
<tr><td bgcolor="#000080"></td></tr>
<tr><td>
<table width="100%" border=0>
<tr><td colspan=6><input type="text" readOnly name="answer" size=30 maxlength=30 onChange="CheckNumber(this.value)"></td></tr>
<tr><td colspan=6>
<table border=0 cellpadding=0>
<tr><td>
<input type="text" name="mem" size=3 maxlength=3 readOnly style="background:menu"> <input type="button" name="backspace" class="redl" value="退格" onClick="Backspace(document.calculator.answer.value); return false;"> <input type="button" name="CE" class="redl" value="CE" onClick="CECalc(); return false;"> <input type="reset" name="C" class="redl" value="C" onClick="ClearCalc(); return false;">
</td></tr>
</table>
</td></tr>
<tr><td><input type="button" name="MC" class="red" value=" MC " onClick="MemoryClear(); return false;"></td>
<td><input type="button" name="calc7" class="blue" value=" 7 " onClick="CheckNumber('7'); return false;"></td>
<td><input type="button" name="calc8" class="blue" value=" 8 " onClick="CheckNumber('8'); return false;"></td>
<td><input type="button" name="calc9" class="blue" value=" 9 " onClick="CheckNumber('9'); return false;"></td>
<td><input type="button" name="divide" class="red" value=" / " onClick="DivButton(1); return false;"></td>
<td><input type="button" name="sqrt" class="blue" value="sqrt" onClick="SqrtButton(); return false;"></td></tr>
<tr><td><input type="button" name="MR" class="red" value=" MR " onClick="MemoryRecall(Memory); return false;"></td>
<td><input type="button" name="calc4" class="blue" value=" 4 " onClick="CheckNumber('4'); return false;"></td>
<td><input type="button" name="calc5" class="blue" value=" 5 " onClick="CheckNumber('5'); return false;"></td>
<td><input type="button" name="calc6" class="blue" value=" 6 " onClick="CheckNumber('6'); return false;"></td>
<td><input type="button" name="multiply" class="red" value=" * " onClick="MultButton(1); return false;"></td>
<td><input type="button" name="percent" class="blue" value=" % " onClick="PercentButton(); return false;"></td></tr>
<tr><td><input type="button" name="MS" class="red" value=" MS " onClick="MemorySubtract(document.calculator.answer.value); return false;"></td>
<td><input type="button" name="calc1" class="blue" value=" 1 " onClick="CheckNumber('1'); return false;"></td>
<td><input type="button" name="calc2" class="blue" value=" 2 " onClick="CheckNumber('2'); return false;"></td>
<td><input type="button" name="calc3" class="blue" value=" 3 " onClick="CheckNumber('3'); return false;"></td>
<td><input type="button" name="minus" class="red" value=" - " onClick="SubButton(1); return false;"></td>
<td><input type="button" name="recip" class="blue" value="1/x " onClick="RecipButton(); return false;"></td></tr>
<tr><td><input type="button" name="Mplus" class="red" value=" M+ " onClick="MemoryAdd(document.calculator.answer.value); return false;"></td>
<td><input type="button" name="calc0" class="blue" value=" 0 " onClick="CheckNumber('0'); return false;"></td>
<td><input type="button" name="negate" class="blue" value="+/- " onClick="NegateButton(); return false;"></td>
<td><input type="button" name="dot" class="blue" value=" . " onClick="CheckNumber('.'); return false;"></td>
<td><input type="button" name="plus" class="red" value=" + " onClick="AddButton(1); return false;"></td>
<td><input type="button" name="equal" class="red" value=" = " onClick="EqualButton(); return false;"></td>
</tr>
</table>
</td></tr>
</table>
</td></tr>
</table>
</td></tr>
</table>
</form>
</center>
<script language=javascript>
function keyDown()
{
var iCode=window.event.keyCode;
//alert(iCode);
if(event.shiftKey)
{
switch(iCode)
{
case 56:document.all.multiply.click();break;
case 59:document.all.minus.click();break;
case 60:document.all.plus.click();break;
}
}
if(!event.shiftKey&&!event.ctrlKey&&!event.altKey)
{
switch (iCode)
{
case 8:document.all.backspace.click();break;
case 187:document.all.equal.click();break;
case 96:document.all.calc0.click();break;
case 97:document.all.calc1.click();break;
case 98:document.all.calc2.click();break;
case 99:document.all.calc3.click();break;
case 100:document.all.calc4.click();break;
case 101:document.all.calc5.click();break;
case 102:document.all.calc6.click();break;
case 103:document.all.calc7.click();break;
case 104:document.all.calc8.click();break;
case 105:document.all.calc9.click();break;
case 110:document.all.dot.click();break;
case 13:document.all.equal.click();break;
case 107:document.all.plus.click();break;
case 109:document.all.minus.click();break;
case 106:document.all.multiply.click();break;
case 111:document.all.divide.click();break;
case 48:document.all.calc0.click();break;
case 49:document.all.calc1.click();break;
case 50:document.all.calc2.click();break;
case 51:document.all.calc3.click();break;
case 52:document.all.calc4.click();break;
case 53:document.all.calc5.click();break;
case 54:document.all.calc6.click();break;
case 55:document.all.calc7.click();break;
case 56:document.all.calc8.click();break;
case 57:document.all.calc9.click();break;
case 190:document.all.dot.click();break;
case 13:document.all.equal.click();break;
case 191:document.all.divide.click();break;
}
}
//event.keyCode=0;
//event.returnValue=false;
}
window.document.attachEvent("onkeydown",keyDown)
</script>
</BODY>
</HTML>
http://www.chinadds.com
<HEAD>
<TITLE></TITLE>
<style type="text/css">
p {font-size: 12pt}
.red {color: red;width:34}
.redl{color:red;width:51}
.blue {color: blue;width:34}
</style>
<SCRIPT LANGUAGE="JavaScript">
var Memory = 0;
var Number1 = "";
var Number2 = "";
var NewNumber = "blank";
var opvalue = "";
function Display(displaynumber) {
document.calculator.answer.value = displaynumber;
}
function MemoryClear() {
Memory = 0;
document.calculator.mem.value = "";
}
function MemoryRecall(answer) {
if(NewNumber != "blank") {
Number2 += answer;
} else {
Number1 = answer;
}
NewNumber = "blank";
Display(answer);
}
function MemorySubtract(answer) {
Memory = Memory - eval(answer);
}
function MemoryAdd(answer) {
Memory = Memory + eval(answer);
document.calculator.mem.value = " M ";
NewNumber = "blank";
}
function ClearCalc() {
Number1 = "";
Number2 = "";
NewNumber = "blank";
Display("");
}
function Backspace(answer) {
answerlength = answer.length;
answer = answer.substring(0, answerlength - 1);
if (Number2 != "") {
Number2 = answer.toString();
Display(Number2);
} else {
Number1 = answer.toString();
Display(Number1);
}
}
function CECalc() {
Number2 = "";
NewNumber = "yes";
Display("");
}
function CheckNumber(answer) {
if(answer == ".") {
Number = document.calculator.answer.value;
if(Number.indexOf(".") != -1) {
answer = "";
}
}
if(NewNumber == "yes") {
Number2 += answer;
Display(Number2);
}
else {
if(NewNumber == "blank") {
Number1 = answer;
Number2 = "";
NewNumber = "no";
}
else {
Number1 += answer;
}
Display(Number1);
}
}
function AddButton(x) {
if(x == 1) EqualButton();
if(Number2 != "") {
Number1 = parseFloat(Number1) + parseFloat(Number2);
}
NewNumber = "yes";
opvalue = '+';
Display(Number1);
}
function SubButton(x) {
if(x == 1) EqualButton();
if(Number2 != "") {
Number1 = parseFloat(Number1) - parseFloat(Number2);
}
NewNumber = "yes";
opvalue = '-';
Display(Number1);
}
function MultButton(x) {
if(x == 1) EqualButton();
if(Number2 != "") {
Number1 = parseFloat(Number1) * parseFloat(Number2);
}
NewNumber = "yes";
opvalue = '*';
Display(Number1);
}
function DivButton(x) {
if(x == 1) EqualButton();
if(Number2 != "") {
Number1 = parseFloat(Number1) / parseFloat(Number2);
}
NewNumber = "yes";
opvalue = '/';
Display(Number1);
}
function SqrtButton() {
Number1 = Math.sqrt(Number1);
NewNumber = "blank";
Display(Number1);
}
function PercentButton() {
if(NewNumber != "blank") {
Number2 *= .01;
NewNumber = "blank";
Display(Number2);
}
}
function RecipButton() {
Number1 = 1/Number1;
NewNumber = "blank";
Display(Number1);
}
function NegateButton() {
Number1 = parseFloat(-Number1);
NewNumber = "no";
Display(Number1);
}
function EqualButton() {
if(opvalue == '+') AddButton(0);
if(opvalue == '-') SubButton(0);
if(opvalue == '*') MultButton(0);
if(opvalue == '/') DivButton(0);
Number2 = "";
opvalue = "";
}
</script>
</HEAD>
<BODY>
<center>
<form name="calculator">
<table bgcolor="#aaaaaa" width=230>
<tr><td>
<table bgcolor="#cccccc" border=1>
<tr><td>
<table border=0 cellpadding=0>
<tr><td bgcolor="#000080"></td></tr>
<tr><td>
<table width="100%" border=0>
<tr><td colspan=6><input type="text" readOnly name="answer" size=30 maxlength=30 onChange="CheckNumber(this.value)"></td></tr>
<tr><td colspan=6>
<table border=0 cellpadding=0>
<tr><td>
<input type="text" name="mem" size=3 maxlength=3 readOnly style="background:menu"> <input type="button" name="backspace" class="redl" value="退格" onClick="Backspace(document.calculator.answer.value); return false;"> <input type="button" name="CE" class="redl" value="CE" onClick="CECalc(); return false;"> <input type="reset" name="C" class="redl" value="C" onClick="ClearCalc(); return false;">
</td></tr>
</table>
</td></tr>
<tr><td><input type="button" name="MC" class="red" value=" MC " onClick="MemoryClear(); return false;"></td>
<td><input type="button" name="calc7" class="blue" value=" 7 " onClick="CheckNumber('7'); return false;"></td>
<td><input type="button" name="calc8" class="blue" value=" 8 " onClick="CheckNumber('8'); return false;"></td>
<td><input type="button" name="calc9" class="blue" value=" 9 " onClick="CheckNumber('9'); return false;"></td>
<td><input type="button" name="divide" class="red" value=" / " onClick="DivButton(1); return false;"></td>
<td><input type="button" name="sqrt" class="blue" value="sqrt" onClick="SqrtButton(); return false;"></td></tr>
<tr><td><input type="button" name="MR" class="red" value=" MR " onClick="MemoryRecall(Memory); return false;"></td>
<td><input type="button" name="calc4" class="blue" value=" 4 " onClick="CheckNumber('4'); return false;"></td>
<td><input type="button" name="calc5" class="blue" value=" 5 " onClick="CheckNumber('5'); return false;"></td>
<td><input type="button" name="calc6" class="blue" value=" 6 " onClick="CheckNumber('6'); return false;"></td>
<td><input type="button" name="multiply" class="red" value=" * " onClick="MultButton(1); return false;"></td>
<td><input type="button" name="percent" class="blue" value=" % " onClick="PercentButton(); return false;"></td></tr>
<tr><td><input type="button" name="MS" class="red" value=" MS " onClick="MemorySubtract(document.calculator.answer.value); return false;"></td>
<td><input type="button" name="calc1" class="blue" value=" 1 " onClick="CheckNumber('1'); return false;"></td>
<td><input type="button" name="calc2" class="blue" value=" 2 " onClick="CheckNumber('2'); return false;"></td>
<td><input type="button" name="calc3" class="blue" value=" 3 " onClick="CheckNumber('3'); return false;"></td>
<td><input type="button" name="minus" class="red" value=" - " onClick="SubButton(1); return false;"></td>
<td><input type="button" name="recip" class="blue" value="1/x " onClick="RecipButton(); return false;"></td></tr>
<tr><td><input type="button" name="Mplus" class="red" value=" M+ " onClick="MemoryAdd(document.calculator.answer.value); return false;"></td>
<td><input type="button" name="calc0" class="blue" value=" 0 " onClick="CheckNumber('0'); return false;"></td>
<td><input type="button" name="negate" class="blue" value="+/- " onClick="NegateButton(); return false;"></td>
<td><input type="button" name="dot" class="blue" value=" . " onClick="CheckNumber('.'); return false;"></td>
<td><input type="button" name="plus" class="red" value=" + " onClick="AddButton(1); return false;"></td>
<td><input type="button" name="equal" class="red" value=" = " onClick="EqualButton(); return false;"></td>
</tr>
</table>
</td></tr>
</table>
</td></tr>
</table>
</td></tr>
</table>
</form>
</center>
<script language=javascript>
function keyDown()
{
var iCode=window.event.keyCode;
//alert(iCode);
if(event.shiftKey)
{
switch(iCode)
{
case 56:document.all.multiply.click();break;
case 59:document.all.minus.click();break;
case 60:document.all.plus.click();break;
}
}
if(!event.shiftKey&&!event.ctrlKey&&!event.altKey)
{
switch (iCode)
{
case 8:document.all.backspace.click();break;
case 187:document.all.equal.click();break;
case 96:document.all.calc0.click();break;
case 97:document.all.calc1.click();break;
case 98:document.all.calc2.click();break;
case 99:document.all.calc3.click();break;
case 100:document.all.calc4.click();break;
case 101:document.all.calc5.click();break;
case 102:document.all.calc6.click();break;
case 103:document.all.calc7.click();break;
case 104:document.all.calc8.click();break;
case 105:document.all.calc9.click();break;
case 110:document.all.dot.click();break;
case 13:document.all.equal.click();break;
case 107:document.all.plus.click();break;
case 109:document.all.minus.click();break;
case 106:document.all.multiply.click();break;
case 111:document.all.divide.click();break;
case 48:document.all.calc0.click();break;
case 49:document.all.calc1.click();break;
case 50:document.all.calc2.click();break;
case 51:document.all.calc3.click();break;
case 52:document.all.calc4.click();break;
case 53:document.all.calc5.click();break;
case 54:document.all.calc6.click();break;
case 55:document.all.calc7.click();break;
case 56:document.all.calc8.click();break;
case 57:document.all.calc9.click();break;
case 190:document.all.dot.click();break;
case 13:document.all.equal.click();break;
case 191:document.all.divide.click();break;
}
}
//event.keyCode=0;
//event.returnValue=false;
}
window.document.attachEvent("onkeydown",keyDown)
</script>
</BODY>
</HTML>
http://www.chinadds.com
2013-07-08
展开全部
效果图 <!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>
<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>
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询