发布网友 发布时间:2022-04-30 02:28
共1个回答
热心网友 时间:2022-06-29 08:00
<html> <head> <style type="text/css"> <!-- table{border:4px double #0078F0; background-repeat:repeat; margin: 8px; padding: 10px; font-family: "隶书"; font-size: 12px; color:#FF0000} .aa{text-align:right} .red {color: red} --> </style> <script language="javascript" type="text/javascript"> <!-- var opt1,opt2,result; var opt=0,newnum=true; function a7(x){ if(x == "."){ number = num.value; if(number.indexOf(".") != -1) { x = ""; } } if(newnum == true){ num.value=""; } if(num.value == "0"){ if(x == "."){ num.value += x; } else num.value=x; } else num.value = num.value+x; newnum=false; } function operation(op){ if(opt != 0)cal(); opt1=num.value; opt=op; newnum=true; } function cal(){ opt2=num.value; switch(opt) { case 1: result=parseFloat(opt1)+parseFloat(opt2); break; case 2: result=parseFloat(opt1)-parseFloat(opt2); break; case 3: result=parseFloat(opt1)*parseFloat(opt2); break; case 4: result=parseFloat(opt1)/parseFloat(opt2); break; default: } num.value=result; newnum=true; //结束一个运算的操作数 opt=opt1=opt2=0; } function clean(){ num.value="0"; opt1=""; opt2=""; opt=0; } function sqr(){ opt1=num.value; if(opt1 >= 0){ result=Math.sqrt(parseFloat(opt1)); num.value=result; } else alert("Error!! 负数 不能开方!"); } function qiu(){ opt1=num.value; if(opt1!= "0") result=1/parseFloat(opt1); else alert("Error!! 0不能作为除数!"); num.value=result; } function qufu(){ opt1=num.value; num.value = parseFloat(-opt1); } function backs(){ opt1=num.value; num.value=opt1.substring(0,opt1.lenght-1); } --> </script> </head> <body> <table align="center" cellspacing="0" cellpadding="0"> <tr><th colspan="5"><font size="+2" color="#FFA6D2" face="华文行楷">计 算 器</font></th></tr> <tr align="center"> <td colspan="4" ><input class="aa" name="num" id="num" type="text" value="" size="25" /></td> <td><input type="button" value="back " name="backs" onClick="backs()"></td> </tr> <tr align="center"> <td><input type="button" name="num7" value=" 7 " onClick="a7(7)" /></td> <td><input type="button" name="num8" value=" 8 " onClick="a7(8)" /></td> <td><input type="button" name="num9" value=" 9 " onClick="a7(9)" /></td> <td><input class="red" type="button" name="chu" value=" / " onClick="operation(4)" /></td> <td><input type="button" name="sqrt" value="sqrt " onClick="sqr()" /></td> </tr> <tr align="center"> <td><input type="button" name="num4" value=" 4 " onClick="a7(4)" /></td> <td><input type="button" name="num5" value=" 5 " onClick="a7(5)" /></td> <td><input type="button" name="num6" value=" 6 " onClick="a7(6)" /></td> <td><input class="red" type="button" name="cheng" value=" * " onClick="operation(3)" /></td> <td><input type="button" name="zhengfu" value=" +/- " onClick="qufu()" /></td> </tr> <tr align="center"> <td><input type="button" name="num1" value=" 1 " onClick="a7(1)" /></td> <td><input type="button" name="num2" value=" 2 " onClick="a7(2)" /></td> <td><input type="button" name="num3" value=" 3 " onClick="a7(3)" /></td> <td><input class="red" type="button" name="jian" value=" - " onClick="operation(2)" /></td> <td><input type="button" name="1/x" value=" 1/x " onClick="qiu()" /></td> </tr> <tr align="center"> <td><input type="button" name="num0" value=" 0 " onClick="a7(0)" /></td> <td><input class="red" type="button" name="deng" value=" = " onClick="cal()" /></td> <td><input class="red"type="reset" name="kong" value=" C " onClick="clean()" /></td> <td><input class="red" type="button" name="jia" value=" + " onClick="operation(1)" /></td> <td><input type="button" name="dian" value=" . " onClick="a7('.')" /></td> </tr> </table> </body> </html>