<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www./TR/html4/loose.dtd"> <html> <head> <title> New Document </title> <meta name="Generator" content="EditPlus"> <meta name="Author" content=""> <meta name="Keywords" content=""> <meta name="Description" content=""> </head> <body> <center> <legend><h1><b> <font size="楷体_GB2312" color="#9900ff" >我的小型计算器:</font></b></h1></legend> <form name="Keypad" method="post" action="" > <table> <b> <table border=3 width=50 height=60 cellpadding=1 cellspacing=5 > <tr bgcolor="#cccccc"> <td colspan=3 align=middle><input name="ReadOut" type="Text" size=24 value="0" width=100%></td> <td><input name="btnClear" type="Button" size=24 value="C" onclick="Clear()"></td> <td><input name="btnClearEntry" type="Button" size=24 value="CE" onclick="ClearEntry()"> </td> </tr> <tr bgcolor="#cccccc"> <td><input name="btnSeven" type="Button" value="7" onclick="NumPressed(7)"></td> <td><input name="btnEight" type="Button" value="8" onclick="NumPressed(8)"></td> <td><input name="btnNine" type="Button" value="9" onclick="NumPressed(9)"></td> <td><input name="btnNeg" type="Button" value="+/-" onclick="Neg()"></td> <td><input name="btnPercent" type="Button" value="%" onclick="Percent()"></td> </tr> <tr bgcolor="#cccccc"> <td><input name="btnFour" type="Button" value="4" onclick="NumPressed(4)"></td> <td><input name="btnFive" type="Button" value="5" onclick="NumPressed(5)"></td> <td><input name="btnSix" type="Button" value="6" onclick="NumPressed(6)"></td> <td align = middle><input name="btnPlus" type="Button" value="+" onclick="Operation('+')"></td> <td align = middle><input name="btnMinus" type="Button" value="-" onclick="Operation('-')"></td> </tr> <tr bgcolor="#cccccc"> <td><input name="btnOne" type="Button" value="1" onclick="NumPressed(1)"></td> <td><input name="btnTow" type="Button" value="2" onclick="NumPressed(2)"></td> <td><input name="btnThree" type="Button" value="3" onclick="NumPressed(3)"></td> <td align = middle><input name="btnMultiply" type="Button" value="*" onclick="Operation('*')"></td> <td align = middle><input name="btnDivide" type="Button" value="/" onclick="Operation('/')"></td> </tr> <tr bgcolor="#cccccc"> <td><input name="btnZero" type="button" value="0" onclick="NumPressed(0)"></td> <td><input name="btnDecimal" type="button" value="." onclick="Decimal()"></td> <td><input name="btnRetreat" type="button" value="<-" onclick="Retreat()"></td> <td></td> <td align = middle><input name="btnEquals" type="Button" value="=" onclick="Operation('=')"></td> </tr> </table> </b> </form> </center> <script language="javascript"> var FKeyPad= document.Keypad; var Accum = 0; var NewNum = false; var PendingOp="Op";//未定义的运算符 PendingOp //数字键 function NumPressed(Num){ if(NewNum){ FKeyPad.ReadOut.value=Num; NewNum=false; } else{ if(FKeyPad.ReadOut.value=="0") FKeyPad.ReadOut.value=Num; else FKeyPad.ReadOut.value+=Num; } } //运算符 function Operation(Op){ var ReadOut=FKeyPad.ReadOut.value if(NewNum&&PendingOp!="="){ FKeyPad.ReadOut.value=Op; } else{ NewNum =true; if(PendingOp=='+'){ Accum += parseFloat(ReadOut);} else if('-'==PendingOp){ Accum -= parseFloat(ReadOut);} else if('*'==PendingOp){ Accum *= parseFloat(ReadOut);} else if('/'==PendingOp){ Accum /= parseFloat(ReadOut);} else Accum = parseFloat(ReadOut); FKeyPad.ReadOut.value=Accum; PendingOp=Op; } }//小数计算 Decimal function Decimal(){ var ReadOut = FKeyPad.ReadOut.value; if(NewNum){ ReadOut="0."; NewNum=false; } else{ if(ReadOut.indexOf(".")==-1){ ReadOut +="."; } FKeyPad.ReadOut.value=ReadOut; } } //清空 function ClearEntry(){ FKeyPad.ReadOut.value="0"; NewNum = true; } function Clear(){ Accum = ""; PendingOp=""; ClearEntry(); } //负数 function Neg(){ FKeyPad.ReadOut.value=parseFloat(FKeyPad.ReadOut.value)*-1; } //百分数 function Percent(){ FKeyPad.ReadOut.value=(parseFloat(FKeyPad.ReadOut.value)/100)*parseFloat(Accum); } //退格 function Retreat(){ var Num = FKeyPad.ReadOut.value; // Num=(Num - Num.substring(FKeyPad.ReadOut.value.length-1))/10; Num = Num.substring(0,Num.length-1); FKeyPad.ReadOut.value=Num; } </script> </body> </html> |
|
来自: 昵称11236445 > 《技术事例》