diff options
| author | ruki <waruqi@gmail.com> | 2018-11-08 00:38:48 +0800 |
|---|---|---|
| committer | ruki <waruqi@gmail.com> | 2018-11-07 21:53:09 +0800 |
| commit | 26105034da4fcce7ac883c899d781f016559310d (patch) | |
| tree | c459a5dc4e3aa0972d9919033ece511ce76dd129 /node_modules/math-expression-evaluator/src/postfix_evaluator.js | |
| parent | 2c77f00f1a7ecb6c8192f9c16d3b2001b254a107 (diff) | |
| download | xmake-docs-26105034da4fcce7ac883c899d781f016559310d.tar.gz xmake-docs-26105034da4fcce7ac883c899d781f016559310d.zip | |
switch to vuepress
Diffstat (limited to 'node_modules/math-expression-evaluator/src/postfix_evaluator.js')
| -rwxr-xr-x | node_modules/math-expression-evaluator/src/postfix_evaluator.js | 105 |
1 files changed, 105 insertions, 0 deletions
diff --git a/node_modules/math-expression-evaluator/src/postfix_evaluator.js b/node_modules/math-expression-evaluator/src/postfix_evaluator.js new file mode 100755 index 00000000..3d162e46 --- /dev/null +++ b/node_modules/math-expression-evaluator/src/postfix_evaluator.js @@ -0,0 +1,105 @@ +var Mexp=require('./postfix.js');
+Mexp.prototype.postfixEval = function (UserDefined) {
+ 'use strict';
+ UserDefined=UserDefined||{};
+ UserDefined.PI=Math.PI;
+ UserDefined.E=Math.E;
+ var stack=[],pop1,pop2,pop3;
+ var disp=[];
+ var temp='';
+ var arr=this.value;
+ var bool=(typeof UserDefined.n!=="undefined");
+ for(var i=0;i<arr.length;i++){
+ if(arr[i].type===1){
+ stack.push({value:arr[i].value,type:1});
+ }
+ else if(arr[i].type===3){
+ stack.push({value:UserDefined[arr[i].value],type:1});
+ }
+ else if(arr[i].type===0){
+ if(typeof stack[stack.length-1].type==="undefined"){
+ stack[stack.length-1].value.push(arr[i]);
+ }
+ else stack[stack.length-1].value=arr[i].value(stack[stack.length-1].value);
+ }
+ else if(arr[i].type===7){
+ if(typeof stack[stack.length-1].type==="undefined"){
+ stack[stack.length-1].value.push(arr[i]);
+ }
+ else stack[stack.length-1].value=arr[i].value(stack[stack.length-1].value);
+ }
+ else if(arr[i].type===8){
+ pop1=stack.pop();
+ pop2=stack.pop();
+ stack.push({type:1,value:arr[i].value(pop2.value,pop1.value)});
+ }
+ else if(arr[i].type===10){
+ pop1=stack.pop();
+ pop2=stack.pop();
+ if(typeof pop2.type==="undefined"){
+ pop2.value=pop2.concat(pop1);
+ pop2.value.push(arr[i]);
+ stack.push(pop2);
+ }
+ else if (typeof pop1.type==="undefined") {
+ pop1.unshift(pop2);
+ pop1.push(arr[i]);
+ stack.push(pop1);
+ }
+ else{
+ stack.push({type:1,value:arr[i].value(pop2.value,pop1.value)});
+ }
+ }
+ else if(arr[i].type===2||arr[i].type===9){
+ pop1=stack.pop();
+ pop2=stack.pop();
+ if(typeof pop2.type==="undefined"){
+ console.log(pop2);
+ pop2=pop2.concat(pop1);
+ pop2.push(arr[i]);
+ stack.push(pop2);
+ }
+ else if (typeof pop1.type==="undefined") {
+ pop1.unshift(pop2);
+ pop1.push(arr[i]);
+ stack.push(pop1);
+ }
+ else{
+ stack.push({type:1,value:arr[i].value(pop2.value,pop1.value)});
+ }
+ }
+ else if(arr[i].type===12){
+ pop1=stack.pop();
+ if (typeof pop1.type!=="undefined") {
+ pop1=[pop1];
+ }
+ pop2=stack.pop();
+ pop3=stack.pop();
+ stack.push({type:1,value:arr[i].value(pop3.value,pop2.value,new Mexp(pop1))});
+ }
+ else if(arr[i].type===13){
+ if(bool){
+ stack.push({value:UserDefined[arr[i].value],type:3});
+ }
+ else stack.push([arr[i]]);
+ }
+ }
+ if (stack.length>1) {
+ throw(new Mexp.exception("Uncaught Syntax error"));
+ }
+ return stack[0].value>1000000000000000?"Infinity":parseFloat(stack[0].value.toFixed(15));
+};
+Mexp.eval=function(str,tokens,obj){
+ if (typeof tokens==="undefined") {
+ return this.lex(str).toPostfix().postfixEval();
+ }
+ else if (typeof obj==="undefined") {
+ if (typeof tokens.length!=="undefined")
+ return this.lex(str,tokens).toPostfix().postfixEval();
+ else
+ return this.lex(str).toPostfix().postfixEval(tokens);
+ }
+ else
+ return this.lex(str,tokens).toPostfix().postfixEval(obj);
+};
+module.exports=Mexp;
\ No newline at end of file |
