C#中,用户自定义公式 [英] C#, User defined formula

查看:816
本文介绍了C#中,用户自定义公式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要启用用户,他可以在DataGridView中写自己的公式。 。像在Excel函数



公式定义的例子:





所以,用户写他自己的公式公式中的单元格,然后在其他表所示结果每个。我怎样才能做到这一点?


解决方案

我想尝试的 NCalc




NCalc在.NET中的数学表达式求值。 NCalc可以解析的任何言论和评价结果,包括静态或动态的参数和自定义函数。




 词典<字符串,整数>字典=新词典<字符串,整数>(){{收入,1000},{税,5}}; 

串expressionString =收入*税;
NCalc.Expression EXPR =新NCalc.Expression(expressionString);
expr.EvaluateParameter + =(名称args)=>
{
args.Result =字典[名]
};

INT结果=(int)的expr.Evaluate();


I need to enable user that he can write own formula in datagridview. Something like a function in Excel.

Example of formula definition:

So, user write his own formula in formula cell and then in other table is shown result for each. How I can do this?

解决方案

I would try NCalc

NCalc is a mathematical expressions evaluator in .NET. NCalc can parse any expression and evaluate the result, including static or dynamic parameters and custom functions.

Dictionary<string, int> dict = new Dictionary<string, int>() { { "Income", 1000 }, { "Tax", 5 } };

string expressionString = "Income * Tax";
NCalc.Expression expr = new NCalc.Expression(expressionString);
expr.EvaluateParameter += (name, args) =>
    {
        args.Result = dict[name];
    };

int result = (int)expr.Evaluate();

这篇关于C#中,用户自定义公式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆