<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<h1>计算器</h1>
<hr/><body>
<form action="" name="myform" method="GET">
数一 :<input type="text" name="num1" size="10.0"/><br/><br/>
数二 :<input type="text" name="num2" size="10.0"/><br/><br/>
结果 :<input type="text" name="res" readonly style="border:10px" size="40"/><br/><br/>
<input type="button" onclick="doFun(1)" value="加"/>
<input type="button" onclick="doFun(2)" value="减"/>
<input type="button" onclick="doFun(3)" value="乘"/>
<input type="button" onclick="doFun(4)" value="除"/>
<input type="button" onclick="doFun(5)" value="十进制转换为二进制"/>
<input type="button" onclick="doFun(6)" value="十进制转换为八进制"/>
<input type="button" onclick="doFun(7)" value="十进制转换为十六进制"/>
<input type="button" onclick="doFun(8)" value="二进制转换为十进制"/>
<input type="button" onclick="doFun(9)" value="八进制转换为十进制"/>
<input type="button" onclick="doFun(10)" value="十六进制转换为十进制"/>
</form>
</body>
<script>
function doFun(c){
var m1 = parseFloat(document.myform.num1.value);
var m2 = parseFloat(document.myform.num2.value);
//console.log(m1,m2);
switch(c){
case 1: var res = m1+"+"+m2+"="+(m1+m2); break;
case 2: var res = m1+"-"+m2+"="+(m1-m2); break;
case 3: var res = m1+"*"+m2+"="+(m1*m2); break;
case 4: var res = m1+"/"+m2+"="+(m1/m2); break;
case 5: var res = m1.toString(2); break;
case 6: var res = m1.toString(8); break;
case 7: var res = m1.toString(16); break;
case 8: var res = parseInt(m1,2); break;
case 9: var res = parseInt(m1,8); break;
case 10: var res = parseInt(m1,16); break;
}
//将结果放置到第三个输入框中
document.myform.res.value = res;
}
</script>
</html>
二进制简易计算器
最新推荐文章于 2022-08-17 15:20:24 发布