VB计算器源代码
vb编写的计算器代码
Option ExplicitConst CC1 = 1E+28, CC2 = 0.000000000000001, CC3 = 100000000000000# Dim Op1, Op2 ' 预先输入操作数。
Dim DecFlag% ' 小数点存在吗?Dim Klast ' 指示上一次按键事件的类型。
Dim OpFlag ' 指示未完成的操作。
Dim Kedt% ' 指示键入状态,0-未操作,1-算过,2-改过Dim MemNum ' 存储器Dim Temp2'Function sqr28(a As V ariant) As V ariantDim c As Doublec = Sqr(a)If c > CC3 Or c < CC2 Thensqr28 = cElsesqr28 = CDec(Sqr(a))sqr28 = sqr28 - (sqr28 * sqr28 - a) / sqr28 * 0.5End IfEnd FunctionFunction cur28(a As V ariant) As V ariantDim t As V ariant, c As Doublec = Abs(a) ^ (1 / 3)If c > 1000000000# Or c < 0.000000001 Thencur28 = c * Sgn(a)Elsecur28 = CDec(c) * Sgn(a)t = cur28 * cur28cur28 = cur28 - (cur28 * t - a) / t / 3End IfEnd Function' 存入存储器Private Sub BtMS_Click()If Kedt = 2 Then GetOp1MemNum = Op1LabMem.Visible = MemNum <> 0Kedt = 1End Sub' 取出存储器数据Private Sub BtMr_Click()CancelEntry_ClickOp1 = MemNumDisp = Op1Kedt = 1End Sub' 清除存储器Private Sub BtMC_Click()MemNum = CDec(0)LabMem.Visible = FalseEnd SubPrivate Sub BtOff_Click()EndEnd SubPrivate Sub Form_KeyPress(KeyAscii As Integer) Dim K As StringK = Chr(KeyAscii)'Select Case KCase Chr(24): EndCase Chr(8): CancelEntry_ClickCase Chr(27): Cancel_ClickCase "0" To "9": Number_Click KeyAscii - 48 Case ".": Decimal_ClickCase "=": Equal_ClickCase "s": BtMS_ClickCase "c": BtMC_ClickCase "r": BtMr_ClickCase "%": Func_Click 4Case "^": Func_Click 1Case "+": Operator_Click 1Case "-": Operator_Click 3Case "*": Operator_Click 2Case "/": Operator_Click 0Case "i": Func_Click 2Case "'": Func_Click 3Case "]": Func_Click 5Case "\": Func_Click 0Case ";": negcmd_ClickEnd SelectEqual.SetFocusEnd Sub' 窗体的初始化过程' 设置所有变量为其初始值。
计算器vb源代码
计算器vb源代码.txt如果中了一千万,我就去买30套房子租给别人,每天都去收一次房租。
哇咔咔~~充实骑白马的不一定是王子,可能是唐僧;带翅膀的也不一定是天使,有时候是鸟人。
完全版的前后台代码...'请把下面的保存为 form1.frmVERSION 5.00Begin VB.Form CalculatorBorderStyle = 1 'Fixed SingleCaption = "计算器"ClientHeight = 2970ClientLeft = 2580ClientTop = 1485ClientWidth = 3270ClipControls = 0 'FalseBeginProperty FontName = "System"Size = 9.75Charset = 0Weight = 700Underline = 0 'FalseItalic = 0 'FalseStrikethrough = 0 'FalseEndPropertyIcon = "CALC.frx":0000LinkMode = 1 'SourceLinkTopic = "Form1"MaxButton = 0 'FalsePaletteMode = 1 'UseZOrderScaleHeight = 2970ScaleWidth = 3270WhatsThisHelp = -1 'TrueBegin mandButton NumberCaption = "7"Height = 480Index = 7Left = 120TabIndex = 7Top = 600Width = 480EndBegin mandButton NumberCaption = "8"Height = 480Index = 8Left = 720 TabIndex = 8Top = 600 Width = 480EndBegin mandButton NumberCaption = "9"Height = 480Index = 9Left = 1320 TabIndex = 9Top = 600 Width = 480EndBegin mandButton CancelCaption = "C"Height = 480Left = 2040 TabIndex = 10Top = 600 Width = 480EndBegin mandButton CancelEntryCaption = "CE"Height = 480Left = 2640 TabIndex = 11Top = 600 Width = 480EndBegin mandButton NumberCaption = "4"Height = 480Index = 4Left = 120 TabIndex = 4Top = 1200 Width = 480EndBegin mandButton NumberCaption = "5"Height = 480Index = 5Left = 720 TabIndex = 5Top = 1200 Width = 480EndBegin mandButton NumberCaption = "6"Height = 480Index = 6Left = 1320 TabIndex = 6Top = 1200 Width = 480EndBegin mandButton OperatorCaption = "+"Height = 480Index = 1Left = 2040 TabIndex = 12Top = 1200 Width = 480EndBegin mandButton OperatorCaption = "-"Height = 480Index = 3Left = 2640 TabIndex = 13Top = 1200 Width = 480EndBegin mandButton NumberCaption = "1"Height = 480Index = 1Left = 120 TabIndex = 1Top = 1800 Width = 480EndBegin mandButton NumberCaption = "2"Height = 480Left = 720 TabIndex = 2Top = 1800 Width = 480EndBegin mandButton NumberCaption = "3"Height = 480Index = 3Left = 1320 TabIndex = 3Top = 1800 Width = 480EndBegin mandButton OperatorCaption = "X"Height = 480Index = 2Left = 2040 TabIndex = 14Top = 1800 Width = 480EndBegin mandButton OperatorCaption = "/"Height = 480Index = 0Left = 2640 TabIndex = 15Top = 1800 Width = 480EndBegin mandButton NumberCaption = "0"Height = 480Index = 0Left = 120 TabIndex = 0Top = 2400 Width = 1080 EndBegin mandButton DecimalCaption = "."Left = 1320TabIndex = 18Top = 2400Width = 480EndBegin mandButton OperatorCaption = "="Height = 480Index = 4Left = 2040TabIndex = 16Top = 2400Width = 480EndBegin mandButton PercentCaption = "%"Height = 480Left = 2640TabIndex = 17Top = 2400Width = 480EndBegin bel ReadoutAlignment = 1 'Right JustifyBackColor = &H0000FFFF&BorderStyle = 1 'Fixed SingleCaption = "0."BeginProperty FontName = "MS Sans Serif" Size = 12Charset = 0Weight = 700Underline = 0 'FalseItalic = 0 'FalseStrikethrough = 0 'FalseEndPropertyForeColor = &H00000000&Height = 375Left = 120TabIndex = 19Top = 105Width = 3000EndEndAttribute VB_Name = "Calculator"Attribute VB_GlobalNameSpace = FalseAttribute VB_Creatable = FalseAttribute VB_PredeclaredId = TrueAttribute VB_Exposed = False' -------------------------------------------------------------------------- ' 版权所有 (C) 1994 Microsoft Corporation'' 您可以免费以任何方式使用、修改、复制并分发您认为有用的' 示例应用程序文件 (或任何修改过的版本)。
科学计算器vb代码
Shortcut = ^V
End
Begin Menu_Cut
Caption = "剪切"
Shortcut = ^X
End
Begin line1
Caption = "-"
End
Begin Menu_All
Caption = "全选"
Shortcut = ^A
End
Begin line2
Caption = "Exp"
Height = 390
Left = 840
TabIndex = 31
Top = 960
Width = 630
End
Begin Cmd_Sin
Caption = "Sin"
Height = 390
Left = 120
T0
Style = 1 'Graphical
TabIndex = 21
Top = 2400
Width = 510
End
Begin Command1
Caption = "."
Height = 390
Index = 11
Left = 3915
TabIndex = 20
Top = 2400
Width = 510
Left = 825
TabIndex = 33
Top = 1935
Width = 630
End
Begin Cmd_Operator
Caption = "X^Y"
Height = 390
vb简单的计算机源代码
vb简单的计算机源代码.txt如果xx的时光在闲散中度过,那么回忆岁月将是一场凄凉的悲剧。
杂草多的地方庄稼少,空话多的地方xx少。
即使路上没有花朵,我仍可以欣赏荒芜。
Private Sub Command1_Click()Form1.Caption = "欢迎使用智能计算器"'载入默认正常显示If Check1.Value = "0" Then'1类分歧ElseIf Text1.Text = "" Or Text2.Text = "" Then'2类分歧Form1.Caption = "xataliq kuruldi"Text3.Text = "运算数值不能为空"ElseIf Text1.Text = "" And Text2.Text = "" Then'2类分歧Form1.Caption = "xataliq kuruldi"Text3.Text = "运算数值不能为空"Else'2类分歧Dim a, b, c As Doublea = Val(Text1.Text)b = Val(Text2.Text)c = a + bText3.Text = cText1.Text = ""Text2.Text = ""End IfIf Check1.Value = "1" Then'1类分歧ElseIf Text1.Text = "" Or Text2.Text = "" Then'2类分歧Form1.Caption = "xataliq kuruldi"Text3.Text = "运算数值不能为空"ElseIf Text1.Text = "" And Text2.Text = "" Then'2类分歧Form1.Caption = "xataliq kuruldi"Text3.Text = "运算数值不能为空"Else'2类分歧Dim d, e, f As Doubled = Val(Text1.Text)e = Val(Text2.Text)f = d + eText3.Text = fEnd IfEnd SubPrivate Sub Command2_Click()Form1.Caption = "欢迎使用智能计算器"If Check1.Value = "1" ThenElseIf Text1.Text = "" Or Text2.Text = "" ThenForm1.Caption = "xataliq kuruldi"Text3.Text = "运算数值不能为空"ElseIf Text1.Text = "" And Text2.Text = "" ThenForm1.Caption = "xataliq kuruldi"Text3.Text = "运算数值不能为空"ElseDim d, e, f As Doubled = Val(Text1.Text)e = Val(Text2.Text)f = d - eText3.Text = fEnd IfIf Check1.Value = "0" ThenElseIf Text1.Text = "" Or Text2.Text = "" Then Form1.Caption = "xataliq kuruldi"Text3.Text = "运算数值不能为空"ElseIf Text1.Text = "" And Text2.Text = "" Then Form1.Caption = "xataliq kuruldi"Text3.Text = "运算数值不能为空"ElseDim a, b, c As Doublea = Val(Text1.Text)b = Val(Text2.Text)c = a - bText3.Text = cText1.Text = ""Text2.Text = ""End IfEnd SubPrivate Sub Command3_Click()Form1.Caption = "欢迎使用智能计算器"If Check1.Value = "1" ThenElseIf Text1.Text = "" Or Text2.Text = "" Then Form1.Caption = "xataliq kuruldi"Text3.Text = "运算数值不能为空"ElseIf Text1.Text = "" And Text2.Text = "" Then Form1.Caption = "xataliq kuruldi"Text3.Text = "运算数值不能为空"ElseDim d, e, f As Doubled = Val(Text1.Text)e = Val(Text2.Text)f = d * eText3.Text = fEnd IfIf Check1.Value = "0" ThenElseIf Text1.Text = "" Or Text2.Text = "" Then Form1.Caption = "xataliq kuruldi"Text3.Text = "运算数值不能为空"ElseIf Text1.Text = "" And Text2.Text = "" Then Form1.Caption = "xataliq kuruldi"Text3.Text = "运算数值不能为空"ElseDim a, b, c As Doublea = Val(Text1.Text)b = Val(Text2.Text)c = a * bText3.Text = cText1.Text = ""Text2.Text = ""End IfEnd SubPrivate Sub Command4_Click()Form1.Caption = "欢迎使用智能计算器"If Check1.Value = "0" ThenElseIf Text1.Text = "" Or Text2.Text = "" ThenForm1.Caption = "xataliq kuruldi"Text3.Text = "运算数值不能为空"ElseIf Text1.Text = "" And Text2.Text = "" Then Form1.Caption = "xataliq kuruldi"Text3.Text = "运算数值不能为空"ElseIf Val(Text2.Text) = "0" ThenForm1.Caption = "xataliq kuruldi"Text3.Text = "分数的分子不能为零"ElseDim a, b, c As Doublea = Val(Text1.Text)b = Val(Text2.Text)c = a / bText3.Text = cText1.Text = ""Text2.Text = ""End IfIf Check1.Value = "1" ThenElseIf Text1.Text = "" Or Text2.Text = "" Then Form1.Caption = "xataliq kuruldi"Text3.Text = "运算数值不能为空"ElseIf Text1.Text = "" And Text2.Text = "" Then Form1.Caption = "xataliq kuruldi"Text3.Text = "运算数值不能为空"ElseIf Val(Text2.Text) = "0" ThenForm1.Caption = "xataliq kuruldi"Text3.Text = "分数的分子不能为零"ElseDim d, e, f As Doubled = Val(Text1.Text)e = Val(Text2.Text)f = d / eText3.Text = fEnd IfEnd SubPrivate Sub Command5_Click()Form1.Caption = "欢迎使用智能计算器" Text1.Text = ""Text2.Text = ""Text3.Text = ""End SubPrivate Sub Command6_Click()Form1.HideForm3.ShowEnd SubPrivate Sub Command7_Click()EndEnd SubPrivate Sub Command8_Click()Form1.Caption = "欢迎使用智能计算器" If Text3.Text <> "" ThenText1.Text = Text3.TextText2.Text = ""Text3.Text = ""ElseForm1.Caption = "xataliq kuruldi"Text3.Text = "没有结果无法继续"End IfEnd SubPrivate Sub Text2_Change()End SubPrivate Sub 乘_Click()If Check1.Value = "1" ThenElseIf Text1.Text = "" Or Text2.Text = "" Then Form1.Caption = "xataliq kuruldi"Text3.Text = "运算数值不能为空"ElseIf Text1.Text = "" And Text2.Text = "" Then Form1.Caption = "xataliq kuruldi"Text3.Text = "运算数值不能为空"ElseDim d, e, f As Doubled = Val(Text1.Text)e = Val(Text2.Text)f = d * eText3.Text = fEnd IfIf Check1.Value = "0" ThenElseIf Text1.Text = "" Or Text2.Text = "" Then Form1.Caption = "xataliq kuruldi"Text3.Text = "运算数值不能为空"ElseIf Text1.Text = "" And Text2.Text = "" Then Form1.Caption = "xataliq kuruldi"Text3.Text = "运算数值不能为空"ElseDim a, b, c As Doublea = Val(Text1.Text)b = Val(Text2.Text)c = a * bText3.Text = cText1.Text = ""Text2.Text = ""End IfEnd SubPrivate Sub 除_Click()Form1.Caption = "欢迎使用智能计算器"If Check1.Value = "0" ThenElseIf Text1.Text = "" Or Text2.Text = "" Then Form1.Caption = "xataliq kuruldi"Text3.Text = "运算数值不能为空"ElseIf Text1.Text = "" And Text2.Text = "" Then Form1.Caption = "xataliq kuruldi"Text3.Text = "运算数值不能为空"ElseIf Val(Text2.Text) = "0" ThenForm1.Caption = "xataliq kuruldi"Text3.Text = "分数的分子不能为零"ElseDim a, b, c As Doublea = Val(Text1.Text)b = Val(Text2.Text)c = a / bText3.Text = cText1.Text = ""Text2.Text = ""End IfIf Check1.Value = "1" ThenElseIf Text1.Text = "" Or Text2.Text = "" Then Form1.Caption = "xataliq kuruldi"Text3.Text = "运算数值不能为空"ElseIf Text1.Text = "" And Text2.Text = "" Then Form1.Caption = "错误"Text3.Text = "运算数值不能为空"ElseIf Val(Text2.Text) = "0" ThenForm1.Caption = "错误"Text3.Text = "分数的分子不能为零"ElseDim d, e, f As Doubled = Val(Text1.Text)e = Val(Text2.Text)f = d / eText3.Text = fEnd IfEnd SubPrivate Sub munasiwetlik_Click()Form1.Caption = "欢迎使用智能计算器" Form1.HideForm3.ShowEnd SubPrivate Sub 继续_Click()Form1.Caption = "欢迎使用智能计算器" If Text3.Text <> "" ThenText1.Text = Text3.TextText2.Text = ""Text3.Text = ""ElseForm1.Caption = "xataliq"Text3.Text = "没有结果无法继续"End IfEnd SubPrivate Sub 加_Click()Form1.Caption = "欢迎使用智能计算器"If Check1.Value = "0" ThenElseIf Text1.Text = "" Or Text2.Text = "" Then Form1.Caption = "xataliq kuruldi"Text3.Text = "运算数值不能为空"ElseIf Text1.Text = "" And Text2.Text = "" Then Form1.Caption = "错误"Text3.Text = "运算数值不能为空"ElseDim a, b, c As Doublea = Val(Text1.Text)b = Val(Text2.Text)c = a + bText3.Text = cText1.Text = ""Text2.Text = ""End IfIf Check1.Value = "1" ThenElseIf Text1.Text = "" Or Text2.Text = "" Then Form1.Caption = "xataliq kuruldi"Text3.Text = "运算数值不能为空"ElseIf Text1.Text = "" And Text2.Text = "" Then Form1.Caption = "xataliq kuruldi"Text3.Text = "运算数值不能为空"ElseDim d, e, f As Doubled = Val(Text1.Text)e = Val(Text2.Text)f = d + eText3.Text = fEnd IfEnd SubPrivate Sub 减_Click()Form1.Caption = "欢迎使用智能计算器"If Check1.Value = "1" ThenElseIf Text1.Text = "" Or Text2.Text = "" Then Form1.Caption = "xataliq kuruldi"Text3.Text = "运算数值不能为空"ElseIf Text1.Text = "" And Text2.Text = "" Then Form1.Caption = "错误"Text3.Text = "运算数值不能为空"ElseDim d, e, f As Doubled = Val(Text1.Text)e = Val(Text2.Text)f = d - eText3.Text = fEnd IfIf Check1.Value = "0" ThenElseIf Text1.Text = "" Or Text2.Text = "" Then Form1.Caption = "错误"Text3.Text = "运算数值不能为空"ElseIf Text1.Text = "" And Text2.Text = "" Then Form1.Caption = "错误"Text3.Text = "运算数值不能为空"ElseDim a, b, c As Doublea = Val(Text1.Text)b = Val(Text2.Text)Text3.Text = cText1.Text = ""Text2.Text = ""End IfEnd SubPrivate Sub 清空_Click()Form1.Caption = "欢迎使用智能计算器" Text1.Text = ""Text2.Text = ""Text3.Text = ""End Sub----------------------------------------------form2Private Sub Command1_Click()Form3.HideForm1.ShowForm1.Text3.Text = ""End SubPrivate Sub Command2_Click()End。
vb简易计算器源码
vb简易计算器源码代码如下:复制代码代码如下:/***Author:乌鸟heart*Version:1.0*/Dim IntX As Double '全局变量,⽤于存储计算的数值Dim IntOperation As Double '标记运算类型Dim isBegin As Boolean '标记是否已经给IntX赋值Public Sub Clear() '清空命令函数screen.Caption = ""End SubPublic Sub SavaToIntX()Select Case IntOperationCase 1 '加法If isBegin = False ThenIntX = Val(screen.Caption)isBegin = TrueElseIntX = IntX + Val(screen.Caption)End IfCase 2 '减法If isBegin = False ThenIntX = Val(screen.Caption)isBegin = TrueElseIntX = IntX - Val(screen.Caption)End IfCase 3 '乘法If isBegin = False ThenIntX = Val(screen.Caption)isBegin = TrueElseIntX = IntX * Val(screen.Caption)'screen.Caption = IntXEnd IfCase 4 '除法If isBegin = False ThenIntX = Val(screen.Caption)isBegin = TrueElseIntX = IntX / Val(screen.Caption)End IfEnd SelectEnd SubPrivate Sub Command0_Click()screen.Caption = screen.Caption & 0End SubPrivate Sub Command1_Click()screen.Caption = screen.Caption & 1End SubPrivate Sub Command2_Click()screen.Caption = screen.Caption & 2End SubPrivate Sub Command3_Click()screen.Caption = screen.Caption & 3End SubPrivate Sub Command4_Click()screen.Caption = screen.Caption & 4End SubPrivate Sub Command5_Click()screen.Caption = screen.Caption & 5End SubPrivate Sub Command6_Click()screen.Caption = screen.Caption & 6End SubPrivate Sub Command7_Click()screen.Caption = screen.Caption & 7End SubPrivate Sub Command8_Click()screen.Caption = screen.Caption & 8End SubPrivate Sub Command9_Click()screen.Caption = screen.Caption & 9End SubPrivate Sub CommandClear_Click() '清空命令isBegin = FalseIntOperation = 0IntX = 0screen.Caption = ""End SubPrivate Sub CommandEqual_Click() '等号运算If IntOperation <> 0 Then '有运算标记的情况Call SavaToIntXIntOperation = 0isBegin = Falsescreen.Caption = IntXEnd IfEnd SubPrivate Sub CommandMinus_Click() '减法运算If IntOperation <> 0 Then '有运算标记的情况Call SavaToIntXIntOperation = 2Call ClearElseIntOperation = 2Call SavaToIntXCall ClearEnd IfEnd SubPrivate Sub CommandMultiple_Click() '乘法运算If IntOperation <> 0 Then '有运算标记的情况Call SavaToIntXIntOperation = 3Call ClearElseIntOperation = 3Call SavaToIntXCall ClearEnd IfEnd SubPrivate Sub CommandPlus_Click() '加法运算If IntOperation <> 0 Then '有运算标记的情况Call SavaToIntXIntOperation = 1Call ClearElseIntOperation = 1Call SavaToIntXCall ClearEnd IfEnd SubPrivate Sub CommandSlash_Click() '除法运算If IntOperation <> 0 Then '有运算标记的情况Call SavaToIntXIntOperation = 4Call ClearElseIntOperation = 4Call SavaToIntXCall ClearEnd IfEnd Sub。
VB计算器源代码
VB计算器源代码Private Data As DoublePrivate lOperation As LongPrivate fClear As BooleanPrivate Sub Command1_Click(Index As Integer)If fClear = True Then Text1.Text = "": fClear = FalseText1.Text = Text1.Text & IndexEnd SubPrivate Sub Command2_Click()If InStr(1, Text1.Text, ".") <= 0 Then Text1.Text = Text1.Text & "."End SubPrivate Sub Command3_Click()If fClear = False ThenSelect Case lOperationCase 0Case 1: Text1.Text = Data + Val(Text1.Text)Case 2: Text1.Text = Data - Val(Text1.Text)Case 3: Text1.Text = Data * Val(Text1.Text)Case 4: If Val(Text1.Text) = 0 Then MsgBox "除数不等于0": Exit Sub Else Text1.Text = Data / Val(Text1.Text)End SelectEnd IfData = Val(Text1.Text)fClear = TruelOperation = 0End SubPrivate Sub Command4_Click()If Len(Text1.Text) > 0 Then Text1.Text = Left(Text1.Text, Len(Text1.Text) - 1)End SubPrivate Sub Command5_Click()If fClear = False ThenSelect Case lOperationCase 0Case 1: Text1.Text = Data + Val(Text1.Text)Case 2: Text1.Text = Data - Val(Text1.Text)Case 3: Text1.Text = Data * Val(Text1.Text)Case 4: If Val(Text1.Text) = 0 Then MsgBox "除数不等于0": Exit Sub Else Text1.Text = Data / Val(Text1.Text)End SelectEnd IfData = Val(Text1.Text)fClear = TruelOperation = 1End SubPrivate Sub Command6_Click()If fClear = False ThenSelect Case lOperationCase 0Case 1: Text1.Text = Data + Val(Text1.Text)Case 2: Text1.Text = Data - Val(Text1.Text)Case 3: Text1.Text = Data * Val(Text1.Text)Case 4: If Val(Text1.Text) = 0 Then MsgBox "除数不等于0": Exit Sub Else Text1.Text = Data / Val(Text1.Text)End SelectEnd IfData = Val(Text1.Text)fClear = TruelOperation = 2End SubPrivate Sub Command7_Click()If fClear = False ThenSelect Case lOperationCase 0Case 1: Text1.Text = Data + Val(Text1.Text)Case 2: Text1.Text = Data - Val(Text1.Text)Case 3: Text1.Text = Data * Val(Text1.Text)Case 4: If Val(Text1.Text) = 0 Then MsgBox "除数不等于0": Exit Sub Else Text1.Text = Data / Val(Text1.Text)End SelectEnd IfData = Val(Text1.Text)fClear = TruelOperation = 3End SubPrivate Sub Command8_Click()If fClear = False ThenSelect Case lOperationCase 0Case 1: Text1.Text = Data + Val(Text1.Text)Case 2: Text1.Text = Data - Val(Text1.Text)Case 3: Text1.Text = Data * Val(Text1.Text)Case 4: If Val(Text1.Text) = 0 Then MsgBox "除数不等于0": Exit Sub Else Text1.Text = Data / Val(Text1.Text)End SelectEnd IfData = Val(Text1.Text)fClear = TruelOperation = 4End SubPrivate Sub Command9_Click()Text1.Text = ""lOperation = 0End Sub。
科学计算器vb代码
TabIndex = 21
Top = 2400
Width = 510
End
Begin Command1
Caption = "."
Height = 390
Index = 11
Left = 3915
TabIndex = 20
Top = 2400
Width = 510
Top = 1455
Width = 510
End
Begin Command1
Caption = "4"
Height = 390
Index = 4
Left = 2820
TabIndex = 4
Top = 1455
Width = 510
End
Begin Command1
Caption = "3"
Height = 390
End
End
End
Attribute VB_Name = "Frm_Scientific"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Caption = "*"
Height = 390
IndexLeabharlann = 1Left = 4470
TabIndex = 11
Top = 1455
Width = 510
End
VB 计算器 代码
VB 计算器代码==表单控件设置================================================数字用了一个控件数组(cmdN),+-*/也用了一个控件数组(cmdY),小数点cmdP,"="cmdE,"C"(清除所有)cmdC,"CE"(清除当前数)cmdCE,Breakspace(清除最后一个输入的数)cmdCELast,输出是Test1所有按钮TabStop = False将Text1.locked = .T.==代码部分====================================================Option ExplicitDim dblN(0 To 1) As Double '用于计算的数Dim dblM As Double '记忆数值Dim blnP As Boolean '小数点Dim bytY As Byte '运算符Private Sub cmdCE1_Click() '清除输入的最后一个键Me.Text1.Text = dblN(1)dblN(1) = CDbl(Left(Me.Text1.Text, Len(Me.Text1.Text) - 1))Me.Text1.Text = dblN(1)End SubPrivate Sub cmdCE_Click() '清除输入的这个数值dblN(1) = 0Me.Text1.Text = dblN(1)End SubPrivate Sub cmdCELast_Click()Me.Text1.Text = dblN(1)If Len(Me.Text1.Text) = 1 ThendblN(1) = 0ElsedblN(1) = CDbl(Left(Me.Text1.Text, Len(Me.Text1.Text) - 1))End IfMe.Text1.Text = dblN(1)End SubPrivate Sub cmdE_Click() '计算结果On Error GoTo errSelect Case bytYCase Is = 0dblN(0) = dblN(0) + dblN(1)Case Is = 1dblN(0) = dblN(0) - dblN(1)Case Is = 2dblN(0) = dblN(0) * dblN(1)Case Is = 3dblN(0) = dblN(0) / dblN(1)End SelectMe.Text1.Text = dblN(0)If Left(Me.Text1.Text, 1) = "." Then Me.Text1.Text = "0" + Me.Text1.Text bytY = 0dblN(1) = 0blnP = FalseExit Suberr:Me.Text1.Text = "错误:" & Error(err.Number)bytY = 0dblN(1) = 0: dblN(0) = 0blnP = FalseEnd SubPrivate Sub cmdN_Click(Index As Integer) '数字输入If Not blnP ThendblN(1) = dblN(1) * 10 + IndexElseIf InStr(CStr(dblN(1)), ".") > 0 ThendblN(1) = Val(CStr(dblN(1)) + CStr(Index))ElsedblN(1) = dblN(1) + Index / 10End IfEnd IfMe.Text1.Text = dblN(1)If Left(Me.Text1.Text, 1) = "." Then Me.Text1.Text = "0" + Me.Text1.Text End SubPrivate Sub cmdP_Click() '小数点输入blnP = TrueEnd SubPrivate Sub cmdY_Click(Index As Integer) '运算符输入Call cmdE_ClickbytY = IndexEnd SubPrivate Sub cmdC_Click() '归零bytY = 0dblN(1) = 0: dblN(0) = 0blnP = FalseMe.Text1.Text = dblN(1)End SubPrivate Sub Text1_KeyUp(KeyCode As Integer, Shift As Integer)'处理键盘输入数值Select Case KeyCodeCase 96 To 105, 48 To 57If Shift = 0 Then Call cmdN_Click(KeyCode - IIf(KeyCode >= 96, 96, 48)) '数字输入If Shift = 1 And KeyCode = 56 Then Call cmdY_Click(2) '大键盘上档*Case 110, 190 '小数点If Shift = 0 Then Call cmdP_ClickCase Is = 107, 187 '+Call cmdY_Click(0)Case 109, 189 '-Call cmdY_Click(1)Case Is = 106 '小键盘*Call cmdY_Click(2)Case 111, 191 '/Call cmdY_Click(3)Case Is = 13 '回车(=)Call cmdE_ClickCase Is = 187If Shift = 1 ThenCall cmdY_Click(0) '大键盘上档+ElseCall cmdE_Click '大键盘=End IfCase Is = 27 'CCall cmdC_ClickEnd SelectEnd SubPrivate Sub Text1_Validate(Cancel As Boolean) '焦点一直在text1Me.Text1.SetFocusEnd Sub。
VB计算器
用VB写计算器这是我设计的计算器样式,代码如下:Dim data1 As Double‘定义变量类型’Dim data2 As DoubleDim j As IntegerDim flag As BooleanDim result As StringPrivate Sub Form_Load()flag = TrueText1 = "0"End Sub0~9的代码:Private Sub Command1_Click(Index As Integer)‘0’If flag = True ThenIf Text1 <> "0" ThenText1 = Text1 + "0"End IfElseText1 = ""Text1 = Text1 + "0"End Ifflag = TrueEnd SubPrivate Sub Command11_Click(Index As Integer)‘1’If flag = True ThenIf Text1 <> "0" ThenText1 = Text1 + "1"ElseText1 = ""Text1 = "1"End IfElseText1 = ""Text1 = Text1 + "1"End Ifflag = TrueEnd SubPrivate Sub Command12_Click(Index As Integer)‘2’If flag = True ThenIf Text1 <> "0" ThenText1 = Text1 + "2"ElseText1 = ""Text1 = "2"End IfElseText1 = ""Text1 = Text1 + "2"End Ifflag = TrueEnd SubPrivate Sub Command13_Click(Index As Integer)‘3’If flag = True ThenIf Text1 <> "0" ThenText1 = Text1 + "3"ElseText1 = ""Text1 = "3"End IfElseText1 = ""Text1 = Text1 + "3"End Ifflag = TrueEnd SubPrivate Sub Command14_Click(Index As Integer)‘4’If flag = True ThenIf Text1 <> "0" ThenText1 = Text1 + "4"ElseText1 = ""Text1 = "4"End IfElseText1 = ""Text1 = Text1 + "4"End Ifflag = TrueEnd SubPrivate Sub Command15_Click(Index As Integer)‘5’If flag = True ThenIf Text1 <> "0" ThenText1 = Text1 + "5"ElseText1 = ""Text1 = "5"End IfElseText1 = ""Text1 = Text1 + "5"End Ifflag = TrueEnd SubPrivate Sub Command16_Click(Index As Integer)‘6’If flag = True ThenIf Text1 <> "0" ThenText1 = Text1 + "6"ElseText1 = ""Text1 = "6"End IfElseText1 = ""Text1 = Text1 + "6"End Ifflag = TrueEnd SubPrivate Sub Command17_Click(Index As Integer)‘7’If flag = True ThenIf Text1 <> "0" ThenText1 = Text1 + "7"ElseText1 = ""Text1 = "7"End IfElseText1 = ""Text1 = Text1 + "7"End Ifflag = TrueEnd SubPrivate Sub Command18_Click(Index As Integer)‘8’If flag = True ThenIf Text1 <> "0" ThenText1 = Text1 + "8"ElseText1 = ""Text1 = "8"End IfElseText1 = ""Text1 = Text1 + "8"End Ifflag = TrueEnd SubPrivate Sub Command19_Click(Index As Integer)‘9’If flag = True ThenIf Text1 <> "0" ThenText1 = Text1 + "9"ElseText1 = ""Text1 = "9"End IfElseText1 = ""Text1 = Text1 + "9"End Ifflag = TrueEnd SubPrivate Sub Command2_Click()‘小数点的实现’If Text1 <> "" ThenText1 = Text1 + "."End Ifflag = TrueEnd SubPrivate Sub Command3_Click()‘正负号的转换’If Text1 > 0 And Text1 < 1 ThenText1 = Trim("-") + Trim(Text1)Exit SubEnd IfIf Text1 > -1 And Text1 < 0 ThenText1 = "0" + Trim(-Text1)Exit SubEnd IfText1 = -Text1End Sub‘+-*/加减乘除的实现’Private Sub Command4_Click()‘+’data1 = Text1j = "1"Text1 = "0"Text1.SetFocusEnd SubPrivate Sub Command5_Click()‘-‘data1 = Text1j = "2"Text1 = "0"Text1.SetFocusEnd SubPrivate Sub Command6_Click()‘*’data1 = Text1j = "3"Text1 = "0"Text1.SetFocusEnd SubPrivate Sub Command7_Click()‘/’data1 = Text1j = "4"Text1 = "0"Text1.SetFocusEnd SubPrivate Sub Command10_Click()‘=’data2 = Text1If j = 1 Thenresult = data1 + data2ElseIf j = 2 Thenresult = data1 - data2ElseIf j = 3 Thenresult = data1 * data2ElseIf j = 4 Thenresult = data1 / data2End IfIf result > 0 And result < 1 Thenresult = "0" + Trim(result)End IfIf result > -1 And result < 0 Thenresult = "0" + Trim(-result)result = "-" + resultEnd IfText1 = resultdata1 = "0"flag = FalseEnd SubPrivate Sub Command9_Click()‘C清除按钮’Text1 = "0"Text1.SetFocusEnd Sub。
用VB编写一个简单计算器
用VB编写一个简单计算器一、功能:实现简单的加减乘除功能,C归零,CE取消输入,%计算并显示第一个操作数的百分比。
二、控件:1个label,20个commandbutton。
三、计算器运行界面:四、详细代码:Option ExplicitDim 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 = 0Op2 = 0Form_LoadEnd Sub' CE (取消输入) 按钮的Click 事件过程Private Sub CancelEntry_Click()Readout = Format(0, "0.")DecimalFlag = FalseLastInput = "CE"End Sub' 小数点(.) 按钮的Click 事件过程' 如果上一次按键为运算符,初始化readout 为"0.";' 否则显示时追加一个小数点Private Sub Decimal_Click()If LastInput = "NEG" ThenReadout = Format(0, "-0.")ElseIf LastInput <> "NUMS" ThenReadout = Format(0, "0.")End IfDecimalFlag = TrueLastInput = "NUMS"End Sub' 窗体的初始化过程' 设置所有变量为其初始值Private Sub Form_Load()DecimalFlag = FalseNumOps = 0LastInput = "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" ThenReadout = Format(0, ".")DecimalFlag = FalseEnd IfIf DecimalFlag ThenReadout = Readout + Number(Index).CaptionElseReadout = Left(Readout, InStr(Readout, Format(0, ".")) - 1) + Number(Index).Caption + Format(0, ".")End IfIf LastInput = "NEG" Then Readout = "-" & ReadoutLastInput = "NUMS"End Sub' 运算符(+, -, x, /, =) 的Click 事件过程' 如果接下来的按键是数字键,增加NumOps。
计算器的VB代码
Imports System.MathPublic Class Form1Public opera As String'记录所要执行的运算soper operatorPublic rdp As Boolean'记录用户是否已经输入小数点bdot radix pointPublic equ As Boolean'记录用户是否已经单击等号bequ equalmarkPublic benum As Double'被操作数dblaccPublic actnum As Double'操作数dbldesPublic result As Double'记录运算结果Private Sub subreset()opera = ""benum = 0actnum = 0rdp = Falseequ = FalseOutPut.Text = ""Pkey.Focus()End SubPrivate Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.LoadDim bt(23) As ButtonDim i As Integerbt(1) = B1bt(2) = B2bt(3) = B3bt(4) = B4bt(5) = B5bt(6) = B6bt(7) = B7bt(8) = B8bt(9) = B9bt(10) = B0bt(11) = Babt(12) = Bjbt(13) = Bqbt(14) = Bmbt(15) = Bdbt(16) = Bebt(17) = BFbt(18) = Bsbt(19) = Bxbt(20) = Bcbt(21) = BackSpacebt(22) = CEbt(23) = CFor i = 1 To 23AddHandler bt(i).Click, New System.EventHandler(AddressOf Me.bt_Click) Nextsubreset()End SubPrivate Sub formmain_Activated(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.ActivatedPkey.Focus()End SubPrivate Sub bt_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Dim stext As StringDim bclick As Buttonbclick = senderstext = bclick.TextSelect Case stextCase"1", "2", "3", "4", "5", "6", "7", "8", "9", "0"If equ ThenOutPut.Text = ""End Ifequ = FalseOutPut.Text = OutPut.Text + stextCase"."If Not rdp ThenOutPut.Text = OutPut.Text + stextEnd IfCase"+", "-", "*", "/", "%"If Not OutPut.Text.Equals("") Thenbenum = OutPut.TextOutPut.Text = ""opera = stextEnd IfCase"="rdp = FalseIf Not equ And Not OutPut.Text.Equals("") Thenactnum = OutPut.TextEnd Ifequ = TrueSelect Case operaCase"+"result = benum + actnumCase"-"result = benum - actnumCase"*"result = benum * actnumCase"/"If actnum=0 thenMsgBox”除数不能为零,请重新输入”End ifresult = benum / actnumCase"%"If actnum=0 thenMsgBox”求余数不能为零,请重新输入”End ifresult = benum Mod actnumEnd SelectOutPut.Text = resultCase"+/-"If OutPut.Text.Substring(0, 1) = "-"ThenOutPut.Text = OutPut.Text.Substring(1, OutPut.Text.Length - 1) ElseOutPut.Text = "-" + OutPut.TextEnd IfCase"C"subreset()Case"CE"OutPut.Text = ""Case"BackSpace"If OutPut.Text <> ""ThenIf OutPut.Text.Substring(OutPut.Text.Length - 1) = "."Thenrdp = FalseEnd IfOutPut.Text = OutPut.Text.Substring(0, OutPut.Text.Length - 1)End IfCase"Sqr"Dim m As Integer = OutPut.TextIf m >= 0 ThenOutPut.Text = Sqrt(m).ToStringEnd IfCase"1/X"Dim m As Integer = OutPut.TextIf m <> 0 ThenOutPut.Text = (1 / m).ToStringEnd IfEnd SelectEnd SubPrivate Sub Pkey_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Pkey.ClickEnd SubEnd Class。
VB计算器 源代码
源代码:PublicClass CalculatorDim flag AsBoolean = FalseDim firstNum AsDouble = 0 '第一个操作数Dim secondNum AsDouble = 0 '第二个操作数Dim result AsDouble = 0 '计算结果Dim sign AsChar'符号PrivateSub Button0_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button0.ClickIf flag = FalseThenTextBox.Text = "0"flag = TrueElseTextBox.Text = TextBox.Text + "0"EndIfPrivateSub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.ClickIf flag = FalseThenTextBox.Text = "1"flag = TrueElseTextBox.Text = TextBox.Text + "1"EndIfEndSubPrivateSub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.ClickIf flag = FalseThenTextBox.Text = "2"flag = TrueElseTextBox.Text = TextBox.Text + "2"EndIfEndSubPrivateSub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.ClickIf flag = FalseThenTextBox.Text = "3"flag = TrueElseTextBox.Text = TextBox.Text + "3"EndIfEndSubPrivateSub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.ClickIf flag = FalseThenTextBox.Text = "4"flag = TrueTextBox.Text = TextBox.Text + "4"EndIfEndSubPrivateSub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.ClickIf flag = FalseThenTextBox.Text = "5"flag = TrueElseTextBox.Text = TextBox.Text + "5"EndIfEndSubPrivateSub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.ClickIf flag = FalseThenTextBox.Text = "6"flag = TrueElseTextBox.Text = TextBox.Text + "6"EndIfEndSubPrivateSub Button7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button7.ClickIf flag = FalseThenTextBox.Text = "7"flag = TrueElseTextBox.Text = TextBox.Text + "7"EndIfEndSubPrivateSub Button8_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button8.ClickIf flag = FalseThenTextBox.Text = "8"flag = TrueElseTextBox.Text = TextBox.Text + "8"EndIfEndSubPrivateSub Button9_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button9.ClickIf flag = FalseThenTextBox.Text = "9"flag = TrueElseTextBox.Text = TextBox.Text + "9"EndIfEndSubPrivateSub Point_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Point.ClickTextBox.Text = TextBox.Text + "."EndSubPrivateSub Add_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Add.Click '加号sign = "+"firstNum = Convert.ToDouble(TextBox.Text)flag = FalseEndSubPrivateSub Sub1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Sub1.Click '减号sign = "-"firstNum = Convert.ToDouble(TextBox.Text)flag = FalseEndSubPrivateSub Mul_Click(ByVal sender As System.Object, ByVal eAs System.EventArgs) Handles Mul.Click '乘号sign = "*"firstNum = Convert.ToDouble(TextBox.Text)flag = FalseEndSubPrivateSub Div_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Div.Click '除号sign = "/"firstNum = Convert.ToDouble(TextBox.Text)flag = FalseEndSubPrivateSub Equal_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Equal.Click '等号SelectCase signCase"+"secondNum = Convert.ToDouble(TextBox.Text) result = firstNum + secondNumTextBox.Text = resultCase"-"secondNum = Convert.ToDouble(TextBox.Text) result = firstNum - secondNumTextBox.Text = resultCase"*"secondNum = Convert.ToDouble(TextBox.Text) result = firstNum * secondNumTextBox.Text = resultCase"/"secondNum = Convert.ToDouble(TextBox.Text) result = firstNum / secondNumTextBox.Text = resultEndSelectflag = FalseEndSubEndClass说明:为了便于理解,已对部分按钮的名称改名,稍作修改就能运行。
VB计算器程序代码
VB计算器程序代码下面是一个简单的VB计算器程序代码,可以进行基本的四则运算:```VBImports System.MathPublic Class Form1Dim num1, num2, result As DoubleDim operatorType As String = ""Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.LoadEnd SubPrivate Sub Button_Click(sender As Object, e As EventArgs) Handles btn0.Click, btn1.Click, btn2.Click, btn3.Click,btn4.Click, btn5.Click, btn6.Click, btn7.Click, btn8.Click, btn9.Click, btnDot.ClickDim button As Button = CType(sender, Button)If button.Text = "." ThenIf Not txtDisplay.Text.Contains(".") ThentxtDisplay.Text += button.TextEnd IfElsetxtDisplay.Text += button.TextEnd IfEnd SubPrivate Sub Operator_Click(sender As Object, e As EventArgs) Handles btnAdd.Click, btnSubtract.Click, btnMultiply.Click, btnDivide.ClickDim button As Button = CType(sender, Button)num1 = CDbl(txtDisplay.Text)operatorType = button.TexttxtDisplay.ClearEnd SubPrivate Sub btnEquals_Click(sender As Object, e As EventArgs) Handles btnEquals.Clicknum2 = CDbl(txtDisplay.Text)Select Case operatorTypeCase "+"result = num1 + num2Case "-"result = num1 - num2Case "*"result = num1 * num2Case "/"If num2 <> 0 Thenresult = num1 / num2ElseMessageBox.Show("除数不能为零!", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error)End IfEnd SelecttxtDisplay.Text = result.ToStringEnd SubPrivate Sub btnClear_Click(sender As Object, e As EventArgs) Handles btnClear.ClicktxtDisplay.ClearEnd SubPrivate Sub btnSqrt_Click(sender As Object, e As EventArgs) Handles btnSqrt.ClickIf txtDisplay.Text = "" ThenMessageBox.Show("请输入一个数字!", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error)Elsenum1 = CDbl(txtDisplay.Text)result = Sqrt(num1)txtDisplay.Text = result.ToStringEnd IfEnd SubPrivate Sub btnPower_Click(sender As Object, e As EventArgs) Handles btnPower.ClickIf txtDisplay.Text = "" ThenMessageBox.Show("请输入一个数字!", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error)Elsenum1 = CDbl(txtDisplay.Text)result = num1 ^ 2txtDisplay.Text = result.ToStringEnd IfEnd SubPrivate Sub btnBackspace_Click(sender As Object, e As EventArgs) Handles btnBackspace.ClickIf txtDisplay.Text.Length > 0 ThentxtDisplay.Text = txtDisplay.Text.Substring(0, txtDisplay.Text.Length - 1)End IfEnd SubPrivate Sub btnNegative_Click(sender As Object, e As EventArgs) Handles btnNegative.ClickIf txtDisplay.Text.Length > 0 ThenIf txtDisplay.Text.Substring(0, 1) = "-" ThentxtDisplay.Text = txtDisplay.Text.Substring(1, txtDisplay.Text.Length - 1)ElsetxtDisplay.Text = "-" + txtDisplay.TextEnd IfEnd IfEnd SubEnd Class```这个计算器程序具有以下功能:1.支持0-9的数字输入和小数点的输入。
计算方法VB源代码
计算方法VB源代码以下是一个计算器的VB源代码,用于进行基本的四则运算:```vbImports System.GlobalizationPublic Class CalculatorFormPrivate operand1 As DoublePrivate operand2 As DoublePrivate operation As StringPrivate Sub CalculateButton_Click(sender As Object, e As EventArgs) Handles CalculateButton.ClickIf Double.TryParse(Operand1TextBox.Text, operand1) AndAlso Double.TryParse(Operand2TextBox.Text, operand2) Then Select Case operationCase "+"ResultTextBox.Text = (operand1 +operand2).ToString(CultureInfo.InvariantCulture)Case "-"ResultTextBox.Text = (operand1 -operand2).ToString(CultureInfo.InvariantCulture)Case "*"ResultTextBox.Text = (operand1 *operand2).ToString(CultureInfo.InvariantCulture)Case "/"If operand2 <> 0 ThenResultTextBox.Text = (operand1 /operand2).ToString(CultureInfo.InvariantCulture)ElseMessageBox.Show("Division by zero is not allowed.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)End IfCase ElseMessageBox.Show("Invalid operation.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)End SelectElseMessageBox.Show("Invalid operands.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)End IfEnd SubPrivate Sub OperationButton_Click(sender As Object, e As EventArgs) Handles AdditionButton.Click, SubtractionButton.Click, MultiplicationButton.Click, DivisionButton.ClickDim button As Button = CType(sender, Button)operation = button.TextEnd SubEnd Class```这是一个简单的计算器窗体应用程序,由两个文本框(Operand1TextBox 和 Operand2TextBox) 用于输入操作数,一个结果文本框 (ResultTextBox) 用于显示结果,以及四个按钮 (AdditionButton、SubtractionButton、MultiplicationButton 和 DivisionButton) 用于选择四则运算操作。
VB实例代码-计算器
VB实例代码-计算器首先,需建立如下控件:标签: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 Double Dim PointIn As Boolean Dim Inputing As Boolean Dim Operation1 As IntegerPrivate Sub Calcu_Click(i As Integer)If Inputing = False Then '在前次运算提交之后尚未输入新的数据Operation1 = i '运算符重置Num1 = Label1 '将显示栏里的数据赋值给第一个操作数Exit SubEnd IfInputing = False '将当前状态置为非输入数据阶段If Num1 <> 0 Then '非首次计算Num2 = Label1 '将显示栏里的数据赋值给第二个操作数Calculate (Operation1) '计算前一次运算并显示结果Else '首次计算Num1 = Label1 '将显示栏里的数据赋值给第一个操作数End IfOperation1 = i '提交运算符End SubPrivate Sub Cback_Click()Dim TheLen As IntegerTheLen = Len(Label1)If TheLen > 2 ThenIf Right(Label1, 1) = "." ThenLabel1 = Left(Label1, TheLen - 2) & "."ElseLabel1 = Left(Label1, TheLen - 1)End IfElseCclear_ClickEnd IfEnd SubPrivate Sub Cclear_Click()Label1 = "0."Num2 = 0PointIn = FalseEnd SubPrivate Sub Ccos_Click()Calculate (6)Inputing = FalseEnd SubPrivate Sub Cequal_Click()If Inputing Then '如果刚输入过数据'Or Operation1 > 0 Then Num2 = Label1 '将显示栏里的数据赋值给第二个操作数End IfInputing = False '将当前状态置为非输入数据阶段Calculate (Operation1) '计算提交的运算并显示结果Num1 = 0Operation1 = 0End SubPrivate Sub Calculate(Oprt As Integer) Select Case OprtCase 1Num1 = Num1 + Num2ShowResult (Num1)Case 2Num1 = Num1 - Num2ShowResult (Num1)Case 3Num1 = Num1 * Num2ShowResult (Num1)Case 4Num1 = Num1 / Num2ShowResult (Num1)Case 5Num2 = Label1Num1 = Sin(Num2)ShowResult (Num1)Case 6Num2 = Label1Num1 = Cos(Num2)ShowResult (Num1)Case 7Num2 = Label1Num1 = 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" & NumElseIf Left(Num, 2) = "-." Then '前两个字符为"-." Label1 = "-0." & Right(CStr(Num), Len(CStr(Num)) - 2) ElseLabel1 = NumEnd IfEnd SubPrivate Sub Cnum_Click(Index As Integer)NumInput (Index)End SubPrivate Sub Cpoint_Click()If Inputing = False ThenLabel1 = "0."Inputing = TrueEnd IfPointIn = TrueEnd SubPrivate Sub Csign_Click()If Label1 <> "0." ThenDim StrTemp As StringStrTemp = Label1If Left(StrTemp, 1) = "-" ThenLabel1 = Right(StrTemp, Len(StrTemp) - 1) ElseLabel1 = "-" & StrTempEnd IfEnd IfEnd SubPrivate Sub Csin_Click()Calculate (5)Inputing = FalseEnd SubPrivate Sub Csqrt_Click()Dim x As Longx = Label1If x >= 0 ThenCalculate (7)ElseLabel1 = "Error!"End IfInputing = FalseEnd SubPrivate Sub Cstart_Click()Label1 = "0."Num1 = 0Num2 = 0PointIn = FalseInputing = TrueOperation1 = 0End SubPrivate Sub Form_KeyDown(KeyCode As Integer, Shift As Integer) Cequal.SetFocusIf KeyCode = 46 Then '按Del键Cclear_ClickEnd IfEnd SubPrivate Sub Form_KeyPress(KeyAscii As Integer)Select Case KeyAscii'键入数字:Case 48 To 57NumInput (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_ClickCase 8 '退格Cback_ClickCase 13 '等于(回车键)Cequal_ClickEnd SelectEnd SubPrivate Sub Form_Load()Me.Top = (Screen.Height - Me.Height) / 2 Me.Left = (Screen.Width - Me.Width) / 2 Me.KeyPreview = TrueCstart_ClickEnd SubPrivate Sub NumInput(n As Integer) If Len(Label1) > 15 ThenExit SubEnd IfIf Inputing = False ThenCclear_ClickLabel1 = n & "."Inputing = TrueElseIf Label1 <> "0." ThenIf Right(Label1, 1) = "." ThenIf PointIn = False ThenDim TheLen As IntegerTheLen = Len(Label1)Label1 = Left(Label1, TheLen - 1) Label1 = Label1 & n & "."ElseLabel1 = Label1 & nEnd IfElseLabel1 = Label1 & nEnd IfElseIf PointIn ThenLabel1 = Label1 & nElseLabel1 = n & "." End IfEnd IfEnd Sub。
VB.NETVB2010 四则计算器源码
Public Class Form1'计算器的原理:'将输入的算式以运算符进行分割并计算'例如1+1分割成1、+、1,三个部分,然后运算Function CutStr(ByVal InPutStr As String) As String()Dim CalStr(3) As StringIf InStr(InPutStr, "+") Then'算式中有+号那么就是加法CalStr(0) = InPutStr.Split("+")(0)CalStr(1) = "+"CalStr(2) = InPutStr.Split("+")(1)ElseIf InStr(InPutStr, "-") Then'算式中有-号那么就是减法CalStr(0) = InPutStr.Split("-")(0)CalStr(1) = "-"CalStr(2) = InPutStr.Split("-")(1)ElseIf InStr(InPutStr, "*") Then'算式中有*号那么就是乘法CalStr(0) = InPutStr.Split("*")(0)CalStr(1) = "*"CalStr(2) = InPutStr.Split("*")(1)ElseIf InStr(InPutStr, "/") Then'算式中有/号那么就是除法CalStr(0) = InPutStr.Split("/")(0)CalStr(1) = "/"CalStr(2) = InPutStr.Split("/")(1)Else'算式中没有运算符号CalStr(0) = InPutStrCalStr(1) = ""CalStr(2) = ""End IfReturn CalStrEnd FunctionPrivate Sub数字键_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles数字键0.Click, 数字键1.Click, 数字键2.Click, 数字键3.Click, 数字键4.Click, 数字键5.Click, 数字键6.Click, 数字键7.Click, 数字键8.Click, 数字键9.Click'输入键,如果当前是0那么覆盖输入,如果当前不是0那么追加输入If显示框.Text = "0"Then显示框.Text = sender.textElse显示框.Text = 显示框.Text & sender.textEnd IfEnd SubPrivate Sub小数点_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles小数点.Click'此处比较复杂举例说明'当前按下后输入'没有运算符没有小数的数字例如:2 2.'没有运算符含有小数的数字例如:2. 不输入仍是:2.'含有运算符没有小数例如:3+3 3+3.'含有运算符含有小数例如:3+3. 不输入仍是3+3.'含有运算符运算符后为空例如:3+ 补0输入:3+0.If CutStr(显示框.Text)(1) = ""Then'表示没有运算符If InStr(显示框.Text, sender.text) = False Then'没有运算符没有小数的数字例如:2 2.显示框.Text = 显示框.Text & sender.textEnd IfExit SubElse'表示含有运算符If InStr(CutStr(显示框.Text)(2), sender.text) = False ThenIf CutStr(显示框.Text)(2) = ""Then'含有运算符运算符后为空例如:3+ 补0输入:3+0.显示框.Text = 显示框.Text & "0" & sender.textElse'含有运算符没有小数例如:3+3 3+3.显示框.Text = 显示框.Text & sender.textEnd IfEnd IfEnd IfEnd SubPrivate Sub运算符_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles运算符乘.Click, 运算符除.Click, 运算符加.Click, 运算符减.Click'如果已经存在运算符了,那么,不允许输入If (InStr(显示框.Text, "+") Or InStr(显示框.Text, "-") Or InStr(显示框.Text, "*") Or InStr(显示框.Text, "/")) = False Then显示框.Text = 显示框.Text & sender.textEnd IfEnd SubPrivate Sub清零键_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles清零键.Click'清零就是在显示框显示0显示框.Text = "0"End SubPrivate Sub运算符等于_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles运算符等于.Click'对运算符进行判断,并进行相应的运算Select Case CutStr(显示框.Text)(1)Case"+"显示框.Text = Val(CutStr(显示框.Text)(0)) + Val(CutStr(显示框.Text)(2)) Case"-"显示框.Text = Val(CutStr(显示框.Text)(0)) - Val(CutStr(显示框.Text)(2)) Case"*"显示框.Text = Val(CutStr(显示框.Text)(0)) * Val(CutStr(显示框.Text)(2)) Case"/"显示框.Text = Val(CutStr(显示框.Text)(0)) / Val(CutStr(显示框.Text)(2)) Case ElseEnd SelectEnd SubEnd Class。
科学计算器vb代码
Width = 630
End
Begin mandButton Cmd_Ln
Caption = "Ln"
Height = 390
Left = 1575
TabIndex = 35
Top = 960
Width = 630
End
Begin mandButton Cmd_Square
Caption = "C"
Height = 405
Left = 4785
TabIndex = 15
Top = 405
Width = 750
End
Begin mandButton Cmd_CE
Caption = "CE"
Height = 405
Left = 3975
TabIndex = 14
Caption = "Tan"
Height = 390
Left = 120
TabIndex = 28
Top = 1935
Width = 630
End
Begin mandButton Cmd_Atan
Caption = "Atan"
Height = 390
Left = 120
TabIndex = 27
TabIndex = 17
Top = 1935
Width = 510
End
Begin mandButton Cmd_sqrt
Caption = "Sqrt"
Height = 390
Left = 5025
TabIndex = 16
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
Private Sub txtDisplay_KeyUp(KeyCode As Integer, Shift As Integer)
Select Case KeyCode
Case vbKeyNumpad0
cmdNumber_Click 0
Case vbKeyNumpad1
' 显示小数点
Private Sub cmdDecimal_Click()
If InStr(txtDisplay.Text, ".") Then
Beep
Else
If NewEntry Then
txtDisplay.Text = "."
NewEntry = False
Else
txtDisplay.Text = txtDisplay.Text & "."
End If
End If
End Sub
'计算上一操作符的运算结果
Private Sub cmdEquals_Click()
Case vbKeyBack, vbKeyDelete
DeleteCharacter
End Select
KeyCode = 0
End Sub
cmdNumber_Click 4
Case vbKeyNumpad5
cmdNumber_Click 5
Case vbKeyNumpad6
cmdNumber_Click 6
Case vbKeyNumpad7
Case vbKeySubtract
cmdOperator_Click opSubtract
Case vbKeyDecimal
cmdDecimal_Click
Case vbKeyDivide
cmdOperator_Click opDivide
Else
txtDisplay.Text = "-" & txtDisplay.Text
End If
End Sub
'处理键盘按键
Private Sub Form_KeyPress(KeyAscii As Integer)
txtDisplay_KeyPress KeyAscii
txtDisplay.Text = Left$(txt, Len(txt) - 1)
Else
txtDisplay.Text = "0"
End If
End Sub
'清除显示内容,保存运算符
Private Sub cmdClear_Click()
Case vbCrLf, vbCr, "="
cmdEquals_Click
Case "-"
cmdOperator_Click opSubtract
Case "."
cmdDecimal_Click
If NewEntry Then
txtDisplay.Text = "-"
ElseIf Left$(txtDisplay.Text, 1) = "-" Then
txtDisplay.Text = Right$(txtDisplay.Text, 2)
cmdNumber_Click 9
Case "*", "x", "X"
cmdOperator_Click opMultiply
Case "+"
cmdOperator_Click opAdd
Case opNone
StoredValue = new_value
Case opAdd
StoredValue = StoredValue + new_value
Case opSubtract
StoredValue = StoredValue - new_value
End Sub
Private Sub txtDisplay_GotFocus()
txtDisplay_Change
End Sub
Private Sub txtDisplay_KeyPress(KeyAscii As Integer)
Dim ch As String
ch = Chr$(KeyAscii)
Select Case ch
Case "0"
cmdNumber_Click 0
Case "1"
cmdNumber_Click 1
Case "2"
End Sub
Private Sub Form_KeyUp(KeyCode As Integer, Shift As Integer)
txtDisplay_KeyUp KeyCode, Shift
End Sub
Private Sub txtDisplay_Change()
txtDisplay.SelStart = Len(txtDisplay.Text)
txt = txtDisplay.Text
If Left$(txt, 1) = "-" Then
min_len = 2
Else
min_len = 1
End If
If Len(txt) > min_len Then
Option Explicit
Private StoredValue As Double
Private Const opNone = 0
Private Const opAdd = 1
Private Const opSubtract = 2
Private Const opMultiply = 3
Case "6"
cmdNumber_Click 6
Case "7"
cmdNumber_Click 7
Case "8"
cmdNumber_Click 8
Case "9"
Case "/"
cmdOperator_Click opDivide
Case "C", "c"
cmdClearEntry_Click
End Select
KeyAscii = 0
End Sub
cmdOperator_Click opMultiply
Case vbKeyAdd
cmdOperator_Click opAdd
Case vbKeySeparator
cmdEquals_Click
Private Sub cmdOperator_Click(Index As Integer)
cmdEquals_Click
Operator = Index
NewEntry = True
End Sub
'改变符号
Private Sub cmdPlusMinus_Click()
cmdNumber_Click 7
Case vbKeyNumpad8
cmdNumber_Click 8
Case vbKeyNumpad9
cmdNumber_Click 9
Case vbKeyMultiply
txtDisplay.Text = Format$(Index)
NewEntry = False
Else
txtDisplay.Text = txtDisplay.Text & Format$(Index)
End If
End Sub
Dim new_value As Double
If txtDisplay.Text = "" Then
new_value = 0
Else
new_value = CDbl(txtDisplay.Text)
End If
Select Case Operator
cmdNumber_Click 1
Case vbKeyNumpad2
cmdNumber_Click 2
Case vbKeyNumpad3
cmdNumber_Click 3
Case vbKeyNumpad4
cmdNumber_Click 2
Case "3"
cmdNumber_Click 3
Case "4"
cmdNumber_Click 4
Case "5"
cmdNumber_Click 5
cmdClearEntry_Click
StoredValue = 0
Operator = opNone
End Sub
'清除显示内容
Private Sub cmdClearEntry_Click()
txtDisplay.Text = ""
End Sub
Case opMultiply
StoredValue = StoredValue * new_value
Case opDivide
StoredValue = StoredValue / new_value
End Select