Dim num As String, num1 As String '存放两个操作数
Dim Op As String '存放运算符
Private Sub Command1_Click() '退格删除运算符最右边一个数
If num <> "" Then num = Left(num, Len(num) - 1)
Disp.Caption = num
End Sub
Private Sub Command10_Click()
num = num + CmdNum(Index).Caption
Disp.Caption = num
End Sub
Private Sub Command11_Click()
num = num + CmdNum(Index).Caption
Disp.Caption = num
End Sub
Private Sub Command13_Click()
num = num + CmdNum(Index).Caption
Disp.Caption = num
End Sub
Private Sub Command14_Click()
num = num + CmdNum(Index).Caption
Disp.Caption = num
End Sub
Private Sub Command15_Click()
num = num + CmdNum(Index).Caption
Disp.Caption = num
End Sub
Private Sub Command17_Click()
num = num + CmdNum(Index).Caption
Disp.Caption = num
End Sub
Private Sub Command18_Click() '正负数取反
If Left(num, 1) <> "-" Then
num = "-" & num
Else
num = Mid(num, 2)
End If
Disp.Caption = num
End Sub
Private Sub Command19_Click() '输入小数点
If InStr(num, ".") <> 0 Then '用instr函数查找num中是否有逗号
Exit Sub '已经有小数点退出该时间过程
Else
num = num + "." '没有小数点把小数点加入到字符串中
End If
Disp.Caption = num
End Sub
Private Sub Command2_Click() '删除运算符
num = ""
Disp.Caption = num
End Sub
Private Sub Command20_Click() '计算出运算结果
If Disp.Caption <> "" Then '如果disp标签为空则输出空
Select Case Op '选择运算符如果运算符为空则不运算并将disp标签输出空
Case "+"
num = CStr(Val(num1) + Val(num))
Case "-"
num = CStr(Val(num1) - Val(num))
Case "×"
num = CStr(Val(num1) * Val(num))
Case "÷"
If Val(num) = 0 Then
MsgBox ("除数不能为零,请重新输入")
num = ""
Else
num = CStr(Val(num1) / Val(num))
End If
Case ""
num = ""
End Select
Else
num = ""
End If
Disp.Caption = num
End Sub
Private Sub Command3_Click() '删除所有操作符恢复初始状态
num = ""
num1 = ""
Disp.Caption = num
End Sub
Private Sub Command5_Click()
num = num + CmdNum(Index).Caption
Disp.Caption = num
End Sub
Private Sub Command6_Click()
num = num + CmdNum(Index).Caption
Disp.Caption = num
End Sub
Private Sub Command7_Click()
num = num + CmdNum(Index).Caption
Disp.Caption = num
End Sub
Private Sub Command9_Click()
num = num + CmdNum(Index).Caption
Disp.Caption = num
End Sub
Private Sub Text1_Change() '记录第一个操作数和运算符
If Disp.Caption <> "" Then '如果DISP标签不为空则表示已经输入一个数据
num1 = num '将第一个操作数存入NUM1
Disp.Caption = "" '清空disp,num,准备输入第二个操作数num
num = ""
End If
Op = CmdOp(Index).Caption '将选择的加减承储运算符,保存在模块变量OP中
End Sub