Subversion Repositories wimsdev

Rev

Rev 10846 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
5857 schaersvoo 1
 
2
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
3
 
4
<html xml:lang="nl" xmlns="http://www.w3.org/1999/xhtml" xmlns:mathml="http://www.w3.org/1998/Math/MathML">
5
<head>
6
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"/>
7
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
8
<meta name="keywords" content="Javascript wims calculator" />
9
<meta name="description" content="Javascript wims calculator" />
10
<title>WIMS Javascript Calculator</title>
11
</head>
12
<body>
13
<script type="text/javascript">
10846 schaersvoo 14
"use strict";
5857 schaersvoo 15
/*    This calculator makes use of "MathParser" written by: Carlos R. L. Rodrigues
934 schaersvoo 16
    http://jsfromhell.com/classes/math-parser [rev. #2]
17
 
18
    The website for very! unreadable javascript...
19
    -
20
    CHANGELOG
21
 
22
    1/12/2008   Modified and Corrected -few bugs-  and turned into a JS Calculator for practical WIMS usage.
23
    19/1/2009   "e+" and "e-" eval_output converted to "*10^" and "*10^-" format.
24
    19/1/2009   Due to "space limitations" deleted the "x^2" button.
25
    19/1/2009   Added asin(),acos(),atan() for J.C.A. Logmans.
2035 schaersvoo 26
    16/12/2009  Added degrees / radians switch , removed button memory_clear due to space limitations
2046 schaersvoo 27
    19/12/2009  In case of "forgotten matching parenthesis" adding a ')' e.g.  sin(90 -> sin(90) = 1
934 schaersvoo 28
 
29
    J.M. Evers
5857 schaersvoo 30
*/
31
//<![CDATA[
934 schaersvoo 32
 
10846 schaersvoo 33
    var MathParser;
2035 schaersvoo 34
    var radians=0;
35
    var Rconst=eval((Math.PI)/180);
36
    var Iconst=eval(180/(Math.PI));
37
 
934 schaersvoo 38
    MathParser = function(){
10960 schaersvoo 39
        var o = this;var p = o.operator = {};
934 schaersvoo 40
        p["+"] = function(n, m){return n + m;}
41
        p["-"] = function(n, m){return n - m;}
42
        p["*"] = function(n, m){return n * m;}
43
        p["/"] = function(m, n){return n / m;}
44
        p["%"] = function(m, n){return n % m;}
45
        p["^"] = function(m, n){return Math.pow(n, m);}
46
        p["~"] = function(m, n){return Math.sqrt(n, m);}
47
 
48
        o.custom = {}, p.f = function(s, n){
49
            if(s=="log"){
50
                return Math.log(n)/Math.log(10);
51
            }
52
            if(s=="ln"){
53
                return Math.log(n);
54
            }
2035 schaersvoo 55
            if(radians == 0){
56
                if( s == "sin" || s == "cos" || s == "tan"){ return Math[s](n*Rconst);}
57
                else
58
                if( s == "asin" || s == "acos" || s == "atan"){ var r = Math[s](n); return eval(r*Iconst);}
59
            }
934 schaersvoo 60
            if(Math[s]) return Math[s](n);
61
            else if(o.custom[s]) return o.custom[s].apply(o, n);
62
            else throw new Error("Function \"" + s + "\" not defined.");
63
        }, o.add = function(n, f){this.custom[n] = f;}
64
    }
65
 
66
    function make_parenthesis(t){
67
        var end,begin, n = "^+-/*)(",power,arg,p;
68
        var org = t.split("");var fun;var funarg;
69
        var l = org.length;
70
        for(var i = 0; i < l; i++){
71
            if(org[i]=="^"){
72
                power="";arg="";
73
                if("lsct".indexOf(org[i+1])!=-1){
74
                    begin=0;end=0;p=i+1;fun="";funarg="";
75
                    while(p<l){
76
                        if(begin==0){if(org[p]=="("){begin=p;}}
77
                        if(end==0){if(org[p]==")"){end=p;}}
78
                        p++;
79
                    }
80
                    for(p=i+1;p<begin;p++){fun=fun+org[p];}
81
                    for(p=begin+1;p<end;p++){funarg=funarg+org[p];}
82
                    power="("+fun+"("+funarg+"))";
83
                }
84
                if(org[i+1]=="("){
85
                    begin=0;end=0;p=i+1;
86
                    while(p<l){
87
                        if(org[p]=="("){begin++;}
88
                        if(org[p]==")"){end++;}
89
                        if(begin != end){power=power+""+org[p];}
90
                        if(begin == end){p=l;}else{p++;}
91
                    }
92
                }
93
                if(org[i-1]==")"){
94
                    begin=0;end=0;p=i-1;
95
                    while(p>-1){
96
                        if(org[p]=="("){begin++;}
97
                        if(org[p]==")"){end++;}
98
                        if(begin != end){arg=org[p]+arg;}
99
                        if(begin==end){p=-1;}else{p--;}
100
                    }
101
                }
102
                if(power==""){
103
                    end=i+1;
104
                    if(org[i+1]=="-" || org[i+1]=="+"){
105
                        power=org[i+1];
106
                        end=i+2;
107
                    }
108
                    while(( n.indexOf(org[end]) == -1) && end < l){
109
                        power=power+""+org[end];
110
                        end++;
111
                    }
112
                }
113
                if(arg==""){
114
                    begin=i-1;
115
                    while(( n.indexOf(org[begin]) == -1) && begin > -1){
116
                        arg=org[begin]+""+arg;
117
                        begin--;
118
                    }
119
                }
120
                t=t.replace(arg+"^"+power,"("+arg+"^("+power+"))");
121
                org=t.split("");
122
                i=i+4;
123
                l=org.length;
124
            }
125
        }
126
        return t;
127
    }
128
 
129
    MathParser.prototype.eval = function(argument){
130
        var v = [], p = [], a, c = 0, s = 0, x, t, d = 0;
131
        var n = "0123456789.", o = "+-*/^%~", f = this.operator;
132
        var e = argument.split("");
133
 
134
        for(var i = 0, l = e.length; i < l; i++){
135
                if(o.indexOf(e[i]) > -1){
136
                    e[i] == "-" && (s > 1 || !d) && ++s, !s && d && (p.push(e[i]), s = 2), "+-".indexOf(e[i]) < (d = 0) && (c = 1);
137
                }
138
                else
139
                {
140
                    if(a = n.indexOf(e[i]) + 1 ? e[i++] : ""){
141
                        while(n.indexOf(e[i]) + 1){ a += e[i++];}
142
                        v.push(d = (s & 1 ? -1 : 1) * a), c && v.push(f[p.pop()](v.pop(), v.pop())) && (c = 0), --i, s = 0;
143
                    }
144
                }
145
        }
146
        for(c = v[0], i = 0, l = p.length; l--; c = f[p[i]](c, v[++i])){}
147
        return c;
148
    }
149
 
150
    MathParser.prototype.parse = function(argument){
151
        var e=make_parenthesis(argument);
10960 schaersvoo 152
        var p = [], f = [], y, ag, n, l, c, a, o = this, v = "0123456789.+-*/^%~(, )";
934 schaersvoo 153
        for(var x, i = 0, l = e.length; i < l; i++){
154
            if(v.indexOf(c = e.charAt(i)) < 0){
155
                for(a = c; v.indexOf(c = e.charAt(++i)) < 0; a += c); f.push((--i, a));
156
            }
157
            else if(!(c == "(" && p.push(i)) && c == ")"){
158
                if(a = e.slice(0, (n = p.pop()) - (x = v.indexOf(e.charAt(n - 1)) < 0 ? y = (c = f.pop()).length : 0)), x)
159
                    for(var j = (ag = e.slice(n, ++i).split(",")).length; j--; ag[j] = o.eval(ag[j]));
160
                        l = (e = a + (x ? o.operator.f(c, ag) : o.eval(e.slice(n, ++i))) + e.slice(i)).length, i -= i - n + c.length;
161
            }
162
        }
163
        while(e.indexOf("e")!=-1){e=e.replace(/e/g,"*10^");}
164
        return o.eval(e);
165
    }
5857 schaersvoo 166
//]]>
934 schaersvoo 167
</script>
168
<script type="text/javascript">
5857 schaersvoo 169
//<![CDATA[
934 schaersvoo 170
    var memory="";
171
    function rawmath(i){
172
        i=i.toLowerCase();i=i.replace(/\ /g,"");i=i.replace(/\*\*/g,"^");
173
        i=i.replace(/\u03c0/g,"pi");i=i.replace(/\u212e/g,"e");
174
        if(i.indexOf("e+")!=-1){i=i.replace("e+","*10^");}
175
        if(i.indexOf("e-")!=-1){i=i.replace("e-","*10^-");}
176
        i=i.replace(/\*\*/g,"*");if(i.charAt(0)=="*"){i=i.substring(1,i.length);}      
177
        var fun=["asin","acos","atan","sin","cos","tan","log","ln","pi","e"];
178
        var cons=["pi","e","0","1","2","3","4","5","6","7","8","9"];
179
        var P;var D;var p;var d;
2046 schaersvoo 180
        var l=cons.length;var cntl=0;var cntr=0;
181
        for(p=0;p<i.length;p++){
182
            if(i.charAt(p) == '('){cntl++;}
183
            if(i.charAt(p) == ')'){cntr++;}
184
        }
185
        if(cntl != cntr){i = i+')';}
934 schaersvoo 186
        for(p=0;p<fun.length;p++){
187
            for(d=0;d<l;d++){
188
                while(i.indexOf(cons[d]+""+fun[p])!=-1){
189
                    i=i.replace(cons[d]+""+fun[p],cons[d]+"*"+fun[p]);
190
                }
191
                while(i.indexOf(fun[p]+""+cons[d])!=-1){
192
                    i=i.replace(fun[p]+""+cons[d],fun[p]+"*"+cons[d]);
193
                }
194
            }
195
        }
196
        if(i.indexOf("(")!=-1){
197
            for(p=0;p<l;p++){
198
                if(i.indexOf(cons[p]+"(")!=-1){
199
                    i=i.replace(cons[p]+"(",cons[p]+"*(");
200
                }
201
                if(i.indexOf(")"+cons[p])!=-1){
202
                    i=i.replace(")"+cons[p],")*"+cons[p]);
203
                }
204
            }
205
            i=i.replace(/\)\(/g,")*(");
206
        }
207
        var PI=Math.PI;var E=Math.E;
208
        while(i.indexOf("pi")!=-1){
209
            i=i.replace(/pi/g,PI);
210
        }
211
        while(i.indexOf("e")!=-1){
212
            i=i.replace(/e/g,E);
213
        }
214
        return i;
215
    }
216
 
217
    function memory_in(){
218
        memory=document.Calc.Input.value;
219
        memory=memory.replace("m:","");
2035 schaersvoo 220
        document.Calc.Input.value="";
221
        var mi=document.getElementById('mem_in');
222
        mi.setAttribute("style","background-color:red;font-weight:normal;color:white;text-align:center;font-size:12px;width:40px;height:30px");
223
        mi.setAttribute("value","M...");
934 schaersvoo 224
    }
225
 
2035 schaersvoo 226
    function memory_out(){
934 schaersvoo 227
        var tmp=document.Calc.Input.value;
2035 schaersvoo 228
        document.Calc.Input.value="";
229
        document.Calc.Input.value=tmp+"("+memory+")";
230
        var mi=document.getElementById('mem_in');
231
        mi.setAttribute("style","background-color:black;font-weight:normal;color:white;text-align:center;font-size:12px;width:40px;height:30px");
232
        mi.setAttribute("value","M in");
934 schaersvoo 233
    }
234
 
2035 schaersvoo 235
    function set_radians(){
236
        var ra=document.getElementById('radian_switch');
237
        if(radians == 1){
238
            radians = 0;
239
            ra.setAttribute("style","background-color:black;font-weight:normal;color:white;text-align:center;font-size:12px;width:45px;height:30px");    
240
            ra.setAttribute("value","deg");
934 schaersvoo 241
        }
2035 schaersvoo 242
        else
243
        {
244
            radians=1;
245
            ra.setAttribute("style","background-color:red;font-weight:normal;color:white;text-align:center;font-size:12px;width:45px;height:30px");    
246
            ra.setAttribute("value","rad");
247
        }
934 schaersvoo 248
    }
249
 
250
    function rekenuit(){
251
        var precision=100000000;
252
        var parser = new MathParser()
253
        var input=document.Calc.Input.value;
254
        input=input.replace("m:","");
255
        input=rawmath(input);
256
        try{
257
            var result=parser.parse(input);
258
            result=(eval((Math.round(precision*result))/precision)).toString();
259
            if(result.indexOf("e+")!=-1){result=result.replace("e+","*10^");}
260
            if(result.indexOf("e-")!=-1){result=result.replace("e-","*10^-");}
261
            document.Calc.Input.value=result;
262
        }catch(e){alert("MATH ERROR\n"+e);}
263
    }
5857 schaersvoo 264
//]]>
934 schaersvoo 265
</script>
266
<form name="Calc" action="">
5857 schaersvoo 267
<table summary="" border="1" bgcolor="black">
934 schaersvoo 268
    <tr>
5857 schaersvoo 269
    <td nowrap="nowrap" align="center">
270
        <input type="text" name="Input" size="12" style="font-size:22px;background-color:lightgreen" />
934 schaersvoo 271
    </td>
272
    </tr>
273
    <tr>
5857 schaersvoo 274
    <td width="100%" nowrap="nowrap" align="center">
275
            <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();" />
276
            <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()" />
277
            <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()" />
278
            <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()" />
934 schaersvoo 279
    </td>
280
    </tr>
1094 schaersvoo 281
    <tr>
5857 schaersvoo 282
    <td nowrap="nowrap">
1094 schaersvoo 283
        <table summary="">
934 schaersvoo 284
            <tr>
285
            <th>
1094 schaersvoo 286
                <table summary="">
934 schaersvoo 287
                    <tr>
5857 schaersvoo 288
                    <td bgcolor="black" nowrap="nowrap">
5731 schaersvoo 289
                        <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'" />
290
                        <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'" />
291
                        <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'" />
934 schaersvoo 292
                    </td>
293
                    </tr><tr>
5857 schaersvoo 294
                    <td nowrap="nowrap">
5731 schaersvoo 295
                        <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'" />
296
                        <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'" />
297
                        <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'" />
934 schaersvoo 298
                    </td>
299
                    </tr><tr>
5857 schaersvoo 300
                    <td nowrap="nowrap">
5731 schaersvoo 301
                        <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'" />
302
                        <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'" />
303
                        <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'" />
934 schaersvoo 304
                    </td>
305
                    </tr><tr>
5857 schaersvoo 306
                    <td nowrap="nowrap">
5731 schaersvoo 307
                        <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'" />
308
                        <input style="background-color:white;font-weight:normal;text-align:center;font-size:17px;width:30px;height:30px"  type="button" name="pi"  value="&pi;"         onclick="Calc.Input.value += '\u03c0'" />
309
                        <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'" />
934 schaersvoo 310
                    </td>
311
                    </tr><tr>
5857 schaersvoo 312
                    <td nowrap="nowrap">
5731 schaersvoo 313
                        <input style="font-weight:normal;text-align:center;font-size:17px;width:30px;height:30px" type="button" name="left"   value="(" onclick="Calc.Input.value += '('" />
314
                        <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 += '.'" />
315
                        <input style="font-weight:normal;text-align:center;font-size:17px;width:30px;height:30px" type="button" name="right"   value=")" onclick="Calc.Input.value += ')'" />
934 schaersvoo 316
                    </td>
317
                    </tr><tr>
5857 schaersvoo 318
                    <td style="height:30px" nowrap="nowrap">
319
                        <hr />
934 schaersvoo 320
                    </td>
321
                    </tr><tr>
5857 schaersvoo 322
                    <td nowrap="nowrap">
5731 schaersvoo 323
                        <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();" />
324
                        <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 = ''" />
934 schaersvoo 325
                    </td>
5857 schaersvoo 326
                    </tr>
934 schaersvoo 327
                </table>
328
            </th>
329
            <th>
1094 schaersvoo 330
                <table summary="">
934 schaersvoo 331
                    <tr>
5857 schaersvoo 332
                    <td bgcolor="black" nowrap="nowrap">
5731 schaersvoo 333
                        <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 += '+'" />
334
                        <input style="background-color:#ccffff;font-weight:normal;text-align:center;font-size:17px;width:45px;height:30px"  type="button" name="minus" value="&minus;" onclick="Calc.Input.value += '-'" />
934 schaersvoo 335
                    </td>
336
                    </tr><tr>
5857 schaersvoo 337
                    <td nowrap="nowrap">
5731 schaersvoo 338
                        <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 += '*'" />
339
                        <input style="background-color:#ccffff;font-weight:normal;text-align:center;font-size:17px;width:45px;height:30px"  type="button" name="div"   value="&frasl;" onclick="Calc.Input.value += '/'" />
934 schaersvoo 340
                    </td>
341
                    </tr><tr>
5857 schaersvoo 342
                    <td nowrap="nowrap">
5731 schaersvoo 343
                        <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 += '^'" />
344
                        <input style="background-color:#ccffff;font-weight:normal;text-align:center;font-size:17px;width:45px;height:30px"  type="button" name="sqrt"  value="&#8730;"  onclick="Calc.Input.value += 'sqrt('" />
934 schaersvoo 345
                    </td>
346
                    </tr><tr>
5857 schaersvoo 347
                    <td nowrap="nowrap">
5731 schaersvoo 348
                        <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('" />
349
                        <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('" />
934 schaersvoo 350
                    </td>
351
                    </tr><tr>
5857 schaersvoo 352
                    <td nowrap="nowrap">
5731 schaersvoo 353
                        <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('" />
354
                        <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('" />
934 schaersvoo 355
                    </td>
356
                    </tr><tr>
5857 schaersvoo 357
                    <td nowrap="nowrap">
5731 schaersvoo 358
                        <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('" />
359
                        <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('" />
934 schaersvoo 360
                    </td>
361
                    </tr><tr>
5857 schaersvoo 362
                    <td nowrap="nowrap">
5731 schaersvoo 363
                        <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('" />
364
                        <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('" />
5857 schaersvoo 365
                    </td>
366
                    </tr>
367
<!--                <td nowrap="nowrap">
5731 schaersvoo 368
                        <input style="background-color:#adffff;font-weight:normal;text-align:center;font-size:17px;width:45px;height:30px"  type="button" name="^3"  value="x&sup3"     onclick="Calc.Input.value += '^3'" />
369
                        <input style="background-color:#adffff;font-weight:normal;text-align:center;font-size:17px;width:45px;height:30px"  type="button" name="^2"  value="x&sup2;"    onclick="Calc.Input.value += '^2'" />  
934 schaersvoo 370
                    </td>
371
-->                
372
                </table>
373
            </th>
374
            </tr>
375
        </table>
376
    </td>
377
    </tr>
378
</table>
379
</form>
380
</body>
381
</html>
382