<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<
html xml:
lang="nl" xmlns
="http://www.w3.org/1999/xhtml" xmlns:mathml
="http://www.w3.org/1998/Math/MathML">
<
meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"/>
<
meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
<
meta name="keywords" content="Javascript wims calculator" />
<
meta name="description" content="Javascript wims calculator" />
<
script type="text/javascript">
"use strict";
/* This calculator makes use of "MathParser" written by: Carlos R. L. Rodrigues
http://jsfromhell.com/classes/math-parser [rev. #2]
The website for very! unreadable javascript...
-
CHANGELOG
1/12/2008 Modified and Corrected -few bugs- and turned into a JS Calculator for practical WIMS usage.
19/1/2009 "e+" and "e-" eval_output converted to "*10^" and "*10^-" format.
19/1/2009 Due to "space limitations" deleted the "x^2" button.
19/1/2009 Added asin(),acos(),atan() for J.C.A. Logmans.
16/12/2009 Added degrees / radians switch , removed button memory_clear due to space limitations
19/12/2009 In case of "forgotten matching parenthesis" adding a ')' e.g. sin(90 -> sin(90) = 1
J.M. Evers
*/
//<![CDATA[
var MathParser;
var radians=0;
var Rconst=eval((Math.PI)/180);
var Iconst=eval(180/(Math.PI));
MathParser = function(){
var o = this;var p = o.operator = {};
p["+"] = function(n, m){return n + m;}
p["-"] = function(n, m){return n - m;}
p["*"] = function(n, m){return n * m;}
p["/"] = function(m, n){return n / m;}
p["%"] = function(m, n){return n % m;}
p["^"] = function(m, n){return Math.pow(n, m);}
p["~"] = function(m, n){return Math.sqrt(n, m);}
o.custom = {}, p.f = function(s, n){
if(s=="log"){
return Math.log(n)/Math.log(10);
}
if(s=="ln"){
return Math.log(n);
}
if(radians == 0){
if( s == "sin" || s == "cos" || s == "tan"){ return Math[s](n*Rconst);}
else
if( s == "asin" || s == "acos" || s == "atan"){ var r = Math[s](n); return eval(r*Iconst);}
}
if(Math[s]) return Math[s](n);
else if(o.custom[s]) return o.custom[s].apply(o, n);
else throw new Error("Function \"" + s + "\" not defined.");
}, o.add = function(n, f){this.custom[n] = f;}
}
function make_parenthesis(t){
var end,begin, n = "^+-/*)(",power,arg,p;
var org = t.split("");var fun;var funarg;
var l = org.length;
for(var i = 0; i < l; i++){
if(org[i]=="^"){
power="";arg="";
if("lsct".indexOf(org[i+1])!=-1){
begin=0;end=0;p=i+1;fun="";funarg="";
while(p<l){
if(begin==0){if(org[p]=="("){begin=p;}}
if(end==0){if(org[p]==")"){end=p;}}
p++;
}
for(p=i+1;p<begin;p++){fun=fun+org[p];}
for(p=begin+1;p<end;p++){funarg=funarg+org[p];}
power="("+fun+"("+funarg+"))";
}
if(org[i+1]=="("){
begin=0;end=0;p=i+1;
while(p<l){
if(org[p]=="("){begin++;}
if(org[p]==")"){end++;}
if(begin != end){power=power+""+org[p];}
if(begin == end){p=l;}else{p++;}
}
}
if(org[i-1]==")"){
begin=0;end=0;p=i-1;
while(p>-1){
if(org[p]=="("){begin++;}
if(org[p]==")"){end++;}
if(begin != end){arg=org[p]+arg;}
if(begin==end){p=-1;}else{p--;}
}
}
if(power==""){
end=i+1;
if(org[i+1]=="-" || org[i+1]=="+"){
power=org[i+1];
end=i+2;
}
while(( n.indexOf(org[end]) == -1) && end < l){
power=power+""+org[end];
end++;
}
}
if(arg==""){
begin=i-1;
while(( n.indexOf(org[begin]) == -1) && begin > -1){
arg=org[begin]+""+arg;
begin--;
}
}
t=t.replace(arg+"^"+power,"("+arg+"^("+power+"))");
org=t.split("");
i=i+4;
l=org.length;
}
}
return t;
}
MathParser.prototype.eval = function(argument){
var v = [], p = [], a, c = 0, s = 0, x, t, d = 0;
var n = "0123456789.", o = "+-*/^%~", f = this.operator;
var e = argument.split("");
for(var i = 0, l = e.length; i < l; i++){
if(o.indexOf(e[i]) > -1){
e[i] == "-" && (s > 1 || !d) && ++s, !s && d && (p.push(e[i]), s = 2), "+-".indexOf(e[i]) < (d = 0) && (c = 1);
}
else
{
if(a = n.indexOf(e[i]) + 1 ? e[i++] : ""){
while(n.indexOf(e[i]) + 1){ a += e[i++];}
v.push(d = (s & 1 ? -1 : 1) * a), c && v.push(f[p.pop()](v.pop(), v.pop())) && (c = 0), --i, s = 0;
}
}
}
for(c = v[0], i = 0, l = p.length; l--; c = f[p[i]](c, v[++i])){}
return c;
}
MathParser.prototype.parse = function(argument){
var e=make_parenthesis(argument);
var p = [], f = [], y, ag, n, l, c, a, o = this, v = "0123456789.+-*/^%~(, )";
for(var x, i = 0, l = e.length; i < l; i++){
if(v.indexOf(c = e.charAt(i)) < 0){
for(a = c; v.indexOf(c = e.charAt(++i)) < 0; a += c); f.push((--i, a));
}
else if(!(c == "(" && p.push(i)) && c == ")"){
if(a = e.slice(0, (n = p.pop()) - (x = v.indexOf(e.charAt(n - 1)) < 0 ? y = (c = f.pop()).length : 0)), x)
for(var j = (ag = e.slice(n, ++i).split(",")).length; j--; ag[j] = o.eval(ag[j]));
l = (e = a + (x ? o.operator.f(c, ag) : o.eval(e.slice(n, ++i))) + e.slice(i)).length, i -= i - n + c.length;
}
}
while(e.indexOf("e")!=-1){e=e.replace(/e/g,"*10^");}
return o.eval(e);
}
//]]>
<
script type="text/javascript">
//<![CDATA[
var memory="";
function rawmath(i){
i=i.toLowerCase();i=i.replace(/\ /g,"");i=i.replace(/\*\*/g,"^");
i=i.replace(/\u03c0/g,"pi");i=i.replace(/\u212e/g,"e");
if(i.indexOf("e+")!=-1){i=i.replace("e+","*10^");}
if(i.indexOf("e-")!=-1){i=i.replace("e-","*10^-");}
i=i.replace(/\*\*/g,"*");if(i.charAt(0)=="*"){i=i.substring(1,i.length);}
var fun=["asin","acos","atan","sin","cos","tan","log","ln","pi","e"];
var cons=["pi","e","0","1","2","3","4","5","6","7","8","9"];
var P;var D;var p;var d;
var l=cons.length;var cntl=0;var cntr=0;
for(p=0;p<i.length;p++){
if(i.charAt(p) == '('){cntl++;}
if(i.charAt(p) == ')'){cntr++;}
}
if(cntl != cntr){i = i+')';}
for(p=0;p<fun.length;p++){
for(d=0;d<l;d++){
while(i.indexOf(cons[d]+""+fun[p])!=-1){
i=i.replace(cons[d]+""+fun[p],cons[d]+"*"+fun[p]);
}
while(i.indexOf(fun[p]+""+cons[d])!=-1){
i=i.replace(fun[p]+""+cons[d],fun[p]+"*"+cons[d]);
}
}
}
if(i.indexOf("(")!=-1){
for(p=0;p<l;p++){
if(i.indexOf(cons[p]+"(")!=-1){
i=i.replace(cons[p]+"(",cons[p]+"*(");
}
if(i.indexOf(")"+cons[p])!=-1){
i=i.replace(")"+cons[p],")*"+cons[p]);
}
}
i=i.replace(/\)\(/g,")*(");
}
var PI=Math.PI;var E=Math.E;
while(i.indexOf("pi")!=-1){
i=i.replace(/pi/g,PI);
}
while(i.indexOf("e")!=-1){
i=i.replace(/e/g,E);
}
return i;
}
function memory_in(){
memory=document.Calc.Input.value;
memory=memory.replace("m:","");
document.Calc.Input.value="";
var mi=document.getElementById('mem_in');
mi.setAttribute("style","background-color:red;font-weight:normal;color:white;text-align:center;font-size:12px;width:40px;height:30px");
mi.setAttribute("value","M...");
}
function memory_out(){
var tmp=document.Calc.Input.value;
document.Calc.Input.value="";
document.Calc.Input.value=tmp+"("+memory+")";
var mi=document.getElementById('mem_in');
mi.setAttribute("style","background-color:black;font-weight:normal;color:white;text-align:center;font-size:12px;width:40px;height:30px");
mi.setAttribute("value","M in");
}
function set_radians(){
var ra=document.getElementById('radian_switch');
if(radians == 1){
radians = 0;
ra.setAttribute("style","background-color:black;font-weight:normal;color:white;text-align:center;font-size:12px;width:45px;height:30px");
ra.setAttribute("value","deg");
}
else
{
radians=1;
ra.setAttribute("style","background-color:red;font-weight:normal;color:white;text-align:center;font-size:12px;width:45px;height:30px");
ra.setAttribute("value","rad");
}
}
function rekenuit(){
var precision=100000000;
var parser = new MathParser()
var input=document.Calc.Input.value;
input=input.replace("m:","");
input=rawmath(input);
try{
var result=parser.parse(input);
result=(eval((Math.round(precision*result))/precision)).toString();
if(result.indexOf("e+")!=-1){result=result.replace("e+","*10^");}
if(result.indexOf("e-")!=-1){result=result.replace("e-","*10^-");}
document.Calc.Input.value=result;
}catch(e){alert("MATH ERROR\n"+e);}
}
//]]>
<
form name="Calc" action="">
<
table summary="" border="1" bgcolor="black">
<
td nowrap="nowrap" align="center">
<
input type="text" name="Input" size="12" style="font-size:22px;background-color:lightgreen" />
<
td width="100%" nowrap="nowrap" align="center">
<
input id="radian_switch" style="background-color:black;font-weight:normal;color:white;text-align:center;font-size:12px;width:45px;height:30px" type="button" name="deg;" value="deg" onclick="javascript:set_radians();" />
<
input style="background-color:black;font-weight:normal;color:white;text-align:center;font-size:12px;width:45px;height:30px" type="button" name="mem_in" id="mem_in" value="M in" onclick="javascript:memory_in()" />
<
input style="background-color:black;font-weight:normal;color:white;text-align:center;font-size:12px;width:45px;height:30px" type="button" name="mem_out" value="Mout" onclick="javascript:memory_out()" />
<
input style="background-color:red;font-weight:normal;color:black;text-align:center;font-size:12px;width:45px;height:30px" type="button" name="exit" value="Exit" onclick="javascript:window.close()" />
<
td bgcolor="black" nowrap="nowrap">
<
input style="background-color:white;font-weight:normal;text-align:center;font-size:17px;width:30px;height:30px" type="button" name="one" value="1" onclick="Calc.Input.value += '1'" />
<
input style="background-color:white;font-weight:normal;text-align:center;font-size:17px;width:30px;height:30px" type="button" name="two" value="2" onclick="Calc.Input.value += '2'" />
<
input style="background-color:white;font-weight:normal;text-align:center;font-size:17px;width:30px;height:30px" type="button" name="three" value="3" onclick="Calc.Input.value += '3'" />
<
input style="background-color:white;font-weight:normal;text-align:center;font-size:17px;width:30px;height:30px" type="button" name="four" value="4" onclick="Calc.Input.value += '4'" />
<
input style="background-color:white;font-weight:normal;text-align:center;font-size:17px;width:30px;height:30px" type="button" name="five" value="5" onclick="Calc.Input.value += '5'" />
<
input style="background-color:white;font-weight:normal;text-align:center;font-size:17px;width:30px;height:30px" type="button" name="six" value="6" onclick="Calc.Input.value += '6'" />
<
input style="background-color:white;font-weight:normal;text-align:center;font-size:17px;width:30px;height:30px" type="button" name="seven" value="7" onclick="Calc.Input.value += '7'" />
<
input style="background-color:white;font-weight:normal;text-align:center;font-size:17px;width:30px;height:30px" type="button" name="eight" value="8" onclick="Calc.Input.value += '8'" />
<
input style="background-color:white;font-weight:normal;text-align:center;font-size:17px;width:30px;height:30px" type="button" name="nine" value="9" onclick="Calc.Input.value += '9'" />
<
input style="background-color:white;font-weight:normal;text-align:center;font-size:17px;width:30px;height:30px" type="button" name="zero" value="0" onclick="Calc.Input.value += '0'" />
<
input style="background-color:white;font-weight:normal;text-align:center;font-size:17px;width:30px;height:30px" type="button" name="pi" value="π" onclick="Calc.Input.value += '\u03c0'" />
<
input style="background-color:white;font-weight:normal;text-align:center;font-size:17px;width:30px;height:30px" type="button" name="e" value="e" onclick="Calc.Input.value += '\u212e'" />
<
input style="font-weight:normal;text-align:center;font-size:17px;width:30px;height:30px" type="button" name="left" value="(" onclick="Calc.Input.value += '('" />
<
input style="background-color:white;font-weight:normal;text-align:center;font-size:17px;width:30px;height:30px" type="button" name="punt" value="." onclick="Calc.Input.value += '.'" />
<
input style="font-weight:normal;text-align:center;font-size:17px;width:30px;height:30px" type="button" name="right" value=")" onclick="Calc.Input.value += ')'" />
<
td style="height:30px" nowrap="nowrap">
<
input style="background-color:lightgreen;font-weight:normal;text-align:center;font-size:17px;width:45px;height:30px" type="button" name="DoIt" value="=" onclick="javascript:rekenuit();" />
<
input style="background-color:orange;font-weight:normal;text-align:center;font-size:17px;width:45px;height:30px" type="button" name="clear" value="C" onclick="Calc.Input.value = ''" />
<
td bgcolor="black" nowrap="nowrap">
<
input style="background-color:#ccffff;font-weight:normal;text-align:center;font-size:17px;width:45px;height:30px" type="button" name="plus" value="+" onclick="Calc.Input.value += '+'" />
<
input style="background-color:#ccffff;font-weight:normal;text-align:center;font-size:17px;width:45px;height:30px" type="button" name="minus" value="−" onclick="Calc.Input.value += '-'" />
<
input style="background-color:#ccffff;font-weight:normal;text-align:center;font-size:17px;width:45px;height:30px" type="button" name="times" value="x" onclick="Calc.Input.value += '*'" />
<
input style="background-color:#ccffff;font-weight:normal;text-align:center;font-size:17px;width:45px;height:30px" type="button" name="div" value="⁄" onclick="Calc.Input.value += '/'" />
<
input style="background-color:#ccffff;font-weight:normal;text-align:center;font-size:17px;width:45px;height:30px" type="button" name="pwr" value="^" onclick="Calc.Input.value += '^'" />
<
input style="background-color:#ccffff;font-weight:normal;text-align:center;font-size:17px;width:45px;height:30px" type="button" name="sqrt" value="√" onclick="Calc.Input.value += 'sqrt('" />
<
input style="background-color:#afffff;font-style:italic;font-weight:normal;text-align:center;font-size:17px;width:45px;height:30px" type="button" name="log" value="log" onclick="Calc.Input.value += 'log('" />
<
input style="background-color:#afffff;font-style:italic;font-weight:normal;text-align:center;font-size:17px;width:45px;height:30px" type="button" name="ln" value="ln" onclick="Calc.Input.value += 'ln('" />
<
input style="background-color:#afffff;font-style:italic;font-weight:normal;text-align:center;font-size:17px;width:45px;height:30px" type="button" name="sin" value="sin" onclick="Calc.Input.value += 'sin('" />
<
input style="background-color:#afffff;font-style:italic;font-weight:normal;text-align:center;font-size:17px;width:45px;height:30px" type="button" name="asin" value="asin" onclick="Calc.Input.value += 'asin('" />
<
input style="background-color:#afffff;font-style:italic;font-weight:normal;text-align:center;font-size:17px;width:45px;height:30px" type="button" name="cos" value="cos" onclick="Calc.Input.value += 'cos('" />
<
input style="background-color:#afffff;font-style:italic;font-weight:normal;text-align:center;font-size:17px;width:45px;height:30px" type="button" name="acos" value="acos" onclick="Calc.Input.value += 'acos('" />
<
input style="background-color:#afffff;font-style:italic;font-weight:normal;text-align:center;font-size:17px;width:45px;height:30px" type="button" name="tan" value="tan" onclick="Calc.Input.value += 'tan('" />
<
input style="background-color:#afffff;font-style:italic;font-weight:normal;text-align:center;font-size:17px;width:45px;height:30px" type="button" name="atan" value="atan" onclick="Calc.Input.value += 'atan('" />
<!-- <
td nowrap="nowrap">
<
input style="background-color:#adffff;font-weight:normal;text-align:center;font-size:17px;width:45px;height:30px" type="button" name="^3" value="x³" onclick="Calc.Input.value += '^3'" />
<
input style="background-color:#adffff;font-weight:normal;text-align:center;font-size:17px;width:45px;height:30px" type="button" name="^2" value="x²" onclick="Calc.Input.value += '^2'" />
-->