惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

IT之家
IT之家
Y
Y Combinator Blog
月光博客
月光博客
Blog — PlanetScale
Blog — PlanetScale
GbyAI
GbyAI
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
博客园 - 三生石上(FineUI控件)
S
SegmentFault 最新的问题
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
美团技术团队
雷峰网
雷峰网
酷 壳 – CoolShell
酷 壳 – CoolShell
Last Week in AI
Last Week in AI
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
有赞技术团队
有赞技术团队
博客园 - 司徒正美
V
Visual Studio Blog
小众软件
小众软件
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
T
Tailwind CSS Blog
Apple Machine Learning Research
Apple Machine Learning Research
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
A
About on SuperTechFans
The Cloudflare Blog

博客园 - 明仔¥

TP6 实现上传文件 页面自适应纯CSS,使用rem单位 jQuery解决高度统一问题 dedecms Ajax异步获取文章列表 手机端页面自适应解决方案—rem布局 mysql中bigint、int、mediumint、smallint 和 tinyint的取值范围 修改PHP 上传文件大小限制 WampServer下修改和重置MySQL密码 CSS文件添加 @charset "utf-8"; 可能会引起样式在IE6下失效 Windows Server 2003下MySQL 5.1.32安装图文 如何彻底卸载MySQL 最好用的mysql密码忘记的解决方法 ASP将ACCESS表中的数据记录导入到EXCEL文件中 - 明仔¥ - 博客园 clng与cint的区别及防溢出函数 - 明仔¥ - 博客园 几个比较好看的Button的CSS - 明仔¥ - 博客园 网站样式切换效果 - 明仔¥ - 博客园 asp生成excel报表 utf-8编码用于asp 出现乱码的问题--从数据库调用的是乱码--gb2312转换utf-8 - 明仔¥ - 博客园 关于js中window.location.href,location.href,parent.location.href,top.location.href的用法 - 明仔¥ - 博客园
VB中如何将字符串转换成数值来运算?
明仔¥ · 2010-11-30 · via 博客园 - 明仔¥

'使用下面提供给你的函数可以做到这点!
'先定义2个结构Parent和Plus,把这2个定义放在代码段最前面
Private Type Parent
  s As String
  value As Double
End Type
Private Type Plus
  s As String
  value As Double
End Type

'下面是实现此功能的函数的定义,比较长
Public Function ValueOfExpression(ByVal Express As String) As Double
Dim Pa() As Parent, ParNum As Integer, Ps() As Plus, OperNum As Integer
Dim str0 As String

'按括号分解表达式 Express:Begin-----/*
Dim lenExp As Integer, Lenstr1 As Integer, i As Integer, j As Integer, k As Integer, str1 As String, str2 As String, intPar As Integer
Dim intStart As Integer, intEnd As Integer
lenExp = Len(Express)
For i = 1 To lenExp
  If Mid(Express, i, 1) = "(" Then intPar = intPar + 1
Next
ParNum = intPar
ReDim Pa((intPar / 10 + 1) * 10)
For i = 1 To intPar
  If intPar < 1 Then Exit For
  For j = 1 To lenExp
    If Mid(Express, j, 1) = ")" Then
      str1 = Mid(Express, 1, j - 1)
      Exit For
    End If
  Next
  Lenstr1 = Len(str1)
  For k = 1 To Lenstr1
    If Mid(str1, Lenstr1 + 1 - k, 1) = "(" Then
      Pa(i).s = Mid(str1, Lenstr1 + 2 - k)
      Exit For
    End If
  Next
  Express = Mid(Express, 1, Lenstr1 - k) & Chr(128) & CStr(i) & Mid(Express, j + 1)
  lenExp = Len(Express)
Next
Pa(0).s = Express
'*/-----End

'按加减号进一步分解:Begin-----/*
Dim n As Integer, strLeft As String
For i = 0 To ParNum
  k = 0
  For j = 1 To Len(Pa(i).s)
    str1 = Mid(Pa(i).s, j, 1)
    If str1 = "+" Or str1 = "-" Then k = k + 1
  Next
  If k > OperNum Then OperNum = k
Next
ReDim Ps(ParNum, OperNum)
For i = 0 To ParNum
  strLeft = Pa(i).s: n = 0: str2 = ""
  Do
    If Len(strLeft) = 0 Then Exit Do
    For j = 1 To Len(strLeft)
      str1 = Mid(strLeft, j, 1)
      If str1 = "+" Or str1 = "-" Then
        Ps(i, n).s = str2 & Mid(strLeft, 1, j - 1)
        n = n + 1
        str2 = IIf(str1 = "-", str1, "")
        strLeft = Mid(strLeft, j + 1)
        Exit For
      End If
      If j = Len(strLeft) Then
        Ps(i, n).s = str2 & strLeft: j = 0
        Exit For
      End If
    Next
  Loop Until j = 0
Next
'*/-----End

'计算最后分成的多个简单表达式的值的总和,即表达式 Express 的值
Dim Total As Double, value As Double
For i = 1 To ParNum + 1
  If i = ParNum + 1 Then i = 0
  Total = 0
  For j = 0 To OperNum
    Express = Ps(i, j).s: value = 0
    Dim lasti As Integer, operator As String
    lenExp = Len(Express): lasti = 0: operator = ""
    For k = 1 To lenExp
      str0 = Mid(Express, k, 1)
      If InStr("*/^", str0) > 0 Or k = lenExp Then
        If k = lenExp Then k = k + 1
        str1 = Mid(Express, lasti + 1, k - 1 - lasti)
        Dim sign As Integer, Valstr1 As Double
        If Mid(str1, 1, 1) = "-" Then
          sign = -1
          str1 = Mid(str1, 2)
        Else
          sign = 1
        End If
        n = InStr(1, "/sin" & Chr(128) & "/cos" & Chr(128) & "/tan" & Chr(128) & "/abs" & Chr(128) & "/atn" & Chr(128) & "/exp" & Chr(128) & "/int" & Chr(128) & "/fix" & Chr(128) & "/sgn" & Chr(128) & "/sqr" & Chr(128) & "/", "/" & Mid(str1, 1, 4) & "/")
        If n > 0 Then
          Valstr1 = Choose((n + 4) / 5, Sin(Pa(Val(Mid(str1, 5))).value), Cos(Pa(Val(Mid(str1, 5))).value), Tan(Pa(Val(Mid(str1, 5))).value), Abs(Pa(Val(Mid(str1, 5))).value), Atn(Pa(Val(Mid(str1, 5))).value), Exp(Pa(Val(Mid(str1, 5))).value), Int(Pa(Val(Mid(str1, 5))).value), Fix(Pa(Val(Mid(str1, 5))).value), Sgn(Pa(Val(Mid(str1, 5))).value), Sqr(Pa(Val(Mid(str1, 5))).value))
        Else
          n = InStr(1, "/lg" & Chr(128) & "/ln" & Chr(128) & "/", Mid(str1, 1, 3))
          If n > 0 Then
            Valstr1 = Choose((n + 3) / 4, Log(Pa(Val(Mid(str1, 4))).value) / Log(10), Log(Pa(Val(Mid(str1, 4))).value))
          Else
            If Mid(str1, 1, 1) = Chr(128) Then
              Valstr1 = Pa(Val(Mid(str1, 2))).value
            ElseIf Right(str1, 1) = "!" Then
              If Val(str1) = 0 Then
                Valstr1 = 1
              Else
                Valstr1 = 1
                For n = 1 To Val(str1)
                  Valstr1 = Valstr1 * n
                Next
              End If
            Else
              Valstr1 = Val(str1)
            End If
          End If
        End If
        Valstr1 = Valstr1 * sign
        Select Case operator
          Case ""
            value = Valstr1
          Case "*"
            value = value * Valstr1
          Case "/"
            value = value / Valstr1
          Case "^"
            value = value ^ Valstr1
        End Select
        lasti = k: operator = str0
      End If
    Next
    Ps(i, j).value = value
    Total = Total + Ps(i, j).value
  Next
  Pa(i).value = Total
  If i = 0 Then Exit For
Next
ValueOfExpression = Pa(0).value
End Function
'OK,到这里结束了该函数的定义
'使用例子:
'A="5+5+5"
'Print ValueOfExpression(A)

'其它说明:该函数支持很多数学函数如sin、cos、tan等等,如:
'Print ValueOfExpression("sin(1)")
'注意后面要加括号
'该函数支持运算符+-*/^!,还有括号,^为乘幂,其优先级与*/相同,这是使用该函数需要注意的地方;!为阶乘
'自己试试就知道怎么回事了!