我自己写的(VB6.0),可以运行
现在还有一个小问题,就是当其他按钮拥有焦点时,按Enter不起等号的作用
Option Explicit
Dim ClearResult As Boolean '状态
Dim Operand As Double '操作数
Dim Operator As String '运算符
Private Sub CmdClear_Click() '清除结果
ClearResult = False
Operand = 0
txtResult.Text = "0"
CmdEqual.SetFocus
End Sub
Private Sub CmdDot_Click() '小数点
If ClearResult = True Then
txtResult.Text = "0"
ClearResult = False
End If
If txtResult.Text = "除数不能为零。" Then
Exit Sub
End If
If InStr(1, txtResult.Text, ".") Then
Exit Sub
Else
txtResult.Text = txtResult.Text + "."
End If
CmdEqual.SetFocus
End Sub
Private Sub CmdEqual_Click() '等号
On Error GoTo ErrHand:
If ClearResult = False Then
Select Case Operator
Case "+"
txtResult = Operand + Val(txtResult)
Case "-"
txtResult = Operand - Val(txtResult)
Case "*"
txtResult = Operand * Val(txtResult)
Case "/"
If txtResult <> "0" Then
txtResult = Operand / Val(txtResult)
Else
txtResult.Text = "除数不能为零。"
ClearResult = True
Operand = 0
End If
End Select
End If
ClearResult = True
Operand = 0
Operator = ""
CmdEqual.SetFocus
Exit Sub
ErrHand:
MsgBox "操作发生错误:" & vbCrLf & Err.Description, vbOKOnly + vbCritical, "计算器出错"
ClearResult = True
Operand = 0
Operator = ""
CmdEqual.SetFocus
End Sub
Private Sub CmdNum_Click(Index As Integer) '数字按钮
If ClearResult = True Then
txtResult.Text = ""
ClearResult = False
End If
If txtResult.Text = "0" Then
txtResult.Text = CmdNum(Index).Caption
Else
txtResult.Text = txtResult.Text + CmdNum(Index).Caption
End If
CmdEqual.SetFocus
End Sub
Private Sub CmdOperator_Click(Index As Integer) '运算符按钮
If ClearResult = False And Operator <> "" Then
Select Case Operator
Case "+"
txtResult = Operand + Val(txtResult)
Case "-"
txtResult = Operand - Val(txtResult)
Case "*"
txtResult = Operand * Val(txtResult)
Case "/"
If txtResult <> "0" Then
txtResult = Operand / Val(txtResult)
Else
txtResult.Text = "除数不能为零。"
ClearResult = True
Operand = 0
End If
End Select
End If
Operand = Val(txtResult.Text)
Operator = CmdOperator(Index).Caption
ClearResult = True
CmdEqual.SetFocus
End Sub
Private Sub CmdRecip_Click() '求倒数
If ClearResult = False Then
Select Case Operator
Case "+"
txtResult = Operand + Val(txtResult)
Case "-"
txtResult = Operand - Val(txtResult)
Case "*"
txtResult = Operand * Val(txtResult)
Case "/"
If txtResult <> "0" Then
txtResult = Operand / Val(txtResult)
Else
txtResult.Text = "除数不能为零。"
ClearResult = True
Operand = 0
End If
End Select
End If
If Val(txtResult.Text) <> 0 Then
txtResult = 1 / Val(txtResult.Text)
Else
txtResult.Text = "除数不能为零。"
End If
ClearResult = True
CmdEqual.SetFocus
End Sub
Private Sub CmdSign_Click() '正负号
If txtResult.Text = "除数不能为零。" Then
Exit Sub
End If
If ClearResult = False Or Operator = "" Then
txtResult.Text = -txtResult.Text
Else
txtResult.Text = "0"
End If
CmdEqual.SetFocus
End Sub
Private Sub CmdSqrt_Click() '求平方根
If txtResult.Text = "除数不能为零。" Then
Exit Sub
End If
txtResult = Math.Sqr(Val(txtResult.Text))
ClearResult = True
CmdEqual.SetFocus
End Sub
'键盘按键,包括数字,运算符,小数点,等号
Private Sub Form_KeyPress(KeyAscii As Integer)
Select Case KeyAscii
Case 42
CmdOperator_Click (0)
Case 43
CmdOperator_Click (2)
Case 45
CmdOperator_Click (3)
Case 47
CmdOperator_Click (1)
Case 46
CmdDot_Click
Case 48
CmdNum_Click (0)
Case 49
CmdNum_Click (1)
Case 50
CmdNum_Click (2)
Case 51
CmdNum_Click (3)
Case 52
CmdNum_Click (4)
Case 53
CmdNum_Click (5)
Case 54
CmdNum_Click (6)
Case 55
CmdNum_Click (7)
Case 56
CmdNum_Click (8)
Case 57
CmdNum_Click (9)
Case 13
CmdEqual_Click
End Select
End Sub
Private Sub Form_Load() '程序启动时加载窗体
If App.PrevInstance Then '只允许运行一个实例
MsgBox "已经有一个本程序的实例在运行!", vbOKOnly + vbInformation, "提示"
Unload Me
Exit Sub
End If
ClearResult = True
End Sub
Dim a As Double
Dim b As Integer
Private Sub Command1_Click()
'清除按钮代码
Text1.Text = ""
End Sub
Private Sub Command10_Click()
'1按钮代码
Text1.Text = Text1.Text + "1"
End Sub
Private Sub Command11_Click()
Rem 0按钮代码
Text1.Text = Text1.Text + "0"
End Sub
Private Sub Command12_Click()
Rem .按钮代码
Text1.Text = Text1.Text + "."
End Sub
Private Sub Command13_Click()
'+按钮代码
a = Val(Text1.Text)
Text1.Text = ""
b = 1
End Sub
Private Sub Command14_Click()
'-按钮代码
a = Val(Text1.Text)
Text1.Text = ""
b = 2
End Sub
Private Sub Command15_Click()
'*按钮代码
a = Val(Text1.Text)
Text1.Text = ""
b = 3
End Sub
Private Sub Command16_Click()
'/按钮代码
a = Val(Text1.Text)
Text1.Text = ""
b = 4
End Sub
Private Sub Command17_Click()
'=按钮代码
Rem 判断操作符号,并给出相应的计算公式
Select Case b
Case 1
Text1.Text = Str(a + Val(Text1.Text))
Case 2
Text1.Text = Str(a - Val(Text1.Text))
Case 3
Text1.Text = Str(a * Val(Text1.Text))
Case 4
'错误处理程序
If Val(Text1.Text) = 0 Then
Text1.Text = "注意除数不能为0!"
Else
Text1.Text = Str(a / Val(Text1.Text))
End If
Case Else
Text1.Text = ""
End Select
End Sub
Private Sub Command2_Click()
'9按钮代码
Text1.Text = Text1.Text + "9"
End Sub
Private Sub Command3_Click()
'8按钮代码
Text1.Text = Text1.Text + "8"
End Sub
Private Sub Command4_Click()
'7按钮代码
Text1.Text = Text1.Text + "7"
End Sub
Private Sub Command5_Click()
'6按钮代码
Text1.Text = Text1.Text + "6"
End Sub
Private Sub Command6_Click()
'5按钮代码
Text1.Text = Text1.Text + "5"
End Sub
Private Sub Command7_Click()
'4按钮代码
Text1.Text = Text1.Text + "4"
End Sub
Private Sub Command8_Click()
'3按钮代码
Text1.Text = Text1.Text + "3"
End Sub
Private Sub Command9_Click()
'2按钮代码
Text1.Text = Text1.Text + "2"
End Sub