<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html lang=en> <head> <meta http-equiv="author" content="T. Nakatsuka"> <meta http-equiv="Content-Type" content="text/html;charset=iso-8859-1"> <meta http-equiv="Content-Script-Type" content="text/javascript"> <title>RPN calculator</title> <!-- =====( NOTICE )===== This is a HTML (+ Javascript) program to realize "RPN caculator" on a Web browser, consisting of a source program file 'calc.html' and an image file 'calc.gif'. [ RPN : Reverse Polish Notation] The author, T. Nakatsuka, does not insist any right on this program, i.e., free to re-distribute to any place with/without any modification, although the "address" section near the end of this source file should be removed in case of modification. The author acknowledges Hewlett-Packard Corp. for their intelligent product of micro-calculators, which were borne in mind during designing. --> <script type="text/javascript"> <!-- var key, ksn; var mem; var sel; /* * key : Status flag for Reg.X (0-7) * 0 : Reg.X shall be cleared by New-Value entry * [after ENTER/CLR/CLX/STO operation] * 1 : CHS input after key=0 * 2 : Reg.X shall be pushed (on stack) by New-Value entry * [after Arithmetic or SWAP/ROT operation, * or definite value entry by RCL/PI/'e'] * 3 : CHS input after key=2 * 4-7 : on the way of New-Value entry * [after Numeric input keys (0-9/.(dot)/EEX)] * 4 : neither .(dot) nor EEX input yet * 5 : .(dot) already input but EEX not yet * 6 : EEX already input but no exponent digit yet * 7 : at least 1 exponent digit already input */ function x_init() { resizeTo(280,450); key = 0; ksn = 0; mem = 0.; sel = ''; } function x_push1() { with (document.f.x) { if (value.substring(0,1) == "-") value = value.substring(1,value.length); else value = "-" + value; } x_push(); } function x_push() { document.f.t.value = document.f.z.value; document.f.z.value = document.f.y.value; document.f.y.value = document.f.x.value; } function x_opr0(sel) { if (key == 6) document.f.x.value = document.f.x.value + '0'; if (sel == 'ent') x_push(); else if (sel == 'clr') { document.f.t.value = '0'; document.f.z.value = '0'; document.f.y.value = '0'; document.f.x.value = '0'; } else if (sel == 'clx') document.f.x.value = '0'; else if (sel == 'sto') mem = eval(document.f.x.value); key = 0; } function x_num(sel) { if (key < 4) { if (key == 2) x_push(); else if (key == 3) x_push1(); if ((key==1) || (key==3)) document.f.x.value = '-'; else document.f.x.value = ''; key = 4; } document.f.x.value = document.f.x.value + sel; if (key == 6) key = 7; } function x_dot() { if (key < 5) { if (key < 4) { if (key == 2) x_push(); else if (key == 3) x_push1(); if ((key==1) || (key==3)) document.f.x.value = '-0'; else document.f.x.value = '0'; } document.f.x.value = document.f.x.value + '.'; key = 5; } } function x_eex() { if (key < 6) { if (key < 4) { if (key == 2) x_push(); else if (key == 3) x_push1(); if ((key==1) || (key==3)) document.f.x.value = '-1.'; else document.f.x.value = '1.'; } document.f.x.value = document.f.x.value + 'E'; key = 6; } } function x_chs() { var len, lce; with (document.f.x) { len = value.length; if (key < 6) { if (key == 0) key = 1; else if (key == 1) key = 0; else if (key == 2) key = 3; else if (key == 3) key = 2; if (value.substring(0,1) == '-') value = value.substring(1,len); else value = '-' + value; } else if (key == 6) { if (value.substring(len-1,len) == 'E') value = value + '-'; else value = value.substring(0,len-1); } else if (key == 7) { lce = value.indexOf('E') + 1; if (value.substring(lce,lce+1) == '-') value = value.substring(0,lce) + value.substring(lce+1,len); else value = value.substring(0,lce) + '-' + value.substring(lce,len); } } } function x_const(sel) { if (key == 6) document.f.x.value = document.f.x.value + '0'; if (key < 2) ; else if (key == 3) x_push1(); else x_push(); if (sel == 'r') { if ((key==1) || (key==3)) { if (mem.substring(0,1) != "-") document.f.x.value = "-" + mem; else document.f.x.value = mem.substring(1,mem.length); } else document.f.x.value = mem; } else if (sel == 'p') { if ((key==1) || (key==3)) document.f.x.value = "-" + Math.PI; else document.f.x.value = Math.PI; } else if (sel == 'e') { if ((key==1) || (key==3)) document.f.x.value = "-" + Math.E; else document.f.x.value = Math.E; } key = 2; } function x_opr2(sel) { var wx, wy; if (key == 6) document.f.x.value = document.f.x.value + '0'; wx = document.f.x.value; wy = document.f.y.value; if (sel == 'swp') { document.f.x.value = wy; document.f.y.value = wx; } else if (sel == 'rot') { document.f.x.value = wy; document.f.y.value = document.f.z.value; document.f.z.value = document.f.t.value; document.f.t.value = wx; } else if (sel == 'pow') { if (eval(wy) <= 0.) alert("Invalid POWER operation (Y <= 0.)"); else { document.f.x.value = Math.pow(wy, wx); document.f.y.value = document.f.z.value; document.f.z.value = document.f.t.value; } } else { if (sel == 'add') document.f.x.value = eval(wy) + eval(wx); else if (sel == 'sub') document.f.x.value = eval(wy) - eval(wx); else if (sel == 'mul') document.f.x.value = eval(wy) * eval(wx); else if (sel == 'div') document.f.x.value = eval(wy) / eval(wx); document.f.y.value = document.f.z.value; document.f.z.value = document.f.t.value; } key = 2; } function x_func(sel) { var tmp; if (key == 6) document.f.x.value = document.f.x.value + '0'; tmp = eval(document.f.x.value); if (sel == 'inv') document.f.x.value = 1 / tmp; else if (sel == 'sqr') document.f.x.value = Math.sqrt(tmp); else if (sel == 'exp') document.f.x.value = Math.exp(tmp); else if (sel == 'log') document.f.x.value = Math.log(tmp); else if (sel == 'p10') document.f.x.value = Math.pow(10,tmp); else if (sel == 'l10') document.f.x.value = Math.log(tmp) / Math.LN10; else if (sel == 'sin') { if (document.f.angl[0].checked) tmp = tmp * Math.PI/180.; document.f.x.value = Math.sin(tmp); } else if (sel == 'cos') { if (document.f.angl[0].checked) tmp = tmp * Math.PI/180.; document.f.x.value = Math.cos(tmp); } else if (sel == 'tan') { if (document.f.angl[0].checked) tmp = tmp * Math.PI/180.; document.f.x.value = Math.tan(tmp); } else { if ((sel != 'atn') && ((tmp<-1.) || (1.<tmp))) alert("Invalid ARCsin/cos operation (X<-1. or 1.<X)"); else { if (sel == 'asn') tmp = Math.asin(tmp); else if (sel == 'acs') tmp = Math.acos(tmp); else if (sel == 'atn') tmp = Math.atan(tmp); if (document.f.angl[0].checked) tmp = tmp * 180./Math.PI; document.f.x.value = tmp; } } key = 2; } --> </script> </head> <body bgcolor="cyan" text=black link=black vlink=black alink=black onload="x_init()"> <form name="f" action="none"> <center><b>RPN calculator</b> (JavaScript)</center> <table border=5 cellspacing=0 width=240 summary="calculator"> <tr><td align=center bgcolor=gray><font size=-1 color=white>Angle: <input name="angl" type=radio id=deg checked><label for=deg>Deg</label> <input name="angl" type=radio id=rad><label for=rad>Rad</label></font> </td></tr> <tr><td align=center bgcolor=black> <font size=-1 color=white> <code>T</code> <input name="t" type=text size=21 value='0'><br> <code>Z</code> <input name="z" type=text size=21 value='0'><br> <code>Y</code> <input name="y" type=text size=21 value='0'><br> <code>X</code> <input name="x" type=text size=21 value='0'></font></td></tr> <tr><td align=center bgcolor=black> <img src="calc.gif" usemap="#calc"> <map name="calc"> <area shape=rect coords="002,002,039,022" href="javascript:x_func('p10')"> <area shape=rect coords="044,002,081,022" href="javascript:x_func('l10')"> <area shape=rect coords="086,002,123,022" href="javascript:x_func('asn')"> <area shape=rect coords="128,002,165,022" href="javascript:x_func('acs')"> <area shape=rect coords="170,002,207,022" href="javascript:x_func('atn')"> <area shape=rect coords="002,027,039,047" href="javascript:x_func('exp')"> <area shape=rect coords="044,027,081,047" href="javascript:x_func('log')"> <area shape=rect coords="086,027,123,047" href="javascript:x_func('sin')"> <area shape=rect coords="128,027,165,047" href="javascript:x_func('cos')"> <area shape=rect coords="170,027,207,047" href="javascript:x_func('tan')"> <area shape=rect coords="002,052,039,072" href="javascript:x_opr2('pow')"> <area shape=rect coords="044,052,081,072" href="javascript:x_func('inv')"> <area shape=rect coords="086,052,123,072" href="javascript:x_func('sqr')"> <area shape=rect coords="128,052,165,072" href="javascript:x_opr2('swp')"> <area shape=rect coords="170,052,207,072" href="javascript:x_opr2('rot')"> <area shape=rect coords="002,077,081,097" href="javascript:x_opr0('ent')"> <area shape=rect coords="086,077,123,097" href="javascript:x_chs()"> <area shape=rect coords="128,077,165,097" href="javascript:x_eex()"> <area shape=rect coords="170,077,207,097" href="javascript:x_opr0('clr')"> <area shape=rect coords="002,102,039,122" href="javascript:x_opr2('div')"> <area shape=rect coords="044,102,081,122" href="javascript:x_num('7')"> <area shape=rect coords="086,102,123,122" href="javascript:x_num('8')"> <area shape=rect coords="128,102,165,122" href="javascript:x_num('9')"> <area shape=rect coords="170,102,207,122" href="javascript:x_opr0('clx')"> <area shape=rect coords="002,127,039,147" href="javascript:x_opr2('mul')"> <area shape=rect coords="044,127,081,147" href="javascript:x_num('4')"> <area shape=rect coords="086,127,123,147" href="javascript:x_num('5')"> <area shape=rect coords="128,127,165,147" href="javascript:x_num('6')"> <area shape=rect coords="170,127,207,147" href="javascript:x_opr0('sto')"> <area shape=rect coords="002,152,039,172" href="javascript:x_opr2('sub')"> <area shape=rect coords="044,152,081,172" href="javascript:x_num('1')"> <area shape=rect coords="086,152,123,172" href="javascript:x_num('2')"> <area shape=rect coords="128,152,165,172" href="javascript:x_num('3')"> <area shape=rect coords="170,152,207,172" href="javascript:x_const('r')"> <area shape=rect coords="002,177,039,197" href="javascript:x_opr2('add')"> <area shape=rect coords="044,177,081,197" href="javascript:x_num('0')"> <area shape=rect coords="086,177,123,197" href="javascript:x_dot()"> <area shape=rect coords="128,177,165,197" href="javascript:x_const('p')"> <area shape=rect coords="170,177,207,197" href="javascript:x_const('e')"> </map> </td></tr> </table> <address><font size=-2 color=green>Designed and Coded by <a href="../../message.html" onclick="resizeTo(800,600)"> <font color=blue>T. Nakatsuka</font></a> </font></address> </form> </body> </html>