Dim num1, t, d, cat, a
Private Sub Command1_Click(Index As Integer)
If Index <= 9 Then
   If t > 9 And t <> 15 Then
   Text1.Text = Index
   Else
    If Text1.Text = "0" Then
     Text1.Text = Index
     Else
     Text1.Text = Text1.Text & Index
    End If
   End If
Else
  Select Case Index
  Case 10 '加法
  If cat = 2 Then
    num1 = num1 - Val(Text1.Text)
    Text1.Text = num1
  ElseIf cat = 3 Then
    num1 = num1 * Val(Text1.Text)
    Text1.Text = num1
  ElseIf cat = 4 Then
    num1 = num1 / Val(Text1.Text)
    Text1.Text = num1
  ElseIf d = 0 Then
    d = 1
    num1 = Text1.Text
    Text1.Text = 0
   Else
    num1 = num1 + Val(Text1.Text)
    Text1.Text = num1
   End If
   cat = 1
   p = 0
  Case 11 '减法
    If cat = 1 Then
    num1 = num1 + Val(Text1.Text)
    Text1.Text = num1
    
  ElseIf cat = 3 Then
    num1 = num1 * Val(Text1.Text)
    Text1.Text = num1
    
  ElseIf cat = 4 Then
    num1 = num1 / Val(Text1.Text)
    Text1.Text = num1
  ElseIf d = 0 Then
     d = 1
     num1 = Text1.Text
     Text1.Text = 0
    Else
     num1 = num1 - Val(Text1.Text)
     Text1.Text = num1
    End If
      cat = 2
      p = 0
  Case 12 '乘法
    If cat = 1 Then
    num1 = num1 + Val(Text1.Text)
    Text1.Text = num1
   
  ElseIf cat = 2 Then
    num1 = num1 - Val(Text1.Text)
    Text1.Text = num1
    
  ElseIf cat = 4 Then
    num1 = num1 / Val(Text1.Text)
    Text1.Text = num1
  ElseIf d = 0 Then
     d = 1
     num1 = Text1.Text
     Text1.Text = 0
    Else
     num1 = num1 * Val(Text1.Text)
     Text1.Text = num1
    End If
  cat = 3
  p = 0
  Case 13 '除法
    If cat = 1 Then
    num1 = num1 + Val(Text1.Text)
    Text1.Text = num1
  ElseIf cat = 2 Then
    num1 = num1 - Val(Text1.Text)
    Text1.Text = num1
  ElseIf cat = 3 Then
    num1 = num1 * Val(Text1.Text)
    Text1.Text = num1
  ElseIf d = 0 Then
     d = 1
     num1 = Text1.Text
     Text1.Text = 0
    Else
     num1 = num1 / Val(Text1.Text)
     Text1.Text = num1
    End If
   cat = 4
   p = 0
  Case 15 '小数点
  If p = 0 Then
  Text1.Text = Text1.Text + "."
  p = 1
  End If
  
  
  
   Case 16 '等于
   
       If cat = 1 Then
    num1 = num1 + Val(Text1.Text)
    Text1.Text = num1
   ElseIf cat = 3 Then
    num1 = num1 + Val(Text1.Text)
    Text1.Text = num1
   
   ElseIf cat = 2 Then
    num1 = num1 - Val(Text1.Text)
    Text1.Text = num1
    
   ElseIf cat = 4 Then
     num1 = num1 / Val(Text1.Text)
     Text1.Text = num1
       End If
       p = 0
  End Select
End If
t = Index
End SubPrivate Sub Command2_Click()
a = Len(Text1.Text)
If a = 1 Then
Text1.Text = 0
Else
Text1.Text = Left(Text1.Text, a - 1)
End IfEnd SubPrivate Sub Command3_Click()
Text1.Text = 0
num1 = 0
d = 0
p = 0
End SubPrivate Sub Form_Load()
d = 0
End Sub

解决方案 »

  1.   

    逆波兰表达式
    你试试 msgbox eval("123.4*5.67+89")Public Sub Push(Data As String, ByRef SP As String)
        SP = SP & Data & ","
    End SubPublic Function Pop(ByRef SP As String) As String
        Dim Arr() As String, I As Integer
        If Len(SP) = 0 Then
            Pop = ""
        Else
            Arr = Split(SP, ",")
            I = UBound(Arr) - 1
            SP = Left(SP, Len(SP) - Len(Arr(I)) - 1)
            Pop = Arr(I)
        End If
    End Function
    Public Function RPN(Str As String) As String
        Dim SP As String, A As String, B As String, C As String, R As String
        Dim PA As Integer, PB As Integer
        Dim I As Integer, D As Integer
        C = Str & "#"
        D = Len(C)
        R = "0"
        For I = 1 To D
            A = Mid(C, I, 1)
            Select Case A
                Case "+", "-", "*", "/", "(", ")", ">", "<", "=", "#"
                    R = R & IIf(Len(R) > 0, "$", "")
                    Do
                        If (Len(SP) = 0) Or (A = "(") Then
                            If A <> ")" Then SP = SP & A
                            Exit Do
                        Else
                            B = Right(SP, 1)
                            Select Case A
                                Case "*", "/"
                                    PA = 4
                                Case "+", "-"
                                    PA = 3
                                Case "<", ">", "="
                                    PA = 2
                                Case "(", ")"
                                    PA = 1
                                Case Else
                                    PA = 0
                            End Select
                            Select Case B
                                Case "*", "/"
                                    PB = 4
                                Case "+", "-"
                                    PB = 3
                                Case "<", ">", "="
                                    PB = 2
                                Case "(", ")"
                                    PB = 1
                                Case Else
                                    PB = 0
                            End Select
                            If PA > PB Then
                                SP = SP & A
                                Exit Do
                            Else
                                SP = Left(SP, Len(SP) - 1)
                                B = Replace(B, "(", "")
                                R = R & B
                            End If
                        End If
                    Loop
                Case Else
                    R = R & A
            End Select
        Next
        RPN = R
    End FunctionPublic Function Eval(Str As String) As Single
        Dim Arr() As String
        Dim X As String, SP1 As String, SP2 As String, R As String
        Dim CHA As String, CHB As String, CHC As String, CHD As String
        Dim C As Integer, D As Integer, I As Integer, J As Integer
        X = RPN("0$" & Str)
        C = Len(X)
        For I = 1 To C
            CHA = Mid(X, I, 1)
            Select Case CHA
                Case "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "."
                    CHB = CHB & CHA
                Case "$"
                    Push CHB, SP1
                    CHB = ""
                Case Else
                    CHB = Pop(SP1)
                    CHC = Pop(SP1)
                    If Len(CHC) = 0 Then CHC = "0"
                    Select Case CHA
                        Case "+"
                            CHD = CStr(CSng(CHC) + CSng(CHB))
                        Case "-"
                            CHD = CStr(CSng(CHC) - CSng(CHB))
                        Case "*"
                            CHD = CStr(CSng(CHC) * CSng(CHB))
                        Case "/"
                            CHD = CStr(CSng(CHC) / CSng(CHB))
                        Case ">"
                            CHD = IIf(CSng(CHC) > CSng(CHB), "1", "0")
                        Case "<"
                            CHD = IIf(CSng(CHC) < CSng(CHB), "1", "0")
                        Case "="
                            CHD = IIf(CSng(CHC) = CSng(CHB), "1", "0")
                    End Select
                    Push CHD, SP1
                    CHB = ""
            End Select
        Next
        Eval = CSng(Pop(SP1))
    End Function