公式解析 公式中去掉Math
字符串中 Math 要大写,其余都要小写, 具体是JScript 的语法 , 遵循就是
//****************公式中去掉 Math
/// <summary>
/// 计算字符串表达式的通用类。
/// 引用Microsoft.JScript.Eval和Microsoft.JScript.Vsa.VsaEngine来实现公式计算方法
/// </summary>
/// <param name="strequation">字符串表达式</param>
/// <param name="paras">传入的参数</param>
/// <param name="paravalues">传入参数对应的值</param>
/// <param name="repara">返回的参数名</param>
/// <returns></returns>
public object RunFormula(string strequation, List<string> paras, List<string> paravalues, string repara)
{
StringBuilder myCode = new StringBuilder();
myCode.Append("function getValue() : double {");
for (int i = 0; i < paras.Count; i++)
{
myCode.Append("var " + paras[i] + " = " + paravalues[i] + " ;");
}
myCode.Append("var " + repara + "=0; with (Math){ ");
myCode.Append(repara + "=" + strequation + "};");
myCode.Append(" return " + repara + ";");
myCode.Append("}");
try
{
object ret = Microsoft.JScript.Eval.JScriptEvaluate(myCode + "getValue()",
Microsoft.JScript.Vsa.VsaEngine.CreateEngine());
return ret;
}
catch
{
return "false";
}
}