计算器代码

<html>
<head><title>计算器 var curState;
var curOper;
var num1;
var preOper;
function setStartState()
{
curState="beStart";
num1=0;
curOper="start";
preOper=false;
}
function addNum(i)
{
if(preOper)
{
     cal.total.value=i;
     preOper=false;
}
else
{
     if(cal.total.value=="0")
     {
      cal.total.value=i;
      curState="beInteger";
     }
     else
     cal.total.value+=i;
}
}
function addPoint()
{
if(curState=="beStart" || curState=="beInteger")
{
     cal.total.value+=".";
     curState="beFloat";
}
}
function cleartext()
{
cal.total.value="0";
curState="beStart";
curOper="start";
}
function count()
{
if(curOper!="start")
{
     switch(curOper)
     {
      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;
     }
     cal.total.value=num1;
     preOper=true;
}
curOper="start";
}
function setOper(oper)
{
preOper=true;
if(curOper="start")
{
     /*num1=parseFloat(cal.total.value);*/
       num1=0+cal.total.value;
     curOper=oper;
}
else
{
     count();
     curOper=oper;
}
}
function useFunc(func)
{
switch(func)
{
case "sin":
cal.total.value=Math.sin(cal.total.value);
preOper=true;
break;
case "cos":
cal.total.value=Math.cos(cal.total.value);
preOper=true;
break;
case "asin":
cal.total.value=Math.asin(cal.total.value);
preOper=true;
break;
case "acos":
cal.total.value=Math.acos(cal.total.value);
preOper=true;
break;
case "tan":
cal.total.value=Math.tan(cal.total.value);
preOper=true;
break;
case "atan":
cal.toatl.value=Math.atan(cal.total.value);
preOper=true;
break;
case "log":
cal.total.value=Math.log(cal.total.vlaue);
preOper=true;
break;
case "exp":
cal.total.value=Math.exp(cal.total.value);
preOper=true;
break;
case "sqrt":
cal.total.value=Math.sqrt(cal.total.value);
preOper=true;
break;
case "1/x":
cal.total.value=1/cal.total.value;
preOper=true;
break;
}
}
</script>
<body onLoad="setStartState()">
<div align="center"><font color="#c0c0c0" size="7"><strong>计算器</strong></font></div>
<form name="cal">
<font size="6" color="#002063" >
<table width="300"  align="center" bordercolor="#00ff00" style="height: 213px" >
<tr align="center">
<td height="48" colspan="6">
   <input name="total" type="text" value="0" style="width: 317px; height: 24px"></td>
</tr>
<tr align="center">
<td width="72" height="44">
<input type="button" value="sin" onClick="useFunc('sin')" style="width: 40px; height: 23px" size="18"></td>
<td style="width: 52px">
<input type="button" value="cos" onClick="useFunc('cos')" style="width: 40px; height: 23px" size="18"></td>
<td width="72">
<input type="button" value="1" onClick="addNum('1')" style="width: 50px; height: 32px; color: mediumblue; font-family: 华文新魏;"></td>
<td width="72">
<input type="button" value="2" onClick="addNum('2')" style="width: 50px; height: 32px; color: mediumblue; font-family: 华文新魏;"></td>
<td width="72">
<input type="button" value="3" onClick="addNum('3')" style="width: 50px; height: 32px; color: mediumblue; font-family: 华文新魏;"></td>
<td width="72">
<input type="button" value="+" onClick="setOper('+')" style="width: 50px; height: 32px; color: mediumblue; font-family: 华文新魏;"></td>
</tr>
<tr align="center">
<td height="30">
<input type="button" value="asin" onClick="useFunc('asin')" style="width: 40px; height: 23px" size="18"></td>
<td style="width: 52px">
<input type="button" value="acos" onClick="useFunc('acos')" style="width: 40px; height: 23px" size="18"></td>
<td>
<input type="button" value="4" onClick="addNum('4')" style="width: 50px; height: 32px; color: mediumblue; font-family: 华文新魏;"></td>
<td>
<input type="button" value="5" onClick="addNum('5')" style="width: 50px; height: 32px; color: mediumblue; font-family: 华文新魏;"></td>
<td>
<input type="button" value="6" onClick="addNum('6')" style="width: 50px; height: 32px; color: mediumblue; font-family: 华文新魏;"></td>
<td>
<input type="button" value="-" onClick="setOper('-')" style="width: 50px; height: 32px; color: mediumblue; font-family: 华文新魏;"></td>
</tr>
<tr align="center">
<td height="30">
<input type="button" value="tan" onClick="useFunc('tan')" style="width: 40px; height: 23px" size="18"></td>
<td style="width: 52px">
<input type="button" value="atan" onClick="useFunc('atan')" style="width: 40px; height: 23px" size="18"></td>
<td>
<input type="button" value="7" onClick="addNum('7')" style="width: 50px; height: 32px; color: mediumblue; font-family: 华文新魏;"></td>
<td>
<input type="button" value="8" onClick="addNum('8')" style="width: 50px; height: 31px; color: #6666ff; font-family: 华文新魏;"></td>
<td>
<input type="button" value="9" onClick="addNum('9')" style="width: 50px; height: 32px; color: mediumblue; font-family: 华文新魏;"></td>
<td>
<input type="button" value="*" onClick="setOper('*')" style="width: 50px; height: 32px; color: mediumblue; font-family: 华文新魏;"></td>
</tr>
<tr align="center">
<td height="30">
<input type="button" value="log" onClick="useFunc('log')" style="width: 40px; height: 23px" size="18"></td>
<td style="width: 52px">
<input type="button" value="exp" onClick="useFunc('exp')" style="width: 40px; height: 23px" size="18"></td>
<td>
<input type="button" value="0" onClick="addNum('0')" style="width: 50px; height: 32px; color: mediumblue; font-family: 华文新魏;"></td>
<td>
<input type="button" value="." onClick="addPoint()" style="width: 50px; height: 32px; color: mediumblue; font-family: 华文新魏;"></td>
<td>
<input type="button" value="=" onClick="count()" style="width: 50px; height: 32px; color: mediumblue; font-family: 华文新魏;"></td>
<td>
<input type="button" value="/" onClick="setOper('/')" style="width: 50px; height: 32px; color: mediumblue; font-family: 华文新魏;"></td>
</tr>
<tr align="center" >
<td height="30">
<input type="button" value="sqrt" onClick="useFunc('sqrt')" style="width: 40px; height: 23px" size="18"></td>
<td style="width: 52px">
<input type="button" value="1/x" onClick="useFunc('1/x')" style="width: 40px; height: 23px" size="18"></td>
<td>
<input type="button" value="pi" onClick="cal.total.value=Math.PI" style="width: 40px; height: 23px"></td>
<td>
<input type="button" value="E" onClick="cal.total.value=Math.E" style="width: 40px; height: 23px"></td>
<td colspan="2">
<input type="button" value="清除" onClick="cleartext()" style="width: 40px; height: 23px"></td></tr>
</table></font>
<p align="center"><font size="7"><a href="
http://656707738.qzone.qq.com/"style=text-decoration:none><font color="#FF0000" face="华文新魏">进入我的 QQ 空间</font></a></font> </p>
<p align="center"><font color="#CCFF00" size="6" face="华文行楷"><a href="http://656707738.qzone.qq.com/"style=text-decoration:none><font face="宋体">QQ;656707738</font></a></font></p>
</form>
</body>
</html>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值