JS写的计算器_2010开始起航_百度空间

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>计算器</title>
<!-- 计算器 sose 版权所有 version 1.0 2010-04-03 -->
</head>
<body>
<form name="calform" >
<table border="2" align="center" cellpadding="0" cellspacing="0" bordercolor="#666666">
    <tr>
      <td><table border="2" align="center" cellpadding="0" cellspacing="0" bordercolor="#FFFFFF" bgcolor="#99CCCC">
          <tr align="center">
            <td colspan="4" align="center">计算器</td>
          </tr>
          <tr align="center">
            <td colspan="3"><input name="calout" type="text" value="0" style="text-align:right ; background-color:lightgrey" readonly="true"></td>
            <td><input type="button" name="CE" value=" C E " onClick="init()"></td>
          </tr>
          <tr>
            <td><input type="button" name="1" value=" 1 " onClick="press(1)"></td>
            <td><input type="button" name="2" value=" 2 " onClick="press(2)"></td>
            <td><input type="button" name="3" value=" 3 " onClick="press(3)"></td>
            <td><input type="button" name="add" value=" + " onClick="operate('+')" ></td>
          </tr>
          <tr>
            <td><input type="button" name="4" value=" 4 " onClick="press(4)"></td>
            <td><input type="button" name="5" value=" 5 " onClick="press(5)"></td>
            <td><input type="button" name="6" value=" 6 " onClick="press(6)"></td>
            <td><input type="button" name="subtract" value=" - " onClick="operate('-')"></td>
          </tr>
          <tr>
            <td><input type="button" name="7" value=" 7 " onClick="press(7)"></td>
            <td><input type="button" name="8" value=" 8 " onClick="press(8)"></td>
            <td><input type="button" name="9" value=" 9 " onClick="press(9)"></td>
            <td><input type="button" name="multiply" value=" * " onClick="operate('*')"></td>
          </tr>
          <tr>
            <td><input type="button" name="0" value=" 0 " onClick="press(0)"></td>
            <td><input type="button" name="dot" value=" . " onClick="decimal()"></td>
            <td><input type="button" name="=" value=" = " onClick="operate('=')"></td>
            <td><input type="button" name="divide" value=" / " onClick="operate('/')"></td>
          </tr>
        </table></td>
    </tr>
</table>
</form>

<script language="javascript">

var op = "";               //操作符
var calResult = 0;              //计算结果
var flag = true;              //false开始一个新的数据,在按operate之后将其置为false

function press(num){             //输入数据
   if(flag){
    if(document.calform.calout.value == "0"){      //此处若是用==0,则会出错
     document.calform.calout.value = num;
    }else{
     document.calform.calout.value += num;
    }
   }else{
    document.calform.calout.value = num;
    flag = true;
   }
}

function operate(tempop){
   document.calform.dot.disabled = false;
   if(tempop == "=" && calResult!=0 && flag == true){     //是=;{dy}个数不能为0,防止做被除数;按operate之后有输入第二个数
    var secnum = parseFloat(document.calform.calout.value);
    switch(op){
     case '+':
      calResult +=secnum;
      break;
     case '-':
      calResult -=secnum;
      break;
     case '*':
      calResult *=secnum;
      break;
     case '/':
      if(secnum == 0){
       calResult = "被除数不能为零";
      }else{
       calResult /=secnum;
      }
      break;
    }
    document.calform.calout.value = calResult;      //显示计算结果
    flag = false;              //当继续输入数据时开始一个新的输入
   }else{                //不是=,保存当前值和操作符
    if(document.calform.calout.value == "0."){
     calResult = 0;
    }else{
     calResult = parseFloat(document.calform.calout.value); //保存当前值
    }
    op = tempop;             //保存操作符
    flag = false;             //开始一个新的输入
   }
}

function decimal(){
   if(flag){
    if(document.calform.calout.value.indexOf(".") == -1){   //检查是否含有小数点
     document.calform.calout.value += ".";
     secNumFlag = true;
    }
   }else{
    document.calform.calout.value = "0.";
    flag = true;
    secNumFlag = true;
   }
   check();
}

function init(){
   op = "";               //xx操作数
   document.calform.calout.value = 0;         //显示为0,初始状态
   calResult = 0;              //保存{dy}个操作数的变量清零
   flag = true;              //开始输入一个新数
}

function delone(){              //删除一个数字
   var str = document.calform.calout.value;
   if(str.length>1){
    document.calform.calout.value = str.substring(0,str.length-1);
   }else{
    document.calform.calout.value = 0;
   }
   check();
}

function keydowm(){
  
   //alert(event.keyCode);
  
   switch(event.keyCode){
    case 8:
     delone();break;            //删除一位
    case 106:
     operate('*');break;
    case 107:
     operate('+');break;
    case 109:
    case 189:
     operate('-');break;
    case 190:
    case 110:
     decimal();break;
    case 111:
     operate('/');break;
    case 187:
     operate('=');break;
    case 116:
     break;
   
    default :
     if(parseInt(event.keyCode)>47 && parseInt(event.keyCode)<58){
      press(parseInt(event.keyCode)-48);
     }
     break;
   }
}
function check(){               //检查小数点,存在的话就使按钮失效,此检查有点鸡肋
   if(document.calform.calout.value.indexOf('.',0) != -1){
    document.calform.dot.disabled = true;
   }else{
    document.calform.dot.disabled = false;
   }
}
document.onkeydown= keydowm,check;
document.onclick = check;
</script>

</body>
</html>



郑重声明:资讯 【JS写的计算器_2010开始起航_百度空间】由 发布,版权归原作者及其所在单位,其原创性以及文中陈述文字和内容未经(企业库qiyeku.com)证实,请读者仅作参考,并请自行核实相关内容。若本文有侵犯到您的版权, 请你提供相关证明及申请并与我们联系(qiyeku # qq.com)或【在线投诉】,我们审核后将会尽快处理。
—— 相关资讯 ——