怎样解析带有函数的字符串
比如:abs(abs(1)+2*(1+3))

解决方案 »

  1.   

    我实在datatable上进行操作的。就是在列之间进行的运算。比如:第二列值=abs(第一列)+2*(第二列值)之类的
      

  2.   


    DataTable列表达式计算:有abs函数,表达式不支持abs函数/// <summary>
    /// 采用DataTable的Columns属性的Expression属性计算表达式结果
    /// </summary>        
    //  如下结构的数据表:
    //  A1    A2    A3    A4    A5    A6
    //  5     12    23    45    90    87
    //  56    78    45    32    56    97
    //  解析以下四个表达式:
    //  <1> A6=A1*1.5-A2
    //  <2> A6=(A1+A2+A3+A4+A5)/5+SUM(A4)+AVG(A2)
    //  <3> A6=IIF(A1>A2,A1+A2,A1-A2)
    //  <4> A6=IIF(MAX(A1)>100 AND MIN(A3) > 10,'0','1')        
    private void CalcExpressions(string strEx)
    {
        // this.m_dt => DataTable
        // this.m_bs => BindingSource
        if (strEx != null && strEx.Trim().Length > 0)
        {
            // 按照"="拆解字符串
            int nIndex = strEx.IndexOf("=");
            if (nIndex != -1)
            {
                // 计算表达式,并立即改写DataTable数据
                this.m_dt.Columns[strEx.Substring(0,nIndex).Trim()].Expression = strEx.Substring(nIndex + 1,strEx.Length - nIndex - 1).Trim();
                // 修改数据后,强制UI刷新数据
                this.m_bs.ResetBindings(false);
            }
        }
    }
    这个如何:C#的动态编译,并返回结果/// <summary>
    /// 采用动态编译,计算表达式的结果
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    private void button6_Click(object sender, EventArgs e)
    {
        // 声明要计算C#代码,要求必须完整的C#代码
        string strCode = @"
                        using System;
                        namespace ParseEx
                        {
                            public class ParseExC
                            {
                                public static double GetValue()
                                {
                                    return 1+5*8-(Math.Sin(12)*2.5);
                                }
                            }
                        }";    CodeDomProvider comp  = new CSharpCodeProvider();
        CompilerParameters cp = new CompilerParameters();                StringBuilder codeBuilder = new StringBuilder();            
        codeBuilder.AppendLine(strCode);    cp.ReferencedAssemblies.Add("System.dll");
        cp.GenerateExecutable = false;
        cp.GenerateInMemory   = true;
        
        CompilerResults cr = comp.CompileAssemblyFromSource(cp, codeBuilder.ToString());
        if (cr.Errors.HasErrors)
        {
            MessageBox.Show("Error!");
        }
        else
        {
            Assembly a = cr.CompiledAssembly;
            if (a != null)
            {
                Type t = a.GetType("ParseEx.ParseExC");
                if (t != null)
                {
                    // 因为是静态调用,不需要创建实例了
                    // object mode = a.CreateInstance("Mode");
                    MethodInfo mi = t.GetMethod("GetValue", BindingFlags.Static | BindingFlags.Public);
                    if (mi != null)
                    {
                        double d = (double)mi.Invoke(null, null);
                        MessageBox.Show(d.ToString());
                    }
                }
            }
        }
    }
      

  3.   

    花了点时间弄好的。不过现在不打算开源。
    类似于excel中的函数。
      

  4.   

    to isjoe
    这个怎样发图片啊
    我的问题想给你截图说明
      

  5.   

    to isjoe
    怎样发图片啊,我的问题想通过我的截图给你说明啊
      

  6.   

    to isjoe怎样发图片啊,我的问题想通过截图给你说明。
      

  7.   

    to isjoe
    http://writeblog.csdn.net/PostList.aspx
      

  8.   

    http://www.cnblogs.com/michaelhuwei/archive/2008/03/26/1123401.html早就开源了,支持函数的解析
      

  9.   

    我的函数不光是数学函数还有一些其他函数,例如球字符串的长度:Length("我们是");
    之类的
     if (char.IsDigit(c))
                                {
                                    ProcDigit(ref i, ref strExpression);
                                }
                                else //其它的字符全当成函数
                                {
                                    ProcFunc(ref i, ref strExpression);
                                }
                                break;
    他把参数值也当成函数啊
      

  10.   

    他那个可以扩充。
    我写的是用在一个项目上。功能支持函数嵌套。循环。返回object对象。
    参数也可以是object对象。支持一切c#写的函数。不过函数重载还在考虑加不加上。那个有点影响效率。
    这个只是那个项目很小的一部分。
    暂时没开算开源。将来不打算混淆。
    不开源,不免费,不混淆。
      

  11.   

    to isjoe
    如果我想调用string类里面的函数这块怎写?
     using System;
                        namespace ParseEx
                        {
                            public class ParseExC
                            {
                                public static double GetValue()
                                {
                                    return 1+5*8-(Math.Sin(12)*2.5);                            }
                            }
                        }";
      

  12.   


    return 1+5*8-(Math.Sin(12)*2.5);只要你写的语句符合C#语法,就可以通过并返回结果
      

  13.   

    我刚才看了看
    http://www.cnblogs.com/michaelhuwei/archive/2008/03/26/1123401.html里面写的计算表达式的代码,稍加修改就可以支持Length("我们是")这样的函数。
      

  14.   

    to isjoe
         不知道怎样写参数是字符串类的函数,希望你指点。我的qq476155749.想要获得你更多的指点。