怎么用Javascript做计算器的退格功能?

JavaScript027

怎么用Javascript做计算器的退格功能?,第1张

***源码也木有,很讨厌这样直接扯皮~~。

***如此,这个问题只好假设着来解答了。在JS里,所有的数据是无类型定义的,所以其实很轻松可以实现“退格功能”。

***举个例子,比如起初输入“55”,我们在代码上直接赋值给一个变量,在计算比如“*/-+sqrt”的时候JS解释器会将它当作数值,自动调用内置的valueof()进行数据类型转换成浮点型。而我们想进行退格操作,只需要把它当成字符串类型就可以了。比如var

a=“55“a.tostring()

**注意这里不可以直接写成55.tostring(),会抛出类型异常。

麻烦采纳,谢谢!

我给你提供思路。

退格你可能需要用到字符串函数,substring(1)来截取不包含第一个字符的后面所有的字符,然后把值传给文本框。

求倒数,你需要定义一个临时变量,把分母和分子的的值作交换就行了。

代码你自己写,不难的。

我刚学javaScript做一计算器,请看看!多多交流!~~``

<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=0i<comma1-comma2i++) str2 += "0"

comma = (type=="/")?0:comma1

}else

{

for (var i=0i<comma2-comma1i++) 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=1i<=bi++)

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=1x<=yx++)

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>