新聞中心
方法一:DataColumn.Expression & DataTable.Compute方法(不支持函數(shù))
成都創(chuàng)新互聯(lián)公司專注為客戶提供全方位的互聯(lián)網(wǎng)綜合服務(wù),包含不限于網(wǎng)站建設(shè)、做網(wǎng)站、卓資網(wǎng)絡(luò)推廣、重慶小程序開發(fā)、卓資網(wǎng)絡(luò)營銷、卓資企業(yè)策劃、卓資品牌公關(guān)、搜索引擎seo、人物專訪、企業(yè)宣傳片、企業(yè)代運(yùn)營等,從售前售中售后,我們都將竭誠為您服務(wù),您的肯定,是我們最大的嘉獎(jiǎng);成都創(chuàng)新互聯(lián)公司為所有大學(xué)生創(chuàng)業(yè)者提供卓資建站搭建服務(wù),24小時(shí)服務(wù)熱線:18982081108,官方網(wǎng)址: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() '''''' 構(gòu)造函數(shù) ''' '''Shared Sub New() End Sub ''' ''' 獲取實(shí)例 ''' '''''' ''' 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
當(dāng)前標(biāo)題:公式字符串轉(zhuǎn)換為公式或結(jié)果
文章分享:http://fisionsoft.com.cn/article/ijddde.html