求教当n的值为空值时,解决类型错误的问题,谢谢!!!如式子改为3+n+6 ,当n=""时,3+n+6=0请将下面的代码保存为ExpressionForm.FRM运行即可。
VERSION 5.00
Begin VB.Form ExpressionForm 
   Caption         =   "Expression"
   ClientHeight    =   2310
   ClientLeft      =   1380
   ClientTop       =   2100
   ClientWidth     =   6615
   LinkTopic       =   "Form1"
   PaletteMode     =   1  'UseZOrder
   ScaleHeight     =   2310
   ScaleWidth      =   6615
   Begin VB.TextBox ExprText 
      Height          =   285
      Left            =   0
      TabIndex        =   0
      Text            =   "3+n"
      Top             =   360
      Width           =   3615
   End
   Begin VB.CommandButton CmdEvaluate 
      Appearance      =   0  'Flat
      BackColor       =   &H80000005&
      Caption         =   "Evaluate"
      Default         =   -1  'True
      Height          =   495
      Left            =   1200
      TabIndex        =   11
      Top             =   960
      Width           =   1215
   End
   Begin VB.Frame Frame1 
      Appearance      =   0  'Flat
      Caption         =   "Primitives"
      ForeColor       =   &H80000008&
      Height          =   2295
      Left            =   3720
      TabIndex        =   12
      Top             =   0
      Width           =   2895
      Begin VB.TextBox NameText 
         Height          =   285
         Index           =   0
         Left            =   240
         TabIndex        =   1
         Text            =   "n"
         Top             =   480
         Width           =   1215
      End
      Begin VB.TextBox ValueText 
         Height          =   285
         Index           =   0
         Left            =   1560
         TabIndex        =   2
         Top             =   480
         Width           =   1215
      End
      Begin VB.TextBox NameText 
         Height          =   285
         Index           =   1
         Left            =   240
         TabIndex        =   3
         Top             =   840
         Width           =   1215
      End
      Begin VB.TextBox ValueText 
         Height          =   285
         Index           =   1
         Left            =   1560
         TabIndex        =   4
         Top             =   840
         Width           =   1215
      End
      Begin VB.TextBox NameText 
         Height          =   285
         Index           =   2
         Left            =   240
         TabIndex        =   5
         Top             =   1200
         Width           =   1215
      End
      Begin VB.TextBox ValueText 
         Height          =   285
         Index           =   2
         Left            =   1560
         TabIndex        =   6
         Top             =   1200
         Width           =   1215
      End
      Begin VB.TextBox NameText 
         Height          =   285
         Index           =   3
         Left            =   240
         TabIndex        =   7
         Top             =   1560
         Width           =   1215
      End
      Begin VB.TextBox ValueText 
         Height          =   285
         Index           =   3
         Left            =   1560
         TabIndex        =   8
         Top             =   1560
         Width           =   1215
      End
      Begin VB.TextBox NameText 
         Height          =   285
         Index           =   4
         Left            =   240
         TabIndex        =   9
         Top             =   1920
         Width           =   1215
      End
      Begin VB.TextBox ValueText 
         Height          =   285
         Index           =   4
         Left            =   1560
         TabIndex        =   10
         Top             =   1920
         Width           =   1215
      End
      Begin VB.Label Label1 
         Appearance      =   0  'Flat
         Caption         =   "Name"
         ForeColor       =   &H80000008&
         Height          =   255
         Index           =   0
         Left            =   240
         TabIndex        =   14
         Top             =   240
         Width           =   615
      End
      Begin VB.Label Label1 
         Appearance      =   0  'Flat
         Caption         =   "Value"
         ForeColor       =   &H80000008&
         Height          =   255
         Index           =   1
         Left            =   1560
         TabIndex        =   13
         Top             =   240
         Width           =   615
      End
   End
   Begin VB.Label Label2 
      Appearance      =   0  'Flat
      Caption         =   "Expression"
      ForeColor       =   &H80000008&
      Height          =   255
      Left            =   0
      TabIndex        =   17
      Top             =   0
      Width           =   975
   End
   Begin VB.Label Label3 
      Appearance      =   0  'Flat
      Caption         =   "Result"
      ForeColor       =   &H80000008&
      Height          =   255
      Left            =   480
      TabIndex        =   16
      Top             =   1800
      Width           =   615
   End
   Begin VB.Label ResultLabel 
      BorderStyle     =   1  'Fixed Single
      Height          =   255
      Left            =   1200
      TabIndex        =   15
      Top             =   1800
      Width           =   1215
   End
End
Attribute VB_Name = "ExpressionForm"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Option Explicit
Dim Primitives As Collection
' ************************************************' Evaluate the expression.' ************************************************Private Function EvaluateExpr(ByVal expr As String) As StringConst PREC_NONE = 11Const PREC_UNARY = 10 ' Not actually used.Const PREC_POWER = 9Const PREC_TIMES = 8Const PREC_DIV = 7Const PREC_INT_DIV = 6Const PREC_MOD = 5Const PREC_PLUS = 4
Dim is_unary As BooleanDim next_unary As BooleanDim parens As IntegerDim pos As IntegerDim expr_len As IntegerDim ch As StringDim lexpr As StringDim rexpr As StringDim value As StringDim status As LongDim best_pos As IntegerDim best_prec As Integer
' Remove leading and trailing blanks.expr = Trim$(expr)expr_len = Len(expr)If expr_len = 0 Then Exit Function
' If we find + or - now, it is a unary operator.is_unary = True
' So far we have nothing.best_prec = PREC_NONE
' Find the operator with the lowest precedence.' Look for places where there are no open' parentheses.For pos = 1 To expr_len' Examine the next character.ch = Mid$(expr, pos, 1)
' Assume we will not find an operator. In' that case the next operator will not' be unary.next_unary = False
If ch = " " Then' Just skip spaces.next_unary = is_unaryElseIf ch = "(" Then' Increase the open parentheses count.parens = parens + 1
' An operator after "(" is unary.next_unary = TrueElseIf ch = ")" Then' Decrease the open parentheses count.parens = parens - 1
' An operator after ")" is unary.next_unary = True
' If parens < 0, too many ')'s.If parens < 0 ThenErr.Raise vbObjectError + 1001, "EvaluateExpr", "Too many )s in '" & expr & "'"End IfElseIf parens = 0 Then' See if this is an operator.If ch = "^" Or ch = "*" Or _
ch = "/" Or ch = "\" Or _
ch = "%" Or ch = "+" Or _
ch = "-" _
Then' An operator after an operator' is unary.next_unary = True
Select Case chCase "^"If best_prec >= PREC_POWER Thenbest_prec = PREC_POWERbest_pos = posEnd If
Case "*", "/"If best_prec >= PREC_TIMES Thenbest_prec = PREC_TIMESbest_pos = posEnd If
Case "\"If best_prec >= PREC_INT_DIV Thenbest_prec = PREC_INT_DIVbest_pos = posEnd If
Case "%"If best_prec >= PREC_MOD Thenbest_prec = PREC_MODbest_pos = posEnd If

解决方案 »

  1.   

    Case "+", "-"' Ignore unary operators' for now.If (Not is_unary) And best_prec >= PREC_PLUS Thenbest_prec = PREC_PLUSbest_pos = posEnd IfEnd SelectEnd IfEnd Ifis_unary = next_unaryNext pos
    ' If the parentheses count is not zero,' there's a ')' missing.If parens <> 0 ThenErr.Raise vbObjectError + 1002, "EvaluateExpr", "Missing ) in '" & expr & "'"End If
    ' Hopefully we have the operator.If best_prec < PREC_NONE Thenlexpr = Left$(expr, best_pos - 1)rexpr = Right$(expr, expr_len - best_pos)Select Case Mid$(expr, best_pos, 1)Case "^"EvaluateExpr = EvaluateExpr(lexpr) ^ EvaluateExpr(rexpr)Case "*"EvaluateExpr = EvaluateExpr(lexpr) * EvaluateExpr(rexpr)Case "/"EvaluateExpr = EvaluateExpr(lexpr) / EvaluateExpr(rexpr)Case "\"EvaluateExpr = EvaluateExpr(lexpr) \ EvaluateExpr(rexpr)Case "%"EvaluateExpr = EvaluateExpr(lexpr) Mod EvaluateExpr(rexpr)Case "+"If rexpr = "" Then
    MsgBox rexpr
    End IfEvaluateExpr = Val(EvaluateExpr(lexpr)) + Val(EvaluateExpr(rexpr)) '求教当n的值为空值时,解决类型错误的问题,谢谢(当n=""时,式子就返回0)MsgBox "JK"Case "-"EvaluateExpr = EvaluateExpr(lexpr) - EvaluateExpr(rexpr)End SelectExit FunctionEnd If
    ' If we do not yet have an operator, there' are several possibilities:'' 1. expr is (expr2) for some expr2.' 2. expr is -expr2 or +expr2 for some expr2.' 3. expr is Fun(expr2) for a function Fun.' 4. expr is a primitive.' 5. It's a literal like "3.14159".
    ' Look for (expr2).If Left$(expr, 1) = "(" And Right$(expr, 1) = ")" Then' Remove the parentheses.EvaluateExpr = EvaluateExpr(Mid$(expr, 2, expr_len - 2))Exit FunctionEnd If
    ' Look for -expr2.If Left$(expr, 1) = "-" ThenEvaluateExpr = -EvaluateExpr(Right$(expr, expr_len - 1))Exit FunctionEnd If
    ' Look for +expr2.If Left$(expr, 1) = "+" ThenEvaluateExpr = EvaluateExpr(Right$(expr, expr_len - 1))Exit FunctionEnd If
    ' Look for Fun(expr2).If expr_len > 5 And Right$(expr, 1) = ")" Thenlexpr = LCase$(Left$(expr, 4))rexpr = Mid$(expr, 5, expr_len - 5)Select Case lexprCase "sin("EvaluateExpr = Sin(EvaluateExpr(rexpr))Exit FunctionCase "cos("EvaluateExpr = Cos(EvaluateExpr(rexpr))Exit FunctionCase "tan("EvaluateExpr = Tan(EvaluateExpr(rexpr))Exit FunctionCase "sqr("EvaluateExpr = Sqr(EvaluateExpr(rexpr))Exit FunctionEnd SelectEnd If
    ' See if it's a primitive.On Error Resume Nextvalue = Primitives.Item(expr)status = Err.NumberOn Error GoTo 0If status = 0 Then
    EvaluateExpr = CSng(value)Exit FunctionEnd If
    ' It must be a literal like "2.71828".On Error Resume NextEvaluateExpr = CSng(expr)status = Err.NumberOn Error GoTo 0If status <> 0 ThenErr.Raise status, "EvaluateExpr", "Error evaluating '" & expr & "' as a constant."End IfEnd Function' ************************************************' Evaluate the expression entered by the user.' ************************************************Private Sub CmdEvaluate_Click()Dim i As IntegerDim name As StringDim value As StringDim expr As StringDim rslt As SingleDim pos As Integer
    ' Store the primitives.Set Primitives = New CollectionFor i = 0 To 4name = Trim$(NameText(i).Text)value = Trim$(ValueText(i).Text)If name <> "" ThenPrimitives.Add value, nameEnd IfNext i
    ' Get the expression.expr = ExprText.Text
    ' Evaluate the expression.ResultLabel.Caption = ""On Error GoTo EvaluateErrorrslt = EvaluateExpr(expr)ResultLabel.Caption = Format$(rslt)Exit Sub
    EvaluateError:BeepMsgBox Err.DescriptionEnd Sub
      

  2.   

    很头疼,先判断一下 ,if ……else ……
      

  3.   

    to Tiger_Zhao:
    问题是我不知道在校验加在哪一步,我测试时,总是在下面提示类型不正确
    Case "+"
    EvaluateExpr = Val(EvaluateExpr(lexpr)) + Val(EvaluateExpr(rexpr)) 
      

  4.   

    If status = 0 Then if value<>"" then EvaluateExpr = CSng(value) Exit Function End If
    调试时将 on error 先屏蔽掉就知道了啊. 
      

  5.   

    要是当 n="" 时让3+n+6=9,可以下面这么改!
       If status = 0 Then
        
            If value = "" Then
                value = 0
            End If

            EvaluateExpr = CSng(value)
            
            Exit Function
        
        End If
      

  6.   

    先做个转换的函数  
    private founction sVal (sInt as integer) as integer
        if isnull(sint) = true then 
            sVal = 0
        else
            sVal = sInt
        end if
      

  7.   

    计算之前先做判断和转换,不知你n为什么类型,不妨写成:3+val(n)+6
      

  8.   

    不懂 Begin .  是什么意思的路过  - -