dim a ,b as interg
dim c as sing
c=a*b
可是我在程序中有可能是c=a/b,也有可能是d=b/c, 因此我想问问各位大大,有没有办法给运算符分配一个变量,让程序动态地改变运算符??

解决方案 »

  1.   

    将符号设为string型,在运算中再转为运算符。顺便问一下 sing是什么型?
      

  2.   


        如果只是这样简单的加减乘除运算,用select吧。    dim strA as string
        select case strA
          case  "+"
            c=a+b
          case "-"
            c=a-b
          case "*"
            c=a*b
          case "/"
            c=a/b   '当然,也许还要区分a、b的位置。
        end select
      

  3.   

    可以呀,
    给你个计算函数Public Function 计算(GS As String) As String
        Dim i, n As Integer
        Dim TempGs, Temp As String
        Dim Vl() As String '操作数
        Dim Vls As Integer '操作数的数目
        Dim Si As Integer '上一操作符的位置
        Dim Ads, Sus, Mus, Bys, Lks, Rks As Integer     '操作符的数目
        Dim Adp(), Mup(), Byp(), Lkp(), Rkp() As Integer '操作符的位置
        Dim Adn(), Mun(), Byn() As Integer '操作符的排列次序
        Dim Sig() As Integer '每一个操作符的位置
        
    On Error GoTo Err
    Do While True
        ReDim Adp(Len(GS)), Mup(Len(GS)), Byp(Len(GS)) _
            , Lkp(Len(GS)), Rkp(Len(GS)) As Integer
        ReDim Adn(Len(GS)), Mun(Len(GS)), Byn(Len(GS)) _
            , Lkn(Len(GS)), Rkn(Len(GS)), Sig(Len(GS)) As Integer
        
        ReDim Vl(Len(GS))
        
        If Len(GS) = 0 Then GoTo Err
        If Mid(GS, Len(GS), 1) <> "#" Then
        
            TempGs = GS
            For i = 1 To Len(GS) '将减化加
                
                If Mid(GS, i, 1) = "-" And i <> 1 Then
                    If Mid(GS, i - 1, 1) <> "+" And Mid(GS, i - 1, 1) <> "-" _
                        And Mid(GS, i - 1, 1) <> "*" And Mid(GS, i - 1, 1) <> "/" Then
                        TempGs = Mid(TempGs, 1, i - 1 + n) + "+" + Mid(GS, i)
                        n = n + 1
                    End If
                    
                End If
            Next i
            GS = TempGs
            
            n = 0
            For i = 1 To Len(GS) '处理负负得正
                If Mid(GS, i, 1) = "-" Then
                    If Mid(GS, i + 1, 1) = "-" Then
                        TempGs = Mid(TempGs, 1, i - 1 - n) + Mid(GS, i + 2)
                        n = n + 2
                    End If
                End If
            Next i
            GS = TempGs
            GS = GS + "#"
        End If
        
        Vls = 1
        Ads = 0: Sus = 0: Mus = 0: Bys = 0: Lks = 0: Rks = 0
        
        For i = 1 To Len(GS)
            
            Select Case Mid(GS, i, 1)
                Case "+"
                    Ads = Ads + 1
                    Adp(Ads) = i
                    Adn(Ads) = Vls
                Case "*"
                    Mus = Mus + 1
                    Mup(Mus) = i
                    Mun(Mus) = Vls
                Case "/"
                    Bys = Bys + 1
                    Byp(Bys) = i
                    Byn(Bys) = Vls
                Case "("
                    Lks = Lks + 1
                    Lkp(Lks) = i
                   
                Case ")"
                    Rks = Rks + 1
                    Rkp(Rks) = i
                    
            End Select
            
            If Mid(GS, i, 1) = "+" Or Mid(GS, i, 1) = "*" Or _
                Mid(GS, i, 1) = "/" Or Mid(GS, i, 1) = "#" Then
                
                If Si + 1 = i And Mid(GS, i + 1, 1) <> "#" _
                     Then '操作符非法连续或以操作符开头
                    GoTo Err
                Else
                    Si = i
                End If
                
                If Not IsNumeric(Vl(Vls)) And Mid(GS, i + 1, 1) <> "#" _
                     Then '操作数不是数字
                    GoTo Err
                End If
                Sig(Vls) = i
                Vls = Vls + 1
                
            Else
                If Mid(GS, i, 1) <> "(" And Mid(GS, i, 1) <> ")" Then
                    Vl(Vls) = Vl(Vls) + Mid(GS, i, 1) '制作操作数
                Else
                  If i <> 1 Then
                    If ((Mid(GS, i - 1, 1) = "(" And Mid(GS, i, 1) = ")") Or _
                       (Mid(GS, i - 1, 1) = ")" And Mid(GS, i, 1) = "(")) _
                          Then '判定括号前后符号的合法性
                        GoTo Err
                    End If
                  End If
                End If
            End If
            
        Next i
        
        If Lks <> Rks Then
            GoTo Err '左右括号数是否匹配
        End If
        
        For i = 1 To Lks
            If Lkp(i) > Rkp(i) Then GoTo Err '左右括号出现顺序错误
        Next i
        
        If Lks <> 0 Then '括号处理
          Do While True
            For i = Lks To 1 Step -1
                For n = Rks To 1 Step -1
                    Temp = 计算(Mid(GS, Lkp(i) + 1, Rkp(n) - Lkp(i) - 1))
                    If Temp <> "公式有错误" Then
                        GS = Mid(GS, 1, Lkp(i) - 1) + Temp + Mid(GS, Rkp(n) + 1)
                        Exit Do
                    End If
                Next n
            Next i
            If Temp = "公式有错误" Then GoTo Err
                '括号中有错误退出
          Loop
        Else
            If Mus <> 0 Then '乘法处理
                GS = Mid(GS, 1, Sig(Mun(1) - 1)) + Trim(Str(Val(Vl(Mun(1))) _
                    * Val(Vl(Mun(1) + 1)))) + Mid(GS, Val(Mup(1)) + Len(Vl(Mun(1) _
                    + 1)) + 1)
            Else
                If Bys <> 0 Then '除法处理
                    GS = Mid(GS, 1, Sig(Byn(1) - 1)) + Trim(Str(Val(Vl(Byn(1))) _
                        / Val(Vl(Byn(1) + 1)))) + Mid(GS, Val(Byp(1)) + Len(Vl(Byn(1) _
                        + 1)) + 1)
                Else
                    If Ads <> 0 Then '加法处理
                        GS = Trim(Str(Val(Vl(1)) + Val(Vl(2)))) + Mid(GS, Val(Adp(1)) _
                            + Len(Vl(2)) + 1)
                    Else
                        计算 = Mid(GS, 1, Len(GS) - 1)
                        Exit Function
                    End If
                End If
            End If
        End If
    Loop
        
        
    Err:
        计算 = "公式有错误"
        
    End Function
      

  4.   

    dim a ,b as interga可不是interg哦
    a现在variant
      

  5.   

    dim a ,b as interg
    dim c as sing
    c=a*b写的都是什么东西啊interg?sing?什么意思啊
      

  6.   

    不好意思,写错了,
    是integer,single
      

  7.   

    那用简单的select case就可以了
      

  8.   

    早说嘛。24点:VERSION 5.00
    Begin VB.Form frmMain 
       BorderStyle     =   1  'Fixed Single
       Caption         =   "  24点"
       ClientHeight    =   4020
       ClientLeft      =   45
       ClientTop       =   330
       ClientWidth     =   5325
       Icon            =   "Form1.frx":0000
       LinkTopic       =   "Form1"
       MaxButton       =   0   'False
       MinButton       =   0   'False
       ScaleHeight     =   4020
       ScaleWidth      =   5325
       StartUpPosition =   3  '窗口缺省
       Begin VB.Timer t 
          Interval        =   20000
          Left            =   4440
          Top             =   2310
       End
       Begin VB.CommandButton cmdExit 
          Caption         =   "退出"
          Height          =   525
          Left            =   3330
          TabIndex        =   6
          Top             =   3030
          Width           =   975
       End
       Begin VB.CommandButton cmdDo 
          Caption         =   "求解"
          Height          =   525
          Left            =   2160
          TabIndex        =   1
          Top             =   3030
          Width           =   975
       End
       Begin VB.CommandButton cmdMake 
          Caption         =   "出题"
          Height          =   525
          Left            =   960
          TabIndex        =   0
          Top             =   3030
          Width           =   975
       End
       Begin VB.Label lresult 
          AutoSize        =   -1  'True
          BeginProperty Font 
             Name            =   "宋体"
             Size            =   15.75
             Charset         =   134
             Weight          =   400
             Underline       =   0   'False
             Italic          =   0   'False
             Strikethrough   =   0   'False
          EndProperty
          Height          =   315
          Left            =   450
          TabIndex        =   7
          Top             =   1560
          Width           =   165
       End
       Begin VB.Label lNumberD 
          AutoSize        =   -1  'True
          Caption         =   "0"
          BeginProperty Font 
             Name            =   "宋体"
             Size            =   48
             Charset         =   134
             Weight          =   400
             Underline       =   0   'False
             Italic          =   0   'False
             Strikethrough   =   0   'False
          EndProperty
          ForeColor       =   &H0000C000&
          Height          =   960
          Left            =   4200
          TabIndex        =   5
          Top             =   300
          Width           =   480
       End
       Begin VB.Label lNumberB 
          AutoSize        =   -1  'True
          Caption         =   "0"
          BeginProperty Font 
             Name            =   "宋体"
             Size            =   48
             Charset         =   134
             Weight          =   400
             Underline       =   0   'False
             Italic          =   0   'False
             Strikethrough   =   0   'False
          EndProperty
          ForeColor       =   &H0080FF80&
          Height          =   960
          Left            =   1680
          TabIndex        =   4
          Top             =   300
          Width           =   480
       End
       Begin VB.Label lNumberC 
          AutoSize        =   -1  'True
          Caption         =   "0"
          BeginProperty Font 
             Name            =   "宋体"
             Size            =   48
             Charset         =   134
             Weight          =   400
             Underline       =   0   'False
             Italic          =   0   'False
             Strikethrough   =   0   'False
          EndProperty
          ForeColor       =   &H0000FF00&
          Height          =   960
          Left            =   2940
          TabIndex        =   3
          Top             =   300
          Width           =   480
       End
       Begin VB.Label lNumberA 
          AutoSize        =   -1  'True
          Caption         =   "0"
          BeginProperty Font 
             Name            =   "宋体"
             Size            =   48
             Charset         =   134
             Weight          =   400
             Underline       =   0   'False
             Italic          =   0   'False
             Strikethrough   =   0   'False
          EndProperty
          ForeColor       =   &H00C0FFC0&
          Height          =   960
          Left            =   420
          TabIndex        =   2
          Top             =   300
          Width           =   480
       End
    End
    Attribute VB_Name = "frmMain"
    Attribute VB_GlobalNameSpace = False
    Attribute VB_Creatable = False
    Attribute VB_PredeclaredId = True
    Attribute VB_Exposed = False
    Dim NumberA, NumberB, NumberC, NumberD As Single
    Dim A, B, C, D As Single
    Dim Jieguo As Single
    Dim ResultExpression As String
    Dim i As Integer
    Dim Num As Boolean
    Sub Kind(i As Integer)
    Select Case i
      Case 1
      A = NumberA: B = NumberB: C = NumberC: D = NumberD
      Case 2
      A = NumberA: B = NumberB: D = NumberC: C = NumberD
      Case 3
      A = NumberA: C = NumberB: B = NumberC: D = NumberD
      Case 4
      A = NumberA: C = NumberB: D = NumberC: B = NumberD
      Case 5
      A = NumberA: D = NumberB: B = NumberC: C = NumberD
      Case 6
      A = NumberA: D = NumberB: C = NumberC: B = NumberD
      
      Case 7
      B = NumberA: A = NumberB: C = NumberC: D = NumberD
      Case 8
      B = NumberA: A = NumberB: D = NumberC: C = NumberD
      Case 9
      B = NumberA: C = NumberB: A = NumberC: D = NumberD
      Case 10
      B = NumberA: C = NumberB: D = NumberC: A = NumberD
      Case 11
      B = NumberA: D = NumberB: A = NumberC: C = NumberD
      Case 12
      B = NumberA: D = NumberB: C = NumberC: A = NumberD
      
      Case 13
      C = NumberA: A = NumberB: B = NumberC: D = NumberD
      Case 14
      C = NumberA: A = NumberB: D = NumberC: B = NumberD
      Case 15
      C = NumberA: B = NumberB: A = NumberC: D = NumberD
      Case 16
      C = NumberA: B = NumberB: D = NumberC: A = NumberD
      Case 17
      C = NumberA: D = NumberB: A = NumberC: B = NumberD
      Case 18
      C = NumberA: D = NumberB: B = NumberC: A = NumberD
      
      Case 19
      D = NumberA: A = NumberB: B = NumberC: C = NumberD
      Case 20
      D = NumberA: A = NumberB: C = NumberC: B = NumberD
      Case 21
      D = NumberA: B = NumberB: A = NumberC: C = NumberD
      Case 22
      D = NumberA: B = NumberB: C = NumberC: A = NumberD
      Case 23
      D = NumberA: C = NumberB: A = NumberC: B = NumberD
      Case 24
      D = NumberA: C = NumberB: B = NumberC: A = NumberD
    End Select
      

  9.   

    接上:End SubPrivate Sub cmdDo_Click()
    For j = 1 To 30
      Select Case j
         Case 1
         For i = 1 To 1
          Call Kind(i)
          Jieguo = A + B + C + D
          If Jieguo = 24 Then
           ResultExpression = A & "+" & B & "+" & C & "+" & D & "=24"
           Num = True
           lresult.Caption = ResultExpression
           Exit For
          End If
         Next i
         Case 2
         For i = 1 To 24
          Call Kind(i)
          Jieguo = A + B + C - D
          If Jieguo = 24 Then
           ResultExpression = A & "+" & B & "+" & C & "-" & D & "=24"
           Num = True
           lresult.Caption = ResultExpression
           Exit For
          End If
         Next i
         Case 3
         For i = 1 To 24
          Call Kind(i)
          Jieguo = A * B + C + D
          If Jieguo = 24 Then
           ResultExpression = A & "*" & B & "+" & C & "+" & D & "=24"
           Num = True
           lresult.Caption = ResultExpression
           Exit For
          End If
         Next i
         Case 4
         For i = 1 To 24
          Call Kind(i)
          Jieguo = A * B + C - D
          If Jieguo = 24 Then
           ResultExpression = A & "*" & B & "+" & C & "-" & D & "=24"
           Num = True
           lresult.Caption = ResultExpression
           Exit For
          End If
         Next i
         Case 5
         For i = 1 To 24
          Call Kind(i)
          Jieguo = A * B * C + D
          If Jieguo = 24 Then
           ResultExpression = A & "*" & B & "*" & C & "+" & D & "=24"
           Num = True
           lresult.Caption = ResultExpression
           Exit For
          End If
         Next i
         Case 6
         For i = 1 To 24
          Call Kind(i)
          Jieguo = A * B * C - D
          If Jieguo = 24 Then
           ResultExpression = A & "*" & B & "*" & C & "-" & D & "=24"
           Num = True
           lresult.Caption = ResultExpression
           Exit For
          End If
         Next i
         Case 7
         For i = 1 To 24
          Call Kind(i)
          Jieguo = A * B * C * D
          If Jieguo = 24 Then
           ResultExpression = A & "*" & B & "*" & C & "*" & D & "=24"
           Num = True
           lresult.Caption = ResultExpression
           Exit For
          End If
         Next i
         Case 8
         For i = 1 To 24
          Call Kind(i)
          Jieguo = A * B + C * D
          If Jieguo = 24 Then
           ResultExpression = A & "*" & B & "+" & C & "*" & D & "=24"
           Num = True
           lresult.Caption = ResultExpression
           Exit For
          End If
         Next i
         Case 9
         For i = 1 To 24
          Call Kind(i)
          Jieguo = A * B - C * D
          If Jieguo = 24 Then
           ResultExpression = A & "*" & B & "-" & C & "*" & D & "=24"
           Num = True
           lresult.Caption = ResultExpression
           Exit For
          End If
         Next i
         Case 10
         For i = 1 To 24
          Call Kind(i)
          Jieguo = A / B + C + D
          If Jieguo = 24 Then
           ResultExpression = A & "/" & B & "+" & C & "+" & D & "=24"
           Num = True
           lresult.Caption = ResultExpression
           Exit For
          End If
         Next i
         Case 11
         For i = 1 To 24
          Call Kind(i)
          Jieguo = A * B / C + D
          If Jieguo = 24 Then
           ResultExpression = A & "*" & B & "/" & C & "+" & D & "=24"
           Num = True
           lresult.Caption = ResultExpression
           Exit For
          End If
         Next i
         Case 12
         For i = 1 To 24
          Call Kind(i)
          Jieguo = A * B / C - D
          If Jieguo = 24 Then
           ResultExpression = A & "*" & B & "/" & C & "-" & D & "=24"
           Num = True
           lresult.Caption = ResultExpression
           Exit For
          End If
         Next i
         Case 13
         For i = 1 To 24
          Call Kind(i)
          Jieguo = A * B * C / D
          If Jieguo = 24 Then
           ResultExpression = A & "*" & B & "*" & C & "/" & D & "=24"
           Num = True
           lresult.Caption = ResultExpression
           Exit For
          End If
         Next i
         Case 14
         For i = 1 To 24
          Call Kind(i)
          Jieguo = A * B / C / D
          If Jieguo = 24 Then
           ResultExpression = A & "*" & B & "/" & C & "/" & D & "=24"
           Num = True
           lresult.Caption = ResultExpression
           Exit For
          End If
         Next i
         Case 15
         For i = 1 To 24
          Call Kind(i)
          Jieguo = (A + B) * C / D
          If Jieguo = 24 Then
           ResultExpression = "(" & A & "+" & B & ")*" & C & "/" & D & "=24"
           Num = True
           lresult.Caption = ResultExpression
           Exit For
          End If
         Next i
         Case 16
         For i = 1 To 24
          Call Kind(i)
          Jieguo = (A + B) * C * D
          If Jieguo = 24 Then
           ResultExpression = "(" & A & "+" & B & ")*" & C & "*" & D & "=24"
           Num = True
           lresult.Caption = ResultExpression
           Exit For
          End If
         Next i
         Case 17
         For i = 1 To 24
          Call Kind(i)
          Jieguo = (A + B) * (C + D)
          If Jieguo = 24 Then
           ResultExpression = "(" & A & "+" & B & ")*(" & C & "+" & D & ")=24"
           Num = True
           lresult.Caption = ResultExpression
           Exit For
          End If
         Next i
         Case 18
         For i = 1 To 24
          Call Kind(i)
          Jieguo = (A + B) * (C - D)
          If Jieguo = 24 Then
           ResultExpression = "(" & A & "+" & B & ")*(" & C & "-" & D & ")=24"
           Num = True
           lresult.Caption = ResultExpression
           Exit For
          End If
         Next i
         Case 19
         For i = 1 To 24
          Call Kind(i)
          Jieguo = (A - B) * (C - D)
          If Jieguo = 24 Then
           ResultExpression = "(" & A & "-" & B & ")*(" & C & "-" & D & ")=24"
           Num = True
           lresult.Caption = ResultExpression
           Exit For
          End If
         Next i
         Case 20
         For i = 1 To 24
          Call Kind(i)
          Jieguo = (A - B) * C / D
          If Jieguo = 24 Then
           ResultExpression = "(" & A & "-" & B & ")*" & C & "/" & D & "=24"
           Num = True
           lresult.Caption = ResultExpression
           Exit For
          End If
         Next i
         Case 21
         For i = 1 To 24
          Call Kind(i)
          Jieguo = (A - B) * C * D
          If Jieguo = 24 Then
           ResultExpression = "(" & A & "-" & B & ")*" & C & "*" & D & "=24"
           Num = True
           lresult.Caption = ResultExpression
           Exit For
          End If
         Next i
         Case 22
         For i = 1 To 24
          Call Kind(i)
          Jieguo = (A + B + C) * D
          If Jieguo = 24 Then
           ResultExpression = "(" & A & "+" & B & "+" & C & ")*" & D & "=24"
           Num = True
           lresult.Caption = ResultExpression
           Exit For
          End If
         Next i
         Case 23
         For i = 1 To 24
          Call Kind(i)
          Jieguo = (A + B + C) / D
          If Jieguo = 24 Then
           ResultExpression = "(" & A & "+" & B & "+" & C & ")/" & D & "=24"
           Num = True
           lresult.Caption = ResultExpression
           Exit For
          End If
         Next i
         Case 24
         For i = 1 To 24
          Call Kind(i)
          Jieguo = (A + B - C) * D
          If Jieguo = 24 Then
           ResultExpression = "(" & A & "+" & B & "-" & C & ")*" & D & "=24"
           Num = True
           lresult.Caption = ResultExpression
           Exit For
          End If
         Next i
         Case 25
         For i = 1 To 24
          Call Kind(i)
          Jieguo = A * (B + C) + D
          If Jieguo = 24 Then
           ResultExpression = A & "*(" & B & "+" & C & ")+" & D & "=24"
           Num = True
           lresult.Caption = ResultExpression
           Exit For
          End If
         Next i
         Case 26
         For i = 1 To 24
          Call Kind(i)
          Jieguo = A * (B - C) + D
          If Jieguo = 24 Then
           ResultExpression = A & "*(" & B & "-" & C & ")+" & D & "=24"
           Num = True
           lresult.Caption = ResultExpression
           Exit For
          End If
         Next i
         Case 27
         For i = 1 To 24
          Call Kind(i)
          Jieguo = A * (B + C) - D
          If Jieguo = 24 Then
           ResultExpression = A & "*(" & B & "+" & C & ")-" & D & "=24"
           Num = True
           lresult.Caption = ResultExpression
           Exit For
          End If
         Next i
      

  10.   

    接上:Case 28
         For i = 1 To 24
          Call Kind(i)
          Jieguo = A * (B - C) - D
          If Jieguo = 24 Then
           ResultExpression = A & "*(" & B & "-" & C & ")-" & D & "=24"
           Num = True
           lresult.Caption = ResultExpression
           Exit For
          End If
         Next i
         Case 29
         For i = 1 To 24
          Call Kind(i)
          Jieguo = A + (B + C) / D
          If Jieguo = 24 Then
           ResultExpression = A & "+(" & B & "+" & C & ")/" & D & "=24"
           Num = True
           lresult.Caption = ResultExpression
           Exit For
          End If
         Next i
         Case 30
         ResultExpression = "无解"
         
         Num = True
           lresult.Caption = ResultExpression
           End Select
           
       If Num = True Then Exit For
    Next jEnd Sub
    Private Sub cmdExit_Click()
    End
    End Sub
    Private Sub cmdMake_Click()
    Randomize
    NumberA = Int(Rnd * 10) + 1
    NumberB = Int(Rnd * 10) + 1
    NumberC = Int(Rnd * 10) + 1
    NumberD = Int(Rnd * 10) + 1
    lNumberA.Caption = NumberA
    lNumberB.Caption = NumberB
    lNumberC.Caption = NumberC
    lNumberD.Caption = NumberD
    lresult.Caption = "请按求解键!"
    cmdDo.Enabled = True
    Num = FalseEnd SubPrivate Sub Form_Load()
    cmdDo.Enabled = False
    t.Enabled = False
    lresult.Caption = "请按出题键!"
    Num = FalseEnd Sub
      

  11.   

    谢谢楼上的代码,不过我还是不明白,楼上的遍历是否包括了所有可能,比如如下算法,包括了吗"
    K=(A*(B-C))/D
      

  12.   

    看过,好像没有,没有的再加上去不行吗?Case 31
         For i = 1 To 24
          Call Kind(i)
          Jieguo = A * (B - C) / D
          If Jieguo = 24 Then
           ResultExpression = A & "*(" & B & "-" & C & ")/" & D & "=24"
           Num = True
           lresult.Caption = ResultExpression
           Exit For
          End If
         Next i