用vb6制作数学计算器
窗体:
代码:
Dim s1 As Single, s2 As Single, ysf As String
'定义两个单精度数变量用与存放参与运算的数,一个字符型存放运算符
Private Sub Command1_Click(Index As Integer)
Text1.Text = Text1.Text & Command1(Index).Caption
'将command1的单击事件与文本框显示的内容连接
End Sub
Private Sub Command10_Click()
End
End Sub
Private Sub Command11_Click()
Text1.Text = Sqr(Text1.Text)
If Text1.Text > 0 And Text1.Text < 1 Then
Text1 = IIf(Left(Text1.Text, 1) = ".", 0 & Text1.Text, Text1.Text)
End If
If Text1.Text > -1 And Text1.Text < 0 Then
Text1 = "-0" & (Text1.Text * -1)
End If
End Sub
Private Sub Command12_Click()
Text1.Text = Text1.Text ^ (1 / 3)
If Text1.Text > 0 And Text1.Text < 1 Then
Text1 = IIf(Left(Text1.Text, 1) = ".", 0 & Text1.Text, Text1.Text)
End If
If Text1.Text > -1 And Text1.Text < 0 Then
Text1 = "-0" & (Text1.Text * -1)
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()
s2 = Val(Text1.Text) '开始加减乘除运算
Select Case ysf
Case "+"
Text1.Text = s1 + s2
Case "-"
Text1.Text = s1 - s2
Case "×"
Text1.Text = s1 * s2
Case "÷"
Text1.Text = s1 / s2
If s2 = 0 Then
MsgBox "分母不能为0!"
End If
End Select
If Text1.Text > 0 And Text1.Text < 1 Then
Text1 = IIf(Left(Text1.Text, 1) = ".", 0 & Text1.Text, Text1.Text)
End If
If Text1.Text > -1 And Text1.Text < 0 Then
Text1 = "-0" & (Text1.Text * -1)
End If
End Sub
Private Sub Command4_Click()
If Text1.Text = "" Then
Exit Sub
End If
Text1.Text = Left(Text1.Text, Len(Text1.Text) - 1)
End Sub
Private Sub Command5_Click()
Text1.Text = ""
End Sub
Private Sub Command6_Click(Index As Integer)
s1 = Val(Text1.Text)
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()
Text1.Text = Text1.Text * Text1.Text
If Text1.Text > 0 And Text1.Text < 1 Then
Text1 = IIf(Left(Text1.Text, 1) = ".", 0 & Text1.Text, Text1.Text)
End If
If Text1.Text > -1 And Text1.Text < 0 Then
Text1 = "-0" & (Text1.Text * -1)
End If
End Sub
Private Sub Command9_Click()
Text1.Text = Text1.Text * Text1.Text * Text1.Text
If Text1.Text > 0 And Text1.Text < 1 Then
Text1 = IIf(Left(Text1.Text, 1) = ".", 0 & Text1.Text, Text1.Text)
End If
If Text1.Text > -1 And Text1.Text < 0 Then
Text1 = "-0" & (Text1.Text * -1)
End If
End Sub