通过给出的求值运算公式字符串得到其结果值

在实际开发中有时需要根据用户制定的公式然后经过处理并将数值代替参数后来得出此公式的值,因为刚好也做到这里,看了些资料,于是写了一个类调用来实现此功能

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

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

因各个项目中需要使用根据字符串计算数,这里写出一个算法,专门计算字符串。配有大量常用公式。只有一个人方法,直接调用即可。 类名:CustomMath 函数名:Calculations(string value) 说明:求解算式表达式字符串 表达式中包含的符号或函数: truncate, ceiling,floor,round,log10, sign,sinh,sqrt, asin,atan,cosh, tanh, sin,cos,tan ,abs,acos, exp,log,max,min,pow,mod,+,-,*,/,',',(,) 函数说明:(不区分大小写) truncate(num) 计算指定数的整数部分 truncate(1.23)=1 ceiling (num) 返回大于或等于指定的双精度浮点数的最小整数 ceiling(1.23)=2 floor(num) 返回小于或等于指定双精度浮点数的最大整数 floor(1.23)=1 round(num) 将双精度浮点舍入为最接近的整数 round(1.23)=1 round(num,num1) 将小数按指定的小数位数舍入 round(1.23,1)=1.2 log10(num) 返回指定数字以 10 为底的对数 log10(10)=1 sign(num) 返回表示数字符号的 sign(1.23)=1 sinh(num) 返回指定角度的双曲正弦 sinh(1.23)=1.5644 sqrt(num) 返回指定数字的平方根 sqrt(9)=3 sqrt(num,num1) 返回指定数字的num1根 sqrt(27,3)=3 asin(num) 返回正弦为指定数字的角度 asin(0.5)=PI/6 atan(num) 返回正切为指定数字的角度 atan(1)=45 cosh(num) 返回指定角度的双曲余弦 cosh(1.23)=1.8567 tanh(num) 返回指定角度的双曲正切 tanh(1.23)=0.8425 sin(num) 返回指定角度的正弦 sin(PI/6)=0.5 cos(num) 返回指定角度的余弦 sin(PI/3)=0.5 tan(num) 返回指定角度的余切 sin(PI/4)=1 abs(num) 返回数字的绝对 abs(-12)=12 acos(num) 返回余弦为指定数字的角度 acos(0.5)=PI/3 exp(num) 返回 e 的指定次幂 exp(1)=2.718 log(num) 返回指定数字的自然对数(底为 e) log(e)=1 log(num,num1) 返回指定数字在使用指定底时的对数 log(e,e)=1 max(num,um1) 返回最大 max(1,2)=2 min(num,num1) 返回最小 min(1,2)=1 pow(num,num1) 返回指定数字的指定次幂 pow(2,2)=4 mod(num,num1) 返回余数 mod(3,2)=1 常量: PI :3.14159265358979323846 E :2.7182818284590452354 YEAR :当前年份 MONTH :当前月份 DAY : 当前日 HOUR :当前时 MINUTE :当前分 SECOND :当前秒 RANDOM :一个随机数(0-1 之间) 实例 系统计算:1+2*3/4-0.5=2 函数计算:1+2*3/4-0.5=2 调用方式:CustomMath.Calculations("1+2*3/4-0.5") 系统计算:(1+2)*3/4-0.5=1.75 函数计算:(1+2)*3/4-0.5=1.75 调用方式:CustomMath.Calculations("(1+2)*3/4-0.5") 系统计算:(sin(pi)+sqrt(3+5*7+(2+8/4*5+2)))/6=1.20185042515466 公式计算:(sin(pi)+sqrt(3+5*7+(2+8/4*5+2)))/6=1.20185042515466 调用方式:CustomMath.Calculations("(sin(pi)+sqrt(3+5*7+(2+8/4*5+2)))/6") 系统计算:sin(pow(3,2)/4)+3.5-9*sqrt(81)=-76.7219268031121 函数计算:sin(pow(3,2)/4)+3.5-9*sqrt(81)=-76.7219268031121 调用方式:CustomMath.Calculations("sin(pow(3,2)/4)+3.5-9*sqrt(81)")
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值