一个功能齐全的js计算器代码

发布时间:2019-08-17编辑:脚本学堂
js计算器代码,功能很全面,拆分成了多个函数,包括数字键、函数键、运算符、括号、修改键、转换键,以及进制转换等,功能相当齐全的js计算器代码,有效果图。

例子,js计算器功能代码

效果图,如下:
js计算器
 

复制代码 代码示例:

<html><head>
<title>功能很全的js计算器 - www.jb200.com</title>
<meta http-equiv=content-type content="text/html; charset=gb2312">
<meta http-equiv=content-language content=zh-cn>
<meta http-equiv=page-enter content=blendtrans(duration=0.5)>
<meta http-equiv=page-exit content=blendtrans(duration=0.5)>
<style type=text/css>p {
 font-size: 9pt; font-family: "verdana"
}
td {
 font-size: 9pt; line-height: normal
}
a {
 font-size: 9pt; text-transform: none; color: #326969; text-decoration: none
}
a:hover {
 font-size: 9pt; left: 1px; color: #0000ff; position: relative; top: 1px; text-decoration: none
}
body {
 font-size: 9pt; cursor: url('body.cur'); scrollbar-arrow-color: #ffffff; scrollbar-base-color: #cde0c2
}
.diary_tb {
 width: 100%; word-break: break-all; font-family: "verdana","宋体"; position: relative; text-align: center; word-wrap: break-word
}
.diary_main {
 table-layout: fixed; background-position: left top; font-size: 12px; color: #005782; word-break: break-all; line-height: 24px; font-family: "宋体"; line-break: strict; height: 2px; text-align: left
}
</style>
<meta content="mshtml 6.00.2600.0" name=generator></head>
<center>
<table id=autonumber2 style="border-collapse: collapse" bordercolor=#808080
cellspacing=0 cellpadding=0 width=760 border=0>
</table>
<script language=javascript>
<!--
var endnumber=true
var mem=0
var carry=10
var hexnum="0123456789abcdef"
var angle="d"
var stack=""
var level="0"
var layer=0
//数字键
function inputkey(key)
{
var index=key.charcodeat(0);
if ((carry==2 && (index==48 || index==49))
|| (carry==8 && index>=48 && index<=55)
|| (carry==10 && (index>=48 && index<=57 || index==46))
|| (carry==16 && ((index>=48 && index<=57) || (index>=97 && index<=102))))
if(endnumber)
{
endnumber=false
document.calc.display.value = key
}
else if(document.calc.display.value == null || document.calc.display.value == "0")
document.calc.display.value = key
else
document.calc.display.value += key
}

function changesign()
{
if (document.calc.display.value!="0")
if(document.calc.display.value.substr(0,1) == "-")
document.calc.display.value = document.calc.display.value.substr(1)
else
document.calc.display.value = "-" + document.calc.display.value
}

//函数键
function inputfunction(fun,shiftfun)
{
endnumber=true
if (document.calc.shiftf.checked)
document.calc.display.value=decto(funcalc(shiftfun,(todec(document.calc.display.value,carry))),carry)
else
document.calc.display.value=decto(funcalc(fun,(todec(document.calc.display.value,carry))),carry)
document.calc.shiftf.checked=false
document.calc.hypf.checked=false
inputshift()
}

function inputtrig(trig,arctrig,hyp,archyp)
{
if (document.calc.hypf.checked)
inputfunction(hyp,archyp)
else
inputfunction(trig,arctrig)
}

//运算符
function operation(join,newlevel)
{
endnumber=true
var temp=stack.substr(stack.lastindexof("(")+1)+document.calc.display.value
while (newlevel!=0 && (newlevel<=(level.charat(level.length-1))))
{
temp=parse(temp)
level=level.slice(0,-1)
}
if (temp.match(/^(.*d[+-*/%^&|x])?([+-]?[0-9a-f.]+)$/))
document.calc.display.value=regexp.$2
stack=stack.substr(0,stack.lastindexof("(")+1)+temp+join
document.calc.operator.value=" "+join+" "
level=level+newlevel
}

//括号
function addbracket()
{
endnumber=true
document.calc.display.value=0
stack=stack+"("
document.calc.operator.value=" "
level=level+0

layer+=1
document.calc.bracket.value="(="+layer
}

function disbracket()
{
endnumber=true
var temp=stack.substr(stack.lastindexof("(")+1)+document.calc.display.value
while ((level.charat(level.length-1))>0)
{
temp=parse(temp)
level=level.slice(0,-1)
}

document.calc.display.value=temp
stack=stack.substr(0,stack.lastindexof("("))
document.calc.operator.value=" "
level=level.slice(0,-1)

layer-=1
if (layer>0)
document.calc.bracket.value="(="+layer
else
document.calc.bracket.value=""
}

//等号
function result()
{
endnumber=true
while (layer>0)
disbracket()
var temp=stack+document.calc.display.value
while ((level.charat(level.length-1))>0)
{
temp=parse(temp)
level=level.slice(0,-1)
}

document.calc.display.value=temp
document.calc.bracket.value=""
document.calc.operator.value=""
stack=""
level="0"
}

//修改键
function backspace()
{
if (!endnumber)
{
if(document.calc.display.value.length>1)
document.calc.display.value=document.calc.display.value.substring(0,document.calc.display.value.length - 1)
else
document.calc.display.value=0
}
}

function clearall()
{
document.calc.display.value=0
endnumber=true
stack=""
level="0"
layer=""
document.calc.operator.value=""
document.calc.bracket.value=""
}

//转换键
function inputchangcarry(newcarry)
{
endnumber=true
document.calc.display.value=(decto(todec(document.calc.display.value,carry),newcarry))
carry=newcarry

document.calc.sin.disabled=(carry!=10)
document.calc.cos.disabled=(carry!=10)
document.calc.tan.disabled=(carry!=10)
document.calc.bt.disabled=(carry!=10)
document.calc.pi.disabled=(carry!=10)
document.calc.e.disabled=(carry!=10)
document.calc.kp.disabled=(carry!=10)

document.calc.k2.disabled=(carry<=2)
document.calc.k3.disabled=(carry<=2)
document.calc.k4.disabled=(carry<=2)
document.calc.k5.disabled=(carry<=2)
document.calc.k6.disabled=(carry<=2)
document.calc.k7.disabled=(carry<=2)
document.calc.k8.disabled=(carry<=8)
document.calc.k9.disabled=(carry<=8)
document.calc.ka.disabled=(carry<=10)
document.calc.kb.disabled=(carry<=10)
document.calc.kc.disabled=(carry<=10)
document.calc.kd.disabled=(carry<=10)
document.calc.ke.disabled=(carry<=10)
document.calc.kf.disabled=(carry<=10)
}

function inputchangangle(angletype)
{
endnumber=true
angle=angletype
if (angle=="d")
document.calc.display.value=radianstodegress(document.calc.display.value)
else
document.calc.display.value=degresstoradians(document.calc.display.value)
endnumber=true
}

function inputshift()
{
if (document.calc.shiftf.checked)
{
document.calc.bt.value="deg "
document.calc.ln.value="exp "
document.calc.log.value="expd"

if (document.calc.hypf.checked)
{
document.calc.sin.value="ahs "
document.calc.cos.value="ahc "
document.calc.tan.value="aht "
}
else
{
document.calc.sin.value="asin"
document.calc.cos.value="acos"
document.calc.tan.value="atan"
}

document.calc.sqr.value="x^.5"
document.calc.cube.value="x^.3"

document.calc.floor.value="小数"
}
else
{
document.calc.bt.value="d.ms"
document.calc.ln.value=" ln "
document.calc.log.value="log "

if (document.calc.hypf.checked)
{
document.calc.sin.value="hsin"
document.calc.cos.value="hcos"
document.calc.tan.value="htan"
}
else
{
document.calc.sin.value="sin "
document.calc.cos.value="cos "
document.calc.tan.value="tan "
}

document.calc.sqr.value="x^2 "
document.calc.cube.value="x^3 "

document.calc.floor.value="取整"
}

}
//存储器部分

function clearmemory()
{
mem=0
document.calc.memory.value=" "
}

function getmemory()
{
endnumber=true
document.calc.display.value=decto(mem,carry)
}

function putmemory()
{
endnumber=true
if (document.calc.display.value!=0)
{
mem=todec(document.calc.display.value,carry)
document.calc.memory.value=" m "
}
else
document.calc.memory.value=" "
}

function addmemory()
{
endnumber=true
mem=parsefloat(mem)+parsefloat(todec(document.calc.display.value,carry))
if (mem==0)
document.calc.memory.value=" "
else
document.calc.memory.value=" m "
}

function multimemory()
{
endnumber=true
mem=parsefloat(mem)*parsefloat(todec(document.calc.display.value,carry))
if (mem==0)
document.calc.memory.value=" "
else
document.calc.memory.value=" m "
}

//十进制转换
function todec(num,oldcarry)
{
if (oldcarry==10 || num==0) return(num)
var neg=(num.charat(0)=="-")
if (neg) num=num.substr(1)
var newnum=0
for (var index=1;index<=num.length;index++)
newnum=newnum*oldcarry+hexnum.indexof(num.charat(index-1))
if (neg)
newnum=-newnum
return(newnum)
}

function decto(num,newcarry)
{
var neg=(num<0)
if (newcarry==10 || num==0) return(num)
num=""+math.abs(num)
var newnum=""
while (num!=0)
{
newnum=hexnum.charat(num%newcarry)+newnum
num=math.floor(num/newcarry)
}
if (neg)
newnum="-"+newnum
return(newnum)
}

//表达式解析
function parse(string)
{
if (string.match(/^(.*d[+-*/%^&|x<])?([+-]?[0-9a-f.]+)([+-*/%^&|x<])([+-]?[0-9a-f.]+)$/))
return(regexp.$1+cypher(regexp.$2,regexp.$3,regexp.$4))
else
return(string)
}

//数学运算和位运算
function cypher(left,join,right)
{
left=todec(left,carry)
right=todec(right,carry)
if (join=="+")
return(decto(parsefloat(left)+parsefloat(right),carry))
if (join=="-")
return(decto(left-right,carry))
if (join=="*")
return(decto(left*right,carry))
if (join=="/" && right!=0)
return(decto(left/right,carry))
if (join=="%")
return(decto(left%right,carry))
if (join=="&")
return(decto(left&right,carry))
if (join=="|")
return(decto(left|right,carry))
if (join=="^")
return(decto(math.pow(left,right),carry))
if (join=="x")
return(decto(left^right,carry))
if (join=="<")
return(decto(left<<right,carry))
alert("除数不能为零")
return(left)
}

//函数计算
function funcalc(fun,num)
{
with(math)
{
if (fun=="pi")
return(pi)
if (fun=="e")
return(e)

if (fun=="abs")
return(abs(num))
if (fun=="ceil")
return(ceil(num))
if (fun=="round")
return(round(num))

if (fun=="floor")
return(floor(num))
if (fun=="deci")
return(num-floor(num))


if (fun=="ln" && num>0)
return(log(num))
if (fun=="exp")
return(exp(num))
if (fun=="log" && num>0)
return(log(num)*log10e)
if (fun=="expdec")
return(pow(10,num))


if (fun=="cube")
return(num*num*num)
if (fun=="cubt")
return(pow(num,1/3))
if (fun=="sqr")
return(num*num)
if (fun=="sqrt" && num>=0)
return(sqrt(num))

if (fun=="!")
return(factorial(num))

if (fun=="recip" && num!=0)
return(1/num)

if (fun=="dms")
return(dms(num))
if (fun=="deg")
return(deg(num))

if (fun=="~")
return(~num)

if (angle=="d")
{
if (fun=="sin")
return(sin(degresstoradians(num)))
if (fun=="cos")
return(cos(degresstoradians(num)))
if (fun=="tan")
return(tan(degresstoradians(num)))

if (fun=="arcsin" && abs(num)<=1)
return(radianstodegress(asin(num)))
if (fun=="arccos" && abs(num)<=1)
return(radianstodegress(acos(num)))
if (fun=="arctan")
return(radianstodegress(atan(num)))
}
else
{
if (fun=="sin")
return(sin(num))
if (fun=="cos")
return(cos(num))
if (fun=="tan")
return(tan(num))

if (fun=="arcsin" && abs(num)<=1)
return(asin(num))
if (fun=="arccos" && abs(num)<=1)
return(acos(num))
if (fun=="arctan")
return(atan(num))
}

if (fun=="hypsin")
return((exp(num)-exp(0-num))*0.5)
if (fun=="hypcos")
return((exp(num)+exp(-num))*0.5)
if (fun=="hyptan")
return((exp(num)-exp(-num))/(exp(num)+exp(-num)))

if (fun=="ahypsin" | fun=="hypcos" | fun=="hyptan")
{
alert("对不起,公式还没有查到!")
return(num)
}

alert("超出函数定义范围")
return(num)
}
}

function factorial(n)
{
n=math.abs(parseint(n))
var fac=1
for (;n>0;n-=1)
fac*=n
return(fac)
}

function dms(n)
{
var neg=(n<0)
with(math)
{
n=abs(n)
var d=floor(n)
var m=floor(60*(n-d))
var s=(n-d)*60-m
}
var dms=d+m/100+s*0.006
if (neg)
dms=-dms
return(dms)
}

function deg(n)
{
var neg=(n<0)
with(math)
{
n=abs(n)
var d=floor(n)
var m=floor((n-d)*100)
var s=(n-d)*100-m
}
var deg=d+m/60+s/36
if (neg)
deg=-deg
return(deg)
}

function degresstoradians(degress)
{
return(degress*math.pi/180)
}

function radianstodegress(radians)
{
return(radians*180/math.pi)
}

//界面
//-->
</script>
<div align=center>
<center>
<table id=autonumber2 style="border-collapse: collapse" bordercolor=#808080
cellspacing=0 cellpadding=0 width=760 border=0>
<tbody>
<tr>
<table id=autonumber5 style="border-collapse: collapse"
bordercolor=#808080 height=30 cellspacing=0 cellpadding=0 width="93%"
border=0>
<tbody>
<td align=middle width="100%" bgcolor=#dbe8d2 height=30>
<p> 
<div align=center>
<form name=calc>
<table height=250 width=500 border=0>
<tbody>
<tr>
<td bgcolor=#ecf4ec height=50>
<table width=500>
<tbody>
<tr>
<td width=486>
<div align=center><input readonly
size=67 value=0
name=display></div></td></tr></tbody></table></td></tr>
<tr>
<td bgcolor=#ecf4ec>
<table width=500>
<tbody>
<tr>
<td width=290><input
onclick=inputchangcarry(16) type=radio name=carry> 十六进制
<input onclick=inputchangcarry(10) type=radio checked
name=carry> 十进制 <input onclick=inputchangcarry(8)
type=radio name=carry> 八进制 <input
onclick=inputchangcarry(2) type=radio name=carry> 二进制
</td>
<td></td>
<td width=135><input
onclick="inputchangangle('d')" type=radio checked
value=d name=angle> 角度制 <input
onclick="inputchangangle('r')" type=radio value=r
name=angle> 弧度制 </td></tr></tbody></table>
<table width=500>
<tbody>
<tr>
<td width=170><input
onclick=inputshift() type=checkbox value=on
name=shiftf>上档功能 <input onclick=inputshift()
type=checkbox value=on name=hypf>双曲函数 </td>
<td><input
style="background-color: lightgrey" readonly size=3
name=bracket> <input style="background-color: lightgrey"
readonly size=3 name=memory> <input
style="background-color: lightgrey" readonly size=3
name=operator> </td>
<td width=183><input style="color: red" onclick=backspace() type=button value=" 退格 ">
<input style="color: red" onclick="document.calc.display.value = 0 " type=button value=" 清屏 ">
<input style="color: red" onclick=clearall() type=button value=" 全清">
</td></tr></tbody></table>
<table width=500>
<tbody>
<tr>
<td>
<table>
<tbody>
<tr align=middle>
<td><input style="color: blue" onclick="inputfunction('pi','pi')" type=button value=" pi " name=pi>
</td>
<td><input style="color: blue" onclick="inputfunction('e','e')" type=button value=" e" name=e>
</td>
<td><input style="color: #ff00ff" onclick="inputfunction('dms','deg')" type=button value=d.ms name=bt>
</td></tr>
<tr align=middle>
<td><input style="color: #ff00ff" onclick=addbracket() type=button value=" (">
</td>
<td><input style="color: #ff00ff" onclick=disbracket() type=button value=" )">
</td>
<td><input style="color: #ff00ff" onclick="inputfunction('ln','exp')" type=button value=" ln " name=ln>
</td></tr>
<tr align=middle>
<td><input style="color: #ff00ff" onclick="inputtrig('sin','arcsin','hypsin','ahypsin')" type=button value="sin " name=sin>
</td>
<td><input style="color: #ff00ff" onclick="operation('^',7)" type=button value="x^y ">
</td>
<td><input style="color: #ff00ff" onclick="inputfunction('log','expdec')" type=button value="log " name=log>
</td></tr>
<tr align=middle>
<td><input style="color: #ff00ff" onclick="inputtrig('cos','arccos','hypcos','ahypcos')" type=button value="cos " name=cos>
</td>
<td><input style="color: #ff00ff" onclick="inputfunction('cube','cubt')" type=button value="x^3 " name=cube>
</td>
<td><input style="color: #ff00ff" onclick="inputfunction('!','!')" type=button value=" n! ">
</td></tr>
<tr align=middle>
<td><input style="color: #ff00ff" onclick="inputtrig('tan','arctan','hyptan','ahyptan')" type=button value="tan " name=tan>
</td>
<td><input style="color: #ff00ff" onclick="inputfunction('sqr','sqrt')" type=button value="x^2 " name=sqr>
</td>
<td><input style="color: #ff00ff" onclick="inputfunction('recip','recip')" type=button value="1/x ">
</td></tr></tbody></table></td>
<td width=30></td>
<td>
<table>
<tbody>
<tr>
<td><input style="color: red" onclick=putmemory() type=button value=" 储存 ">
</td></tr>
<tr>
<td><input style="color: red" onclick=getmemory() type=button value=" 取存 ">
</td></tr>
<tr>
<td><input style="color: red" onclick=addmemory() type=button value=" 累存 ">
</td></tr>
<tr>
<td><input style="color: red" onclick=multimemory() type=button value=" 积存 ">
</td></tr>
<tr>
<td height=33><input style="color: red" onclick=clearmemory() type=button value=" 清存 ">
</td></tr></tbody></table></td>
<td width=30></td>
<td>
<table>
<tbody>
<tr align=middle>
<td><input style="color: blue" onclick="inputkey('7')" type=button value=" 7 " name=k7>
</td>
<td><input style="color: blue" onclick="inputkey('8')" type=button value=" 8 " name=k8>
</td>
<td><input style="color: blue" onclick="inputkey('9')" type=button value=" 9 " name=k9>
</td>
<td><input style="color: red" onclick="operation('/',6)" type=button value=" / ">
</td>
<td><input style="color: red" onclick="operation('%',6)" type=button value=取余>
</td>
<td><input style="color: red" onclick="operation('&',3)" type=button value=" 与 ">
</td></tr>
<tr align=middle>
<td><input style="color: blue" onclick="inputkey('4')" type=button value=" 4 " name=k4>
</td>
<td><input style="color: blue" onclick="inputkey('5')" type=button value=" 5 " name=k5>
</td>
<td><input style="color: blue" onclick="inputkey('6')" type=button value=" 6 " name=k6>
</td>
<td><input style="color: red" onclick="operation('*',6)" type=button value=" * ">
</td>
<td><input style="color: red" onclick="inputfunction('floor','deci')" type=button value=取整 name=floor>
</td>
<td><input style="color: red" onclick="operation('|',1)" type=button value=" 或 ">
</td></tr>
<tr align=middle>
<td><input style="color: blue" onclick="inputkey('1')" type=button value=" 1 ">
</td>
<td><input style="color: blue" onclick="inputkey('2')" type=button value=" 2 " name=k2>
</td>
<td><input style="color: blue" onclick="inputkey('3')" type=button value=" 3 " name=k3>
</td>
<td><input style="color: red" onclick="operation('-',5)" type=button value=" - ">
</td>
<td><input style="color: red" onclick="operation('<',4)" type=button value=左移>
</td>
<td><input style="color: red" onclick="inputfunction('~','~')" type=button value=" 非 ">
</td></tr>
<tr align=middle>
<td><input style="color: blue" onclick="inputkey('0')" type=button value=" 0 ">
</td>
<td><input style="color: blue" onclick=changesign() type=button value=+/->
</td>
<td><input style="color: blue" onclick="inputkey('.')" type=button value=" . " name=kp>
</td>
<td><input style="color: red" onclick="operation('+',5)" type=button value=" + ">
</td>
<td><input style="color: red" onclick=result() type=button value=" = ">
</td>
<td><input style="color: red" onclick="operation('x',2)" type=button value=异或>
</td></tr>
<tr align=middle>
<td><input style="color: blue" disabled onclick="inputkey('a')" type=button value=" a " name=ka>
</td>
<td><input style="color: blue" disabled onclick="inputkey('b')" type=button, value=" b " name=kb>
</td>
<td><input style="color: blue" disabled onclick="inputkey('c')" type=button value=" c " name=kc>
</td>
<td><input style="color: blue" disabled onclick="inputkey('d')" type=button value=" d " name=kd>
</td>
<td><input style="color: blue" disabled onclick="inputkey('e')" type=button value=" e " name=ke>
</td>
<td><input style="color: blue" disabled onclick="inputkey('f')" type=button value=" f " name=kf>
</td></tr></tbody></table></td></tr></tbody></table></td></tr></tbody></table></form></div>
</center></body></html>