用Javascript写简单计算器

<html>
<head>
<title>
文本相关标记的应用</title>
<style type="text/css">
.bgcolor
{
background-color:#CCCCCC
}

</style>
<script language="javascript">
var total =0;
var flagNew=false;//是否是新增的数字
var operation="";//以id 号来确定用户输入的运算符


//获取用户输入的运算符
function Operation(ope)
{
   var ShowOut=document.getElementById("count").value;
   if(flagNew && operation != "=");
   else
    {
       flagNew = true;
       if ( '+' == operation )
       {
      total = parseFloat(total)  +  parseFloat(ShowOut);
   //alert(total)
    }
       else if ( '-' == operation )
       {
      total -= ShowOut;
    }
       else if ( '/' == operation )
       {
      total /= ShowOut;
    }
       else if ( '*' == operation )
       {
      total *= ShowOut;
     }
       else
       {
      total =ShowOut;
  }
       document.getElementById("count").value = total;
       operation = ope;
   }
}


//获取用户按按钮的值
function GetInValue(num)
{
   if(flagNew)
     {
     document.getElementById("count").value=num;
     flagNew=false;
  }
   else
      {
      if( document.getElementById("count").value==0)
      {
        document.getElementById("count").value=num;
      }
     else
        {
       document.getElementById("count").value +=num;
     }
   }
  //total=document.getElementById("count").value;    
}

//清零设置
function ClearEmpty ()
{
document.getElementById("count").value = "0";
flagNew = true;
}     
</script>

</head>
<body  text="#000000" bgcolor="FFFFFF">
<table width="222" height="40" border="1" align="center" cellpadding="0" cellspacing="0">
  <tr>
    <td colspan="4" width="300" height="16" ><input type="text" id="count" value="" width="300" height="16" align="middle"/>&nbsp;</td>
  </tr>
  <tr>
    <td  ><input type="button" id="7" width="35" height="15" value="7" class="bgcolor" onClick="GetInValue(7)" />&nbsp;</td>
    <td ><input type="button" id="8" value="8" width="35" height="15" class="bgcolor" onClick="GetInValue(8)"/>&nbsp;</td>
    <td ><input type="button" id="9" value="9" width="35" height="15" class="bgcolor" onClick="GetInValue(9)"/>&nbsp;</td>
    <td ><input type="button" id="+" value="+" width="35" height="15" class="bgcolor" onClick="Operation('+')"/>&nbsp;</td>
  </tr>
  <tr>
    <td><input type="button" id="4" value="4" width="35" height="15" class="bgcolor" onClick="GetInValue(4)"/>&nbsp;</td>
    <td><input type="button" id="5" value="5" width="35" height="15" class="bgcolor" onClick="GetInValue(5)"/>&nbsp;</td>
    <td><input type="button" id="6" value="6" width="35" height="15" class="bgcolor" onClick="GetInValue(6)"/>&nbsp;</td>
    <td><input type="button" id="-" value="-" width="35" height="15" class="bgcolor" onClick="Operation('-')"/>&nbsp;</td>
  </tr>
  <tr>
    <td><input type="button" id="1" value="1" width="35" height="15" class="bgcolor" onClick="GetInValue(1)"/>&nbsp;</td>
    <td><input type="button" id="2" value="2" width="35" height="15" class="bgcolor" onClick="GetInValue(2)"/>&nbsp;</td>
    <td><input type="button" id="3" value="3" width="35" height="15" class="bgcolor" onClick="GetInValue(3)"/>&nbsp;</td>
    <td><input type="button" id="*" value="*" width="35" height="15" class="bgcolor" onClick="Operation('*')"/>&nbsp;</td>
  </tr>
  <tr>
    <td><input type="button" id="0" value="0" width="35" height="15" class="bgcolor" onClick="GetInValue(0)"/>&nbsp;</td>
    <td><input type="button" id="c" value="c" width="35" height="15" class="bgcolor" onClick="ClearEmpty () "/>&nbsp;</td>
    <td><input type="button" id="/" value="/" width="35" height="15" class="bgcolor" onClick="Operation('/')"/>&nbsp;</td>
    <td><input type="button" id="=" value="=" width="35" height="15" class="bgcolor" onClick="Operation('=')"/>&nbsp;</td>
  </tr>
</table>


</body>
</html>

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值