Dim s1 As Single, s2 As Single, ysf As String
‘定义两个单精度数变量用与存放参与运算的数,一个字符型存放运算符
Private Sub Command1_Click(Index As Integer)
If Text1.Text = “0” Then
Text1.Text = Command1(Index).Caption
Else
Text1.Text = Text1.Text & Command1(Index).Caption
'将command1的单击事件与文本框显示的内容连接
End If
End Sub
Private Sub Command2_Click()
Text1.Text = Text1.Text + “.”
If (InStr(Text1.Text, “.”) = 1) Then '第一位不能为小数
Text1.Text = “”
End If
If InStr(Text1.Text, “.”) < Len(Text1.Text) Then '防止出现两个小数点
Text1.Text = Left(Text1.Text, Len(Text1.Text) - 1)
End If
End Sub
Private Sub Command3_Click(Index As Integer)
s2 = Val(Text1.Text) '开始加减乘除运算
Select Case ysf
Case “+”
Text1.Text = s1 + s2
Case “-”
Text1.Text = s1 - s2
Case “*”
Text1.Text = s1 * s2
Case “/”
If s2 = 0 Then
MsgBox “分母不能为零!”
Text1.Text = " "
Else
Text1.Text = s1 / s2
End If
End Select
Text1 = IIf(Left(Text1.Text, 1) = “.”, 0 & Text1.Text, Text1.Text)
'这个很关键,如果没有这个的话,得出小于1的小数前面没有0
End Sub
Private Sub Command4_Click(Index As Integer)
If Text1.Text = " " Then '文本为空就结束
Text1.Text = “0”
ElseIf Len(Text1.Text) = “1” Then
Text1.Text = “0”
Else
Text1.Text = Left(Text1.Text, Len(Text1.Text) - 1) '文本退一格
End If
End Sub
Private Sub Command5_Click(Index As Integer)
Text1.Text = " "
End Sub
Private Sub Command6_Click(Index As Integer)
s1 = Val(Text1.Text) '将s1隐藏起来
ysf = Command6(Index).Caption
Text1.Text = “”
End Sub
Private Sub Command7_Click()
If Left(Text1.Text, 1) <> “-” Then '判断作为负数
Text1.Text = “-” & Text1.Text
Else
Text1.Text = Right(Text1.Text, Len(Text1.Text) - 1)
End If
End Sub
Private Sub Command8_Click(Index As Integer)
Text1.Text = Text1.Text * Text1.Text
End Sub