1 Star 0 Fork 11

kimmking / js-rpn-expression

forked from phynos / js-rpn-expression 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
MIT

运算表达式类库

采用逆波兰表达式的实现,未打勾表示暂时还没实现但是计划会实现。
实现过程中参考了一些其他资源,就不一一列举了。

测试截图

Chrome浏览器环境测试截图

结果图片

JS标准内核环境测试截图

结果图片

更新说明

更新说明

文件说明

  • expression.js,index.html是完整版本
  • expression_simple.js,index_simple.html是简易版本(只支持四则运算和变量)

主要功能

  • 支持四则运算
  • 支持浮点数
  • 支持自定义变量
  • 支持自定义函数
  • 支持自定义函数参数嵌套
  • 支持自定义对象之对象变量访问(包括嵌套)
  • 支持自定义对象之函数调用(包括嵌套)
  • 浮点数精度问题
  • 对象方法连续调用(比如:obj.fun1(1).eda.fun2(1,2))
  • 标准JS语法,除了测试函数,核心功能不依赖浏览器环境(比如window,dom,console等)

使用举例

    var context = new CalContext();
    //增加值栈——变量
    context.putData("$a",5);
    //增加值栈——变量
    context.putData("PI",3);
    //增加值栈——无参数函数
    context.putData("fun0",function(){ return 1000;});
    //增加值栈——单参数函数
    context.putData("fun1",function(a){ return 10 * a;});
    //增加值栈——多参数函数
    context.putData("fun2",function(a,b){ return 10 * a + b;});
    //增加值栈——对象方法(如果使用了浏览器window对象的方法或属性,则必须传入window作为上下文)
    var t = {
      year: 28,
      fun: function(a) {
        return a + this.year;
      }
    };
    context.putData("fun3",t.fun,t);//这里需要将对象传入,作为回调上下文
    //增加值栈——对象
    context.putData("people",{
       age: 28,
       bat: 3,
       fun1: function(){
         return this.age + 2;
       },
       fun2: function(a){
         return this.age + 10 * a;
       },
       fun3: function(a,b){
         return this.age + 10 * a + b;
       },
       skill: {
          lang : function(a){
              return a + this.bat;
          }
       }
    });

    //获取值
    //var result = context.calc(expr);

    //下面是测试用例

    context.assertEqual("40+8*2",56);

    context.assertEqual("40*8+2",322);

    context.assertEqual("40/8+2",7);

    context.assertEqual("40+8/2",44);

    context.assertEqual("(40+8)/2",24);

    context.assertEqual("40*(8-2)",240);

    context.assertEqual("10%3 + 1",2);

    context.assertEqual("0.1 + 1 + 0.3",1.4);

    context.assertEqual("0.1 *10 + 6.3",7.3);

    context.assertEqual("$a + 5",10);

    context.assertEqual("$a + fun0()",1005);

    context.assertEqual("$a + fun0() + 100",1105);

    context.assertEqual("$a + fun1(1)",15);

    context.assertEqual("$a + fun2(2,1)",26);

    context.assertEqual("$a + fun2(3+7,3)",108);

    context.assertEqual("$a + fun2(fun1(2),3) + 20",228);

    context.assertEqual("11 + people.age / 2 + 6",31);

    context.assertEqual("11 + people.age / 2 + fun1(1)",35);

    context.assertEqual("11 + fun1(people.age / 2)/2 + 1",82);

    context.assertEqual("$a + fun3(2) + people.fun1(1)",65);

    context.assertEqual("$a + fun3(2) + people.skill.lang(1)",39);

    //10 + (28+2) + 106 * 2 + 24 = 276
    context.assertEqual("10 + fun3(2) + people.skill.lang(fun2(3+7,3)) * 2 + 24",276);

    context.assertEqual("PI * 2 + 3",9);
  
The MIT License (MIT) Copyright (c) 2018 phynos Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

简介

逆波兰表达式的js实现 展开 收起
JavaScript
MIT
取消

发行版

暂无发行版

贡献者

全部

近期动态

加载更多
不能加载更多了
JavaScript
1
https://gitee.com/kimmking/js-rpn-expression.git
git@gitee.com:kimmking/js-rpn-expression.git
kimmking
js-rpn-expression
js-rpn-expression
master

搜索帮助