用JavaScript做一个简易的计算器
js做一个简易计算器具体如下:
js运算
第一个数
运算符号
第二个数
计算结果
function js(){
var num1=document.getElementById("onesum").value;
var num2=document.getElementById("twosum").value;
var fh=document.getElementById("fh").value;
var sum=0;
nu
m1=Number(num1);
num2=Number(num2);
if(fh=='+')
{
sum=num1+num2;
}
else if(fh=='-')
{
sum=num1-num2;
}else if(fh=='*')
{
sum=num1*num2;
}else if(fh=='/')
{
sum=num1/num2;
}
//alert(sum);
document.getElementById("sum").value=sum;
}
JavaScript 教程 JavaScript 是属于网络的脚本语言! JavaScript 被数百万计的网页用来改进设计、验证表单、检测浏览器、创建cookies,以及更多的应用。
计算器function Caculate(){var a=parseInt(document.f1.t1.value);var b=parseInt(document.f1.t2.value);var s;for(var i=0;i请输入第一个操作数:请输入第一个操作数:请选择操作+ - * / 结果是:
如果页面乱码,请更改浏览器编码为utf-8!
<html>
<head>
<title>计算器</title>
</head>
<body bgcolor="#33FF99" onload="FKeyPad.ReadOut.focus();FKeyPad.ReadOut.select();">
<FORM name="Keypad" action="">
<TABLE align="center">
<B>
<TABLE align="center" border=3 width=60 height=80 cellpadding=2 cellspacing=5 borderlight="#008080">
<tr align="center">
<td bordercolor="#FF33CC" bordercolordark="#cc0066" bordercolor="#FF9966" bordercolorlight="#CC9933" colspan=10 align=center>网页计算器</td>
</tr>
<tr align="center">
<td bordercolor="#FF33CC" bordercolordark="#cc0066" bordercolor="#FF9966" bordercolorlight="#CC9933" colspan=10 align=right><input name="ReadOut" type="Text" onkeypress="CheckOut()" size=40 value="0"
width=100%></td>
</tr>
<tr align="center">
<td bordercolordark="#cc0066" bordercolor="#FF9966" bordercolorlight="#CC9933"><input name="btnsin" type="Button" value=" sin " onclick="useFun ('sin')"></td>
<td bordercolordark="#cc0066" bordercolor="#FF9966" bordercolorlight="#CC9933"><input name="btncos" type="Button" value=" cos " onclick="useFun ('cos')"></td>
<td bordercolordark="#cc0066" bordercolor="#FF9966" bordercolorlight="#CC9933"><input name="btnOne" type="Button" value=" 1 " onclick="NumPressed(1)"></td>
<td bordercolordark="#cc0066" bordercolor="#FF9966" bordercolorlight="#CC9933"><input name="btnTwo" type="Button" value=" 2 " onclick="NumPressed(2)"></td>
<td bordercolordark="#cc0066" bordercolor="#FF9966" bordercolorlight="#CC9933"><input name="btnThree" type="Button" value=" 3 " onclick="NumPressed(3)"></td>
<td bordercolordark="#cc0066" bordercolor="#FF9966" bordercolorlight="#CC9933"><input name="btnPlus" type="Button" value=" + " onclick="Operation('+')"> </td>
</tr>
<tr align="center">
<td bordercolordark="#cc0066" bordercolor="#FF9966" bordercolorlight="#CC9933"><input name="btnasin" type="Button" value=" asin " onclick="useFun ('asin')"></td>
<td bordercolordark="#cc0066" bordercolor="#FF9966" bordercolorlight="#CC9933"><input name="btnacos" type="Button" value=" acos " onclick="useFun ('acos')"></td>
<td bordercolordark="#cc0066" bordercolor="#FF9966" bordercolorlight="#CC9933"><input name="btnFour" type="Button" value=" 4 " onclick="NumPressed(4)"></td>
<td bordercolordark="#cc0066" bordercolor="#FF9966" bordercolorlight="#CC9933"><input name="btnFive" type="Button" value=" 5 " onclick="NumPressed(5)"></td>
<td bordercolordark="#cc0066" bordercolor="#FF9966" bordercolorlight="#CC9933"><input name="btnSix" type="Button" value=" 6 " onclick="NumPressed(6)"></td>
<td bordercolordark="#cc0066" bordercolor="#FF9966" bordercolorlight="#CC9933"><input name="btnMinus" type="Button" value=" - " onclick="Operation('-')"></td>
</tr>
<tr align="center">
<td bordercolordark="#cc0066" bordercolor="#FF9966" bordercolorlight="#CC9933"><input name="btntan" type="Button" value=" tan " onclick="useFun ('tan')"></td>
<td bordercolordark="#cc0066" bordercolor="#FF9966" bordercolorlight="#CC9933"><input name="btnatan" type="Button" value=" atan " onclick="useFun ('atan')"></td>
<td bordercolordark="#cc0066" bordercolor="#FF9966" bordercolorlight="#CC9933"><input name="btnSeven" type="Button" value=" 7 " onclick="NumPressed(7)"></td>
<td bordercolordark="#cc0066" bordercolor="#FF9966" bordercolorlight="#CC9933"><input name="btnEight" type="Button" value=" 8 " onclick="NumPressed(8)"></td>
<td bordercolordark="#cc0066" bordercolor="#FF9966" bordercolorlight="#CC9933"><input name="btnNine" type="Button" value=" 9 " onclick="NumPressed(9)"></td>
<td bordercolordark="#cc0066" bordercolor="#FF9966" bordercolorlight="#CC9933"><input name="btnMultiply" type="Button" value=" * " onclick="Operation('*')"></td>
</tr>
<tr align="center">
<td bordercolordark="#cc0066" bordercolor="#FF9966" bordercolorlight="#CC9933"><input name="btnlog" type="Button" value=" log " onclick="useFun ('log')"></td>
<td bordercolordark="#cc0066" bordercolor="#FF9966" bordercolorlight="#CC9933"><input name="btnexp" type="Button" value=" exp " onclick="useFun ('exp')"></td>
<td bordercolordark="#cc0066" bordercolor="#FF9966" bordercolorlight="#CC9933"><input name="btnZero" type="Button" value=" 0 " onclick="NumPressed(0)"></td>
<td bordercolordark="#cc0066" bordercolor="#FF9966" bordercolorlight="#CC9933"><input name="btnDecimal" type="Button" value=" . " onclick="Decimal()"></td>
<td bordercolordark="#cc0066" bordercolor="#FF9966" bordercolorlight="#CC9933"><input name="btnNeg" type="Button" value=" +/- " onclick="Neg()"></td>
<td bordercolordark="#cc0066" bordercolor="#FF9966" bordercolorlight="#CC9933"><input name="btnDivide" type="Button" value=" / " onclick="Operation('/')"></td>
</tr>
<tr align="center">
<td bordercolordark="#cc0066" bordercolor="#FF9966" bordercolorlight="#CC9933"><input name="btnsqrt" type="Button" value=" sqrt " onclick="useFun ('sqrt')"></td>
<td bordercolordark="#cc0066" bordercolor="#FF9966" bordercolorlight="#CC9933"><input name="btnOx" type="Button" value=" 1/x " onclick="useFun ('1/x')"></td>
<td bordercolordark="#cc0066" bordercolor="#FF9966" bordercolorlight="#CC9933"><input name="btnXt" type="Button" value=" x^2 " onclick="useFun ('x^2')"></td>
<td bordercolordark="#cc0066" bordercolor="#FF9966" bordercolorlight="#CC9933"><input name="btnXthr" type="Button" value=" x^3 " onclick="useFun ('x^3')"></td>
<td bordercolordark="#cc0066" bordercolor="#FF9966" bordercolorlight="#CC9933"><input name="btnpi" type="Button" value=" pi " onclick=FKeyPad.ReadOut.value=Math.PI></td>
<td bordercolordark="#cc0066" bordercolor="#FF9966" bordercolorlight="#CC9933"><input name="btnEquals" type="Button" value=" = " onclick="Operation('=')"></td>
</tr>
<tr align="center">
<td bordercolordark="#cc0066" bordercolor="#FF9966" bordercolorlight="#CC9933"><input name="btnDivide" type="Button" value=" N! " onclick="ni ()"></td>
<td bordercolordark="#cc0066" bordercolor="#FF9966" bordercolorlight="#CC9933"><input name="btnDivide" type="Button" value=" pow " onclick="Operation('pow')"></td>
<td bordercolordark="#cc0066" bordercolor="#FF9966" bordercolorlight="#CC9933"><input name="btnDivide" type="Button" value=" E " onclick=FKeyPad.ReadOut.value=Math.E></td>
<td bordercolordark="#cc0066" bordercolor="#FF9966" bordercolorlight="#CC9933"><input name="btnClear" type="Button" value=" C " onclick="Clear()"></td>
<td bordercolordark="#cc0066" bordercolor="#FF9966" bordercolorlight="#CC9933"><input name="btnClearEntry" type="Button" value=" CE " onclick="ClearEntry()"></td>
<td bordercolordark="#cc0066" bordercolor="#FF9966" bordercolorlight="#CC9933"><input name="btnReturn" type="Button" value="返 回" onclick="goReturn()"></td>
</tr>
<tr align="center">
<td bordercolordark="#cc0066" bordercolor="#FF9966" bordercolorlight="#CC9933"><input name="btnmod" type="Button" value=" MOD " onclick="Operation('MOD')"></td>
<td bordercolordark="#cc0066" bordercolor="#FF9966" bordercolorlight="#CC9933"><input name="btnDivide" type="Button" value=" INT" onclick=FKeyPad.ReadOut.value=parseInt(FKeyPad.ReadOut.value,10)></td>
<td bordercolordark="#cc0066" bordercolor="#FF9966" bordercolorlight="#CC9933"><input name="btnmod" type="Button" value=" HEX " onclick="hexchange ()"></td>
<td bordercolordark="#cc0066" bordercolor="#FF9966" bordercolorlight="#CC9933"><input name="btnmod" type="Button" value=" OCT " onclick="octchange ()"></td>
<td bordercolordark="#cc0066" bordercolor="#FF9966" bordercolorlight="#CC9933"><input name="btnmod" type="Button" value=" BIN " onclick="binchange ()"></td>
</tr>
</TABLE>
</TABLE>
</B>
</FORM>
</CENTER>
<font face="Verdana, Arial, Helvetica" size=2>
<SCRIPT LANGUAGE="JavaScript">
<!-- Begin
var FKeyPad = document.forms['Keypad'];
var Accum = "0";
var FlagNewNum = false;
var PendingOp = "";
function longCount(str1,str2,type)
{
var comma1 = 0;
if (str1.indexOf(".")!=-1)
{
str1 = str1.replace(/0*$/,"");
comma1 = str1.length - str1.indexOf(".")-1;
}
var comma2 = 0;
if (str2.indexOf(".")!=-1)
{
str2 = str2.replace(/0*$/,"");
comma2 = str2.length - str2.indexOf(".")-1;
}
str1 = str1.replace(/\./,"");
str2 = str2.replace(/\./,"");
var value,comma;
if (type!="*")
{
if (comma1>comma2)
{
for (var i=0;i<comma1-comma2;i++) str2 += "0";
comma = (type=="/")?0:comma1;
}else
{
for (var i=0;i<comma2-comma1;i++) str1 += "0";
comma = (type=="/")?0:comma2;
}
}else
{
comma = comma1 + comma2;
}
if (type=="+")
{
value = parseInt(str1,10) + parseInt(str2,10);
}else if (type=="-")
{
value = parseInt(str1,10) - parseInt(str2,10);
}else if (type=="*")
{
value = parseInt(str1,10) * parseInt(str2,10);
}else if (type=="/")
{
value = parseInt(str1,10) / parseInt(str2,10);
}
else if (type=="MOD")
value = parseInt(str1,10) % parseInt(str2,10);
else if (type=="pow")
{
var a,b,s=1;
a=parseInt(str1,10);
b=parseInt(str2,10);
for(var i=1;i<=b;i++)
s=s*a;
value=parseInt(s,10);
}
value = String(value);
if (comma>0) value = value.substring(0,value.length-comma)+"."+value.substring(value.length- comma,value.length);
if (value.indexOf(".")!=-1)
value = value.replace(/0*$/,"");
return value;
}
function NumPressed (Num) {
if (FlagNewNum)
{
FKeyPad.ReadOut.value = Num;
FlagNewNum = false;
}
else
{
if (FKeyPad.ReadOut.value == "0")
FKeyPad.ReadOut.value = Num;
else
FKeyPad.ReadOut.value += Num;
}
}
function Operation (Op)
{
var Readout = FKeyPad.ReadOut.value;
if (FlagNewNum && PendingOp != "=");
else
{
FlagNewNum = true;
if ( '+' == PendingOp || '-' == PendingOp || '/' == PendingOp || '*' == PendingOp||'MOD'==PendingOp||'pow'==PendingOp)
Accum = longCount(Accum,Readout,PendingOp);
else
Accum = Readout;
FKeyPad.ReadOut.value = Accum;
PendingOp = Op;
FKeyPad.ReadOut.focus();
FKeyPad.ReadOut.select();
}
}
function useFun (st)
{
switch(st)
{
case "sin":
FKeyPad.ReadOut.value=Math.sin(FKeyPad.ReadOut.value);
FlagNewNum=true;
break;
case "cos":
FKeyPad.ReadOut.value=Math.cos(FKeyPad.ReadOut.value);
FlagNewNum=true;
break;
case "asin":
FKeyPad.ReadOut.value=Math.asin(FKeyPad.ReadOut.value);
FlagNewNum=true;
break;
case "acos":
FKeyPad.ReadOut.value=Math.acos(FKeyPad.ReadOut.value);
FlagNewNum=true;
break;
case "tan":
FKeyPad.ReadOut.value=Math.tan(FKeyPad.ReadOut.value);
FlagNewNum=true;
break;
case "atan":
FKeyPad.ReadOut.value=Math.atan(FKeyPad.ReadOut.value);
FlagNewNum=true;
break;
case "log":
FKeyPad.ReadOut.value=Math.log(FKeyPad.ReadOut.value);
FlagNewNum=true;
break;
case "exp":
FKeyPad.ReadOut.value=Math.exp(FKeyPad.ReadOut.value);
FlagNewNum=true;
break;
case "sqrt":
FKeyPad.ReadOut.value=Math.sqrt(FKeyPad.ReadOut.value);
FlagNewNum=true;
break;
case "1/x":
FKeyPad.ReadOut.value=1/FKeyPad.ReadOut.value;
FlagNewNum=true;
break;
case "x^2":
FKeyPad.ReadOut.value=parseFloat(FKeyPad.ReadOut.value)*parseFloat(FKeyPad.ReadOut.value);
FlagNewNum=true;
break;
case "x^3":
FKeyPad.ReadOut.value=parseFloat(FKeyPad.ReadOut.value)*parseFloat(FKeyPad.ReadOut.value)*parseFloat(FKeyPad.ReadOut.value);
FlagNewNum=true;
break;
}
}
function hexchange ()
{
var hex,dec;
dec = parseFloat(FKeyPad.ReadOut.value);
hex = dec.toString(16);
window.alert("该十进制数转成十六进制数为:"+ hex);
}
function octchange ()
{
var oct,dec;
dec = parseFloat(FKeyPad.ReadOut.value);
oct = dec.toString(8);
window.alert("该十进制数转成八进制数为:"+ oct);
}
function binchange ()
{
var bin,dec;
dec = parseFloat(FKeyPad.ReadOut.value);
bin = dec.toString(2);
window.alert("该十进制数转成二进制数为:"+ bin);
}
function ni ()
{var y,k=1;
y=parseFloat(FKeyPad.ReadOut.value);
for(var x=1;x<=y;x++)
k=k*x;
FKeyPad.ReadOut.value=k;
}
function Decimal ()
{
var curReadOut = FKeyPad.ReadOut.value;
if (FlagNewNum)
{
curReadOut = "0.";
FlagNewNum = false;
}
else
{
if (curReadOut.indexOf(".") == -1)
curReadOut += ".";
}
FKeyPad.ReadOut.value = curReadOut;
}
function ClearEntry ()
{
FKeyPad.ReadOut.value = "0";
FlagNewNum = true;
}
function Clear ()
{
Accum = "0";
PendingOp = "";
ClearEntry();
}
function Neg ()
{
window.alert(FKeyPad.ReadOut.value);
FKeyPad.ReadOut.value = longCount(FKeyPad.ReadOut.value,"-1","*");
}
function goReturn()
{
top.returnValue = FKeyPad.ReadOut.value;
self.close();
}
function CheckOut()
{
var keyCode = window.event.keyCode;
if (keyCode>=48 && keyCode<=57)
{
if (FlagNewNum)
{
FKeyPad.ReadOut.value = "";
FlagNewNum = false;
}
return true;
}else if (keyCode==43 || keyCode==45 || keyCode==42 || keyCode==47 || keyCode==61) {
Operation(String.fromCharCode(keyCode));
}else if (keyCode==46) {
if (FKeyPad.ReadOut.value.indexOf(".") == -1)
return true;
}else if (keyCode==27) goReturn();
window.event.returnValue = false;
return false;
}
// End -->
</SCRIPT>
</body>
</html>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>无标题页</title>
<script type="text/javascript">
function cc(x)
{
x.result.value=eval(x.expression.value)
}
</script>
<input type="text" name="expression" size="20" id="tt1"/>
<br />
<input type="button" id="b1" value="请单击这里计算表达式" onclick="cc(form1)" disabled="disabled"/>
</head>
<body>
<h2>一个简单的计算器</h2>
<form id="form1" runat="server">
请输入表达式:
<input type="text" name="expression" size="20" id="tt1"/>
<br />
<input type="button" id="b1" value="请单击这里计算表达式" onclick="cc(form1)" disabled="disabled"/>
<br />
表达式的结果是:
<input type="text" name="result" size="10" id="tt2" />
</form>
</body>
</html>
<HTML>
<HEAD>
<TITLE>JavaScript计算器</TITLE>
<style type="text/css">
p {font-size: 12pt}
.red {color: red;width:34}
.redl{color:red;width:51}
.blue {color: blue;width:34}
</style>
<SCRIPT LANGUAGE="JavaScript" src="calculate.js"></script>
</HEAD>
<BODY>
<center>
<form name="calculator">
<table bgcolor="#aaaaaa" width="230">
<tr><td>
<table bgcolor="#cccccc" border="1">
<tr><td>
<table border=0 cellpadding="0">
<tr><td bgcolor="#000080"></td></tr>
<tr><td>
<table width="100%" border="0">
<tr><td colspan="6"><input type="text" readOnly name="answer" style="text-align:right" size="30" maxlength="30" value="0."></td></tr>
<tr><td colspan="6">
<table border="0" cellpadding="0">
<tr><td>
<input type="text" name="mem" size="3" maxlength="3" style="text-align:center" readOnly style="background:menu"> <input type="button" name="backspace" class="redl" value="退格" tok="\b" onClick="this.form.answer.value = oController.addNumber(this.tok);"> <input type="button" name="CE" class="redl" tok="CE" value="CE" onClick="oController.cleanError();this.form.answer.value = oController.doFun(this.tok)"> <input type="button" name="C" class="redl" tok="CE" value="C" onClick="this.form.answer.value = oController.doFun(this.tok)">
</td></tr>
</table>
</td></tr>
<tr><td><input type="button" name="MC" class="red" value=" MC " tok="MC" onClick="this.form.answer.value = oController.doFun(this.tok);this.form.mem.value='';"></td>
<td><input type="button" name="calc7" class="blue" value=" 7 " tok="7" onClick="this.form.answer.value = oController.addNumber(this.tok)"></td>
<td><input type="button" name="calc8" class="blue" value=" 8 " tok="8" onClick="this.form.answer.value = oController.addNumber(this.tok)"></td>
<td><input type="button" name="calc9" class="blue" value=" 9 " tok="9" onClick="this.form.answer.value = oController.addNumber(this.tok)"></td>
<td><input type="button" name="divide" class="red" value=" / " tok="/" onClick="this.form.answer.value = oController.addOper(this.tok)"></td>
<td><input type="button" name="sqrt" class="blue" value="sqrt" tok="sqrt" onClick="this.form.answer.value = oController.doFun(this.tok)"></td></tr>
<tr><td><input type="button" name="MR" class="red" value=" MR " tok="MR" onClick="this.form.answer.value = oController.doFun(this.tok);"></td>
<td><input type="button" name="calc4" class="blue" value=" 4 " tok="4" onClick="this.form.answer.value = oController.addNumber(this.tok)"></td>
<td><input type="button" name="calc5" class="blue" value=" 5 " tok="5" onClick="this.form.answer.value = oController.addNumber(this.tok)"></td>
<td><input type="button" name="calc6" class="blue" value=" 6 " tok="6" onClick="this.form.answer.value = oController.addNumber(this.tok)"></td>
<td><input type="button" name="multiply" class="red" value=" * " tok="*" onClick="this.form.answer.value = oController.addOper(this.tok)"></td>
<td><input type="button" name="percent" class="blue" value=" % " tok="%" onClick="this.form.answer.value = oController.doFun(this.tok)"></td></tr>
<tr><td><input type="button" name="MS" class="red" value=" MS " tok="MS" onClick="this.form.answer.value = oController.doFun(this.tok);this.form.mem.value='M';"></td>
<td><input type="button" name="calc1" class="blue" value=" 1 " tok="1" onClick="this.form.answer.value = oController.addNumber(this.tok)"></td>
<td><input type="button" name="calc2" class="blue" value=" 2 " tok="2" onClick="this.form.answer.value = oController.addNumber(this.tok)"></td>
<td><input type="button" name="calc3" class="blue" value=" 3 " tok="3" onClick="this.form.answer.value = oController.addNumber(this.tok)"></td>
<td><input type="button" name="minus" class="red" value=" - " tok="-" onClick="this.form.answer.value = oController.addOper(this.tok)"></td>
<td><input type="button" name="recip" class="blue" value="1/x " tok="1/x" onClick="this.form.answer.value = oController.doFun(this.tok)"></td></tr>
<tr><td><input type="button" name="Mplus" class="red" value=" M+ " tok="M+" onClick="this.form.answer.value = oController.doFun(this.tok);this.form.mem.value='M';"></td>
<td><input type="button" name="calc0" class="blue" value=" 0 " tok="0" onClick="this.form.answer.value = oController.addNumber(this.tok)"></td>
<td><input type="button" name="negate" class="blue" value="+/- " tok="+/-" onClick="this.form.answer.value = oController.doFun(this.tok)"></td>
<td><input type="button" name="dot" class="blue" value=" . " tok="." onClick="this.form.answer.value = oController.addNumber(this.tok)"></td>
<td><input type="button" name="plus" class="red" value=" + " tok="+" onClick="this.form.answer.value = oController.addOper(this.tok)"></td>
<td><input type="button" name="equal" class="red" value=" = " tok="=" onClick="this.form.answer.value = oController.addOper(this.tok)"></td>
</tr>
</table>
</td></tr>
</table>
</td></tr>
</table>
</td></tr>
</table>
</form>
</center>
</BODY>
</HTML>
calculate.js
(function(){
//这里是定义一个控制器,用来对外开放计算接口和拦截计算中出现的异常
oController = {
addNumber : function(tok){if(errorState()) return errorResult(); else return addNumber(tok)},
addOper : function(tok){if(errorState()) return errorResult(); else return addOper(tok)},
doFun : function(tok){if(errorState()) {return errorResult();} else return doFun(tok)},
cleanError : cleanError
}
//用来保存记忆的变量
var memery = 0;
//这是符号对应的计算规则,这里采用一个闭包的对照表来处理
var opMap = {
"+":function(a,b){return b + a}, //处理加法运算的闭包
"-":function(a,b){return b - a}, //处理减法运算的闭包
"*":function(a,b){return b * a}, //处理乘法运算的闭包
"/":function(a,b){return b / a}, //处理除法运算的闭包
"=":function(a,b){return a}, //处理最终结果
"C":init, //清零
"CE":init, //清零
"sqrt":function(a){return Math.sqrt(a)}, //计算开方
"1/x":function(a){return 1/a}, //计算倒数
"%":function(a){return a/100}, //求余数
"+/-":function(a){return -a}, //正负号
"MS":function(a){memery = a; return a}, //记忆
"M+":function(a){memery += a; return a}, //累加记忆
"MR":function(a){return memery}, //从记忆中读取
"MC":function(a){memery = 0; return a} //清除记忆
}
//用来存储数值、操作符和输入缓存的数据结构
var oMemery = {
numStack : [], //存储数值
operStack : [], //存储字符串
inBuffer : "" //输入显示缓存
}
//检测计算中出现的数字异常,若有,返回给控制器进行处理
function errorState()
{
with(oMemery)
{
var n = numStack[numStack.length - 1];
return n == Infinity || isNaN(n);
}
}
//若出现异常,这个函数提供出现异常时的数值结果(数值堆栈顶的值)
function errorResult()
{
with(oMemery)
{
return formatBuff(numStack[numStack.length - 1]);
}
}
//清除异常并从错误中恢复
function cleanError()
{
with(oMemery)
{
numStack[numStack.length - 1] = 0;
}
}
function init() //初始化
{
with(oMemery)
{
numStack.length = 0; //清空数值堆栈
operStack.length = 0; //清空操作符堆栈
numStack.push(0); //在数值堆栈中推入一个0作为栈顶
inBuffer = ""; //清空输入缓存
return inBuffer; //将清空后的缓存值(实际上是空字符串'')返回
}
}
function doOper() //计算表达式
{
with(oMemery)
{
if(operStack.length) //如果运算符堆栈中有值
{
try
{
//取得栈顶运算符对应的操作方法
var op = opMap[operStack.pop()];
var args = [];
//该方法需要提供几个操作数,可以通过检查op.length得到
for(var i = 0; i < op.length; i++)
{
//从数值堆栈中依次取相应的操作数进行处理
args.push(numStack.pop());
}
//在这里实际进行计算,并把计算结果重新压入堆栈
numStack.push(op.apply(this, args));
}
catch(ex)
{
alert(ex.message);
}
}
return numStack[numStack.length - 1];
}
}
//格式化显示的数值,主要是为了符合计算器的习惯,比如0显示成0.(带小数点)
function formatBuff(buf)
{
if(buf == "")
return "0.";
else{
buf = parseFloat(buf);
return /\./.test(buf) ? buf : buf + ".";
}
}
function addNumber(tok) //输入数值
{
with(oMemery)
{
try
{
var token;
if(tok == "\b") //如果输入的是一个退格
token = inBuffer.slice(0,-1); //那么把缓存中的内容去掉一个
else
token = inBuffer + tok.toString(); //否则接受新输入的数字
//如果数值的第一位是小数点,显示的时候要补一个0
if(token.slice(0,1) == ".") token = 0 + token;
//判断输入接收后的结果是否满足数值的格式
if(/^([\d]+(\.)?[\d]*)?$/.test(token))
{
inBuffer = token; //如果满足,则确认接受,写入缓存
}
return formatBuff(inBuffer);
}
catch(ex)
{
alert(ex.message);
}
}
}
function addOper(tok) //输入运算符
{
with(oMemery)
{
try
{
//如果缓存中有数值,将它推入数值堆栈
if(inBuffer != "")
numStack.push(parseFloat(inBuffer));
//否则从操作符堆栈中将前一个输入的操作符弹出用当前操作符替代
else
operStack.pop();
var ret = doOper(); //计算表达式
operStack.push(tok); //将新输入的运算符推入堆栈
inBuffer = ""; //清空输入缓存
return formatBuff(ret);
}
catch(ex)
{
alert(ex.message);
}
}
}
function doFun(tok) //处理函数
{
with(oMemery)
{
try{
//假如是一些函数如sqrt
var fun = opMap[tok];
//如果输入缓存无内容
if(inBuffer == "")
inBuffer = numStack.pop(); //从数值堆栈中取数
else
operStack.push(tok); //否则将函数推入操作符堆栈
//计算函数调用结果并放入数值堆栈
numStack.push(fun(parseFloat(inBuffer)));
inBuffer = ""; //清空缓存
return formatBuff(numStack[numStack.length - 1]);
}
catch(ex){
alert(ex.message);
}
}
}
init(); //这里执行前面定义的初始化函数
})();
继续做梦吧
javascript指什么
JavaScript是一种具有函数优先的轻量级,解释型或即时编译型的编程语言。虽然它是作为开发Web页面的脚本语言而出名,但是它也被用到了很多非浏览器环境中,JavaScript基于原型编程、多范式的动态脚本语言,并且支持面向对象、命令式、声明式、函数式编程范式。JavaScript在1995年由Netscape公司的Brendan Eich,在...
JavaScript有哪些主流分类?
JavaScript的成熟分类主要包括核心JavaScript、文档对象模型(DOM)、浏览器对象模型(BOM)以及JavaScript框架与库。核心JavaScript 核心JavaScript是JavaScript语言的基础,它定义了语言的语法、类型、运算符、函数、作用域等核心概念。这部分内容不涉及任何特定环境或宿主对象,是通用且纯粹的编程语言知识。例如,...
javascriptjavascript成熟分类
JavaScript语言是一种非常成熟的编程语言,已经发展出了许多分类。下面将介绍一些关键的JavaScript分类。1. 通用目的编程语言:JavaScript是一种通用目的的编程语言,可以广泛应用于Web开发、移动应用开发、游戏开发等领域。它的灵活性和易用性使其成为众多开发者的首选语言。2. 前端开发:作为Web开发中不可或...
JavaScript是什么?
JavaScript是一种脚本语言。诞生与1995年,当时是为了处理服务端某个语言负责的某个没有填写的必填域。它是由Netscape公司布兰登.艾奇为NetscapeNavigator2开发的一种脚本语言,刚开始的时候是叫LiveScript。由于当时Java语言的盛行,LiveScript在发布前夕,临时更名为JavaScript。JavaScript是一种专为与网页交互而...
浏览器怎么开启javascript支持
浏览器开启JavaScript支持的方法如下:IE浏览器开启JavaScript方法:在IE界面菜单栏中“工具”中选择“Internet选项”–“安全”选项卡–选择“Internet”(蓝色的小地球)–“自定义级别”–找到“脚本”下的“Java小程序脚本”中进行启用。360浏览器开启JavaScript方法:在360界面菜单栏的“工具”中选择“360...
JavaScript有几种分类?
JavaScript 本身并不是以“分类”来直接区分的,但可以从不同的角度和用途来理解和使用 JavaScript。这里,我会列举几种常见的理解和使用 JavaScript 的方式或“分类”:ECMAScript(核心JavaScript):ECMAScript 是 JavaScript 的语言标准,由 ECMA 国际(前身为欧洲计算机制造商协会)制定。它定义了 ...
javascript是什么意思
JavaScript是一种基于对象和事件驱动并具有安全性能的脚本语言。Javascript脚本语言同其他语言一样,有它自身的基本数据类型,表达式和算术运算符及程序的基本程序框架。Javascript提供了四种基本的数据类型和两种特殊数据类型用来处理数据和文字。而变量提供存放信息的地方,表达式则可以完成较复杂的信息处理。JavaScr...
javascript是干什么的
JavaScript是一种属于网络的脚本语言,已经被广泛用于Web应用开发,常用来为网页添加各式各样的动态功能,为用户提供更流畅美观的浏览效果。通常JavaScript脚本是通过嵌入在HTML中来实现自身的功能的是一种解释性脚本语言(代码不进行预编译)主要用来向HTML(标准通用标记语言下的一个应用)页面添加交互行为。可...
javascript不可用怎么办?
如果你在使用网页时发现 JavaScript 不可用,可以尝试以下解决方法:1. 检查浏览器设置:确保 JavaScript 已经启用。在大多数浏览器中,你可以在浏览器设置或选项中找到 JavaScript 设置,并确保它已启用。2. 更新浏览器:如果你的浏览器版本较旧,可能会导致 JavaScript 不可用。尝试更新浏览器到最新版本,...
JS全称是什么
js 的全称是javascript ,JavaScript一种直译式脚本语言,是一种动态类型、弱类型、基于原型的语言,内置支持类型。它的解释器被称为JavaScript引擎,为浏览器的一部分,广泛用于客户端的脚本语言,最早是在HTML(标准通用标记语言下的一个应用)网页上使用,用来给HTML网页增加动态功能。
绪山拜新: <html> <head> <title>js运算</title> <boby> <table> <tr> <td>第一个数</td> <td><input type="text" id="onesum"></td> </tr> <tr> <td>运算符号</td> <td><input type="text" id="fh"></td> </tr> <tr> <td>第二个数</td> <td><input type="text"...
谯城区13114213746: 用javascript制作一个简易的计算器,求代码 - ?
绪山拜新: javascript有相应的内置函数,取框中的值,带入按钮事件,然后调用javascript内置函数就可以啦~ javascript Math:数学函数方法:abs(x) 返回数字的相对值 acos(x) 返回数字的反余弦值 asin(x) 返回数字的反正弦值 atan(x) 返回位于-PI/2 和 PI/2 ...
谯城区13114213746: 用javascript写一个简单的类 - ?
绪山拜新: <script type="text/javascript"><br>function demo()<br>{<br> this.test1 = function(){<br> alert('test1');<br> };<br> this.test2 = function(){<br> alert('test2');<br> }<br>}<br><br>var aa = new demo();<br>aa.test1();<br>aa.test2();<br></script>结果会依次弹出<br>test1<br>test2
谯城区13114213746: 高手进来帮帮忙,用js写一个超级简单的小程序 - ?
绪山拜新: Date test 问候
谯城区13114213746: JavaScript生成一个简单的表格 - ?
绪山拜新: <input type="button" value="创建一个新表格" onclick="createTable(800,8,5)" /> <script type="text/javascript"> //方法参数createTable(表格宽度[不要单位],行数,列数) function createTable(width,rows,cells) {//创建一个表格对象var ...
谯城区13114213746: 用js制作个最为简单的幻灯片思路,初学者,步骤加代码,详细点,谢谢 - ?
绪山拜新: div+css你至少得会吧? 首先定义一个轮播器大盒子.设置overflow为hidden.设置其宽度,高度、position设置为相对 再在里面套一个图片盒子,里面加多个img标签.float设置为left.每张照片的大小就是轮播器的大小.那么在轮播器范围内的...
谯城区13114213746: 利用JavaScript脚本语言编写一个简单的“网页计算器” - ?
绪山拜新: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-trans...
谯城区13114213746: javascript编一个简单的程序?
绪山拜新: function q(){ var s=0; for(var i=1;i<=100;i++){ s=s+i; } }
谯城区13114213746: JS做一个简单的加法程序 - ?
绪山拜新: <html> <head><meta http-equiv="Content-Type" content="text/html; charset=gb2312"><title>新建网页 1</title><script type="text/javascript" language="javascript">function add(){document.write("计算结果:",parseInt(document....
谯城区13114213746: 怎么用JavaScript写一个简单的小说阅读器 - ?
绪山拜新: <html> <meta http-equiv="content-type" content="text/html; charset=UTF-8" /> <head> <title></title> <script type="text/javascript"> function Reader(content, cID, stopID, continueID) { this.conLoad = document.getElementById(cID); this....