日期:2014-05-17  浏览次数:20741 次

jsp制作简单计算器
怎样通过javascript 制作一个 计算器页面,并且实现简单的功能 ...

------解决方案--------------------
HTML code

 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 <html xmlns="http://www.w3.org/1999/xhtml">
 <head>
 <meta http-equiv="Content-Type" content="text/html; charset=gbk" />
 <title>JavaScript计算器</title>
 </head>
 
 
 <script language="JavaScript">
 var baseoper;
 var digitstate;
 var num1;
 var operstate;
 
 function setStartState()
 {
     digitstate="beempty";
     num1=0;
     baseoper="beempty";
     operstate=false;
     
 }
 function add(i)
 {
     if(operstate)
     {
        cal.total.value=i;
        operstate=false;
     }
     else
     {
         if(cal.total.value=="0")
         {
             cal.total.value=i;
             digistate="bein";
         }
         else
         cal.total.value+=i;
     }
 }
 function cleartext()
 {
     cal.total.value="0";
     digitstate="beempty";
     basestate="beempty"
 }
 
 function count()
 {
     if(baseoper!="beempty")
     {
         switch(baseoper)
         {
             case"+":
             num1=parseFloat(num1)+parseFloat(cal.total.value);
             break;
             case"-":
             num1=parseFloat(num1)-parseFloat(cal.total.value);
             break;
             case"*":
             num1=parseFloat(num1)*parseFloat(cal.total.value);
             break;
             case"/":
             num1=parseFloat(num1)/parseFloat(cal.total.value);
             break;
         }
         preoper=true;
         cal.total.value=num1;
     }
     baseoper="beempty";
 }
 function useFunc(func)
 {
     switch(func)
     {
         case"sin":
         cal.total.value=Math.sin(cal.total.value);
         break;
         case"cos":
         cal.total.value=Math.cos(cal.total.value);
         break;
         case"asin":
         cal.total.value=Math.asin(cal.total.value);
         break;
         case"acos":
         cal.total.value=Math.acos(cal.total.value);
         break;
         case"tan":
         cal.total.value=Math.tan(cal.total.value);
         break;
         case"atan":
         cal.total.value=Math.atan(cal.total.value);
         break;
         case"log":
         cal.total.value=Math.log(cal.total.value);
         break;
         case"exp":
         cal.total.value=Math.exp(cal.total.value);
         break;
         case"sqrt":
         cal.total.value=Math.sqrt(cal.total.value);
         break;
         case"1/x":
         cal.total.value=1/cal.total.value;
         break;
     }
     operstate=true;
 }
 function addPoint()
 {
     if(digitstate=="bein"||digitstate=="beempty")
     {
         cal.total.value+=".";
         digistate="befloat";
     }
 }
 function setOper(oper)
 {
     operstate=true;
     if(baseoper=="beempty")
     {
      num1=0+cal.total.value;
      baseoper=oper;
     }
     else
     {
       count();
       baseoper=oper;
     }
 }
         
         </script>
 <body onload="setStartState()">
 <font size=6> JavaScript计算器</font>
                                                 <!--计算器界面--------计算器界面-->
 <form name="cal">                   
 <table border="5" align="left" bordercolor="#FFCCFF">
 <tr align="center">                         
 <td colspan="6">
 <input type="Text" name="total" size="25" value="0">
 </td>
 </tr>
 
 <tr align="ce