Giter Site home page Giter Site logo

ncalculator's Introduction

NCalculator

这是一个简单的, 功能较为完善的计算器, 使用递归下降分析表达式语法.

支持:

  • 四则运算 + - * /
  • 拓展运算 幂(**或^) 求余(%)
  • 嵌套计算 可使用任意层级括号进行嵌套
  • 关系运算 大于(>) 小于(<) 大于等于(>=) 小于等于(<=) 等于(=或==) 不等于(!=或<>或><)
  • 常量变量 可自定义常量与变量
  • 函数运算 可调用 Math 中所有数学运算函数, 同时也支持自定义
  • 逻辑运算 支持逻辑 与(&) 或(|) 非(!)
  • 表达式编译

使用

解析:

Expr expr = NCalc.Parse("1+1");

解析并计算:

Expr expr = NCalc.Parse("1+1");
double value = expr.Eval();

计算:

NCalc.Eval("1+1");   // returns 2
NCalc.Eval("pi");    // returns 3.141592653589793
NCalc.Eval("2 > 3"); // returns 0 (false)
NCalc.Eval("2 > 1"); // returns 1 (true)
NCalc.Eval("1 > 3 ? 114 : 514");   // returns 514
NCalc.Eval("pow(2, 10)");          // returns 1024
NCalc.Eval("sqrt(4)");             // returns 2

带参数计算:

NCalcContext context = new NCalcContext();
context.Varialble["my_var"] = () => DateTime.Now.Year;

double value = NCalc.Eval(context, "my_var + 1");

编译:

Func<double> func = NCalc.Compile("1+1");

带上下文编译:

NCalcContext context = new NCalcContext();
context.Varialble["my_var"] = () => DateTime.Now.Year;

Func<double> func = NCalc.Compile(context, "my_var + 1");

自定义常量:

NCalcContext.Default.Constants["x"] = 10;
NCalcContext.Default.Eval("x + 1"); // returns 11

自定义变量:

double count = 0;
NCalcContext.Default.Variables["count"] = () => count++;
NCalcContext.Default.Eval("count"); // returns 0
NCalcContext.Default.Eval("count"); // returns 1
NCalcContext.Default.Eval("count"); // returns 2

自定义函数:

NCalcContext.Default.Functions["myfunc"] = (values) => values.Sum();
NCalcContext.Default.Eval("myfunc(1, 2, 3)");    // returns 6

类型

  • Expr 及其子类: 可运算的表达式
  • Lexer 词法分析器
  • Parser 语法分析器
  • Token 词法单元
  • NCalc 计算器

ncalculator's People

Contributors

slimenull avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.