网页版计算器

用 javascript

<!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>WEB计算器</title>
<!---计算器功能模块-->
<script language="javascript" type="text/javascript" runat="server">

var lingState="beStart"; //当前状态
var curOper="start"; //当前运算符
var num1=0; //初值
var num2=0; //初值

var subOper=false; // 是否刚刚单击过运算符
var upOper=false;//运算符的标签
var tnumber=false;//MR记住显示框的状态
var cM=false;//记住M启动
var xM=false;//记住按M+
var ms=false;//记住是否按MS
var sk=false;

var twonumber=false;
var ss;//保存MS要保存的值
var sb=0;
//数字键触发功能模块
function number(i)
{

if(subOper ) //表示在此之前刚刚单击过运算符或者刚刚单击过等号
{
form.ipt.value=i; //把输入的值赋给文本框显示
subOper=false;//输入数后,点击了符号
lingState="beInteger";
}
else
{ //表示正在进行或开始一个数字的输入

if(form.ipt.value=="0")//当文本显示框为0时,则把刚刚单击的数值赋给文本框
{
form.ipt.value=i;//当符合条件则把单击的数值(this.value)赋予给显示框1
lingState="beInteger";
}
else
form.ipt.value += i;//当文本显示框不为0,则数值累加显示文本框
}
if(curOper!="start")//判断是否单击过运算符
{
num2=form.ipt.value;
}
// alert("初值num1的值"+num1);
// alert("num2的值"+num2);
//lingState="beStart"; //清除当前状态
upOper=true;//记住输入数字 以便下面+号连+运算
tnumber=true;//记住有值
}

/* + - * / */

//结果运算模块
function sum()
{

if (curOper!="start")//判断是否单击按钮(符号为空
{
switch(curOper)
{
case "+": //判断符号为+时执行+运算
num1= parseFloat(num1)+parseFloat(num2); //把第一次输入的值和第二次的值进行运算
break;
case "-": //判断符号为-时执行+运算
num1= parseFloat(num1)-parseFloat(num2);
break;
case "*": //判断符号为*时执行+运算
num1= parseFloat(num1)*parseFloat(num2);
break;
case "/": //判断符号为/时执行+运算
if(num2=="0")
{
alert("除数不能为零");
}
else{
num1= parseFloat(num1)/parseFloat(num2);
}
break;
}
form.ipt.value=num1;//把运算结果赋给显示框
}
subOper=true;//输入数后,点击了符号
//curOper="start"; //清除当前符号状态
lingState="beStart";//清除当前状态
upOper=false;//=运算一次后记住 避免再按+号又进行运算 ( 清除当前符号状态
sk=true;

}
//常规符号运算功能模块
function allfhao(i)
{
subOper=true;//输入数后 输入符号 进行运算
if (curOper=="start")//实现连运算 原理:当运行当前运算符时实现连运算
{
num1=form.ipt.value; //把第一个数赋值给num1
curOper=i; //单击运算符用变量把运算符记住
tnumber=true;
}
else
{
if(upOper)//当upOper为真时则实现连运算
{
sum();//当符合条件时调用结果运算 实现连运算
}
curOper=i;//单击运算符用变量把运算符记住
}
upOper=false;//=运算一次后记住 避免再按+号又进行运算 ( 清除当前符号状态
lingState="beStart";//清除当前状态
// alert("符号num1的值"+num1);
// alert("num2的值"+num2);

}

//小数点功能模块
function point()
{
if(form.ipt.value.indexOf(".")==-1) //判断是否有小数点,如果有就不显示 如果没有那么进行下面的运算
{
if(lingState=="beStart")//如果进行了等号运算 但并没有小数点 但单击了小数点则显示0.几
{
form.ipt.value="0.";//当符合条件则显示框1 显示0.
subOper=false;//输入数后,点击了符号
lingState="beFloat"; //让一个变量记住以输入小数点
}
if(lingState=="beInteger" )//判断是否有数输入,如果有数数输入但不是接这等号运算则显示小数点
{
form.ipt.value+=".";//当符合条件则显示小数点
lingState="beFloat";//用一个变量记住已经输入一个小数点,当下次输入由于值的改变则不能输入,起到只能输入一个小数点的功能
}
}
}

//全部清除功能模块 C CE Backspace
function cleaktext(i)
{
switch(i)
{
case"C"://清除C
form.ipt.value="0"; //清除文本框内的内容
lingState="beStart"; //清除当前状态
curOper="start"; //清除当前符号状态
subOper=false; // 是否刚刚单击过运算符
upOper=false;//运算符的标签
num1=0;
num2=0;
break;
case"CE": //清除CE
form.ipt.value="0"; //清除文本框内的内容
break;
case"Backspace": //推格删除
if(cM=false)//如果启动MR那么不能实现推格功能
{
if(upOper)
{
if (form.ipt.value.length>1)
{
form.ipt.value=form.ipt.value.substring(0,form.ipt.value.length-1); //运用substring取字符串方法将返回一个包含从原始对象中获得的子字符串的 String 对象。 使用 start 和 end 两者的较小值作为子字符串的起始点。
}
else
{
form.ipt.value="0";//一个一个删除
}
}
break;

}
}
}
/* % 1/x sqrt +/- pi */

//全部的特殊符号运算模块
function alltx(i)
{

switch(i)
{
case "%"://%运算
form.ipt.value=form.ipt.value/100;
num2=form.ipt.value;
break;

case "1/x":
if(form.ipt.value=="0"){

form.ipt.value="除数不能为零。";
}
else {
form.ipt.value=1/form.ipt.value;
num2=form.ipt.value;
}
break;
case "sqrt"://开方
form.ipt.value=Math.sqrt(form.ipt.value); //math对象 开方运算
num2=form.ipt.value;
break;
case "+/-"://+/-运算符 负号运算
if(upOper)//当运行了符号则不能按负号
{
form.ipt.value=0-form.ipt.value;
num2=form.ipt.value;
}
else{
form.ipt.value="0";
}
break;
case "pi":
form.ipt.value="3.1415926";
num2=form.ipt.value;
break;
case "sin":
form.ipt.value=Math.sin(form.ipt.value);
num2=form.ipt.value;
break;
case "cos":
form.ipt.value=Math.sin(form.ipt.value);
num2=form.ipt.value;
break;
case "tan":
form.ipt.value=Math.sin(form.ipt.value);
num2=form.ipt.value;
break;
}

}
//全部M系列功能模块
function checkallM(i){

switch(i)
{
case"M+"://启动M系列功能模块
if(form.ipt.value!="0" && ms==false)
{
form.ipt1.value="M";//在显示框2显示M以告知用户以启动M系列功能
sm=form.ipt.value;
}

if(form.ipt1.value=="M" && ms!=false)
{
sm=eval(ss+'+'+form.ipt.value);//MR保存的值提取实现M+功能
}
xM=true; //记住单击过M+ 以便MR操作
break;
case "MS"://启用M系列功能 启动记忆功能(记忆上一次计算结果)
ss=form.ipt.value;// 把显示框结果给SS保存(予以MR提取
if(form.ipt.value=="0")//当显示框1显示结果为0时单击MS时也可以清除显示框2 M功能
{
form.ipt1.value="";//清除显示框2 M
}
if(form.ipt.value!="0")
{
form.ipt1.value="M";
}
ms=true; //记住单击过Ms 以便MR操作 (看是否赋+运算后的值 还是当前值
break;
case "MR"://提取MS保存数值功能
if(xM)//是否单击m+
{
if(form.ipt1.value=="M" )//当启动M功能时 MR才能起到保存提取的功能
{
form.ipt.value=sm;//当条件符合 则把MS保存的值 并实现M+功能
}
else if(subOper || tnumber){//没有启动M功能 则清除显示框
form.ipt.value="0";
}
}
// else{ form.ipt.value=ss;}
break;
case "MC": // 清除M显示框中M系列的功能
form.ipt1.value="";//清空显示框2的 M功能
xM=false;
ms=false;
break;
}
cM=true;
lingState="beStart";//清除当前状态
}

//onLoad="setStart()"
</script>
<style type="text/css" >

#c{
width:320px;
height:270px;
border:#666666 5px groove;

background-color:#ECE9D8

}
#ipts{ margin:5px 5px 3px 5px; text-align:right; width:270px}
#s{ margin:7px 5px 6px 5px; height:200px}
.bt{ width:30px; height:30px; background-color:#ECE9D8; border:solid 1px #C8C6B0}
.bt2{width:75px; height:30px; color:#F00; background-color:#ECE9D8; border:solid 1px #C8C6B0;}
.bt3{width:20px; height:20px; text-align:center;}
.wz{ font-size:12px}

</style>
</head>
<body >

<!---隐藏层-->
<div id="s">
<FORM METHOD=POST ACTION="" name="form">
<div id="c">
<table width="98%" height="268" border="0" align="center" cellpadding="0">
<tr>
<td height="19" colspan="7">
<table width="294" border="0" cellpadding="0" cellspacing="0" >
<tr>
<td class="wz" width="12" height="19"> </td>
<td class="wz" width="47">编辑(E)</td>
<td class="wz" width="49">查看(V)</td>
<td class="wz" width="186">帮助(H)</td>
</tr>
</table></td></tr>
<tr>
<td height="29" colspan="7">
<div id="ipts">
<input name="ipt" type="text" id="ipts" value="0" size="40" maxlength="20" readonly="readonly" />
</div>
</td></tr>
<!-- ipt1 Backspace CE C -->
<tr>
<td height="38" colspan="7" align="center"><table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td width="14%" align="center"><input name="ipt1" type="text" disabled="disabled" class="bt3" size="1" maxlength="0" /></td>
<td width="86%" align="center"><table width="97%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td align="center"><input name="button23" type="button" class="bt2" id="button23" style="color:#00F" value="Backspace" onclick="cleaktext(this.value)"/></td>
<td align="center"><input style="color:#00F" name="button21" type="button" class="bt2" id="button20" value="CE" onclick="cleaktext(this.value)"/></td>
<td align="center"><input style="color:#00F" name="button20" type="button" class="bt2" id="button21" value="C" onclick="cleaktext(this.value)"/></td>
</tr>
</table></td>
</tr>
</table></td>
</tr>
<!-- MC 7 8 9 / sqrt -->
<tr>
<td width="16%" align="center"><table width="99%" height="162" border="0">
<tr>
<td height="35" align="center"><input style="color:#F00" type="button" value="MC" class="bt" onclick="checkallM(this.value)"/></td>
<td width="16%" align="center"><input style="color:#00F" name="button1" type="button" class="bt" id="button1" value="7" onclick="number(this.value)" /></td>
<td width="14%" align="center"><input style="color:#00F" name="button2" type="button" class="bt" id="button2" value="8" onclick="number(this.value)" /></td>
<td width="14%" align="center"><input style="color:#00F" name="button3" type="button" class="bt" id="button3" value="9" onclick="number(this.value)"/></td>
<td width="13%" align="center"><input style="color:#F00" name="button4" type="button" class="bt" id="button4" value="/" onclick="allfhao(this.value)"/></td>
<td width="14%" align="center"><input style="color:#00F" name="button5" type="button" class="bt" id="button5" value="sqrt" onclick="alltx(this.value)"/></td>
<td width="16%" align="center"><input style="color:#00F" name="button24" type="button" class="bt" id="button24" value="sin" onclick="alltx(this.value)"/></td>
</tr>

<!-- MR 4 5 6 * % -->

<tr>
<td width="13%" height="39" align="center"><input style="color:#F00" type="button" value="MR" class="bt" onclick="checkallM(this.value)"/></td>
<td width="16%" align="center"><input style="color:#00F" name="button6" type="button" class="bt" id="button6" value="4" onclick="number(this.value)" /></td>
<td width="14%" align="center"><input style="color:#00F" name="button7" type="button" class="bt" id="button7" value="5" onclick="number(this.value)"/></td>
<td width="14%" align="center"><input style="color:#00F" name="button8" type="button" class="bt" id="button8" value="6" onclick="number(this.value)"/></td>
<td width="13%" align="center"><input style="color:#F00" name="button9" type="button" class="bt" id="button9" value="*" onclick="allfhao(this.value)"/></td>
<td width="14%" align="center"><input style="color:#00F" name="button10" type="button" class="bt" id="button10" value="%" onclick="alltx(this.value)"/></td>
<td width="16%" align="center"><input style="color:#00F" name="button25" type="button" class="bt" id="button25" value="cos" onclick="alltx(this.value)"/></td>
</tr>

<!-- MS 1 2 3 - 1/x -->
<tr>
<td width="13%" height="38" align="center"><input style="color:#F00" type="button" value="MS" class="bt" onclick="checkallM(this.value)" /></td>
<td width="16%" align="center"><input style="color:#00F" name="button11" type="button" class="bt" id="button11" value="1" onclick="number(this.value)"/></td>
<td width="14%" align="center"><input style="color:#00F" name="button12" type="button" class="bt" id="button12" value="2" onclick="number(this.value)"/></td>
<td width="14%" align="center"><input style="color:#00F" name="button13" type="button" class="bt" id="button13" value="3" onclick="number(this.value)"/></td>
<td width="13%" align="center"><input style="color:#F00" name="button14" type="button" class="bt" id="button14" value="-" onclick="allfhao(this.value)"/></td>
<td width="14%" align="center"><input style="color:#00F" name="button15" type="button" class="bt" id="button15" value="1/x" onclick="alltx(this.value)"/></td>
<td width="16%" align="center"><input style="color:#00F" name="button26" type="button" class="bt" id="button26" value="tan" onclick="alltx(this.value)"/></td>
</tr>

<!-- M+ 0 +/- . + = -->
<tr>
<td width="13%" height="38" align="center"><input style="color:#F00" type="button" value="M+" class="bt" onclick="checkallM(this.value)"/></td>
<td width="16%" height="38" align="center"><input style="color:#00F" name="button16" type="button" class="bt" id="button16" value="0" onclick="number(this.value)"/></td>
<td width="14%" height="38" align="center"><input style="color:#00F" name="button17" type="button" class="bt" id="button17" value="+/-" onclick="alltx(this.value)"/></td>
<td width="14%" align="center"><input style="color:#00F" name="button18" type="button" class="bt" id="button18" value="." onclick="point()"/></td>
<td width="13%" align="center"><input style="color:#F00" name="button19" type="button" class="bt" id="button19" value="+" onclick="allfhao(this.value)" /></td>
<td width="14%" align="center"><input style="color:#F00" name="button22" type="button" class="bt" id="button22" value="=" onclick="sum()"/></td>
<td width="16%" align="center"><input style="color:#00F" name="button27" type="button" class="bt" id="button27" value="pi" onclick="alltx(this.value)"/></td>
</tr>

</table>
</td></tr></table>
</div>

</FORM>
</div>
</body>
</html>
温馨提示:答案为网友推荐,仅供参考
第1个回答  2009-10-13
这个问题建议直接搜索,而不是提问。。

http://www.baidu.com/baidu?wd=js+%BC%C6%CB%E3%C6%F7&tn=cnopera
相似回答