VB计算器代码:
Dim start As Boolean
Dim s1#, s2#, s3#
Dim sn As String
Private Sub Command1_Click()
If start = True Then
Text1.Text = "1"
start = False
Else
Text1.Text = Text1.Text & "1"
End If
End Sub
Private Sub Command10_Click()
Text1.Clear()
start = False
End Sub
Private Sub Command11_Click()
s1 = Val(Text1.Text)
sn = "+"
start = True
End Sub
Private Sub Command12_Click()
s1 = Val(Text1.Text)
sn = "-"
start = True
End Sub
Private Sub Command13_Click()
s1 = Val(Text1.Text)
sn = "*"
start = True
End Sub
Private Sub Command14_Click()
s1 = Val(Text1.Text)
sn = "/"
start = True
End Sub
Private Sub Command15_Click()
s2 = Val(Text1.Text)
If sn = "*" Then
s3 = s1 * s2
ElseIf sn = "/" Then
s3 = s1 / s2
start = True
ElseIf sn = "+" Then
s3 = s1 + s2
start = True
ElseIf sn = "-" Then
s3 = s1 - s2
start = True
End If
start = True
Text1.Text = s3
If s3 <> 0 Or sn <> "" Then
Text1.Text = Format(s3, "0.##")
End If
End Sub
Private Sub Command16_Click()
Text1.Text = 0
start = True
End Sub
Private Sub Command17_Click()
Text1.Text = Text1.Text & "."
start = False
End Sub
Private Sub Command2_Click()
If start = True Then
Text1.Text = "2"
start = False
Else
Text1.Text = Text1.Text & "2"
End If
End Sub
Private Sub Command3_Click()
If start = True Then
Text1.Text = "3"
start = False
Else
Text1.Text = Text1.Text & "3"
End If
End Sub
Private Sub Command4_Click()
If start = True Then
Text1.Text = "4"
start = False
Else
Text1.Text = Text1.Text & "4"
End If
End Sub
Private Sub Command5_Click()
If start = True Then
Text1.Text = "5"
start = False
Else
Text1.Text = Text1.Text & "5"
End If
End Sub
Private Sub Command6_Click()
If start = True Then
Text1.Text = "6"
start = False
Else
Text1.Text = Text1.Text & "6"
End If
End Sub
Private Sub Command7_Click()
If start = True Then
Text1.Text = "7"
start = False
Else
Text1.Text = Text1.Text & "7"
End If
End Sub
Private Sub Command8_Click()
If start = True Then
Text1.Text = "8"
start = False
Else
Text1.Text = Text1.Text & "8"
End If
End Sub
Private Sub Command9_Click()
If start = True Then
Text1.Text = "9"
start = False
Else
Text1.Text = Text1.Text & "9"
End If
End Sub
Private Sub Form_Load()
End Sub
' 版权所有 (C) 1994 Microsoft Corporation
'
' 您可以免费以任何方式使用、修改、复制并分发您认为有用的
' 示例应用程序文件 (或任何修改过的版本)。Microsoft 对任何
' 示例应用程序文件不做任何保证,不负任何责任和义务。
' ------------------------------------------------------------------------
Option Explicit
Dim Op1, Op2 ' 预先输入操作数。
Dim DecimalFlag As Integer ' 小数点存在吗?
Dim NumOps As Integer ' 操作数个数。
Dim LastInput ' 指示上一次按键事件的类型。
Dim OpFlag ' 指示未完成的操作。
Dim TempReadout
' C (取消) 按钮的 Click 事件过程
' 重新设置显示并初始化变量。
Private Sub Cancel_Click()
Readout = Format(0, "0.")
Op1 = 0
Op2 = 0
Form_Load
End Sub
' CE (取消输入) 按钮的 Click 事件过程。
Private Sub CancelEntry_Click()
Readout = Format(0, "0.")
DecimalFlag = False
LastInput = "CE"
End Sub
' 小数点 (.) 按钮的 Click 事件过程
' 如果上一次按键为运算符,初始化 readout 为 "0.";
' 否则显示时追加一个小数点。
Private Sub Decimal_Click()
If LastInput = "NEG" Then
Readout = Format(0, "-0.")
ElseIf LastInput <> "NUMS" Then
Readout = Format(0, "0.")
End If
DecimalFlag = True
LastInput = "NUMS"
End Sub
' 窗体的初始化过程
' 设置所有变量为其初始值。
Private Sub Form_Load()
DecimalFlag = False
NumOps = 0
LastInput = "NONE"
OpFlag = " "
Readout = Format(0, "0.")
'Decimal.Caption = Format(0, ".")
End Sub
' 数字键 (0-9) 的 Click 事件过程
' 向显示中的数追加新数。
Private Sub Number_Click(Index As Integer)
If LastInput <> "NUMS" Then
Readout = Format(0, ".")
DecimalFlag = False
End If
If DecimalFlag Then
Readout = Readout + Number(Index).Caption
Else
Readout = Left(Readout, InStr(Readout, Format(0, ".")) - 1) + Number(Index).Caption + Format(0, ".")
End If
If LastInput = "NEG" Then Readout = "-" & Readout
LastInput = "NUMS"
End Sub
' 运算符 (+, -, x, /, =) 的 Click 事件过程
' 如果接下来的按键是数字键,增加 NumOps。
' 如果有一个操作数,则设置 Op1。
' 如果有两个操作数,则将 Op1 设置为 Op1 与
' 当前输入字符串的运算结果,并显示结果。
Private Sub Operator_Click(Index As Integer)
TempReadout = Readout
If LastInput = "NUMS" Then
NumOps = NumOps + 1
End If
Select Case NumOps
Case 0
If Operator(Index).Caption = "-" And LastInput <> "NEG" Then
Readout = "-" & Readout
LastInput = "NEG"
End If
Case 1
Op1 = Readout
If Operator(Index).Caption = "-" And LastInput <> "NUMS" And OpFlag <> "=" Then
Readout = "-"
LastInput = "NEG"
End If
Case 2
Op2 = TempReadout
Select Case OpFlag
Case "+"
Op1 = CDbl(Op1) + CDbl(Op2)
Case "-"
Op1 = CDbl(Op1) - CDbl(Op2)
Case "X"
Op1 = CDbl(Op1) * CDbl(Op2)
Case "/"
If Op2 = 0 Then
MsgBox "除数不能为零", 48, "计算器"
Else
Op1 = CDbl(Op1) / CDbl(Op2)
End If
Case "="
Op1 = CDbl(Op2)
Case "%"
Op1 = CDbl(Op1) * CDbl(Op2)
End Select
Readout = Op1
NumOps = 1
End Select
If LastInput <> "NEG" Then
LastInput = "OPS"
OpFlag = Operator(Index).Caption
End If
End Sub
' 百分比键 (%) 的 Click 事件过程
' 计算并显示第一个操作数的百分数。
Private Sub Percent_Click()
Readout = Readout / 100
LastInput = "Ops"
OpFlag = "%"
NumOps = NumOps + 1
DecimalFlag = True
End Sub