JS版计算器

<head id="Head1" runat="server">
    <title>网页计算器</title>    
      </head> 
      <script type="text/javascript">
          var t;
          t = self.setInterval("ShowTime()", 10);
          var Exp = "";
          function ShowTime() {
              var Today = new Date(); //这条语句要放在该函数内,而不能作为全局变量        
              hours = Today.getHours();
              minutes = Today.getMinutes();
              second = Today.getSeconds();
              if (hours < 10) hours = "0" + hours
              if (minutes < 10) minutes = "0" + minutes
              if (second < 10) second = "0" + second
              document.all.time.value = hours + ":" + minutes + ":" + second;
          }
          function MyFun() {
              alert(Exp);
          }


          //获取光标在ID为obj控件的位置,返回下标    
          function getPos(obj) {
              obj.focus();
              var s = document.selection.createRange();
              s.setEndPoint("StartToStart", obj.createTextRange())
              return s.text.length;
          }
          //设置光标位置的函数  
          function setCursor(ctrl, pos) {
              if (ctrl.setSelectionRange) {
                  ctrl.focus();
                  ctrl.setSelectionRange(pos, pos);
              }
              else if (ctrl.createTextRange) {
                  var range = ctrl.createTextRange();
                  range.collapse(true);
                  range.moveEnd('character', pos);
                  range.moveStart('character', pos);
                  range.select();
              }
          }
          //删除表达式中的元素    
          function Delete() {
              var Len = document.all.In.value.length;
              Exp = document.all.In.value;
              var Arr = Exp.split("");
              var pos = getPos(In);
              Arr.splice(pos - 1, 1, "");
              var toStr = "";
              for (var j = 0; j < Arr.length; ++j)
                  toStr += Arr[j];
              Exp = toStr;
              Arr = toStr.split("");
              document.all.In.value = toStr;
              setCursor(In, pos - 1);
          }    //清除表达式    
          function Clearn() {
              document.all.In.value = "";
          }

          function addParentheses(strObj) {
              var Temp = strObj;
              var Arr = Temp.split("");
              for (var i = 0; i < Arr.length; ++i) {
                  if (Arr[i] == "s" || Arr[i] == "c" || Arr[i] == "t" || Arr[i] == "√") {
                      if (Arr[i + 1] != "(") {
                          Arr.splice(i, 1, Arr[i] + "(");
                          var toStr = "";
                          for (var j = 0; j < Arr.length; ++j)
                              toStr += Arr[j];
                          Arr = toStr.split("");
                          var Index = i + 2;
                          if (Arr[Index] != "s" && Arr[Index] != "c" && Arr[Index] != "t" && Arr[Index] != "√") {
                              var Bol = true;
                              while (Bol) {
                                  if (Arr[Index + 1] == "+" || Arr[Index + 1] == "-" || Arr[Index + 1] == "*" || Arr[Index + 1] == "/" || Arr.length <= (Index + 1)) {
                                      Arr.splice(Index, 1, Arr[Index] + ")");
                                      var toStr = "";
                                      for (var l = 0; l < Arr.length; ++l)
                                          toStr += Arr[l];
                                      Arr = toStr.split("");
                                      Bol = false;
                                  }
                                  ++Index;
                              }
                              var S = "";
                              for (var k = 0; k < Arr.length; ++k)
                                  S += Arr[k];
                              S = Arr.toString();
                          }
                          //如果参数是函数                  
                          else {
                              var val = 0;
                              var Bol = true;
                              var hasPar = false;
                              while (Bol) {
                                  if (Arr[Index + 1] == "(") {
                                      hasPar = true;
                                      val++;
                                  }
                                  if (Arr[Index + 1] == ")")
                                      --val;
                                  //没有括号的情况                                
                                  if (((Arr[Index + 1] == "+" || Arr[Index + 1] == "-" || Arr[Index + 1] == "*" || Arr[Index + 1] == "/") && !hasPar) || (Index + 1) >= Arr.length) {
                                      Arr.splice(Index, 1, Arr[Index] + ")");
                                      var toStr = "";
                                      for (var j = 0; j < Arr.length; ++j)
                                          toStr += Arr[j];
                                      Arr = toStr.split("");
                                      Bol = false;
                                  }
                                  //有括号的情况                                
                                  if (val == 0 && hasPar) {
                                      Arr.splice(Index + 1, 1, Arr[Index + 1] + ")");
                                      var toStr = "";
                                      for (var j = 0; j < Arr.length; ++j)
                                          toStr += Arr[j];
                                      Arr = toStr.split("");
                                      Bol = false;
                                  }
                                  ++Index;
                              }
                          }
                      }
                  }

                  var Str = "";
                  for (var m = 0; m < Arr.length; ++m)
                      Str += Arr[m];
                  Exp = Str;

              }
          }

          //在表达式中添加表达式元素    
          function addElement(Ele) {
              var Arr = new Array();

              Exp = document.all.In.value; var Index = getPos(In);
              if (Index >= Exp.length) {
                  Exp += Ele;
              }
              else {
                  Arr = Exp.split("");
                  var Cha = Arr[Index];
                  Arr.splice(Index, 1, Ele + Cha);
                  var Str = "";
                  for (var i = 0; i < Arr.length; ++i)
                      Str += Arr[i];
                  Exp = Str;
              }
              document.all.In.value = Exp;
              setCursor(In, Index + Ele.length);
          }
          function Input1() {
              addElement("1");
          }
          function Input2() {
              addElement("2");
          }
          function Input3() {
              addElement("3");
          }
          function Input4() {
              addElement("4");
          }
          function Input5() {
              addElement("5");
          }
          function Input6() {
              addElement("6");
          }
          function Input7() {
              addElement("7");
          }
          function Input8() {
              addElement("8");
          }
          function Input9() {
              addElement("9");
          }
          function Input0() {
              addElement("0");
          }
          function InputDot() {
              addElement(".");
          }
          function InputSin() {
              addElement("sin");
          }
          function InputCos() {
              addElement("cos");
          }
          function InputTan() {
              addElement("tan");
          }
          function InputSqtr() {
              addElement("√");
          }
          function InputAdd() {
              addElement("+");
          }
          function InputMin() {
              addElement("-");
          }
          function InputMult() {
              addElement("*");
          }
          function InputDiv() {
              addElement("/");
          }
          function InputPI() {
              addElement("3.141592");
          }
          function InputLPar() {
              addElement("(");
          }
          function InputRPar() {
              addElement(")");
          }
          function InputLn() {
              addElement("ln");
          }
          function InputLog() {
              addElement("log");
          }
          function InputEql() {
              Exp = document.all.In.value;
              var Str = "";
              Str = Exp.replace(/sin/g, "s");
              Str = Str.replace(/cos/g, "c");
              Str = Str.replace(/tan/g, "t");
              addParentheses(Str);
              Exp = Exp.replace(/t/g, "Math.tan");
              Exp = Exp.replace(/s/g, "Math.sin");
              Exp = Exp.replace(/c/g, "Math.cos");
              Exp = Exp.replace(/√/g, "Math.sqrt");
              var result = eval(Exp);
              document.all.Restlt.value = result;
          } 
</script> 
<body > 
<table  border="8"  bgcolor="rgb(50,200,100)" align= "center" cellspacing="0" cellpadding="0", width="250pt">  
<tr>
<td>
<label id = "Lab" ></label> 计算器
</td>
</tr>  
</table> 
<table border="1"  bgcolor="rgb(50,200,100)" align= "center" cellspacing="0" cellpadding="0", width="250pt">      
<tr>
<td>
<input id = "In" type = "text" style="width:160px"  />
</td>      
<td>
<input id = "bs" type = "button" value = "删除" style="width:75px" onclick = "Delete();"/>
</td>
</tr>      
</table> 
<table  border="1"  bgcolor="rgb(50,200,100)" align= "center" cellspacing="0" cellpadding="0", width="250pt">      
<tr>
<td>
<input id = "Restlt" type = "text" style="width:110px"  />
</td>      
<td>
<input id = "Clearn" type = "button" value = "清除" style="width:60px" onclick = "Clearn();" />
</td>      
<td>
<input id = "time" type = "text" style="width:60px" />
</td>      
</tr>    
</table> 
<table border="5"  bgcolor="rgb(50,200,100)" align= "center" cellspacing="0" cellpadding="0", width="250pt">      
<tr>
<td>
<input type = "button" id = "N1" style="width:50px" value = "1" onclick = "Input1();"/>
</td>            
<td>
<input type = "button" id = "N2" style="width:50px" value = "2" onclick = "Input2();" />
</td>            
<td>
<input type = "button" id = "N3" style="width:50px" value = "3" onclick = "Input3();" />
</td>            
<td>
<input type = "button" id = "Add" style="width:40px" value = "+" onclick ="InputAdd();" />
</td>            
<td>
<input type = "button" id = "Sub" style="width:40px" value = "-" onclick = "InputMin();"/>
</td>
</tr>      
<tr>
<td>
<input type = "button" id = "N4" style="width:50px" value = "4" onclick = "Input4();"/>
</td>            
<td>
<input type = "button" id = "N5" style="width:50px" value = "5" onclick = "Input5();" />
</td>            
<td>
<input type = "button" id = "N6" style="width:50px" value = "6" onclick = "Input6();" />
</td>            
<td><input type = "button" id = "Times" style="width:40px" value = "*" onclick = "InputMult();" />
</td>            
<td><input type = "button" id = "Divd" style="width:40px" value = "/" onclick = "InputDiv();" />
</td>
</tr>        
<tr>
<td>
<input type = "button" id = "N7" style="width:50px" value = "7" onclick = "Input7();"/>
</td>            
<td>
<input type = "button" id = "N8" style="width:50px" value = "8" onclick = "Input8();" />
</td>            
<td>
<input type = "button" id = "N9" style="width:50px" value = "9" onclick = "Input9();" />
</td>            
<td>
<input type = "button" id = "Sqt" style="width:40px" value = "√" onclick = "InputSqtr();" />
</td>            
<td><input type = "button" id = "sin" style="width:40px" value = "sin" onclick = "InputSin();" />
</td>
</tr>        
<tr>
<td>
<input type = "button" id = "N0" style="width:50px" value = "0" onclick = "Input0();"/>
</td>            
<td>
<input type = "button" id = "Dot" style="width:50px" value = "." onclick = "InputDot();" />
</td>            
<td>
<input type = "button" id = "Eql" style="width:50px" value = "=" onclick = "InputEql();" />
</td>            
<td>
<input type = "button" id = "cos" style="width:40px" value = "cos" onclick = "InputCos();" />
</td>            
<td>
<input type = "button" id = "tan" style="width:40px" value = "tan" onclick = "InputTan();" />
</td>
</tr>          
<tr>
<td>
<input type = "button" id = "PI" style="width:50px" value = "π" onclick = "InputPI();"/>
</td>            
<td>
<input type = "button" id = "L_Par" style="width:50px" value = "(" onclick = "InputLPar();" />
</td>            
<td>
<input type = "button" id = "R_Par" style="width:50px" value = ")" onclick = "InputRPar();" />
</td>            
<td>
<input type = "button" id = "Ln" style="width:40px" value = "ln" onclick = "InputLn();" />
</td>            
<td>
<input type = "button" id = "Log" style="width:40px" value = "log" onclick = "InputLog();" />
</td>
</tr>                  
</table>    
<%--<input id = "Input" type = "button" value = "click" onclick = "MyFun();" />    --%>
<form id="form1" runat="server">          
<div>          
</div>    
</form> 
</body> 
</html>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值