javascript实现的简单计算器

 <!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=gb2312" />
<title>无标题文档</title>
<style>
.h {
 font-size: 14px;
 font-weight: normal;
 height: 35px;
 width: 35px;
}
</style>
<script language="javascript" >
var num,num1,flag=true,tr,tag;
function  EnterNum(obj)
{
 if (flag==false)
  { 
   num1=document.form1.show.value+obj.value;
   document.form1.show.value=num1;           
  }
 else
  {num=document.form1.show.value+obj.value;
   document.form1.show.value=num;
   
  } 
}

function Enterop(obj)  //获取符号
{op=obj.value;
 flag=false;
 document.form1.show.value="";
}


function GetResult(obj)  //计算结果
{
 switch(op)
 {case "+":{tr=parseFloat(num)+parseFloat(num1)};break;
  case "-":{tr=parseFloat(num)-parseFloat(num1)};break;
  case "*":{tr=parseFloat(num)*parseFloat(num1)};break;
  case "/":{tr=parseFloat(num)/parseFloat(num1)};break;
 
     }
   num=tr;
  flag=false; 
  document.form1.show.value=tr;
}

function Clear()   //清除
{
   document.form1.show.value="";
  // num1="";
  // num="";
   flag=true;
}

</script>
</head>
<body>
<form id="form1" name="form1" method="post" action="">
  <table width="200" border="1" align="center" cellpadding="0" cellspacing="0" >
    <tr>
      <td height="40" colspan="4" bordercolor="#666666"><label>
       
        <div align="center">
          <input name="show" type="text" id="show" />       
        </div>
      </label></td>
    </tr>
    <tr>
      <td width="51" height="40" bordercolor="#666666"><label>
        <div align="center">
          <input name="b1" type="button" class="h" id="b1" value="1"  οnclick="EnterNum(this)"/>
        </div>
      </label></td>
      <td width="46" height="40" bordercolor="#666666"><div align="center">
        <input name="b2" type="button" class="h" id="b2" value="2" οnclick="EnterNum(this)"/>
      </div></td>
      <td width="48" height="40" bordercolor="#666666"><div align="center">
        <input name="b3" type="button" class="h" id="b3" value="3" οnclick="EnterNum(this)" />
      </div></td>
      <td width="45" height="40" bordercolor="#666666"><div align="center">
        <input name="b4" type="button" class="h" id="b4" value="+"  οnclick="Enterop(this)"/>
      </div></td>
    </tr>
    <tr>
      <td height="40" bordercolor="#666666"><div align="center">
        <input name="b5" type="button" class="h" id="b5" value="4" οnclick="EnterNum(this)"/>
      </div></td>
      <td height="40" bordercolor="#666666"><div align="center">
        <input name="b6" type="button" class="h" id="b6" value="5" οnclick="EnterNum(this)" />
      </div></td>
      <td height="40" bordercolor="#666666"><div align="center">
        <input name="b7" type="button" class="h" id="b7" value="6" οnclick="EnterNum(this)"/>
      </div></td>
      <td height="40" bordercolor="#666666"><div align="center">
        <input name="b8" type="button" class="h" id="b8" value="-"  οnclick="Enterop(this)"/>
      </div></td>
    </tr>
    <tr>
      <td height="40" bordercolor="#666666"><div align="center">
        <input name="b9" type="button" class="h" id="b9" value="7" οnclick="EnterNum(this)"/>
      </div></td>
      <td height="40" bordercolor="#666666"><div align="center">
        <input name="b10" type="button" class="h" id="b10" value="8" οnclick="EnterNum(this)"/>
      </div></td>
      <td height="40" bordercolor="#666666"><div align="center">
        <input name="b11" type="button" class="h" id="b11" value="9" οnclick="EnterNum(this)"/>
      </div></td>
      <td height="40" bordercolor="#666666"><div align="center">
        <input name="b12" type="button" class="h" id="b12" value="*"   οnclick="Enterop(this)"/>
      </div></td>
    </tr>
    <tr>
      <td height="40" bordercolor="#666666"><div align="center">
        <input name="b13" type="button" class="h" id="b13" value="C" οnclick="Clear()"/>
      </div></td>
      <td height="40" bordercolor="#666666"><div align="center">
        <input name="b14" type="button" class="h" id="b14" value="0" οnclick="EnterNum(this)"/>
      </div></td>
      <td height="40" bordercolor="#666666"><div align="center">
        <input name="b15" type="button" class="h" id="b15" value="="  οnclick="GetResult(this)"/>
      </div></td>
      <td height="40" bordercolor="#666666"><div align="center">
        <input name="b16" type="button" class="h" id="b16" value="/"   οnclick="Enterop(this)"/>
      </div></td>
    </tr>
  </table>
</form>
</body>
</html>

 

***************************************************************

本代码能实现多个数的连续加减乘除,但必须得第一个算术式得出结果后再接着计算。        **

如:1+2-3,就应先计算不1+2=3,再-3,按“=”得出最后结果。                              **

实现连续计算时不是很方便,此程序也可通过正则表达式来实现,可本人对正则表达式       **

理解不是很清楚,有好的方法希望分享,我待候好消息。。。。。。。。。                      **

***************************************************************

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值