VB简易计算器程序《用VB实现简易计算器》 一.内容要求:1. 加、减、乘、除,清零等一定要有;2. 乘方、开方、小数点

1个回答

  • Dim shishu As Double

    Dim yi As Double

    Dim er As Double

    Dim fuhao As String

    ‘按件数组

    Private Sub Command1_Click(Index As Integer)

    din = 0

    ‘数字设置:

    Select Case Index

    Case 1 To 9

    Text1.Text = Val(Text1.Text & Index)

    ‘ 零的设置:

    Case 0

    If Val(Text1.Text) = 0 Then

    Text1.Text = Index

    Else

    Text1.Text = Val(Text1.Text & "0")

    End If

    点号设置:

    Case 14

    For i = 1 To Len(Text1.Text)

    If Mid(Text1.Text, i, 1) = "." Then din = 1

    Next i

    If din = 1 Then

    Else

    Text1.Text = Text1.Text & "."

    End If

    符号记录:

    Case 10

    fuhao = "+"

    yi = Val(Text1.Text)

    Text1.Text = ""

    Command1(10).Enabled = False

    Command1(11).Enabled = False

    Command1(12).Enabled = False

    Command1(13).Enabled = False

    Case 11

    fuhao = "-"

    yi = Val(Text1.Text)

    Text1.Text = ""

    Command1(10).Enabled = False

    Command1(11).Enabled = False

    Command1(12).Enabled = False

    Command1(13).Enabled = False

    Case 12

    fuhao = "*"

    yi = Val(Text1.Text)

    Text1.Text = ""

    Command1(10).Enabled = False

    Command1(11).Enabled = False

    Command1(12).Enabled = False

    Command1(13).Enabled = False

    Case 13

    fuhao = "/"

    yi = Val(Text1.Text)

    Text1.Text = ""

    Command1(10).Enabled = False

    Command1(11).Enabled = False

    Command1(12).Enabled = False

    Command1(13).Enabled = False

    End Select

    End Sub

    ‘进制函数:

    Private Function panda(ByVal sel As Single) As String

    Dim a As String

    Dim ms As String

    Do While sel 0

    a = Str(sel Mod 2)

    sel = sel 2

    ms = a & ms

    Loop

    panda = ms

    End Function

    等号设置:

    Private Sub Command2_Click()

    er = Val(Text1.Text)

    If fuhao = "+" Then

    Text1.Text = yi + er

    ElseIf fuhao = "-" Then

    Text1.Text = yi - er

    ElseIf fuhao = "*" Then

    Text1.Text = yi * er

    Else

    If fuhao = "/" And er 0 Then

    Text1.Text = Format(yi / er, "0.00")

    Else

    MsgBox "除数不能为零!", , "INFORMATION"

    End If

    End If

    Command1(10).Enabled = True

    Command1(11).Enabled = True

    Command1(12).Enabled = True

    Command1(13).Enabled = True

    End Sub

    清零设置:

    Private Sub Command3_Click()

    Text1.Text = "0"

    fuhao = ""

    yi = 0

    er = 0

    shishu = 0

    End Sub

    正负号设置:

    Private Sub Command4_Click()

    Text1.Text = Val(0 - Text1.Text)

    End Sub

    Private Sub Form_Load()

    Text1.Text = 0

    End Sub

    进制设置:

    Private Sub Option1_Click()

    shishu = Val(Text1.Text)

    Text1.Text = Hex(shishu)

    For i = 1 To 14

    Command1(i).Enabled = False

    Next i

    Command2.Enabled = False

    Command3.Enabled = False

    Command4.Enabled = False

    End Sub

    Private Sub Option2_Click()

    Text1.Text = shishu

    For i = 1 To 14

    Command1(i).Enabled = True

    Next i

    Command2.Enabled = True

    Command3.Enabled = True

    Command4.Enabled = True

    End Sub

    Private Sub Option3_Click()

    shishu = Val(Text1.Text)

    Text1.Text = panda(shishu)

    For i = 1 To 14

    Command1(i).Enabled = False

    Next i

    Command2.Enabled = False

    Command3.Enabled = False

    Command4.Enabled = False

    End Sub 0