Browse Source

[fix] 修复乘方计算顺序bug

Steven Yan 1 year ago
parent
commit
146e645487
7 changed files with 115 additions and 41 deletions
  1. 67 2
      README.md
  2. 3 2
      package.json
  3. 1 1
      src/app.js
  4. 27 27
      src/calculator.js
  5. 9 9
      src/jison/calculator.jison
  6. 0 0
      src/scripts/build.sh
  7. 8 0
      src/test_cases.js

File diff suppressed because it is too large
+ 67 - 2
README.md


+ 3 - 2
package.json

@@ -1,12 +1,13 @@
 {
     "name": "jison-calculator",
-    "version": "1.0.1",
+    "version": "1.0.2",
     "description": "",
     "main": "app.js",
     "scripts": {
         "start": "cd src && node app.js",
         "make": "cd src && jison jison/calculator.jison",
-        "build": "bash scripts/build.sh"
+        "test": "npm run make && npm start",
+        "build": "bash src/scripts/build.sh"
     },
     "author": "",
     "license": "ISC",

+ 1 - 1
src/app.js

@@ -54,7 +54,7 @@ function print(tests) {
                 exp: v.value,
                 result: ans,
                 expect: expect,
-                correct: ans === expect,
+                correct: ans === expect ? true : '✘',
             };
             prints.push(o);
         });

File diff suppressed because it is too large
+ 27 - 27
src/calculator.js


+ 9 - 9
src/jison/calculator.jison

@@ -167,11 +167,11 @@ var dComp = function (v) {
 
 /* operator associations and precedence */
 %left '+' '-'
-%left 'sin' 'cos' 'tan' 'asin' 'acos' 'atan' 'ln' 'log'
 %left 'SQRT' '*' '/'
 %right '%'
-%left '^'
+%right '^'
 %right '!'
+%left 'sin' 'cos' 'tan' 'asin' 'acos' 'atan' 'ln' 'log'
 %left UMINUS
 %token INVALID
 %start expressions
@@ -184,9 +184,7 @@ expressions
     ;
 
 e
-    : '-' e   %prec UMINUS
-        {$$ = -$2;}
-    | NUMBER
+    : NUMBER
         {$$ = Number(yytext);}
     | 'Math.E'
         {$$ = Math.E;}
@@ -194,8 +192,6 @@ e
         {$$ = Math.PI;}
     | e '%'
         {$$ = $1 / 100;}
-    | e '!'
-        {$$ = fac($1);}
     | e '^' e
         {$$ = Math.pow($1, $3);}
     | e '*' e
@@ -214,6 +210,8 @@ e
         {$$ = $2 ** 0.5}
     | e 'SQRT' e
         {$$ = ($3) ** (1 / $1);}
+    | 'isDeg' e
+        {$$ = setIsDeg($2);}
     | 'sin' e
         {$$ = sin($2);}
     | 'cos' e
@@ -226,9 +224,11 @@ e
         {$$ = acos($2);}
     | 'atan' e
         {$$ = atan($2);}
+    | e '!'
+        {$$ = fac($1);}
     | '(' e ')'
         {$$ = $2;}
-    | 'isDeg' e
-        {$$ = setIsDeg($2);}
+    | '-' e
+        {$$ = -$2;}
     ;
 

+ 0 - 0
scripts/build.sh → src/scripts/build.sh


+ 8 - 0
src/test_cases.js

@@ -132,5 +132,13 @@ module.exports = [
     {
         value: '√(log(23) + 3! × 9 % ) - 63.1 × tan(2! × e)',
         result: 72.7202938705
+    },
+    {
+        value: '-2^2^3',
+        result: -256
+    },
+    {
+        value: '1 + ( - 2^6)',
+        result: -63
     }
 ];

Some files were not shown because too many files changed in this diff