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>
代码实现了计算器的基本功能!
<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>
代码实现了计算器的基本功能!
相关文章推荐
- js实现简易计算器功能
- 简易计算器用js实现的代码
- 用js实现简易计算器(只能实现加减乘除)
- 用js实现简易计算器
- Js与BootStrap实现简易计算器
- js实现简易计算器
- JS_实现一个简易计算器
- 浏览器实现复制内容到剪贴板 -- clipboard.js
- 用JS实现GridView背景颜交替、点击行变色
- JS实现文字放大效果的方法
- js实现数字高亮显示
- AJAX 的简单实例 (JS实现 和JQuery 实现)
- JS实现级联查询
- 关于无限分级(ASP+数据库+JS)的实现代码
- 用js实现IBM100年庆徽标竞猜功能
- ArcGIS JS 学习笔记2 实现仿百度的拖拽画圆
- js 处理form表单整理成数组,结合php 完成排序功能的实现
- JavaScriptCore, WebKit的JS实现(一)
- 原生JS实现美图瀑布流布局赏析
- js实现仿淘宝返回顶部