Java教程

简易计算器

本文主要是介绍简易计算器,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

模仿window自带计数器
1.实现简单的加减乘除

<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>点击版的计算机</title>
</head>
<body>
<form  >
<input type="text" id="t1" style="width: 160px"></input><br>
<input type="text" id="zhang" style="width: 160px" value="0" disabled="disabled"></input><br><br>

<input type="button" value="归零" style="width: 80px" onclick="a(this.value)"></input>
<input type="button" value="退格" style="width: 80px" onclick="tuiGe()"></input><br><br>

<input name="i1" id="i1" type="button" value="1" onclick="z(this.value)"></input>&nbsp;&nbsp;&nbsp;
<input name="i2" id="i2" type="button" value="2" onclick="z(this.value)"></input>&nbsp;&nbsp;&nbsp;
<input name="i3" id="i3" type="button" value="3" onclick="z(this.value)"></input>&nbsp;&nbsp;&nbsp;
<input name="i10" id="i10" type="button" value="+" onclick="f(this.value)"></input><br><br>

<input name="i4" id="i4" type="button" value="4" onclick="z(this.value)"></input>&nbsp;&nbsp;&nbsp;
<input name="i5" id="i5" type="button" value="5" onclick="z(this.value)"></input>&nbsp;&nbsp;&nbsp;
<input name="i6" id="i6" type="button" value="6" onclick="z(this.value)"></input>&nbsp;&nbsp;&nbsp;
<input name="11" id="11" type="button" value="-" onclick="f(this.value)"></input><br><br>

<input name="i7" id="i7" type="button" value="7" onclick="z(this.value)"></input>&nbsp;&nbsp;&nbsp;
<input name="i8" id="i8" type="button" value="8" onclick="z(this.value)"></input>&nbsp;&nbsp;&nbsp;
<input name="i9" id="i9" type="button" value="9" onclick="z(this.value)"></input>&nbsp;&nbsp;&nbsp;
<input name="i12" id="i9" type="button" value="*" onclick="f(this.value)"></input><br><br>

<input name="i13" id="i13" type="button" value="0" onclick="z(this.value)"></input>&nbsp;&nbsp;&nbsp;
<input name="i14" id="i14" type="button" value="." onclick="dianHao(this.value)"></input>&nbsp;&nbsp;&nbsp;
<input name="i15" id="i15" type="button" value="=" onclick="h(this.value)"></input>&nbsp;&nbsp;&nbsp;
<input name="i16" id="i16" type="button" value="/" onclick="f(this.value)"></input><br><br>

<input type="button" value="关于" style="width: 80px" id="19" onclick="guanYu()"></input>
<input type="button" value="退出" style="width: 80px" onclick="window.close()" ></input><br><br>
</form>


<script type="text/javascript"> //变量
var k=0;
var dianhao=0;
var dh="=";
var temp1=0;
var temp2=0;
var fh="";
var fuhao=0;
var b=document.getElementById("t1")
var j=document.getElementById("zhang")


function guanYu(){  //关于
	alert("零度工作室");
}

function dianHao(X){ //点号
	if (dianhao==0) {
		if (k==0) {
			j.value="0.";
		} else if(j.value=="除数不能为零" || j.value=="运算符出错"){
			j.value="0.";
			b.value="";
		
		} else {
			j.value=j.value+X;
		}
		k=1;
		dianhao=1;
	} 

}//大if

function tuiGe(){    //退格
	var temp=j.value;
	if (j.value=="除数不能为零" || j.value=="运算符出错" ) {
		j.value="0";
		b.value="";
		k=0;
	} else {
		j.value=temp.substring(0, temp.length-1);
		
	}
}	

function a(a) {   //归零
	k=0;
    temp1=0;
	temp2=0;
	fh="";
	b.value=""; 
	j.value="0";
	dh="=";
	dianhao=0;
	fuhao==0;
	
}
function z(n){   //0-9
	var i=document.getElementById("i"+n);
    if(k==0 && n==0){
    	
    }else if (k==0 && j.value!="0"){
    	j.value="";
    	j.value=n;
    	k=1;
    	
    }else if(k==0 && j.value!=""){
    		a(n);
           j.value=n;
           k=1;
    } else {
    	 j.value=j.value+n;	
    	 k=1;
    }  
    
   
}

function f(m){   //符号
	if (m=="-" && fuhao==0 && k==0) {
		j.value="-";
		k=1;
	} else {
	run();
	fh=m;
    temp1=j.value; 
     k=0;
     dianhao=0;
     j.value="";
     b.value=temp1+fh;
  }
  if (condition) {
	
} else {

}
}


function h(c){    //等号
     run();
     b.value=temp1+fh+temp2+dh; 
     k=0;
     temp1=0;
 	 temp2=0;
   	 fh="";
 	 dh="=";
 	 fuhao==0;
 }


function run() {   //运算函数
	if (j.value=="") {
		temp2=0;
	} else {
		 temp2=j.value;
	}
	
	switch (fh) {
	case "+":
		j.value=parseFloat(temp1)+parseFloat(temp2);
		break;
	case "-":
		j.value=parseFloat(temp1)-parseFloat(temp2);
		break;
	case "*":
		j.value=parseFloat(temp1)*parseFloat(temp2);
		break;
	case "/":
	if (parseFloat(temp2)=="0") {  //除数不能为零的判断
		j.value="除数不能为零";
		
	
	} else {
		j.value=parseFloat(temp1)/parseFloat(temp2);
	} 
	
	if (parseFloat(temp1)/parseFloat(temp2)*10000000000000000/1!= 0) {
		dh="≈";
	}
		break;
		
	default:     //防止乘号出错
		if (fh=="") {
			break;
		} else {
			j.value="运算符出错";
			k=0;
		    temp1=0;
		 	temp2=0;
		   	fh="";
		 	dh="=";
		 	dianhao=0;
			break;
		}
	}
	
}
</script>
</body>
</html>

                    
这篇关于简易计算器的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!