html:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>计算器</title>
<link rel="stylesheet" type="text/css" href="./css/css.css">
<script type="text/javascript" src="./js/new_file.js"></script>
</head>
<body>
<div class="borderK">
<input type="text" id="anss" class="ansk" value="" disabled="disabled"/>
<table>
<tr>
<td><button type="button" id="mod" onclick="getbh('%')">%</button></td>
<td><button type="button" id="sqr" onclick="getsqrt()">sqrt</button></td>
<td><button type="button" id="pows" onclick="getpingfang()">X²</button></td>
<td><button type="button" id="1/x" onclick="getdaoshu()">1/x</button></td>
</tr>
<tr>
<td><button type="button" id="CE" onclick="csh1()">CE</button></td>
<td><button type="button" id="C" onclick="csh1()">C</button></td>
<td><button type="button" id="BACK"onclick="Backing()">Back</button></td>
<td><button type="button" id="CHU"onclick="getbh('/')">/</button></td>
</tr>
<tr>
<td><button type="button" id="Seven" class="sz" onclick="inputKey(7)">7</button></td>
<td><button type="button" id="Eight"class="sz"onclick="inputKey(8)">8</button></td>
<td><button type="button" id="Nine"class="sz"onclick="inputKey(9)">9</button></td>
<td><button type="button" id="cheng"class="sz"onclick="getbh('*')">*</button></td>
</tr>
<tr>
<td><button type="button" id="Four"class="sz"onclick="inputKey(4)">4</button></td>
<td><button type="button" id="Five"class="sz"onclick="inputKey(5)">5</button></td>
<td><button type="button" id="Six"class="sz"onclick="inputKey(6)">6</button></td>
<td><button type="button" id="Jian"onclick="getbh('-')">-</button></td>
</tr>
<tr>
<td><button type="button" id="One"class="sz"onclick="inputKey(1)">1</button></td>
<td><button type="button" id="Two"class="sz"onclick="inputKey(2)">2</button></td>
<td><button type="button" id="Three"class="sz"onclick="inputKey(3)">3</button></td>
<td><button type="button" id="Jia"onclick="getbh('+')">+</button></td>
</tr>
<tr>
<td><button type="button" id="Abs" onclick=" getxf()">+-</button></td>
<td><button type="button" id="Zero"class="sz"onclick="inputKey(0)">0</button></td>
<td><button type="button" id="xiaoshu" onclick="setxs()">.</button></td>
<td><button type="button" id="Ans" onclick="getans()">=</button></td>
</tr>
</table>
</div>
</body>
</html>
css:
*{
padding: 0;
margin: 0;
}
.borderK{
height: 367px;
width: 350px;
background-color:grey;
}
.ansk{
height: 50px;
width: 346px;
text-align: right;
font-size: 50px;
}
button{
height: 50px;
width: 70px;
right: 5px;
margin-right: 20px;
font-size: 30px;
}
js
var now=0;
var a=new Array(2);
var bh;
var flag=false;
var s;
var backflag=false;
function csh(){
now=0;
a=new Array(2);
document.getElementById("anss").value=cutZero(document.getElementById("anss").value);
}
function csh1(){
now=0;
flag=false;
backflag=false;
bh=s;
a=new Array(2);
document.getElementById("anss").value="";
}
function inputKey(values){
var v=values+"";
if(typeof(a[now]) == "undefined" || flag==true){
a[now]=v;
flag=false;
}
else a[now]=a[now]+v;
backflag=true;
if(a[now].length>12){
csh();
document.getElementById("anss").value="error";
return;
}
document.getElementById("anss").value=a[now];
}
function getbh(values){
if(typeof(bh) == "undefined"){
now=(now+1)%2;
bh=values;
}
else bh=values;
}
function getans(){
backflag=false;
if(typeof(a[0]) == "undefined" || typeof(a[1]) == "undefined"||typeof(a[0]) == null ||typeof(a[1]) == null)
{
csh();
document.getElementById("anss").value="error";
return ;
}
if(bh=='+')
{
document.getElementById("anss").value=(parseFloat(a[0])*Math.pow(10,12)+parseFloat(a[1])*Math.pow(10,12))/Math.pow(10,12);
var s1=document.getElementById("anss").value;
csh();
now=0;
a[0]=s1;
bh=s;
flag=true;
return ;
}
if(bh=='-')
{
document.getElementById("anss").value=(parseFloat(a[0])*Math.pow(10,12)-parseFloat(a[1])*Math.pow(10,12))/Math.pow(10,12);
var s1=document.getElementById("anss").value;
csh();
now=0;
a[0]=s1;
bh=s;
flag=true;
return ;
}
if(bh=='*')
{
document.getElementById("anss").value=(parseFloat(a[0])*parseFloat(a[1]))*Math.pow(10,12)/Math.pow(10,12);
var s1=document.getElementById("anss").value;
csh();
now=0;
a[0]=s1;
bh=s;
flag=true;
return ;
}
if(bh=='/')
{
document.getElementById("anss").value=parseFloat(a[0])/parseFloat(a[1]);
var s1=document.getElementById("anss").value;
csh();
now=0;
a[0]=s1;
bh=s;
flag=true;
return ;
}
if(bh=='%')
{
document.getElementById("anss").value=parseFloat(a[0])%parseFloat(a[1]);
var s1=document.getElementById("anss").value;
csh();
now=0;
a[0]=s1;
bh=s;
flag=true;
return ;
}
}
function getsqrt(){
backflag=false;
document.getElementById("anss").value=(Math.sqrt(parseFloat((a[0])))+"").substring(0,12)
var s1=document.getElementById("anss").value;
csh();
now=0;
a[0]=s1;
bh=s;
flag=true;
}
function getpingfang(){
backflag=false;
var ss=parseFloat(a[0])*parseFloat(a[0]);
ss=ss+"";
if(ss.length>12){
csh();
document.getElementById("anss").value="error";
return;
}
document.getElementById("anss").value=ss;
csh();
now=0;
a[0]=ss;
bh=s;
flag=true;
}
function getdaoshu(){
backflag=false;
if(a[0])
var ss=1/parseFloat(a[0]);
ss=ss+"";
if(ss.length>12){
csh();
now=0;
a[0]=ss;
bh=s;
flag=true;
document.getElementById("anss").value=ss.substring(0,13);
return;
}
csh();
now=0;
a[0]=ss;
bh=s;
flag=true;
document.getElementById("anss").value=ss;
}
function getxf(){
backflag=false;
var xf=document.getElementById("anss").value;
xf=0-xf;
document.getElementById("anss").value=xf;
}
function Backing(){
if(backflag){
var s1=document.getElementById("anss").value;
s1=s1.substring(0,s1.length-1);
a[now]=s1;
document.getElementById("anss").value=s1;
}
else csh1();
}
function setxs(){
if(!backflag){
csh1();
document.getElementById("anss").value="0.";
a[now]="0."
}
else {
if(document.getElementById("anss").value.indexOf(".")==-1){
document.getElementById("anss").value=document.getElementById("anss").value+".";
a[now]=document.getElementById("anss").value;
}
}
}
function cutZero(old){
//拷贝一份 返回去掉零的新串
newstr=old;
//循环变量 小数部分长度
var leng = old.length-old.indexOf(".")-1
//判断是否有效数
if(old.indexOf(".")>-1){
//循环小数部分
for(i=leng;i>0;i--){
//如果newstr末尾有0
if(newstr.lastIndexOf("0")>-1 && newstr.substr(newstr.length-1,1)==0){
var k = newstr.lastIndexOf("0");
//如果小数点后只有一个0 去掉小数点
if(newstr.charAt(k-1)=="."){
return newstr.substring(0,k-1);
}else{
//否则 去掉一个0
newstr=newstr.substring(0,k);
}
}else{
//如果末尾没有0
return newstr;
}
}
}
return old;
}