JavaScript 写计算器改进版
<html>
<head>
<style>
.short{height:50px;width:55px;float:left;}
</style>
</head>
<body>
<script>
var xianshi="";
var shuzi="";
var huanchun="";
//如果连续输入数字,则累加其值
function num(a){
var pingmu=document.getElementById("pingmu");
xianshi+=a;
pingmu.value=xianshi;
shuzi+=a
}
//如果缓存和数字为空,则第一个输入不能是符号
function fum(b){
if(shuzi==""&&huanchun=="")return;
huanchun+=shuzi+b;
shuzi="";
xianshi="";
}
//求出缓存值,并清空其他值
function sum(){
huanchun+=shuzi;
var pingmu=document.getElementById("pingmu");
huanchun=eval(huanchun);
pingmu.value=huanchun;
shuzi=""
xianshi=""
}
//将屏幕上的值添加-号后,保存到缓存或者数字当中
function sign(){
var pingmu=document.getElementById("pingmu")
var value=pingmu.value;
value="(-("+value+"))"
value=eval(value)
pingmu.value=value;
if(shuzi=="")huanchun=value;
else{shuzi=value;}
}
//倒数
function reciprocal(){
var pingmu=document.getElementById("pingmu")
var value=pingmu.value;
value="1/"+value
value=eval(value);
pingmu.value=value;
if(shuzi=="")huanchun=value;
else{shuzi=value;}
}
//开方
function nsqrt(){
var pingmu=document.getElementById("pingmu")
var value=pingmu.value;
value=eval(value);
value=Math.sqrt(value);
pingmu.value=value;
if(shuzi=="")huanchun=value;
else{shuzi=value;}
}
//小数点
function spot(){
var pingmu=document.getElementById("pingmu");
if(shuzi==""||xianshi==""){shuzi="0";xianshi="0"}
shuzi+=".";
xianshi+=".";
pingmu.value=xianshi;
}
//十进制
function ten(){
var total=0,pows=0
var pingmu=document.getElementById("pingmu");
var value=pingmu.value;
var arr=value.split("");
arr=arr.reverse();
for(i=0;i<arr.length;i++){
arr[i]-=0;
pows=Math.pow(2,i);
total+=arr[i]*pows;
}
pingmu.value=total;
jisuan="";xianshi="";
}
//二进制
function two(){
var pingmu=document.getElementById("pingmu");
var value=pingmu.value;
value-=0;
var arr=new Array();
for(i=0;1;i++){
if(value==0)break;
arr[i]=value%2;
value/=2;
value=parseInt(value);
}
arr=arr.reverse();
str=arr.join("");
pingmu.value=str;
xianshi="";jisuan="";
}
//清空
function cee(){
shuzi="";
xianshi="";
document.getElementById('pingmu').value="";
}
//清除
function ccc(){
shuzi="";
huanchun="";
xianshi="";
document.getElementById('pingmu').value="";
}
//退符
function cbb(){
shuzi=shuzi.substring(0,shuzi.length-1)
xianshi=xianshi.substring(0,xianshi.length-1);
pingmu.value=xianshi;
}
</script>
<input id="pingmu" style="height: 36px; width: 282px; text-align: right;" type="text" /><br /><br />
<button class="short" onclick="cbb()" value="c">←</button>
<button class="short" onclick="cee()" value="c">CE</button>
<button class="short" onclick="ccc()" value="c">C</button>
<button class="short" onclick="ten()" value="x">dec</button>
<button class="short" onclick="two()" value="x">bin</button><br /><br /><br />
<button class="short" onclick="num(this.value)" value="7">7</button>
<button class="short" onclick="num(this.value)" value="8">8</button>
<button class="short" onclick="num(this.value)" value="9">9</button>
<button class="short" onclick="fum(this.value)" value="*">*</button>
<button class="short" onclick="fum(this.value)" value="/">/</button><br /><br /><br />
<button class="short" onclick="num(this.value)" value="4">4</button>
<button class="short" onclick="num(this.value)" value="5">5</button>
<button class="short" onclick="num(this.value)" value="6">6</button>
<button class="short" onclick="fum(this.value)" value="-">-</button>
<button class="short" onclick="fum(this.value)" value="%">%</button><br /><br /><br />
<button class="short" onclick="num(this.value)" value="1">1</button>
<button class="short" onclick="num(this.value)" value="2">2</button>
<button class="short" onclick="num(this.value)" value="3">3</button>
<button class="short" onclick="fum(this.value)" value="+">+</button>
<button class="short" onclick="sum()" value="=">=</button>
<br /><br /><br />
<button class="short" onclick="num(this.value)" value="0">0</button>
<button class="short" onclick="sign()" value="+/-">+/-</button>
<button class="short" onclick="spot()" value=".">.</button>
<button class="short" onclick="reciprocal()" value="1/x">1/x</button>
<button class="short" onclick="nsqrt()" value="sqrt">sqrt</button><br /><br /><br /></p>
</script>
</body>