当前位置:网站首页>简单测试轻量级表达式计算器Flee
简单测试轻量级表达式计算器Flee
2022-06-26 22:59:00 【gc_2299】
Flee是GitHub上基于.net的轻量级数学表达式解析和计算器,它支持计算数学表达式、带变量的数学表达式、带特殊处理函数的数学表达式等。从微信公众号中看到关于Flee的介绍文章(参考文献1是百度出来的同名的博客文章),准备测试一下,使用该模块计算数学表达式及带变量的数学表达式。
使用VS2019创建Winform程序,通过NuGet查找并安装Flee包及依赖项,如下图所示:

使用Flee计算不带变量和特殊处理函数的数学表达式很简单,直接使用其官网给的示例代码即可,测试代码及运行界面如下图所示。
try
{
ExpressionContext context = new ExpressionContext();
IDynamicExpression calcResult = context.CompileDynamic(txtExpression.Text);
txtResult.Text =Convert.ToString(calcResult.Evaluate());
}
catch (ExpressionCompileException ex)
{
if (ex.Reason == CompileExceptionReason.SyntaxError)
{
txtResult.Text = "Check your expression syntax" + "\r\n" + ex.Message;
}
else
{
txtResult.Text = ex.Message;
}
}

接下来测试带变量的数学表达式,这时最重要的是ExpressionContext类,该类用于保存表达式中的变量,测试代码及运行界面如下图所示。
try
{
ExpressionContext context = new ExpressionContext();
foreach(DataGridViewRow dgvr in dgvParams.Rows)
{
decimal tmpValue = 0;
if(string.IsNullOrEmpty(Convert.ToString(dgvr.Cells[0].Value))
|| !decimal.TryParse(Convert.ToString(dgvr.Cells[1].Value),out tmpValue))
{
continue;
}
context.Variables[Convert.ToString(dgvr.Cells[0].Value)] = tmpValue;
}
IDynamicExpression calcResult = context.CompileDynamic(txtExpression.Text);
txtResult.Text =Convert.ToString(calcResult.Evaluate());
}
catch (ExpressionCompileException ex)
{
if (ex.Reason == CompileExceptionReason.SyntaxError)
{
txtResult.Text = "Check your expression syntax" + "\r\n" + ex.Message;
}
else
{
txtResult.Text = ex.Message;
}
}

Flee的功能比较强大,本文只是测试了其中的最简单的使用方式,后续还会继续学习Flee的用法。
参考文献:
[1]https://github.com/mparlak/Flee
[2]https://github.com/mparlak/Flee/wiki/Examples
边栏推荐
- [Old Wei makes machines] issue 090: keyboard? host? Full function keyboard host!
- Microservices, an important part of cloud native architecture
- Electronic Society C language level 1 31. Calculate line segment length
- Brief analysis of the self inspection contents of the blue team in the attack and defense drill
- 分享三种在Excel表格中自动求和的方法
- [fundamentals of image processing] GUI image histogram equalization system based on MATLAB [including Matlab source code 1924]
- Solution of valuenotifier < list < t > > monitoring problem in fluent
- Unity3d plug-in anyportrait 2D bone animation
- leetcode - 买卖股票的最佳时机
- 分享三種在Excel錶格中自動求和的方法
猜你喜欢

Bs-gx-016 implementation of textbook management system based on SSM

【图像处理基础】基于matlab GUI图像直方图均衡化系统【含Matlab源码 1924期】

数据清洗工具flashtext,效率直接提升了几十倍数
![[fundamentals of image processing] GUI image histogram equalization system based on MATLAB [including Matlab source code 1924]](/img/8a/f5847eef7318f3db54aa2d4405b06a.jpg)
[fundamentals of image processing] GUI image histogram equalization system based on MATLAB [including Matlab source code 1924]

DAST 黑盒漏洞扫描器 第五篇:漏洞扫描引擎与服务能力

WP collection plug-in tutorial no thanks for WordPress collection of rules

Three solutions for improving embedded software development environment

阿里云服务器的购买、基本配置、(xshell)远程连接、搭建环境

WordPress collection plug-ins are recommended to be free collection plug-ins

Word chess based on heuristic search
随机推荐
电子协会 C语言 1级 30 、 等差数列末项计算
【数学建模】基于matlab GUI随机节点的生成树【含Matlab源码 1919期】
LabVIEW Arduino TCP/IP远程智能家居系统(项目篇—5)
[interface] pyqt5 and swing transformer for face recognition
Design of master-slave replication system
FPGA -vga display
Vulnhub's dc9
Extensions de l'éditeur d'unityeditor - fonctions de table
分享三种在Excel表格中自动求和的方法
【混合编程jni 】第七篇之JNI 的命令行们
【BUG反馈】WebIM在线聊天系统发消息时间问题
Centos7编译安装Redis
[hybrid programming JNI] details of JNA in Chapter 11
CVPR 2022 - Interpretation of selected papers of meituan technical team
打新债注册开户有没有什么风险?安全吗?
Introduction to operator
leetcode:152. Product maximum subarray [consider DP of two dimensions]
[kotlin] keyword suspend learning of thread operation and async understanding
300题 第三讲 向量组
[mixed programming JNI] Part 9: JNI summary