tribute
2 years ago
autoNumeric-1.9.45.js
2 years ago
calculator.css
2 years ago
calculator.js
2 years ago
calculator_editor.js
2 years ago
formula_evaluator-min.js
2 years ago
wpforms-calculator-admin.js
2 years ago
formula_evaluator-min.js
927 lines
| 1 | /** math-expression-evaluator version 1.3.8 |
| 2 | Dated:2021-07-03 */ |
| 3 | |
| 4 | (function(f){if(typeof exports==="object"&&typeof module!=="undefined"){module.exports=f()}else if(typeof define==="function"&&define.amd){define([],f)}else{var g;if(typeof window!=="undefined"){g=window}else if(typeof global!=="undefined"){g=global}else if(typeof self!=="undefined"){g=self}else{g=this}g.mexp = f()}})(function(){var define,module,exports;return (function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(require,module,exports){ |
| 5 | var Mexp=require('./postfix_evaluator.js'); |
| 6 | Mexp.prototype.formulaEval = function () { |
| 7 | "use strict"; |
| 8 | var stack=[],pop1,pop2,pop3; |
| 9 | var disp=[]; |
| 10 | var temp=''; |
| 11 | var arr=this.value; |
| 12 | for(var i=0;i<arr.length;i++){ |
| 13 | if(arr[i].type===1||arr[i].type===3){ |
| 14 | disp.push({value:arr[i].type===3?arr[i].show:arr[i].value,type:1}); |
| 15 | } |
| 16 | else if(arr[i].type===13){ |
| 17 | disp.push({value:arr[i].show,type:1}); |
| 18 | } |
| 19 | else if(arr[i].type===0){ |
| 20 | disp[disp.length-1]={value:arr[i].show+(arr[i].show!="-"?"(":"")+disp[disp.length-1].value+(arr[i].show!="-"?")":""),type:0}; |
| 21 | } |
| 22 | else if(arr[i].type===7){ |
| 23 | disp[disp.length-1]={value:(disp[disp.length-1].type!=1?"(":"")+disp[disp.length-1].value+(disp[disp.length-1].type!=1?")":"")+arr[i].show,type:7}; |
| 24 | } |
| 25 | else if(arr[i].type===10){ |
| 26 | pop1=disp.pop(); |
| 27 | pop2=disp.pop(); |
| 28 | if(arr[i].show==='P'||arr[i].show==='C')disp.push({value:"<sup>"+pop2.value+"</sup>"+arr[i].show+"<sub>"+pop1.value+"</sub>",type:10}); |
| 29 | else disp.push({value:(pop2.type!=1?"(":"")+pop2.value+(pop2.type!=1?")":"")+"<sup>"+pop1.value+"</sup>",type:1}); |
| 30 | } |
| 31 | else if(arr[i].type===2||arr[i].type===9){ |
| 32 | pop1=disp.pop(); |
| 33 | pop2=disp.pop(); |
| 34 | disp.push({value:(pop2.type!=1?"(":"")+pop2.value+(pop2.type!=1?")":"")+arr[i].show+(pop1.type!=1?"(":"")+pop1.value+(pop1.type!=1?")":""),type:arr[i].type}); |
| 35 | } |
| 36 | else if(arr[i].type===12){ |
| 37 | pop1=disp.pop(); |
| 38 | pop2=disp.pop(); |
| 39 | pop3=disp.pop(); |
| 40 | disp.push({value:arr[i].show+"("+pop3.value+","+pop2.value+","+pop1.value+")",type:12}); |
| 41 | } |
| 42 | } |
| 43 | return disp[0].value; |
| 44 | }; |
| 45 | module.exports=Mexp; |
| 46 | },{"./postfix_evaluator.js":5}],2:[function(require,module,exports){ |
| 47 | 'use strict' |
| 48 | var Mexp = require('./math_function.js') |
| 49 | function inc (arr, val) { |
| 50 | for (var i = 0; i < arr.length; i++) { |
| 51 | arr[i] += val |
| 52 | } |
| 53 | return arr |
| 54 | } |
| 55 | var token = [ |
| 56 | 'sin', |
| 57 | 'cos', |
| 58 | 'tan', |
| 59 | 'pi', |
| 60 | '(', |
| 61 | ')', |
| 62 | 'P', |
| 63 | 'C', |
| 64 | ' ', |
| 65 | 'asin', |
| 66 | 'acos', |
| 67 | 'atan', |
| 68 | '7', |
| 69 | '8', |
| 70 | '9', |
| 71 | 'int', |
| 72 | 'cosh', |
| 73 | 'acosh', |
| 74 | 'ln', |
| 75 | '^', |
| 76 | 'root', |
| 77 | '4', |
| 78 | '5', |
| 79 | '6', |
| 80 | '/', |
| 81 | '!', |
| 82 | 'tanh', |
| 83 | 'atanh', |
| 84 | 'Mod', |
| 85 | '1', |
| 86 | '2', |
| 87 | '3', |
| 88 | '*', |
| 89 | 'sinh', |
| 90 | 'asinh', |
| 91 | 'e', |
| 92 | 'log', |
| 93 | '0', |
| 94 | '.', |
| 95 | '+', |
| 96 | '-', |
| 97 | ',', |
| 98 | 'Sigma', |
| 99 | 'n', |
| 100 | 'Pi', |
| 101 | 'pow', |
| 102 | 'floor' |
| 103 | ] |
| 104 | var show = [ |
| 105 | 'sin', |
| 106 | 'cos', |
| 107 | 'tan', |
| 108 | 'π', |
| 109 | '(', |
| 110 | ')', |
| 111 | 'P', |
| 112 | 'C', |
| 113 | ' ', |
| 114 | 'asin', |
| 115 | 'acos', |
| 116 | 'atan', |
| 117 | '7', |
| 118 | '8', |
| 119 | '9', |
| 120 | 'Int', |
| 121 | 'cosh', |
| 122 | 'acosh', |
| 123 | ' ln', |
| 124 | '^', |
| 125 | 'root', |
| 126 | '4', |
| 127 | '5', |
| 128 | '6', |
| 129 | '÷', |
| 130 | '!', |
| 131 | 'tanh', |
| 132 | 'atanh', |
| 133 | ' Mod ', |
| 134 | '1', |
| 135 | '2', |
| 136 | '3', |
| 137 | '×', |
| 138 | 'sinh', |
| 139 | 'asinh', |
| 140 | 'e', |
| 141 | ' log', |
| 142 | '0', |
| 143 | '.', |
| 144 | '+', |
| 145 | '-', |
| 146 | ',', |
| 147 | 'Σ', |
| 148 | 'n', |
| 149 | 'Π', |
| 150 | 'pow', |
| 151 | 'floor' |
| 152 | ] |
| 153 | var eva = [ |
| 154 | Mexp.math.sin, |
| 155 | Mexp.math.cos, |
| 156 | Mexp.math.tan, |
| 157 | 'PI', |
| 158 | '(', |
| 159 | ')', |
| 160 | Mexp.math.P, |
| 161 | Mexp.math.C, |
| 162 | ' '.anchor, |
| 163 | Mexp.math.asin, |
| 164 | Mexp.math.acos, |
| 165 | Mexp.math.atan, |
| 166 | '7', |
| 167 | '8', |
| 168 | '9', |
| 169 | Math.floor, |
| 170 | Mexp.math.cosh, |
| 171 | Mexp.math.acosh, |
| 172 | Math.log, |
| 173 | Math.pow, |
| 174 | Math.sqrt, |
| 175 | '4', |
| 176 | '5', |
| 177 | '6', |
| 178 | Mexp.math.div, |
| 179 | Mexp.math.fact, |
| 180 | Mexp.math.tanh, |
| 181 | Mexp.math.atanh, |
| 182 | Mexp.math.mod, |
| 183 | '1', |
| 184 | '2', |
| 185 | '3', |
| 186 | Mexp.math.mul, |
| 187 | Mexp.math.sinh, |
| 188 | Mexp.math.asinh, |
| 189 | 'E', |
| 190 | Mexp.math.log, |
| 191 | '0', |
| 192 | '.', |
| 193 | Mexp.math.add, |
| 194 | Mexp.math.sub, |
| 195 | ',', |
| 196 | Mexp.math.sigma, |
| 197 | 'n', |
| 198 | Mexp.math.Pi, |
| 199 | Math.pow, |
| 200 | Math.floor |
| 201 | ] |
| 202 | var preced = { |
| 203 | 0: 11, |
| 204 | 1: 0, |
| 205 | 2: 3, |
| 206 | 3: 0, |
| 207 | 4: 0, |
| 208 | 5: 0, |
| 209 | 6: 0, |
| 210 | 7: 11, |
| 211 | 8: 11, |
| 212 | 9: 1, |
| 213 | 10: 10, |
| 214 | 11: 0, |
| 215 | 12: 11, |
| 216 | 13: 0, |
| 217 | 14: -1 // will be filtered after lexer |
| 218 | } // stores precedence by types |
| 219 | var type = [ |
| 220 | 0, 0, 0, 3, 4, 5, 10, 10, 14, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 10, 0, 1, 1, 1, 2, 7, 0, 0, 2, 1, 1, |
| 221 | 1, 2, 0, 0, 3, 0, 1, 6, 9, 9, 11, 12, 13, 12, 8 |
| 222 | ] |
| 223 | /* |
| 224 | 0 : function with syntax function_name(Maths_exp) |
| 225 | 1 : numbers |
| 226 | 2 : binary operators like * / Mod left associate and same precedence |
| 227 | 3 : Math constant values like e,pi,Cruncher ans |
| 228 | 4 : opening bracket |
| 229 | 5 : closing bracket |
| 230 | 6 : decimal |
| 231 | 7 : function with syntax (Math_exp)function_name |
| 232 | 8: function with syntax function_name(Math_exp1,Math_exp2) |
| 233 | 9 : binary operator like +,- |
| 234 | 10: binary operator like P C or ^ |
| 235 | 11: , |
| 236 | 12: function with , seperated three parameters and third parameter is a string that will be mexp string |
| 237 | 13: variable of Sigma function |
| 238 | */ |
| 239 | var type0 = { |
| 240 | 0: true, |
| 241 | 1: true, |
| 242 | 3: true, |
| 243 | 4: true, |
| 244 | 6: true, |
| 245 | 8: true, |
| 246 | 9: true, |
| 247 | 12: true, |
| 248 | 13: true, |
| 249 | 14: true |
| 250 | } // type2:true,type4:true,type9:true,type11:true,type21:true,type22 |
| 251 | var type1 = { |
| 252 | 0: true, |
| 253 | 1: true, |
| 254 | 2: true, |
| 255 | 3: true, |
| 256 | 4: true, |
| 257 | 5: true, |
| 258 | 6: true, |
| 259 | 7: true, |
| 260 | 8: true, |
| 261 | 9: true, |
| 262 | 10: true, |
| 263 | 11: true, |
| 264 | 12: true, |
| 265 | 13: true |
| 266 | } // type3:true,type5:true,type7:true,type23 |
| 267 | var type1Asterick = { |
| 268 | 0: true, |
| 269 | 3: true, |
| 270 | 4: true, |
| 271 | 8: true, |
| 272 | 12: true, |
| 273 | 13: true |
| 274 | } |
| 275 | var empty = {} |
| 276 | var type3Asterick = { |
| 277 | 0: true, |
| 278 | 1: true, |
| 279 | 3: true, |
| 280 | 4: true, |
| 281 | 6: true, |
| 282 | 8: true, |
| 283 | 12: true, |
| 284 | 13: true |
| 285 | } // type_5:true,type_7:true,type_23 |
| 286 | var type6 = { |
| 287 | 1: true |
| 288 | } |
| 289 | var newAr = [ |
| 290 | [], |
| 291 | [ |
| 292 | '1', |
| 293 | '2', |
| 294 | '3', |
| 295 | '7', |
| 296 | '8', |
| 297 | '9', |
| 298 | '4', |
| 299 | '5', |
| 300 | '6', |
| 301 | '+', |
| 302 | '-', |
| 303 | '*', |
| 304 | '/', |
| 305 | '(', |
| 306 | ')', |
| 307 | '^', |
| 308 | '!', |
| 309 | 'P', |
| 310 | 'C', |
| 311 | 'e', |
| 312 | '0', |
| 313 | '.', |
| 314 | ',', |
| 315 | 'n', |
| 316 | ' ' |
| 317 | ], |
| 318 | ['pi', 'ln', 'Pi'], |
| 319 | ['sin', 'cos', 'tan', 'Del', 'int', 'Mod', 'log', 'pow'], |
| 320 | ['asin', 'acos', 'atan', 'cosh', 'root', 'tanh', 'sinh'], |
| 321 | ['acosh', 'atanh', 'asinh', 'Sigma','floor'] |
| 322 | ] |
| 323 | |
| 324 | function match (str1, str2, i, x) { |
| 325 | for (var f = 0; f < x; f++) { |
| 326 | if (str1[i + f] !== str2[f]) { |
| 327 | return false |
| 328 | } |
| 329 | } |
| 330 | return true |
| 331 | } |
| 332 | Mexp.addToken = function (tokens) { |
| 333 | for (var i = 0; i < tokens.length; i++) { |
| 334 | var x = tokens[i].token.length |
| 335 | var temp = -1 |
| 336 | |
| 337 | // newAr is a specially designed data structure index of 1d array = length of tokens |
| 338 | newAr[x] = newAr[x] || [] |
| 339 | for (var y = 0; y < newAr[x].length; y++) { |
| 340 | if (tokens[i].token === newAr[x][y]) { |
| 341 | temp = token.indexOf(newAr[x][y]) |
| 342 | break |
| 343 | } |
| 344 | } |
| 345 | if (temp === -1) { |
| 346 | token.push(tokens[i].token) |
| 347 | type.push(tokens[i].type) |
| 348 | if (newAr.length <= tokens[i].token.length) { |
| 349 | newAr[tokens[i].token.length] = [] |
| 350 | } |
| 351 | newAr[tokens[i].token.length].push(tokens[i].token) |
| 352 | eva.push(tokens[i].value) |
| 353 | show.push(tokens[i].show) |
| 354 | } else { |
| 355 | // overwrite |
| 356 | token[temp] = tokens[i].token |
| 357 | type[temp] = tokens[i].type |
| 358 | eva[temp] = tokens[i].value |
| 359 | show[temp] = tokens[i].show |
| 360 | } |
| 361 | } |
| 362 | } |
| 363 | |
| 364 | function tokenize (string) { |
| 365 | var nodes = [] |
| 366 | var length = string.length |
| 367 | var key, x, y |
| 368 | for (var i = 0; i < length; i++) { |
| 369 | if (i < length - 1 && string[i] === ' ' && string[i + 1] === ' ') { |
| 370 | continue |
| 371 | } |
| 372 | key = '' |
| 373 | for ( |
| 374 | x = string.length - i > newAr.length - 2 ? newAr.length - 1 : string.length - i; |
| 375 | x > 0; |
| 376 | x-- |
| 377 | ) { |
| 378 | if (newAr[x] === undefined) continue |
| 379 | for (y = 0; y < newAr[x].length; y++) { |
| 380 | |
| 381 | if (match(string, newAr[x][y], i, x)) { |
| 382 | key = newAr[x][y] |
| 383 | |
| 384 | y = newAr[x].length |
| 385 | x = 0 |
| 386 | } |
| 387 | } |
| 388 | } |
| 389 | i += key.length - 1 |
| 390 | |
| 391 | if (key === '') { |
| 392 | throw new Mexp.Exception("Can't understand after " + string.slice(i)) |
| 393 | } |
| 394 | var index = token.indexOf(key) |
| 395 | |
| 396 | nodes.push({ |
| 397 | index: index, |
| 398 | token: key, |
| 399 | type: type[index], |
| 400 | eval: eva[index], |
| 401 | precedence: preced[type[index]], |
| 402 | show: show[index] |
| 403 | }) |
| 404 | } |
| 405 | return nodes |
| 406 | } |
| 407 | |
| 408 | Mexp.lex = function (inp, tokens) { |
| 409 | 'use strict' |
| 410 | |
| 411 | var changeSignObj = { |
| 412 | value: Mexp.math.changeSign, |
| 413 | type: 0, |
| 414 | pre: 21, |
| 415 | show: '-' |
| 416 | } |
| 417 | var closingParObj = { |
| 418 | value: ')', |
| 419 | show: ')', |
| 420 | type: 5, |
| 421 | pre: 0 |
| 422 | } |
| 423 | var openingParObj = { |
| 424 | value: '(', |
| 425 | type: 4, |
| 426 | pre: 0, |
| 427 | show: '(' |
| 428 | } |
| 429 | var str = [openingParObj] |
| 430 | |
| 431 | var ptc = [] // Parenthesis to close at the beginning is after one token |
| 432 | var inpStr = inp |
| 433 | var allowed = type0 |
| 434 | var bracToClose = 0 |
| 435 | var asterick = empty |
| 436 | var prevKey = '' |
| 437 | var i |
| 438 | if (typeof tokens !== 'undefined') { |
| 439 | Mexp.addToken(tokens) |
| 440 | } |
| 441 | var obj = {} |
| 442 | var nodes = tokenize(inpStr) |
| 443 | for (i = 0; i < nodes.length; i++) { |
| 444 | var node = nodes[i] |
| 445 | if (node.type === 14) { |
| 446 | if ( |
| 447 | i > 0 && |
| 448 | i < nodes.length - 1 && |
| 449 | nodes[i + 1].type === 1 && |
| 450 | (nodes[i - 1].type === 1 || nodes[i - 1].type === 6) |
| 451 | ) { throw new Mexp.Exception('Unexpected Space') } |
| 452 | continue |
| 453 | } |
| 454 | var cToken = node.token |
| 455 | var cType = node.type |
| 456 | var cEv = node.eval |
| 457 | var cPre = node.precedence |
| 458 | var cShow = node.show |
| 459 | var pre = str[str.length - 1] |
| 460 | var j |
| 461 | for (j = ptc.length; j--;) { |
| 462 | // loop over ptc |
| 463 | if (ptc[j] === 0) { |
| 464 | if ([0, 2, 3, 4, 5, 9, 11, 12, 13].indexOf(cType) !== -1) { |
| 465 | if (allowed[cType] !== true) { |
| 466 | throw new Mexp.Exception(cToken + ' is not allowed after ' + prevKey) |
| 467 | } |
| 468 | str.push(closingParObj) |
| 469 | allowed = type1 |
| 470 | asterick = type3Asterick |
| 471 | inc(ptc, -1).pop() |
| 472 | } |
| 473 | } else break |
| 474 | } |
| 475 | if (allowed[cType] !== true) { |
| 476 | throw new Mexp.Exception(cToken + ' is not allowed after ' + prevKey) |
| 477 | } |
| 478 | if (asterick[cType] === true) { |
| 479 | cType = 2 |
| 480 | cEv = Mexp.math.mul |
| 481 | cShow = '×' |
| 482 | cPre = 3 |
| 483 | i = i - 1 |
| 484 | } |
| 485 | obj = { |
| 486 | value: cEv, |
| 487 | type: cType, |
| 488 | pre: cPre, |
| 489 | show: cShow |
| 490 | } |
| 491 | if (cType === 0) { |
| 492 | allowed = type0 |
| 493 | asterick = empty |
| 494 | inc(ptc, 2).push(2) |
| 495 | str.push(obj) |
| 496 | str.push(openingParObj) |
| 497 | } else if (cType === 1) { |
| 498 | if (pre.type === 1) { |
| 499 | pre.value += cEv |
| 500 | inc(ptc, 1) |
| 501 | } else { |
| 502 | str.push(obj) |
| 503 | } |
| 504 | allowed = type1 |
| 505 | asterick = type1Asterick |
| 506 | } else if (cType === 2) { |
| 507 | allowed = type0 |
| 508 | asterick = empty |
| 509 | inc(ptc, 2) |
| 510 | str.push(obj) |
| 511 | } else if (cType === 3) { |
| 512 | // constant |
| 513 | str.push(obj) |
| 514 | allowed = type1 |
| 515 | asterick = type3Asterick |
| 516 | } else if (cType === 4) { |
| 517 | inc(ptc, 1) |
| 518 | bracToClose++ |
| 519 | allowed = type0 |
| 520 | asterick = empty |
| 521 | str.push(obj) |
| 522 | } else if (cType === 5) { |
| 523 | if (!bracToClose) { |
| 524 | throw new Mexp.Exception('Closing parenthesis are more than opening one, wait What!!!') |
| 525 | } |
| 526 | bracToClose-- |
| 527 | allowed = type1 |
| 528 | asterick = type3Asterick |
| 529 | str.push(obj) |
| 530 | inc(ptc, 1) |
| 531 | } else if (cType === 6) { |
| 532 | if (pre.hasDec) { |
| 533 | throw new Mexp.Exception('Two decimals are not allowed in one number') |
| 534 | } |
| 535 | if (pre.type !== 1) { |
| 536 | pre = { |
| 537 | value: 0, |
| 538 | type: 1, |
| 539 | pre: 0 |
| 540 | } // pre needs to be changed as it will the last value now to be safe in later code |
| 541 | str.push(pre) |
| 542 | inc(ptc, -1) |
| 543 | } |
| 544 | allowed = type6 |
| 545 | inc(ptc, 1) |
| 546 | asterick = empty |
| 547 | pre.value += cEv |
| 548 | pre.hasDec = true |
| 549 | } else if (cType === 7) { |
| 550 | allowed = type1 |
| 551 | asterick = type3Asterick |
| 552 | inc(ptc, 1) |
| 553 | str.push(obj) |
| 554 | } |
| 555 | if (cType === 8) { |
| 556 | allowed = type0 |
| 557 | asterick = empty |
| 558 | inc(ptc, 4).push(4) |
| 559 | str.push(obj) |
| 560 | str.push(openingParObj) |
| 561 | } else if (cType === 9) { |
| 562 | if (pre.type === 9) { |
| 563 | if (pre.value === Mexp.math.add) { |
| 564 | pre.value = cEv |
| 565 | pre.show = cShow |
| 566 | inc(ptc, 1) |
| 567 | } else if (pre.value === Mexp.math.sub && cShow === '-') { |
| 568 | pre.value = Mexp.math.add |
| 569 | pre.show = '+' |
| 570 | inc(ptc, 1) |
| 571 | } |
| 572 | } else if ( |
| 573 | pre.type !== 5 && |
| 574 | pre.type !== 7 && |
| 575 | pre.type !== 1 && |
| 576 | pre.type !== 3 && |
| 577 | pre.type !== 13 |
| 578 | ) { |
| 579 | // changesign only when negative is found |
| 580 | if (cToken === '-') { |
| 581 | // do nothing for + token |
| 582 | // don't add with the above if statement as that will run the else statement of parent if on Ctoken + |
| 583 | allowed = type0 |
| 584 | asterick = empty |
| 585 | inc(ptc, 2).push(2) |
| 586 | str.push(changeSignObj) |
| 587 | str.push(openingParObj) |
| 588 | } |
| 589 | } else { |
| 590 | str.push(obj) |
| 591 | inc(ptc, 2) |
| 592 | } |
| 593 | allowed = type0 |
| 594 | asterick = empty |
| 595 | } else if (cType === 10) { |
| 596 | allowed = type0 |
| 597 | asterick = empty |
| 598 | inc(ptc, 2) |
| 599 | str.push(obj) |
| 600 | } else if (cType === 11) { |
| 601 | allowed = type0 |
| 602 | asterick = empty |
| 603 | str.push(obj) |
| 604 | } else if (cType === 12) { |
| 605 | allowed = type0 |
| 606 | asterick = empty |
| 607 | inc(ptc, 6).push(6) |
| 608 | str.push(obj) |
| 609 | str.push(openingParObj) |
| 610 | } else if (cType === 13) { |
| 611 | allowed = type1 |
| 612 | asterick = type3Asterick |
| 613 | str.push(obj) |
| 614 | } |
| 615 | inc(ptc, -1) |
| 616 | prevKey = cToken |
| 617 | } |
| 618 | for (j = ptc.length; j--;) { |
| 619 | // loop over ptc |
| 620 | if (ptc[j] === 0) { |
| 621 | str.push(closingParObj) |
| 622 | inc(ptc, -1).pop() |
| 623 | } else break // if it is not zero so before ptc also cant be zero |
| 624 | } |
| 625 | if (allowed[5] !== true) { |
| 626 | throw new Mexp.Exception('complete the expression') |
| 627 | } |
| 628 | while (bracToClose--) { |
| 629 | str.push(closingParObj) |
| 630 | } |
| 631 | |
| 632 | str.push(closingParObj) |
| 633 | return new Mexp(str) |
| 634 | } |
| 635 | module.exports = Mexp |
| 636 | |
| 637 | },{"./math_function.js":3}],3:[function(require,module,exports){ |
| 638 | "use strict"; |
| 639 | var Mexp = function (parsed) { |
| 640 | this.value = parsed |
| 641 | } |
| 642 | |
| 643 | Mexp.math = { |
| 644 | isDegree: true, // mode of calculator |
| 645 | acos: function (x) { |
| 646 | return (Mexp.math.isDegree ? 180 / Math.PI * Math.acos(x) : Math.acos(x)) |
| 647 | }, |
| 648 | add: function (a, b) { |
| 649 | return a + b |
| 650 | }, |
| 651 | asin: function (x) { |
| 652 | return (Mexp.math.isDegree ? 180 / Math.PI * Math.asin(x) : Math.asin(x)) |
| 653 | }, |
| 654 | atan: function (x) { |
| 655 | return (Mexp.math.isDegree ? 180 / Math.PI * Math.atan(x) : Math.atan(x)) |
| 656 | }, |
| 657 | acosh: function (x) { |
| 658 | return Math.log(x + Math.sqrt(x * x - 1)) |
| 659 | }, |
| 660 | asinh: function (x) { |
| 661 | return Math.log(x + Math.sqrt(x * x + 1)) |
| 662 | }, |
| 663 | atanh: function (x) { |
| 664 | return Math.log((1 + x) / (1 - x)) |
| 665 | }, |
| 666 | C: function (n, r) { |
| 667 | var pro = 1 |
| 668 | var other = n - r |
| 669 | var choice = r |
| 670 | if (choice < other) { |
| 671 | choice = other |
| 672 | other = r |
| 673 | } |
| 674 | for (var i = choice + 1; i <= n; i++) { |
| 675 | pro *= i |
| 676 | } |
| 677 | return pro / Mexp.math.fact(other) |
| 678 | }, |
| 679 | changeSign: function (x) { |
| 680 | return -x |
| 681 | }, |
| 682 | cos: function (x) { |
| 683 | if (Mexp.math.isDegree) x = Mexp.math.toRadian(x) |
| 684 | return Math.cos(x) |
| 685 | }, |
| 686 | cosh: function (x) { |
| 687 | return (Math.pow(Math.E, x) + Math.pow(Math.E, -1 * x)) / 2 |
| 688 | }, |
| 689 | div: function (a, b) { |
| 690 | return a / b |
| 691 | }, |
| 692 | fact: function (n) { |
| 693 | if (n % 1 !== 0) return 'NaN' |
| 694 | var pro = 1 |
| 695 | for (var i = 2; i <= n; i++) { |
| 696 | pro *= i |
| 697 | } |
| 698 | return pro |
| 699 | }, |
| 700 | inverse: function (x) { |
| 701 | return 1 / x |
| 702 | }, |
| 703 | log: function (i) { |
| 704 | return Math.log(i) / Math.log(10) |
| 705 | }, |
| 706 | mod: function (a, b) { |
| 707 | return a % b |
| 708 | }, |
| 709 | mul: function (a, b) { |
| 710 | return a * b |
| 711 | }, |
| 712 | P: function (n, r) { |
| 713 | var pro = 1 |
| 714 | for (var i = Math.floor(n) - Math.floor(r) + 1; i <= Math.floor(n); i++) { |
| 715 | pro *= i |
| 716 | } |
| 717 | return pro |
| 718 | }, |
| 719 | Pi: function (low, high, ex) { |
| 720 | var pro = 1 |
| 721 | for (var i = low; i <= high; i++) { |
| 722 | pro *= Number(ex.postfixEval({ |
| 723 | n: i |
| 724 | })) |
| 725 | } |
| 726 | return pro |
| 727 | }, |
| 728 | pow10x: function (e) { |
| 729 | var x = 1 |
| 730 | while (e--) { |
| 731 | x *= 10 |
| 732 | } |
| 733 | return x |
| 734 | }, |
| 735 | sigma: function (low, high, ex) { |
| 736 | var sum = 0 |
| 737 | for (var i = low; i <= high; i++) { |
| 738 | sum += Number(ex.postfixEval({ |
| 739 | n: i |
| 740 | })) |
| 741 | } |
| 742 | return sum |
| 743 | }, |
| 744 | sin: function (x) { |
| 745 | if (Mexp.math.isDegree) x = Mexp.math.toRadian(x) |
| 746 | return Math.sin(x) |
| 747 | }, |
| 748 | sinh: function (x) { |
| 749 | return (Math.pow(Math.E, x) - Math.pow(Math.E, -1 * x)) / 2 |
| 750 | }, |
| 751 | sub: function (a, b) { |
| 752 | return a - b |
| 753 | }, |
| 754 | tan: function (x) { |
| 755 | if (Mexp.math.isDegree) x = Mexp.math.toRadian(x) |
| 756 | return Math.tan(x) |
| 757 | }, |
| 758 | tanh: function (x) { |
| 759 | return Mexp.sinha(x) / Mexp.cosha(x) |
| 760 | }, |
| 761 | toRadian: function (x) { |
| 762 | return x * Math.PI / 180 |
| 763 | } |
| 764 | } |
| 765 | Mexp.Exception = function (message) { |
| 766 | this.message = message |
| 767 | } |
| 768 | module.exports = Mexp |
| 769 | |
| 770 | },{}],4:[function(require,module,exports){ |
| 771 | |
| 772 | var Mexp = require('./lexer.js'); |
| 773 | |
| 774 | Mexp.prototype.toPostfix = function () { |
| 775 | 'use strict'; |
| 776 | var post = [], elem, popped, prep, pre, ele; |
| 777 | var stack = [{ value: "(", type: 4, pre: 0 }]; |
| 778 | var arr = this.value; |
| 779 | for (var i = 1; i < arr.length; i++) { |
| 780 | if (arr[i].type === 1 || arr[i].type === 3 || arr[i].type === 13) { //if token is number,constant,or n(which is also a special constant in our case) |
| 781 | if (arr[i].type === 1) |
| 782 | arr[i].value = Number(arr[i].value); |
| 783 | post.push(arr[i]); |
| 784 | } |
| 785 | else if (arr[i].type === 4) { |
| 786 | stack.push(arr[i]); |
| 787 | } |
| 788 | else if (arr[i].type === 5) { |
| 789 | while ((popped = stack.pop()).type !== 4) { |
| 790 | post.push(popped); |
| 791 | } |
| 792 | } |
| 793 | else if (arr[i].type === 11) { |
| 794 | while ((popped = stack.pop()).type !== 4) { |
| 795 | post.push(popped); |
| 796 | } |
| 797 | stack.push(popped); |
| 798 | } |
| 799 | else { |
| 800 | elem = arr[i]; |
| 801 | pre = elem.pre; |
| 802 | ele = stack[stack.length - 1]; |
| 803 | prep = ele.pre; |
| 804 | var flag = ele.value == 'Math.pow' && elem.value == 'Math.pow'; |
| 805 | if (pre > prep) stack.push(elem); |
| 806 | else { |
| 807 | while (prep >= pre && !flag || flag && pre < prep) { |
| 808 | popped = stack.pop(); |
| 809 | ele = stack[stack.length - 1]; |
| 810 | post.push(popped); |
| 811 | prep = ele.pre; |
| 812 | flag = elem.value == 'Math.pow' && ele.value == 'Math.pow'; |
| 813 | } |
| 814 | stack.push(elem); |
| 815 | } |
| 816 | } |
| 817 | } |
| 818 | return new Mexp(post); |
| 819 | }; |
| 820 | module.exports = Mexp; |
| 821 | },{"./lexer.js":2}],5:[function(require,module,exports){ |
| 822 | var Mexp=require('./postfix.js'); |
| 823 | Mexp.prototype.postfixEval = function (UserDefined) { |
| 824 | 'use strict'; |
| 825 | UserDefined=UserDefined||{}; |
| 826 | UserDefined.PI=Math.PI; |
| 827 | UserDefined.E=Math.E; |
| 828 | var stack=[],pop1,pop2,pop3; |
| 829 | var disp=[]; |
| 830 | var temp=''; |
| 831 | var arr=this.value; |
| 832 | var bool=(typeof UserDefined.n!=="undefined"); |
| 833 | for(var i=0;i<arr.length;i++){ |
| 834 | if(arr[i].type===1){ |
| 835 | stack.push({value:arr[i].value,type:1}); |
| 836 | } |
| 837 | else if(arr[i].type===3){ |
| 838 | stack.push({value:UserDefined[arr[i].value],type:1}); |
| 839 | } |
| 840 | else if(arr[i].type===0){ |
| 841 | if(typeof stack[stack.length-1].type==="undefined"){ |
| 842 | stack[stack.length-1].value.push(arr[i]); |
| 843 | } |
| 844 | else stack[stack.length-1].value=arr[i].value(stack[stack.length-1].value); |
| 845 | } |
| 846 | else if(arr[i].type===7){ |
| 847 | if(typeof stack[stack.length-1].type==="undefined"){ |
| 848 | stack[stack.length-1].value.push(arr[i]); |
| 849 | } |
| 850 | else stack[stack.length-1].value=arr[i].value(stack[stack.length-1].value); |
| 851 | } |
| 852 | else if(arr[i].type===8){ |
| 853 | pop1=stack.pop(); |
| 854 | pop2=stack.pop(); |
| 855 | stack.push({type:1,value:arr[i].value(pop2.value,pop1.value)}); |
| 856 | } |
| 857 | else if(arr[i].type===10){ |
| 858 | pop1=stack.pop(); |
| 859 | pop2=stack.pop(); |
| 860 | if(typeof pop2.type==="undefined"){ |
| 861 | pop2.value=pop2.concat(pop1); |
| 862 | pop2.value.push(arr[i]); |
| 863 | stack.push(pop2); |
| 864 | } |
| 865 | else if (typeof pop1.type==="undefined") { |
| 866 | pop1.unshift(pop2); |
| 867 | pop1.push(arr[i]); |
| 868 | stack.push(pop1); |
| 869 | } |
| 870 | else{ |
| 871 | stack.push({type:1,value:arr[i].value(pop2.value,pop1.value)}); |
| 872 | } |
| 873 | } |
| 874 | else if(arr[i].type===2||arr[i].type===9){ |
| 875 | pop1=stack.pop(); |
| 876 | pop2=stack.pop(); |
| 877 | if(typeof pop2.type==="undefined"){ |
| 878 | pop2=pop2.concat(pop1); |
| 879 | pop2.push(arr[i]); |
| 880 | stack.push(pop2); |
| 881 | } |
| 882 | else if (typeof pop1.type==="undefined") { |
| 883 | pop1.unshift(pop2); |
| 884 | pop1.push(arr[i]); |
| 885 | stack.push(pop1); |
| 886 | } |
| 887 | else{ |
| 888 | stack.push({type:1,value:arr[i].value(pop2.value,pop1.value)}); |
| 889 | } |
| 890 | } |
| 891 | else if(arr[i].type===12){ |
| 892 | pop1=stack.pop(); |
| 893 | if (typeof pop1.type!=="undefined") { |
| 894 | pop1=[pop1]; |
| 895 | } |
| 896 | pop2=stack.pop(); |
| 897 | pop3=stack.pop(); |
| 898 | stack.push({type:1,value:arr[i].value(pop3.value,pop2.value,new Mexp(pop1))}); |
| 899 | } |
| 900 | else if(arr[i].type===13){ |
| 901 | if(bool){ |
| 902 | stack.push({value:UserDefined[arr[i].value],type:3}); |
| 903 | } |
| 904 | else stack.push([arr[i]]); |
| 905 | } |
| 906 | } |
| 907 | if (stack.length>1) { |
| 908 | throw(new Mexp.Exception("Uncaught Syntax error")); |
| 909 | } |
| 910 | return stack[0].value>1000000000000000?"Infinity":parseFloat(stack[0].value.toFixed(15)); |
| 911 | }; |
| 912 | Mexp.eval=function(str,tokens,obj){ |
| 913 | if (typeof tokens==="undefined") { |
| 914 | return this.lex(str).toPostfix().postfixEval(); |
| 915 | } |
| 916 | else if (typeof obj==="undefined") { |
| 917 | if (typeof tokens.length!=="undefined") |
| 918 | return this.lex(str,tokens).toPostfix().postfixEval(); |
| 919 | else |
| 920 | return this.lex(str).toPostfix().postfixEval(tokens); |
| 921 | } |
| 922 | else |
| 923 | return this.lex(str,tokens).toPostfix().postfixEval(obj); |
| 924 | }; |
| 925 | module.exports=Mexp; |
| 926 | },{"./postfix.js":4}]},{},[1])(1) |
| 927 | }); |