JAVASCRIPT:计算器代码

本文介绍了一个使用HTML和JavaScript实现的简易网页计算器。该计算器具备基本的算术运算功能,并支持开方和取反操作。文章详细展示了计算器的界面布局及核心代码逻辑。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

<html>
<head>
<title>我的计算器</title>
<script language="javascript">
var point=true;
var result=0;
var op="";
var cle=true;
var fristNum =true;
var lastNum;
function count(num)
{
 if((fristNum==true)&&(isNaN(num)==false))
 {
  document.counter.screenTxt.value="";
  fristNum=false;
 }
 
 if((num==".")&&(point==true))
 {
  if(document.counter.screenTxt.value.length==0)
   {
    document.counter.screenTxt.value="0";
    point=false;
   }
  else
   {
        document.counter.screenTxt.value=document.counter.screenTxt.value+num;
    point=false;
   }
   lastNum=document.counter.screenTxt.value;
  
 }
 if(isNaN(num)==false)
 {
  if(document.counter.screenTxt.value.length==0)
   document.counter.screenTxt.value=num;
  else
   document.counter.screenTxt.value=document.counter.screenTxt.value+num;
  lastNum=document.counter.screenTxt.value;
 }

 if((isNaN(num)==true)&&(num!=".")&&(num!="C")&&(num!="x")&&(num!="s"))
 {
  point=true;
  fristNum=true;
  
  
  if(num!="=")
  {
   op=num;
   if(cle==true)
   {
    if(lastNum!="")
     result =eval(result+op+lastNum);
    cle=false;
   }
   else
   {       
   result = eval(result+op+lastNum);
   }
   document.counter.screenTxt.value=result;
  }
  else
  {
      if(lastNum!="")
   document.counter.screenTxt.value=eval(result+op+lastNum);
   result=document.counter.screenTxt.value;
   lastNum="";
   op="";
   cle=true;
  }
  
 }
 if(num=="x")
 {
   document.counter.screenTxt.value=-parseFloat(document.counter.screenTxt.value);
   lastNum="";
   result=document.counter.screenTxt.value;
   
 }
 if(num=="s")
 {
  document.counter.screenTxt.value=Math.sqrt(parseFloat(document.counter.screenTxt.value));    //document.counter.screenTxt.value=parseFloat(document.counter.screenTxt.value)*parseFloat(document.counter.screenTxt.value);
  result=document.counter.screenTxt.value;
 }
 if(num=="C")
 {
  document.counter.screenTxt.value="";
  point=true;
  result=0;
  op="";
  cle=true;
  fristNum =true;
  lastNum = 0;
 }
 if(document.counter.screenTxt.value=="NaN")
        document.counter.screenTxt.value="";
  
}
</script>
</head>

<body>
<table  width="32%"  border="2" align="center" cellpadding="2" bordercolor="#333333" cellspacing="0"><form  name="counter" style="text-align:center" dir="rtl">
  <tr>
    <td  height="60px" colspan="5" align="left" valign="top">
      <label>
      <div align="right">
        <input name="screenTxt" type="text"  style=" border-bottom-color:#003333; height:60px; border:ridge; font-size:40px; color:#000000; width:250px; "    maxlength="15" readonly="true" />
      </div>
      </label>      </td>
  </tr>
  <tr>
    <td width="47" height="40">      <div align="center">
      <input name="7" onClick="count('7')" type="button"  style="width:40px;height:40px"  value="7"/>   
    </div></td>
    <td width="47" height="40">        <div align="center">
      <input name="8" type="button" id="8" onClick="count('8')"  style="width:40px;height:40px"  value="8"/>     
    </div></td>
    <td width="47" height="40">      <div align="center">
      <input name="9" onClick="count('9')"  type="button"  style="width:40px;height:40px"  value="9"/>   
    </div></td>
    <td width="44" height="40">      <div align="center">
      <input name="+" onClick="count('+')"  type="button" id="+" style="width:40px;height:40px"  value="+"/>   
    </div></td>
    <td width="49">      <div align="center">
      <input name="C" type="button" onClick="count('C')"  id="C" style="width:40px;height:40px"  value="C"/>   
    </div></td>
  </tr>
  <tr>
    <td width="47" height="40"><div align="center">
      <input name="4" onClick="count('4')"  type="button" id="4" style="width:40px;height:40px"  value="4"/>
    </div></td>
    <td width="47" height="40"><div align="center">
      <input name="5" onClick="count('5')"  type="button" id="5" style="width:40px;height:40px"  value="5"/>
    </div></td>
    <td width="47" height="40"><div align="center">
      <input name="6" onClick="count('6')"  type="button" id="6" style="width:40px;height:40px"  value="6"/>
    </div></td>
    <td width="44" height="40"><div align="center">
      <input name="-" onClick="count('-')"  type="button" id="-" style="width:40px;height:40px"  value="-"/>
    </div></td>
    <td width="49" height="40"><div align="center">
      <input name="*" type="button" onClick="count('*')"  style="width:40px;height:40px"  value="*"/>
    </div></td>
  </tr>
  <tr>
    <td width="47" height="40"><div align="center">
      <input name="1" onClick="count('1')"  type="button"  value="1" style="width:40px;height:40px"/>
    </div></td>
    <td width="47" height="40"><div align="center">
      <input name="2" onClick="count('2')"  type="button"  style="width:40px;height:40px"  value="2"/>
    </div></td>
    <td width="47" height="40"><div align="center">
      <input name="3" onClick="count('3')"  type="button"  style="width:40px;height:40px"  value="3"/>
    </div></td>
    <td width="44" height="40"><div align="center">
      <input name="/" onClick="count('/')"  type="button" id="/" style="width:40px;height:40px"  value="/"/>
    </div></td>
    <td width="49" height="40"><div align="center">
      <input name="sqrt" onClick="count('s')"  type="button" style="width:40px;height:40px"  value="SQRT"/>
    </div></td>
  </tr>
  <tr>
    <td width="47" height="40"><div align="center">
      <input name="0" onClick="count('0')"  type="button"  style="width:40px;height:40px"  value="0"/>
    </div></td>
    <td width="47" height="40"><div align="center">
      <input name="." onClick="count('.')"  type="button"  style="width:40px;height:40px"  value="."/>
    </div></td>
    <td height="40"><div align="center">
      <input name="-(X)" type="button" id="-(X)"  style="width:40px;height:40px" onClick="count('x')"  value="-(X)"/>
    </div></td>
    <td height="40" colspan="2"><div align="center">
      <input name="=" type="button" onClick="count('=')"  style="width:80px;height:40px;"  value="="/>
    </div></td>
    </tr>
  </form>
</table>
</body>
</html>
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值