[csharp] view plain copy print?
//方法一 利用DataTable中的Compute方法 例如:1*2-(4/1)+2*4=6
string formulate = string.Format("{0}*{1} - {2}/{3} +{1}*{2}", , , , );
DataTable dt = new DataTable();
Response.Write(dt.Compute(formulate, "").ToString()); Response.Write("<br/>"); //方法二 利用JS中强大的Eval函数 可以带if-else判断 添加COM引用:Microsoft Sctipt Control 1.0
MSScriptControl.ScriptControl sc = new MSScriptControl.ScriptControlClass();
sc.Language = "JavaScript";
string formulate1 = string.Format("if({0}==2)((({0}*{1})-{3}+({1}*{2}))+{4}/{0});else(1+2*3)", , , , ,);
object objResult = sc.Eval(formulate1);
if (objResult != null)
{
Response.Write(objResult.ToString());//1+12+3
}
[csharp] view plain copy print?
/* 方法三 需将Microsoft.JScript 和Microsoft.Vsa加入参考并引用Microsoft.JScript */
public object EvalExpress(string sExpression)
{
Microsoft.JScript.Vsa.VsaEngine ve = Microsoft.JScript.Vsa.VsaEngine.CreateEngine();
return Microsoft.JScript.Eval.JScriptEvaluate(sExpression, ve);
}