简易计算器源码
2015-09-17 19:17
253 查看
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>计算器</title>
<script>
var initData="";
var lastop = "";
var num1 = 0;
var num2 = 0;
function getNum(num){
initData+=num;
showData(initData);}
function getOp(op){
lastop = op;//保存最后点击的符号
//保存第一个数
if(initData!=""){
num1 = parseFloat(initData);
initData = "";
}
}
//计算
function calc(){
//1第一个数:num1
//2符号lastop
//3第二个数
num2 = parseFloat(initData);
//4计算
var result = 0;
initData="";
switch(lastop){
case '+':result = num1 + num2;break;
case '-':result = num1 - num2;break;
case '*':result = num1 * num2;break;
case '/':result = num1 / num2;break;
case '%':result = num1 % num2;
}
//显示result
showData(result);
}
function clearAll(){
initData="";
lastop = "";
num1 = 0;
num2 = 0;
opflag = 0;
showData(initData);
}
function clearError(){
initData = initData.substring(0,initData.length-1);
showData(initData);
}
function back(){
initData = initData.substring(initData.length);
showData(initData);
}
//显示数据
function showData(data){
document.getElementById("result").value = data;
}
</script>
</head>
<body>
<form id="form1" name="form1" method="post" action="">
<table width="467" border="1" align="center">
<tr>
<td height="67" colspan="3" align="center"><label for="shuru"></label>
<input type="text" name="result" id="result" width="180" height="30" align=""/></td>
<td width="91" align="center"><input type="button" name="opc" id="opc" value=" C " onclick="clearAll();"/></td>
<td width="98" align="center"><input type="button" name="opc" id="opc" value=" CE " onclick="clearError();"/></td>
</tr>
<tr>
<td width="75" height="64" align="center"><input type="button" name="num7" id="num7" value=" 7 " onclick="getNum('7');"/></td>
<td width="83" align="center"><input type="button" name="num8" id="num" value=" 8 " onclick="getNum('8');"/></td>
<td width="86" align="center"><input type="button" name="num9" id="num9" value=" 9 " onclick="getNum('9');"/></td>
<td align="center"><input type="button" name="button4" id="button3" value=" +/- " onclick="getOp('+/-');"/></td>
<td align="center"><input type="button" name="button6" id="button5" value=" % " onclick="getOp('%');"/></td>
</tr>
<tr>
<td height="66" align="center"><input type="button" name="num4" id="num4" value=" 4 " onclick="getNum('4');"/></td>
<td align="center"><input type="button" name="num5" id="num5" value=" 5 " onclick="getNum('5');"/></td>
<td align="center"><input type="button" name="num6" id="num6" value=" 6 "onclick="getNum('6');" /></td>
<td align="center"><input type="button" name="button10" id="button9" value=" + " onclick="getOp('+');"/></td>
<td align="center"><input type="button" name="button11" id="button10" value=" - " onclick="getOp('-');"/></td>
</tr>
<tr>
<td height="54" align="center"><input type="button" name="num1" id="num1" value=" 1 " onclick="getNum('1');"/></td>
<td align="center"><input type="button" name="num2" id="num2" value=" 2 "onclick="getNum('2');" /></td>
<td align="center"><input type="button" name="num3" id="num3" value=" 3 "onclick="getNum('3');" /></td>
<td align="center"><input type="button" name="button15" id="button14" value=" * " onclick="getOp('*');"/></td>
<td align="center"><input type="button" name="button16" id="button15" value=" / " onclick="getOp('/');" /></td>
</tr>
<tr>
<td height="49" align="center"><input type="button" name="num10" id="num10" value=" 0 "onclick="getNum('0');" /></td>
<td align="center"><input type="button" name="button18" id="button17" value=" . " onclick="getNum('.');"/></td>
<td> </td>
<td align="center"><input type="button" name="button19" id="button18" value=" = " onclick="calc();"/></td>
<td align="center"><input type="button" name="button20" id="button19" value=" 返回 " onclick="back();"/></td>
</tr>
</table>
</form>
</body>
</html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>计算器</title>
<script>
var initData="";
var lastop = "";
var num1 = 0;
var num2 = 0;
function getNum(num){
initData+=num;
showData(initData);}
function getOp(op){
lastop = op;//保存最后点击的符号
//保存第一个数
if(initData!=""){
num1 = parseFloat(initData);
initData = "";
}
}
//计算
function calc(){
//1第一个数:num1
//2符号lastop
//3第二个数
num2 = parseFloat(initData);
//4计算
var result = 0;
initData="";
switch(lastop){
case '+':result = num1 + num2;break;
case '-':result = num1 - num2;break;
case '*':result = num1 * num2;break;
case '/':result = num1 / num2;break;
case '%':result = num1 % num2;
}
//显示result
showData(result);
}
function clearAll(){
initData="";
lastop = "";
num1 = 0;
num2 = 0;
opflag = 0;
showData(initData);
}
function clearError(){
initData = initData.substring(0,initData.length-1);
showData(initData);
}
function back(){
initData = initData.substring(initData.length);
showData(initData);
}
//显示数据
function showData(data){
document.getElementById("result").value = data;
}
</script>
</head>
<body>
<form id="form1" name="form1" method="post" action="">
<table width="467" border="1" align="center">
<tr>
<td height="67" colspan="3" align="center"><label for="shuru"></label>
<input type="text" name="result" id="result" width="180" height="30" align=""/></td>
<td width="91" align="center"><input type="button" name="opc" id="opc" value=" C " onclick="clearAll();"/></td>
<td width="98" align="center"><input type="button" name="opc" id="opc" value=" CE " onclick="clearError();"/></td>
</tr>
<tr>
<td width="75" height="64" align="center"><input type="button" name="num7" id="num7" value=" 7 " onclick="getNum('7');"/></td>
<td width="83" align="center"><input type="button" name="num8" id="num" value=" 8 " onclick="getNum('8');"/></td>
<td width="86" align="center"><input type="button" name="num9" id="num9" value=" 9 " onclick="getNum('9');"/></td>
<td align="center"><input type="button" name="button4" id="button3" value=" +/- " onclick="getOp('+/-');"/></td>
<td align="center"><input type="button" name="button6" id="button5" value=" % " onclick="getOp('%');"/></td>
</tr>
<tr>
<td height="66" align="center"><input type="button" name="num4" id="num4" value=" 4 " onclick="getNum('4');"/></td>
<td align="center"><input type="button" name="num5" id="num5" value=" 5 " onclick="getNum('5');"/></td>
<td align="center"><input type="button" name="num6" id="num6" value=" 6 "onclick="getNum('6');" /></td>
<td align="center"><input type="button" name="button10" id="button9" value=" + " onclick="getOp('+');"/></td>
<td align="center"><input type="button" name="button11" id="button10" value=" - " onclick="getOp('-');"/></td>
</tr>
<tr>
<td height="54" align="center"><input type="button" name="num1" id="num1" value=" 1 " onclick="getNum('1');"/></td>
<td align="center"><input type="button" name="num2" id="num2" value=" 2 "onclick="getNum('2');" /></td>
<td align="center"><input type="button" name="num3" id="num3" value=" 3 "onclick="getNum('3');" /></td>
<td align="center"><input type="button" name="button15" id="button14" value=" * " onclick="getOp('*');"/></td>
<td align="center"><input type="button" name="button16" id="button15" value=" / " onclick="getOp('/');" /></td>
</tr>
<tr>
<td height="49" align="center"><input type="button" name="num10" id="num10" value=" 0 "onclick="getNum('0');" /></td>
<td align="center"><input type="button" name="button18" id="button17" value=" . " onclick="getNum('.');"/></td>
<td> </td>
<td align="center"><input type="button" name="button19" id="button18" value=" = " onclick="calc();"/></td>
<td align="center"><input type="button" name="button20" id="button19" value=" 返回 " onclick="back();"/></td>
</tr>
</table>
</form>
</body>
</html>
相关文章推荐
- CodeForces 12B - Correct Solution?
- 安卓平台的开发环境的发展演变
- iOS中的动画
- Linux内核工程导论——网络:服务质量与安全性
- 研磨数据结构与算法-01开山
- Index of super-prime - SGU 116(素数+背包)
- leetcode:151Reverse Words in a String java实现
- Android 架构师之路
- boost及其子库pool内存池的使用
- 2016中科院推免
- reason: '-[__NSCFNumber isEqualToString:]: unrecognized selector sent to instance 0xbffffffffffff9a3
- Javascript 继承的六种方法
- CSS3文本
- Maven 项目关于 plexus-utils:jar的错误解决
- 项目三——求集合并集
- 安卓中sqlite数据库的增删改查操作的工具类
- CSAPP读书笔记——程序的机器级表示之栈帧结构
- OpenWrt-LuCI 路由器设置端口映射(RDP & SSH)
- Mysql数据库的内外联查询
- poj 1703