用VB程序设计一个多功能计算器
发布网友
发布时间:2022-04-20 08:09
我来回答
共1个回答
热心网友
时间:2023-10-18 11:05
Private Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" (ByVal hwnd As Long, ByVal lpOperation As String, ByVal lpFile As String, ByVal lpParameters As String, ByVal lpDirectory As String, ByVal nShowCmd As Long) As Long
Private Const SW_SHOWNORMAL = 1
Dim num1 As Double
Dim num2 As Double
Dim tmpStr As String
Dim curMark As Boolean
Dim oper As String
Private Sub Command1_Click(Index As Integer)
tmpStr = Val(tmpStr & Index)
Text1.Text = Val(tmpStr)
End Sub
Private Sub Command2_Click(Index As Integer)
If curMark = False Then
num1 = Val(tmpStr)
curMark = True
Else
num2 = Val(tmpStr)
End If
tmpStr = ""
Select Case Index
Case 0
oper = "+"
Case 1
oper = "-"
Case 2
oper = "*"
Case 3
oper = "/"
Case 4
Text1.Text = Calcu(oper)
End Select
End Sub
Private Sub Command3_Click()
If tmpStr <> "" Then
tmpStr = Left(tmpStr, Len(tmpStr) - 1)
Text1.Text = Val(tmpStr)
End If
End Sub
Private Sub Command4_Click()
num1 = 0
num2 = 0
tmpStr = "0"
Text1.Text = "0"
curMark = False
End Sub
Private Sub Command6_Click()
tmpStr = -(Val(tmpStr))
Text1.Text = tmpStr
End Sub
Private Sub Command7_Click()
If tmpStr <> "" Then
If InStr(1, tmpStr, ".") = 0 Then
tmpStr = tmpStr & "."
Text1.Text = tmpStr
End If
End If
End Sub
Private Sub Form_Load()
Dim obj As CommandButton
tmpStr = "0"
Text1.Text = Val(tmpStr)
For Each obj In Command1
obj.Caption = obj.Index
Next
End Sub
Private Function Calcu(oper As String) As Double
On Error GoTo calcErr
Select Case oper
Case "+"
Calcu = num1 + num2
Case "-"
Calcu = num1 - num2
Case "*"
Calcu = num1 * num2
Case "/"
Calcu = num1 / num2
End Select
num1 = Calcu
num2 = 0
Exit Function
calcErr:
MsgBox "操作失败,无法进行此计算操作", vbOKOnly, "计算错误"
Calcu = 0
End Function
Private Sub Form_Unload(Cancel As Integer)
End
End Sub
Private Sub mAbout_Click()
frmAbout.Show vbModal
End Sub
Private Sub mExit_Click()
Unload Me
End Sub
Private Sub mHelpFor_Click()
ShellExecute Me.hwnd, "open", App.HelpFile, "", "", SW_SHOWNORMAL
End Sub
Private Sub mInput_Click()
Dim inCalcu As String
inCalcu = InputBox("请输入运算表达式", "表达式", "1+1")
Text1.Text = calc(inCalcu)
num1 = Val(Text1.Text)
curMark = True
End Sub
Private Sub Toolbar1_ButtonClick(ByVal Button As MSComctlLib.Button)
Select Case Button.Key
Case "tInput"
mInput_Click
Case "tAbout"
mAbout_Click
Case "tHelp"
mHelpFor_Click
Case "tExit"
End
End Select
End Sub