最近需要計算帶參數的數學表達式,比如“X+Y+10+sin(Y)”之類的的公式,
原來用VC的一個計算表達式的類,挺方便的,但是在網上找.Net居然沒找了好久。
不過還是找到兩個,一個是這個http://download.csdn.Net/source/346496
上面這個用起來還是比較方便比較快
下面這個是老外寫的,動態編譯,可以學習,但是處理起來比較慢
using System;
using System.CodeDom;
using System.CodeDom.Compiler;
using Microsoft.CSharp;
using System.Text;
using System.Reflection;
using System.Text.RegularExpressions;
namespace ADOGuy
...{
/**//// <summary>
/// Summary description for Class1.
/// </summary>
public class Evaluator
...{
Construction#region Construction
public Evaluator(EvaluatorItem[] items)
...{
ConstructEvaluator(items);
}
public Evaluator(Type returnType, string expression, string name)
...{
EvaluatorItem[] items = ...{ new EvaluatorItem(returnType, expression, name) };
ConstructEvaluator(items);
}
public Evaluator(EvaluatorItem item)
...{
EvaluatorItem[] items = ...{ item };
ConstructEvaluator(items);
}
private void Const
-ructEvaluator(EvaluatorItem[] items)
...{
ICodeCompiler comp = (new CSharpCodeProvider().CreateCompiler());
CompilerParameters cp = new CompilerParameters();
cp.ReferencedAssemblIEs.Add("system.dll");
cp.ReferencedAssemblIEs.Add("system.data.dll");
cp.ReferencedAssemblIEs.Add("system.XML.dll");
cp.GenerateExecutable = false;
cp.GenerateInMemory = true;
StringBuilder code = new StringBuilder();
code.Append("using System; ");
code.Append("using System.Data; ");
code.Append("using System.Data.SqlClIEnt; ");
code.Append("using System.Data.OleDb; ");
code.Append("using System.XML; ");
code.Append("namespace ADOGuy { ");
code.Append(" public class _Evaluator { ");
foreach (EvaluatorItem item in items)
...{
code.AppendFormat(" public {0} {1}() ",
item.ReturnType.Name,
item.Name);
code.Append("{ ");
code.AppendFormat(" return ({0}); ", item.Expression);
code.Append("} ");
}
code.Append("} }");
CompilerResults cr = comp.CompileAssemblyFromSource(cp, code.ToString());
if (cr.Errors.HasErrors)
...{
StringBuilder error = new StringBuilder();
error.Append("Error Compiling Expression: ");
foreach (CompilerError err in cr.Errors)
...{
error.AppendFormat("{0} ", err.ErrorText);
}
throw new Exception("Error Compiling Expression: " + error.ToString());
}
Assembly a = cr.CompiledAssembly;
_Compiled = a.CreateInstance("ADOGuy._Evaluator");
}
#endregion
Public Members#region Public Members
public int EvaluateInt(string name)
...{
return (int)Evaluate(name);
}
public string EvaluateString(string name)
...{
return (string)Evaluate(name);
}
public bool EvaluateBool(string name)
...{
return (bool)Evaluate(name);
}
public object Evaluate(string name)
...{
MethodInfo mi = _Compiled.GetType().GetMethod(name);
return mi.Invoke(_Compiled, null);
}
#endregion
Static Members#region Static Members
static public int EvaluateToInteger(string code)
...{
Evaluator eval = new Evaluator(typeof(int), code, staticMethodName);
return (int)eval.Evaluate(staticMethodName);
}
static public string EvaluateToString(string code)
...{
Evaluator eval = new Evaluator(typeof(string), code, staticMethodName);
return (string)eval.Evaluate(staticMethodName);
}
static public bool EvaluateToBool(string code)
...{
Evaluator eval = new Evaluator(typeof(bool), code, staticMethodName);
return (bool)eval.Evaluate(staticMethodName);
}
static public object EvaluateToObject(string code)
...{
Evaluator eval = new Evaluator(typeof(object), code, staticMethodName);
return eval.Evaluate(staticMethodName);
}
#endregion
Private#region Private
const string staticMethodName = "__foo";
Type _CompiledType = null;
object _Compiled = null;
#endregion
}
public class EvaluatorItem
...{
public EvaluatorItem(Type returnType, string expression, string name)
...{
ReturnType = returnType;
Expression = expression;
Name = name;
}
public Type ReturnType;
public string Name;
public string Expression;
}
}
/**////
static int Compute(string expression, params int[] n)
...{
foreach (int i in n)
expression = Regex.Replace(expression, @"(?''a''.*?)(?!d)w+(?''b''.*)", "${a}" + i + "${b}");
ADOGuy.Evaluator calculator = new ADOGuy.Evaluator(typeof(int), expression, "EvaluateInt");
return calculator.EvaluateInt("EvaluateInt");
}
int L = 5;
int N = 10;
int SUM = Compute("L + N + 10", L, N);
Console.WriteLine(SUM);
Console.Read();