一个像样的计算器

想了几天用各种语言编写出一个计算器,今天下午终于在图书馆实现了一下。虽然浪费了一个下午,但是觉得还是值得的.

实现的界面如图所示:

附上原码,大家可以指正一下错误....

    var operator = 0;
	var pressoperator = false;
	var first;
	var second;
	var flag=0;
	var finish= true;
	var point = false;
	function bt1() 
	{
		var temp = document.getElementById("result");
		if(finish){
			temp.value="";
			finish=0;
			}
		if(!pressoperator)
		{
           if(parseFloat(temp.value)>=0 )
           {
           	 temp.value+= "1";
           }
           else 
           	temp.value="1";
		}
		else
		{
			pressoperator =0;
			temp.value="1";
		}
	}
	function bt2() 
	{
		
	    var temp = document.getElementById("result");
	    if(finish){
			temp.value="";
			finish=0;
			}
		if(!pressoperator)
		{
           if(parseFloat(temp.value)>=0)
           {
           	 temp.value+= "2";
           }
           else 
           	temp.value="2";
		}
		else
		{
			temp.value="2";
			pressoperator =0;
		}
	}
	function bt3() 
	{
		
	    var temp = document.getElementById("result");
	    if(finish){
			temp.value="";
			finish=0;
			}
		if(!pressoperator)
		{
           if(parseFloat(temp.value)>=0)
           {
           	 temp.value+= "3";
           }
           else 
           	temp.value="3";
		}
		else
		{
			temp.value="3";
			pressoperator =0;
		}
	}
	function bt4() 
	{
		
	    var temp = document.getElementById("result");
	    if(finish){
			temp.value="";
			finish=0;
			}
		if(!pressoperator)
		{
           if(parseFloat(temp.value)>=0)
           {
           	 temp.value+= "4";
           }
           else 
           	temp.value="4";
		}
		else
		{
			temp.value="4";
			pressoperator =0;
		}
	}
	function bt5() 
	{
		
	    var temp = document.getElementById("result");
	    if(finish){
			temp.value="";
			finish=0;
			}
		if(!pressoperator)
		{
           if(parseFloat(temp.value)>=0)
           {
           	 temp.value+= "5";
           }
           else 
           	temp.value="5";
		}
		else
		{
			temp.value="5";
			pressoperator =0;
		}
	}
	function bt6() 
	{
		
	    var temp = document.getElementById("result");
	    if(finish){
			temp.value="";
			finish=0;
			}
		if(!pressoperator)
		{
           if(parseFloat(temp.value)>=0)
           {
           	 temp.value+= "6";
           }
           else 
           	temp.value="6";
		}
		else
		{
			temp.value="6";
			pressoperator =0;
		}
	}
   function bt7() 
	{
		
	    var temp = document.getElementById("result");
	    if(finish){
			temp.value="";
			finish=0;
			}
		if(!pressoperator)
		{
           if(parseFloat(temp.value)>=0)
           {
           	 temp.value+= "7";
           }
           else 
           	temp.value="7";
		}
		else
		{
			temp.value="7";
			pressoperator =0;
		}
	}
	function bt8() 
	{
		
	    var temp = document.getElementById("result");
	    if(finish){
			temp.value="";
			finish=0;
			}
		if(!pressoperator)
		{
           if(parseFloat(temp.value)>=0)
           {
           	 temp.value+= "8";
           }
           else 
           	temp.value="8";
		}
		else
		{
			temp.value="8";
			pressoperator =0;
		}
	}
	function bt9() 
	{
		
	    var temp = document.getElementById("result");
	    if(finish){
			temp.value="";
			finish=0;
			}
		if(!pressoperator)
		{
           if(parseFloat(temp.value)>=0)
           {
           	 temp.value+= "9";
           }
           else 
           	temp.value="9";
		}
		else
		{
			temp.value="9";
			pressoperator =0;
		}
	}
	function bt0() 
	{
	    var temp = document.getElementById("result");
	    if(finish){
			temp.value="";
			finish=0;
			}
		if(!pressoperator)
		{
           if(parseFloat(temp.value)>=0)
           {
           	 temp.value+= "0";
           }
           else 
           	temp.value="0";
		}
		else
		{
			temp.value="0";
			pressoperator =0;
		}
	}
    function btpoint () {	
    	var temp = document.getElementById("result");
	    if(finish){
			temp.value="0";
			finish=0;
			}
		if(!pressoperator)
		{
           if(parseFloat(temp.value)>=0.0 && !point)
           {
           	 temp.value+= ".";
           }
           else 
           	temp.value="0.";
		}
		else
		{
			temp.value="0";
			pressoperator =0;
		}
    }
	function btadd()
	{
		var temp = document.getElementById("result");
		first =parseFloat(temp.value);
		pressoperator = 1;
		flag=1;
		
	}
	function btadd()
	{
		var temp = document.getElementById("result");
		first =parseFloat(temp.value);
		pressoperator = 1;
		flag=1;
		
	}
	function btsub()
	{
		var temp = document.getElementById("result");
		first =parseFloat(temp.value);
		pressoperator = 1;
		flag=2;
		
	}
	function btmul()
	{
		var temp = document.getElementById("result");
		first =parseFloat(temp.value);
		pressoperator = 1;
		flag=3;
		
	}
	function btdiv()
	{
		var temp = document.getElementById("result");
		first =parseFloat(temp.value);
		pressoperator = 1;
		flag=4;
		
	}
	function btequal()
	{
       var temp = document.getElementById("result");
       second = parseFloat(temp.value);
       switch(flag)
       {
       	case 1:first+=second;break;
       	case 2:first-=second;break;
       	case 3:first*=second;break;
       	case 4:
       	if(second!=0)
       		first/=second;
       	break;
       	case 5:{
       	first = Math.pow(first,second);
       	break;
       	}
       	default:break;
       }
       temp.value = parseFloat(first);
       finish = 1;
	}
	function btsin(){
	   var temp = document.getElementById("result");
	   var tmp = Math.sin(temp.value);
	   temp.value = parseFloat(tmp);
	}
	function btcos(){
	   var temp = document.getElementById("result");
	   var tmp = Math.cos(temp.value);
	   temp.value = parseFloat(tmp);
	}
	function btsqrt(){
	   var temp = document.getElementById("result");
	   var tmp = Math.sqrt(temp.value);
	   temp.value = parseFloat(tmp);
	}
	function btclear () {
		var temp = document.getElementById("result");
		temp.value ="";
	}
	function btxy () {
		var temp = document.getElementById("result");
		first =parseFloat(temp.value);
		pressoperator = 1;
		flag=5;
	}
	

这个是clac.js,下面的一个是clac.html

<html>
<head>
	<title>
		计算器
	</title>
	<script type="text/javascript" src="./cacl.js"></script>
    </head>
   <body>
   	<center>
   		<h1>为不买计算器的同学</h1><hr>
   	<input type = "text" placeholder="计算" class = "span1"  style =" font-size:20px; width: 400px;
	  height: 50px;"  id = "result" name = "result" ><br/>
   <input type = "button" class = btn" name = "one"  style ="font-size:20px;  width: 80px;
	  height: 50px;"value = "1" οnclick="bt1()">
   <input type = "button" class = "btn" name = "two" style =" font-size:20px; width: 80px;
	  height: 50px;" value = "2" οnclick="bt2()">
   <input type = "button" class = "btn" name = "three" style ="font-size:20px;  width: 80px;
	  height: 50px;"value = "3" οnclick="bt3()">
   <input type = "button" class = "btn" name = "add"   style ="font-size:20px;  width: 80px;
	  height: 50px;"value = "+" οnclick="btadd()"><br/>
   <input type = "button" class = "btn" name = "four"   style =" font-size:20px; width: 80px;
	  height: 50px;"  value = "4" οnclick="bt4()">
   <input type = "button" class = "btn" name = "five"   style ="font-size:20px;  width: 80px;
	  height: 50px;"  value = "5" οnclick="bt5()">
   <input type = "button" class = "btn" name = "six"    style =" font-size:20px; width: 80px;
	  height: 50px;"  value = "6" οnclick="bt6()">
   <input type = "button" class = "btn" name = "sub"    style ="font-size:20px; width: 80px;
	  height: 50px;"  value = "-" οnclick="btsub()"><br/>
   <input type = "button" class = "btn" name = "seven"  style =" font-size:20px; width: 80px;
	  height: 50px;"  value = "7" οnclick="bt7()">
   <input type = "button" class = "btn" name = "eight"  style ="font-size:20px; width: 80px;
	  height: 50px;"  value = "8" οnclick="bt8()">
   <input type = "button" class = "btn" name = "nine"   style ="font-size:20px;  width: 80px;
	  height: 50px;"  value = "9" οnclick="bt9()">
   <input type = "button" class = "btn" name = "mul"    style =" font-size:20px; width: 80px;
	  height: 50px;"  value = "*" οnclick="btmul()"><br/>
   <input type = "button" class = "btn" name = "zero"   style ="font-size:20px;  width: 80px;
	  height: 50px;"  value = "0" οnclick="bt0()">
   <input type = "button" class = "btn" name = "point"  style =" font-size:20px; width: 80px;
	  height: 50px;"  value = "." οnclick="btpoint()">
   <input type = "button" class = "btn" name = "equal"  style ="font-size:20px;  width: 80px;
	  height: 50px;"  value = "=" οnclick="btequal()">
   <input type = "button" class = "btn" name = "div"    style ="font-size:20px;  width: 80px;
	  height: 50px;"  value = "/" οnclick="btdiv()"><br/><br/>
    <input type = "button"class = "btn" name = "sin"    style ="font-size:20px;  width: 80px;
	  height: 50px;"  value = "sin" οnclick="btsin()">
   <input type = "button" class = "btn" name = "cos"    style ="font-size:20px;  width: 80px;
	  height: 50px;"  value = "cos" οnclick="btcos()">
   <input type = "button" class = "btn" name = "√"      style =" font-size:20px; width: 80px;
	  height: 50px;"  value = "√" οnclick="btsqrt()">
   <input type = "button" class = "btn" name = "√"      style =" font-size:20px; width: 80px;
	  height: 50px;"  value = "x^y" οnclick="btxy()"><br/>
  

   <input type = "reset" id = "reset" name = "reset" style =" font-size:20px; width: 400px;
	  height: 50px;"  value = "clear" οnclick="btclear()">
   </center>
    
   	</body>
</html>
就这样,一个很挫的界面就设计好了...

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值