快上网专注成都网站设计 成都网站制作 成都网站建设
成都网站建设公司服务热线:028-86922220

网站建设知识

十年网站开发经验 + 多家企业客户 + 靠谱的建站团队

量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决

公式字符串转换为公式或结果

方法一:DataColumn.Expression & DataTable.Compute方法(不支持函数)

成都创新互联公司专注为客户提供全方位的互联网综合服务,包含不限于网站建设、做网站、卓资网络推广、重庆小程序开发、卓资网络营销、卓资企业策划、卓资品牌公关、搜索引擎seo、人物专访、企业宣传片、企业代运营等,从售前售中售后,我们都将竭诚为您服务,您的肯定,是我们最大的嘉奖;成都创新互联公司为所有大学生创业者提供卓资建站搭建服务,24小时服务热线:18982081108,官方网址:www.cdcxhl.com

DataColumn.Expression:

Dim objData As New DataTable
objData.Columns.Add("a", Type.GetType(Integer))
objData.Columns.Add("b", Type.GetType(Integer))
objData.Columns.Add("c", Type.GetType(Integer))
Dim objCol As DataColumn = _
    objData.Columns.Add("x", Type.GetType(Integer))
objCol.Expression = "a+b*c-2"
Dim objRow As DataRow = DataTable.NewRow()
objRow("a") = 2
objRow("b") = 3
objRow("c") = 4
objData.Rows.Add(objRow)
Console.WriteLine(objData.Rows(0)("x"))

DataTable.Compute:

MessageBox.Show((new DataTable()).Compute("1+2*3/4", "").ToString())

方法二:SQL的方式

方法三:解析

Imports System.Text.RegularExpressions
Public Class Evaluate
    Shared ReadOnly m_instance As New Evaluate()
    ''' 
    ''' 构造函数
    ''' 
    ''' 
    Shared Sub New()
    End Sub
    ''' 
    ''' 获取实例
    ''' 
    ''' 
    ''' 
    ''' 
    Public Shared ReadOnly Property Instance() As Evaluate
        Get
            Return m_instance
        End Get
    End Property
    ' A number is a sequence of digits optionally followed by a dot and
    ' another sequence of digits. The number in parenthesis in order to
    ' define an unnamed group.
    Private Const Num As String = "(\-?\d+\.?\d*)"
    ' List of 1-operand functions.
    Private Const Func1 As String = "(exp|log|log10|abs|sqr|sqrt|sin|cos|tan|asin|acos|atan)"
    ' List of 2-operand functions.
    Private Const Func2 As String = "(atan2)"
    ' List of N-operand functions.
    Private Const FuncN As String = "(min|max)"
    ' List of predefined constants.
    Private Const Constants As String = "(e|pi)"
    Function Eval(ByVal expr As String) As Double
        ' Define one Regex object for each supported operation.
        ' They are outside the loop, so that they are compiled only once.
        ' Binary operations are defined as two numbers with a symbol between them
        ' optionally separated by spaces.
        Dim rePower As New Regex(Num & "\s*(\^)\s*" & Num)
        Dim reAddSub As New Regex(Num & "\s*([-+])\s*" & Num)
        Dim reMulDiv As New Regex(Num & "\s*([*/])\s*" & Num)
        ' These Regex objects resolve call to functions. (Case insensitivity.)
        Dim reFunc1 As New Regex(Func1 & "\(\s*" & Num & "\s*\)", _
            RegexOptions.IgnoreCase)
        Dim reFunc2 As New Regex(Func2 & "\(\s*" & Num & "\s*,\s*" & Num _
            & "\s*\)", RegexOptions.IgnoreCase)
        Dim reFuncN As New Regex(FuncN & "\((\s*" & Num & "\s*,)+\s*" & Num _
            & "\s*\)", RegexOptions.IgnoreCase)
        ' This Regex object drop a + when it follows an operator.
        Dim reSign1 As New Regex("([-+/*^])\s*\+")
        ' This Regex object converts a double minus into a plus.
        Dim reSign2 As New Regex("\-\s*\-")
        ' This Regex object drops parenthesis around a number.
        ' (must not be preceded by an alphanum char (it might be a function name)
        Dim rePar As New Regex("(? ""
            ' Get the argument, replace any comma to space, and convert to double.
            args.Add(CDbl(m.Groups(i).Value.Replace(","c, " "c)))
            i += 1
        Loop
        Dim str As String = ""
        ' function name is 1st group.
        Select Case m.Groups(1).Value.ToUpper
            Case "MIN"
                args.Sort()
                str = args(0).ToString
            Case "MAX"
                args.Sort()
                str = args(args.Count - 1).ToString
        End Select
        Return str
    End Function
End Class

方法四:

http://www.codeproject.com/vb/net/expression_evaluator.asp

参考资料:http://blog.csdn.net/fangxinggood/article/details/5992661


网页名称:公式字符串转换为公式或结果
本文网址:http://6mz.cn/article/ijddde.html

其他资讯