javascriptで書いた簡易計算機


<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>jisuanqi-ren</title>
<style type="text/css">
.btn{
background-color:#4BF3A7;
height:50px;
width: 60px;
}

.kuang{
height: 50px;
width: 254px;
}

.div1{
background-color:#00FFCE;
width: 260px;
height: 400px;
}

.image{
float: right;
margin-right: 500px;
width:400px;
height: 400px;


}
</style>
</head>
<body>
<div id="div2">
<h1><strong><center>   </center></strong></h1>
<table>
<input id="change" type="text" value="" >//   

<input style="height: 50px;width: 254px; " id="tuige" type="button" value="<-" onclick="tuige()">//   
<tr>//     
<td><input type="button" value="1" onclick="number(this)"></td>
<td><input type="button" value="2" onclick="number(this)"></td>
<td><input type="button" value="3" onclick="number(this)"></td>
<td><input type="button" value="C" id="qingchu" onclick="qingchu()"></td>
</tr>

<tr>
<td><input type="button" value="4" onclick="number(this)"></td>
<td><input type="button" value="5" onclick="number(this)"></td>
<td><input type="button" value="6" onclick="number(this)"></td>
<td><input type="button" value="=" onclick="result()"></td>
</tr>
<tr>
<td><input type="button" value="7" onclick="number(this)"></td>
<td><input type="button" value="8" onclick="number(this)"></td>
<td><input type="button" value="9" onclick="number(this)"></td>
<td><input type="button" value="0" onclick="number(this)"></td>
</tr>

<tr>
<td><input type="button" value="+" onclick="jisuan(this)"></td>
<td><input type="button" value="-" onclick="jisuan(this)"></td>
<td><input type="button" value="*" onclick="jisuan(this)"></td>
<td><input type="button" value="/" onclick="jisuan(this)"></td>
</tr>


</table>
</div>

<script >

//      
function number(num){
var kuang=document.getElementById("change");
var zhengze=/\b=\b/;
var test=zhengze.exec(kuang.value)
//        ,       
if(test=="=")
{
test="";
kuang.value=""
}
kuang.value+=num.value;
}

//    
function tuige(){
var kuang=document.getElementById("change");
kuang.value=kuang.value.substring(0,kuang.value.length-1);
}

//    
function qingchu(){
var kuang=document.getElementById("change");
kuang.value="";
}

//      
function jisuan(fuhao){
var kuang=document.getElementById("change");
fuhao1=fuhao.value;
kuang.value+=fuhao1;
}

//       ,    
function result(){

var kuang=document.getElementById("change");

//       
var zhengze2=/\d\D\d/;
var fuhao2=zhengze2.exec(kuang.value);
if (fuhao2===null) {
alert("error");
}
else{

var result=eval(kuang.value);
kuang.value=kuang.value+"="+result;}
}


</script>
</body>
</html>