以前自己写的一个,可能参考一下。
Option Explicit
Dim strtemp As String
Dim i, j, k As Integer
Dim blnNM As Boolean
Dim blnEnd As Boolean
Dim KeyASCII As Integer
Option Base 1
Dim strNum(100) As String
Dim intNz As Integer
Dim intOpt(100)
As Integer
Dim intOptt As Integer
Dim intM(100), intMM As Integer
Dim intMz As Integer
Private Sub cmdBack_Click()
If Not blnNM And Len(strtemp) > 0 Then
strtemp = Left(strtemp, Len(strtemp) - 1)
TxtDisplay.Text = Left(TxtDisplay.Text, Len(TxtDisplay.Text) - 1)
End If
If blnNM Then
intMz = intMz - 1
TxtDisplay.Text = Left(TxtDisplay.Text, Len(TxtDisplay.Text) - 1)
End If
End Sub
Private Sub cmdCalc_Click(Index As Integer)
intOptt = Index
Select Case Index
Case 0
intMM = 1
TxtDisplay.Text = TxtDisplay.Text & "+"
Case 1
intMM = 1
TxtDisplay.Text = TxtDisplay.Text & "-"
Case 2
intMM = 2
TxtDisplay.Text = TxtDisplay.Text & "*"
Case 3
intMM = 2
TxtDisplay.Text = TxtDisplay.Text & "/"
Case 4
intMM = 3
TxtDisplay.Text = TxtDisplay.Text & "("
Case 5
intMM = 0
TxtDisplay.Text = TxtDisplay.Text & ")"
End Select
If blnEnd Then blnEnd = False
Call PushM
If Not blnNM And Not blnEnd And Len(strtemp) <> 0 Then
Call PushN
'入栈
blnNM = True
End If
If intNz > 1 Then Call operationA
End Sub
Private Sub cmdClear_Click()
TxtDisplay.Text = ""
intNz = 0
intMz = 0
strtemp = ""
blnEnd = False
blnNM = False
End Sub
Private Sub cmdEnter_Click()
blnEnd = True
If Not blnNM Then Call PushN
Do While intMz > 0 And intNz > 1
Call operationA
Loop
intMz = 0
TxtDisplay.Text = TxtDisplay.Text & "=" & strNum(intNz)
End Sub
Private Sub cmdNumber_Click(Index As Integer)
blnEnd = False
If blnNM Then
blnNM = False
End If
If Index = 11 And Len(strtemp) = 0 Then
strtemp = "-"
Else
If Index = 11 And Len(strtemp) >= 1 Then
If Index = 11 And Len(strtemp) > 1 Then
TxtDisplay.Text = Left(TxtDisplay.Text, Len(TxtDisplay.Text) - Len(strtemp))
strtemp = "-"
Else
If Index = 11 And Len(strtemp) = 1 And strtemp = "-" Then
TxtDisplay.Text = Left(TxtDisplay.Text, Len(TxtDisplay.Text) - Len(strtemp))
strtemp = ""
Else
TxtDisplay.Text = Left(TxtDisplay.Text, Len(TxtDisplay.Text) - Len(strtemp))
strtemp = "-"
End If
End If
End If
End If
If Index < 10 Then
strtemp = strtemp + CStr(Index)
Else
If InStr(strtemp, ".") = 0 And Index = 10 Then strtemp = strtemp + "."
End If
If Len(strtemp) > 1 And Left(strtemp, 1) = "0" And Mid(strtemp, 2, 1) <> "." Then
strtemp = Right(strtemp, Len(strtemp) - 1)
End If
TxtDisplay.Text = TxtDisplay.Text & Right(strtemp, 1)
End Sub
Public Sub operationA()
If intMz > 1 Then
If intM(intMz) = 0 And intM(intMz - 1) = 3 Then intMz = intMz - 2
End If
If intNz > 1 And intMz > 1 Then
If intM(intMz - 1) >= intM(intMz) And intM(intMz - 1) <> 3 And intM(intMz - 1) <> 0 And Not blnEnd Then Call operationB
If blnEnd And intM(intMz) <> 0 Then Call operationC
If blnEnd And intM(intMz) = 0 Then Call operationCC
End If
If intNz > 1 And intMz > 0 And blnEnd And intM(intMz) <> 0 Then Call operationC
If intMz > 1 Then
If intM(intMz) = 0 And intM(intMz - 1) = 3 Then intMz = intMz - 2
End If
End Sub
Public Sub operationCC()
Select Case intOpt(intMz - 1)
Case 0
strNum(intNz - 1) = CStr(Val(strNum(intNz - 1)) + Val(strNum(intNz)))
Case 1
strNum(intNz - 1) = CStr(Val(strNum(intNz - 1)) - Val(strNum(intNz)))
Case 2
strNum(intNz - 1) = CStr(Val(strNum(intNz - 1)) * Val(strNum(intNz)))
Case 3
If Val(strNum(intNz)) <> 0 Then
strNum(intNz - 1) = CStr(Val(strNum(intNz - 1)) / Val(strNum(intNz)))
Else
If Val(strNum(intNz)) = 0 Then MsgBox "除数不能为0!", 16, "计算器"
End If
End Select
Call PopN
Call PopM
End Sub
Public Sub operationC()
Select Case intOpt(intMz)
Case 0
strNum(intNz - 1) = CStr(Val(strNum(intNz - 1)) + Val(strNum(intNz)))
Case 1
strNum(intNz - 1) = CStr(Val(strNum(intNz - 1)) - Val(strNum(intNz)))
Case 2
strNum(intNz - 1) = CStr(Val(strNum(intNz - 1)) * Val(strNum(intNz)))
Case 3
If Val(strNum(intNz)) <> 0 Then
strNum(intNz - 1) = CStr(Val(strNum(intNz - 1)) / Val(strNum(intNz)))
Else
If Val(strNum(intNz)) = 0 Then MsgBox "除数不能为0!", 16, "计算器"
End If
End Select
Call PopN
Call PopMend
End Sub
Public Sub operationB()
Select Case intOpt(intMz - 1)
Case 0
strNum(intNz - 1) = CStr(Val(strNum(intNz - 1)) + Val(strNum(intNz)))
Case 1
strNum(intNz - 1) = CStr(Val(strNum(intNz - 1)) - Val(strNum(intNz)))
Case 2
strNum(intNz - 1) = CStr(Val(strNum(intNz - 1)) * Val(strNum(intNz)))
Case 3
If Val(strNum(intNz)) <> 0 Then
strNum(intNz - 1) = CStr(Val(strNum(intNz - 1)) / Val(strNum(intNz)))
Else
If Val(strNum(intNz)) = 0 Then MsgBox "除数不能为0!", 16, "计算器"
End If
End Select
intOpt(intMz - 1) = intOpt(intMz)
intM(intMz - 1) = intM(intMz)
Call PopN
Call PopM
End Sub
Public Sub InitStack()
intNz = 0
intMz = 0
End Sub
Public Sub PushN()
If intNz < 100 Then
intNz = intNz + 1
strNum(intNz) = strtemp
Else
TxtDisplay.Text = "栈满"
End If
strtemp = ""
End Sub
Public Sub PushM()
If intMz < 100 Then
intMz = intMz + 1
intOpt(intMz) = intOptt
intM(intMz) = intMM
Else
TxtDisplay.Text = "栈满"
End If
End Sub
Public Sub PopN()
If intNz > 0 Then
intNz = intNz - 1
Else
TxtDisplay.Text = "栈空"
End If
End Sub
Public Sub PopM()
If intMz > 1 Then
intOpt(intMz - 1) = intOpt(intMz)
intM(intMz - 1) = intM(intMz)
intMz = intMz - 1
End If
End Sub
Public Sub PopMend()
If intMz > 0 Then
intMz = intMz - 1
End If
End Sub
Private Sub form_Keypress(KeyASCII As Integer)
If KeyASCII < 58 And KeyASCII > 47 Then cmdNumber_Click (Chr$(KeyASCII))
If KeyASCII = 46 Then cmdNumber_Click (10)
If KeyASCII = 95 Then cmdNumber_Click (11)
If KeyASCII = 61 Then cmdCalc_Click (0)
If KeyASCII = 45 Then cmdCalc_Click (1)
If KeyASCII = 42 Then cmdCalc_Click (2)
If KeyASCII = 47 Then cmdCalc_Click (3)
If KeyASCII = 40 Then cmdCalc_Click (4)
If KeyASCII = 41 Then cmdCalc_Click (5)
If KeyASCII = 8 Then cmdBack_Click
If KeyASCII = 0 Then cmdEnter_Click
End Sub
Private Sub Form_Load()
TxtDisplay.Text = ""
intNz = 0
intMz = 0
End Sub
Private Sub MenuAbout_Click()
formAbout.Show
End Sub
Private Sub MenuHelp_Click()
Formhelp.Show
End Sub
Private Sub calculator_Keypress(KeyASCII As Integer)
If KeyASCII = 13 Then
KeyASCII = 0
Call cmdEnter_Click
End If
End Sub