您的位置:首页 > Web前端 > JavaScript

js实现简易计算器

2010-12-12 11:15 253 查看
代码如下:

<html>
<head>
<title>计算器</title>
</head>
<script language="javascript">
var isNew = false;
var opNum1;
var opNum2;
var opFfu;
var results;
function showNum(str)
{
if(document.getElementById("show").value == 0)
{
document.getElementById("show").value = str;//在文本框中显示数据
}
else
{
if(isNew == false)
{
document.getElementById("show").value += str;
}
else
{
document.getElementById("show").value = str;
isNew = false;
}
}
}

function opFu(op)
{
opNum1 = document.getElementById("show").value;
opFfu = op;
isNew = true;

}

function result()
{ //显示结果
opNum2 = document.getElementById("show").value;
results = eval(opNum1+opFfu+opNum2);
document.getElementById("show").value = results;
}

function dowNum()
{ //求倒数
opNum1 = document.getElementById("show").value;
results = 1/opNum1;
document.getElementById("show").value = results;

}

function sSqrt()
{ //开根号
opNum1 = document.getElementById("show").value;
results = Math.sqrt(opNum1);
document.getElementById("show").value = results;

}

function sSqr()
{ //求平方
opNum1 = document.getElementById("show").value;
results = opNum1 * opNum1;
document.getElementById("show").value = results;
}

function oop()
{ //给数值加负号
var num = document.getElementById("show").value;
results = 0 - num;
document.getElementById("show").value = results;
}

function clearEach()
{ //逐个清除
var a = document.getElementById("show").value.split("");
document.getElementById("show").value = "";
for(var i=0;i<a.length-1;i++)
{
document.getElementById("show").value += a[i];
}
}

function clearAll()
{ //一次性全部清除
document.getElementById("show").value = "";
}
</script>
<body>
<form>
<center>
<h1><font color="red" face="隶书">计算器</font></h1>
<table border="1" bgcolor="lightblue">
<tr>
<td colspan="5">
<input type="text" size="42" id="show" value="0"/>
</td>
</tr>
<tr>
<td><input type="button" value=" CE " onclick="clearAll()"/></td>
<td><input type="button" value=" C " onclick="clearEach()"/></td>
<td colspan="3" align="right"><input type="button" value=" sqrt " onclick="sSqrt()"/></td>
</tr>
<tr>

<td><input type="button" value=" / " onclick="opFu('/')"/></td>
<td><input type="button" value=" * " onclick="opFu('*')"/></td>
<td><input type="button" value=" - " onclick="opFu('-')"/></td>
<td><input type="button" value=" + " onclick="opFu('+')"/></td>
<td><input type="button" value=" sqr " onclick="sSqr()"/></td>
</tr>
<tr>
<td><input type="button" value=" 7 " onclick="showNum(7)"/></td>
<td><input type="button" value=" 8 " onclick="showNum(8)"/></td>
<td><input type="button" value=" 9 " onclick="showNum(9)"/></td>
<td><input type="button" value=" . " onclick="showNum('.')"/></td>
<td><input type="button" value=" % " onclick="opFu('%')"/></td>

</tr>
<tr>
<td><input type="button" value=" 4 " onclick="showNum(4)"/></td>
<td><input type="button" value=" 5 " onclick="showNum(5)"/></td>
<td><input type="button" value=" 6 " onclick="showNum(6)"/></td>
<td><input type="button" value=" +/- " onclick="oop()"/></td>
<td><input type="button" value=" 1/x " onclick="dowNum()"/></td>

</tr>
<tr>
<td><input type="button" value=" 1 " onclick="showNum(1)"/></td>
<td><input type="button" value=" 2 " onclick="showNum(2)"/></td>
<td><input type="button" value=" 3 " onclick="showNum(3)"/></td>
<td><input type="button" value=" 0 " onclick="showNum(0)"/></td>
<td><input type="button" value=" = " onclick="result()"/></td>

</tr>
</table>
</center>
</form>
</body>
</html>

代码实现了计算器的基本功能!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: