VB实例代码-计算器.doc
首先,需建立如下控件:标签:Label1命令按钮:名称 Caption值Cback Cclear CECstart CCsin SinCcos CosCsqrt SqrtCsign +/-Cpoint CpointCequal =控件数组:(命令按钮)Calcu(1) +Calcu(2) -Calcu(3) *Calcu(4) /Cnum(0) 0Cnum(1) 1.Cnum(9) 9然后在代码窗口写源码:Option ExplicitDim Num1, Num2 As DoubleDim PointIn As BooleanDim Inputing As BooleanDim Operation1 As IntegerPrivate Sub Calcu_Click(i As Integer) If Inputing = False Then '在前次运算提交之后尚未输入新的数据 Operation1 = i '运算符重置 Num1 = Label1 '将显示栏里的数据赋值给第一个操作数 Exit Sub End If Inputing = False '将当前状态置为非输入数据阶段 If Num1 <> 0 Then '非首次计算 Num2 = Label1 '将显示栏里的数据赋值给第二个操作数 Calculate (Operation1) '计算前一次运算并显示结果 Else '首次计算 Num1 = Label1 '将显示栏里的数据赋值给第一个操作数 End If Operation1 = i '提交运算符End SubPrivate Sub Cback_Click() Dim TheLen As Integer TheLen = Len(Label1) If TheLen > 2 Then If Right(Label1, 1) = "." Then Label1 = Left(Label1, TheLen - 2) & "." Else Label1 = Left(Label1, TheLen - 1) End If Else Cclear_Click End IfEnd SubPrivate Sub Cclear_Click() Label1 = "0." Num2 = 0 PointIn = FalseEnd SubPrivate Sub Ccos_Click() Calculate (6) Inputing = FalseEnd SubPrivate Sub Cequal_Click() If Inputing Then '如果刚输入过数据'Or Operation1 > 0 Then Num2 = Label1 '将显示栏里的数据赋值给第二个操作数 End If Inputing = False '将当前状态置为非输入数据阶段 Calculate (Operation1) '计算提交的运算并显示结果 Num1 = 0 Operation1 = 0End SubPrivate Sub Calculate(Oprt As Integer) Select Case Oprt Case 1 Num1 = Num1 + Num2 ShowResult (Num1) Case 2 Num1 = Num1 - Num2 ShowResult (Num1) Case 3 Num1 = Num1 * Num2 ShowResult (Num1) Case 4 Num1 = Num1 / Num2 ShowResult (Num1) Case 5 Num2 = Label1 Num1 = Sin(Num2) ShowResult (Num1) Case 6 Num2 = Label1 Num1 = Cos(Num2) ShowResult (Num1) Case 7 Num2 = Label1 Num1 = Sqr(Num2) ShowResult (Num1) End SelectEnd SubPrivate Sub ShowResult(Num As Double) If Num = Fix(Num) Then '整数 Label1 = Num & "." ElseIf Left(Num, 1) = "." Then '第一个字符为小数点 Label1 = "0" & Num ElseIf Left(Num, 2) = "-." Then '前两个字符为"-." Label1 = "-0." & Right(CStr(Num), Len(CStr(Num) - 2) Else Label1 = Num End IfEnd SubPrivate Sub Cnum_Click(Index As Integer) NumInput (Index)End SubPrivate Sub Cpoint_Click() If Inputing = False Then Label1 = "0." Inputing = True End If PointIn = TrueEnd SubPrivate Sub Csign_Click() If Label1 <> "0." Then Dim StrTemp As String StrTemp = Label1 If Left(StrTemp, 1) = "-" Then Label1 = Right(StrTemp, Len(StrTemp) - 1) Else Label1 = "-" & StrTemp End If End IfEnd SubPrivate Sub Csin_Click() Calculate (5) Inputing = FalseEnd SubPrivate Sub Csqrt_Click() Dim x As Long x = Label1 If x >= 0 Then Calculate (7) Else Label1 = "Error!" End If Inputing = FalseEnd SubPrivate Sub Cstart_Click() Label1 = "0." Num1 = 0 Num2 = 0 PointIn = False Inputing = True Operation1 = 0End SubPrivate Sub Form_KeyDown(KeyCode As Integer, Shift As Integer) Cequal.SetFocus If KeyCode = 46 Then '按Del键 Cclear_Click End If End SubPrivate Sub Form_KeyPress(KeyAscii As Integer) Select Case KeyAscii '键入数字: Case 48 To 57 NumInput (KeyAscii - 48) Case 46 '小数点 Cpoint_Click '键入运算符: Case 43 '加号 Calcu_Click (1) Case 45 '减号 Calcu_Click (2) Case 42 '乘号 Calcu_Click (3) Case 47 '除号 Calcu_Click (4) Case 27 '重新开始(退出键) Cstart_Click Case 8 '退格 Cback_Click Case 13 '等于(回车键) Cequal_Click End SelectEnd SubPrivate Sub Form_Load() Me.Top = (Screen.Height - Me.Height) / 2 Me.Left = (Screen.Width - Me.Width) / 2 Me.KeyPreview = True Cstart_ClickEnd SubPrivate Sub NumInput(n As Integer) If Len(Label1) > 15 Then Exit Sub End If If Inputing = False Then Cclear_Click Label1 = n & "." Inputing = True ElseIf Label1 <> "0." Then If Right(Label1, 1) = "." Then If PointIn = False Then Dim TheLen As Integer TheLen = Len(Label1) Label1 = Left(Label1, TheLen - 1) Label1 = Label1 & n & "." Else Label1 = Label1 & n End If Else Label1 = Label1 & n End If Else If PointIn Then Label1 = Label1 & n Else Label1 = n & "." End If End IfEnd Sub