【发布】★通过给出的求值运算公式字符串得到其结果值的类(关键字:公式,Evaluator)★

stoway 2004-06-16 01:58:40
在实际开发中有时需要根据用户制定的公式然后经过处理并将数值代替参数后来得出此公式的值,因为刚好也做到这里,看了些资料,于是写了一个类调用来实现此功能
public class Evaluator {
object _compiled = null;

private string m_formula;

/// <summary>
/// 计算公式
/// </summary>
public string Formula{
get{
return m_formula;
}
set{
m_formula = value;
}
}

public Evaluator() {
}

public Evaluator(string formula){
Formula = formula;
}

public Double Execute(){
if(Formula == null || Formula == ""){
throw new Exception("请先设置Formula属性!");
}
return this.Execute(Formula);
}
public Double Execute(string formula){
constructEvaluator(formula);
MethodInfo m = _compiled.GetType().GetMethod("GetValue");
return (Double)m.Invoke(_compiled, null);
}
private void constructEvaluator(string formula) {
ICodeCompiler compiler = (new CSharpCodeProvider().CreateCompiler());
CompilerParameters cp = new CompilerParameters();
cp.ReferencedAssemblies.Add("system.dll");

cp.GenerateExecutable = false;
cp.GenerateInMemory = true;

StringBuilder str = new StringBuilder();
str.Append("using System; \n");
str.Append("namespace Stoway { \n");
str.Append("public class Formula { \n");

str.AppendFormat(" public {0} GetValue()","Double");
str.Append("{");
str.AppendFormat(" return Convert.ToDouble({0}); ", formula);
str.Append("}\n");
str.Append("}\n");
str.Append("}");

CompilerResults cr = compiler.CompileAssemblyFromSource(cp, str.ToString());
if (cr.Errors.HasErrors) {
throw new Exception("不是正确的表达式");
}
Assembly a = cr.CompiledAssembly;
_compiled = a.CreateInstance("Stoway.Formula");
}
public static Double GetValue(string formula){
return new Evaluator().Execute(formula);
}
}
-----------
调用方法:
Evaluator evaluator = new Evaluator();
Double num = evaluator.Execute("( 3 + 5 ) * 2 + 56 / 0.25");
也可以:
Double num = Evaluator.GetValue("( 3 + 5 ) * 2 + 56 / 0.25");

是否还有其他方法来实现同样的功能,希望大家多多讨论!
-------------------------------------------------------

相关:
http://www.codeproject.com/csharp/matheval.asp
http://www.codeproject.com/csharp/runtime_eval.asp

其他:
产生自增长ID的存储过程
http://community.csdn.net/Expert/topic/2984/2984428.xml?temp=7.694644E-02
.Net 下安装、调试的常见问题与错误
http://community.csdn.net/Expert/topic/2837/2837439.xml?temp=.878277
...全文
492 21 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
21 条回复
切换为时间正序
请发表友善的回复…
发表回复
潜水员2099 2004-06-23
  • 打赏
  • 举报
回复
此方法确实比编译原理中的解析么左、右、中值表达式简单多了
应为:
此方法确实比编译原理中的解析什么左、右、中值表达式简单多了

^_^
潜水员2099 2004-06-23
  • 打赏
  • 举报
回复
楼主的类忘了添加引用:

using System;
using System.Text;
using System.CodeDom.Compiler;
using Microsoft.CSharp;
using System.Reflection;

此方法确实比编译原理中的解析么左、右、中值表达式简单多了

hertcloud 2004-06-23
  • 打赏
  • 举报
回复
学习
潜水员2099 2004-06-23
  • 打赏
  • 举报
回复
支持!!!!!
eliphe 2004-06-18
  • 打赏
  • 举报
回复
SkyecMath数学专家组件 v1.0

介绍:
  本组件集成函数图像绘制生成接口。
  本组件集成超长整数四则运算接口。
  本组件集成自由数学表达式解析接口。


下载地址:
http://www.skyec.com/U_View_cpxz_detail.asp?ProductID=17
yemao20 2004-06-18
  • 打赏
  • 举报
回复
学习
UP
Jinniu 2004-06-18
  • 打赏
  • 举报
回复
学习!
timiil 2004-06-17
  • 打赏
  • 举报
回复
mark!
感谢!
bingeng 2004-06-17
  • 打赏
  • 举报
回复
学习
huangsuipeng 2004-06-17
  • 打赏
  • 举报
回复
好!最近我在搞编译原理
请问楼主,是不是用算符优先法写的呢?还是LR(1),SLR(1),LALR?
bitsbird 2004-06-17
  • 打赏
  • 举报
回复
good
acewang 2004-06-17
  • 打赏
  • 举报
回复
看着像阎宏博士的那个例子,可惜阎宏博士的网站现在上不去了.
daguzhang 2004-06-17
  • 打赏
  • 举报
回复
非常感谢!学习中 希望以后能多看到你的帖子
  • 打赏
  • 举报
回复
mark
nanker 2004-06-17
  • 打赏
  • 举报
回复
mark
lin_lin 2004-06-17
  • 打赏
  • 举报
回复
mark
ShengNet 2004-06-17
  • 打赏
  • 举报
回复
mark一下
elite2018 2004-06-16
  • 打赏
  • 举报
回复
good
meixiaofeng 2004-06-16
  • 打赏
  • 举报
回复
经典
Tomgus 2004-06-16
  • 打赏
  • 举报
回复
学习!!!
加载更多回复(1)

111,096

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • AIGC Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧