下面是公试,但他是活的,也就是说在数据库中取出来他不一定是这样,也可能是jw.fq106.hbs - jw.fq106.qbs,偍IF这个公式是最复杂的。
if((jw.fq106.hbs - jw.fq106.qbs)<0,(jw.fq106.hbs - jw.fq106.qbs+ jw.fq106.yblc ),(jw.fq106.hbs - jw.fq106.qbs)) * jw.fq106.ybxs
这是PB代码。就是一个字符串。我要做公试解析,这里要判断"if" "." "<" "," "(" ")" "+-*/";比如 jw.fq106.hbs 它是三个变量, jw是物资名,fq106是仪表位号,hbs是后表。这个话的意思就是去数据库中查询字段wzxx=jw,ybwh=fql106,xbbx=hbs ,符合这三个条件的数值,整个公式就是算到最后就是一个值。请有高手做一个类,谢谢了。跪求一类。
if((jw.fq106.hbs - jw.fq106.qbs)<0,(jw.fq106.hbs - jw.fq106.qbs+ jw.fq106.yblc ),(jw.fq106.hbs - jw.fq106.qbs)) * jw.fq106.ybxs
这是PB代码。就是一个字符串。我要做公试解析,这里要判断"if" "." "<" "," "(" ")" "+-*/";比如 jw.fq106.hbs 它是三个变量, jw是物资名,fq106是仪表位号,hbs是后表。这个话的意思就是去数据库中查询字段wzxx=jw,ybwh=fql106,xbbx=hbs ,符合这三个条件的数值,整个公式就是算到最后就是一个值。请有高手做一个类,谢谢了。跪求一类。
if (jw.fq106.hbs - jw.fq106.qbs)<0
(jw.fq106.hbs - jw.fq106.qbs+ jw.fq106.yblc )
else jw.fq106.hbs - jw.fq106.qbs)) * jw.fq106.ybxs大家帮忙吧。会有好心人的。
{
public ExpressionErrException(){} public ExpressionErrException( string mes ) : base(mes)
{} }
/// <summary>
/// 数学表达式解析,可以解析加减乘除,利用编译器功能实现
/// </summary>
public class MathExpressionParser
{// private string _expression ;
// public MathExpressionParser( string expression )
// {
// _expression = expression ;
// } public MathExpressionParser( )
{ } private CompilerErrorCollection _errors; /// <summary>
/// 编译错误
/// </summary>
public CompilerErrorCollection CompilerErrors
{
get
{
return _errors;
}
} /// <summary>
/// 计算表达式的值
/// </summary>
/// <param name="cCharpCode"></param>
/// <returns></returns>
public object Eval( string cCharpCode )
{ CSharpCodeProvider csharpCodeProvider = new CSharpCodeProvider(); ICodeCompiler compiler = csharpCodeProvider.CreateCompiler(); CompilerParameters cp = new CompilerParameters(); cp.ReferencedAssemblies.Add("system.dll"); cp.CompilerOptions = "/t:library"; cp.GenerateInMemory = true; StringBuilder myCode = new StringBuilder(); myCode.Append("using System;"); myCode.Append("namespace Beyondbit.DotNet.Math{"); myCode.Append("class MathExpressionClass { public object Eval(){return " + cCharpCode + ";}}"); myCode.Append("}"); CompilerResults cr = compiler.CompileAssemblyFromSource(cp,myCode.ToString()); if( cr.Errors.Count > 0 )
{
_errors = cr.Errors;
string s = "";
foreach (System.CodeDom.Compiler.CompilerError ce in cr.Errors)
{
s += ce.ErrorText + "\n" ;
} throw new ExpressionErrException( s );
//return null;
} Assembly assembly = cr.CompiledAssembly; object tmp = assembly.CreateInstance("Beyondbit.DotNet.Math.MathExpressionClass"); Type type = tmp.GetType(); MethodInfo mi = type.GetMethod("Eval"); object result = mi.Invoke(tmp,null); return result; }
C#应当更内容做的到吧