科学计算器vb代码
VB计算器(界面设计全部代码)

VB计算器此计算器能够实现加减乘除等一系列的功能。
还有实现显示系统时间的附加功能。
程序设计:0-9和点在command1的组内。
其他的按钮式运算符和其他功能。
正切和余切有对特殊值进行警告,除也有对除数为零的情况提示警告,还有阶乘等一系列计算溢出的情况提出警告,还有很多需要完善。
这仅供参考。
Public sum As DoublePublic k As Stri ngPublic dia n As Boolea nPublic b As In tegerPublic poin tflag As Boolea nDim clearFlag As Boolea nPublic F As Long'Dim start As boole nPublic resl As Boolean ' 运算结果存储在resl里数字按钮在一个组内,点Public ff As Double 'resl 里是否为空'llllllllllllllllllllllllllllllllllllllllllllllllllllllll击按钮输入数字 lllllllllllllllllllllllllllllllllllllllllllPrivate Sub comma nd1_click(l ndex As In teger) Select Case In dex Case 1If Not clearFlag The n ' 不是等号,那么显示 1 Text1.Text = Text1.Text & 1 Else'是等号,那么清空Text1.Text = 1 clearFlag = False End IfIf Len(Text1.Text) = 2 And InStr(1, Text1, "0") = 1 Then Text1 = Right(Text1, 1) '如果第一位的数位 0,字符串的长度为 2.那么取字符串右面的数,即两个数中右面的数Case 2If Not clearFlag Then Text1.Text = Text1.Text & 2 ElseText1.Text = 2 clearFlag = False End IfIf Len (Text1.Text) = 2 And In Str(1, Text1, "0") = 1 The n Text1 : Case 3 If Not clearFlag The n Text1.Text = Text1.Text & 3 Else=Right(Text1, 1)Text1.Text clearFlag = End If=3 False If Len (Text1.Text) = 2 And In Str(1, Text1, "0") = 1 The n Text1 : =Right(Text1, 1)Case 4If Not clearFlag The nText1.Text =Text1.Text & 4ElseText1.Text =4clearFlag = FalseEnd IfIf Len (Text1.Text) = 2 And In Str(1, Text1, "0") = 1 The n Text1 : =Right(Text1, 1)Case 5If Not clearFlag ThenTextl.Text = Textl.Text & 5 ElseTextl.Text = 5 clearFlag = False End IfIf Len(Text1.Text) = 2 And InStr(1, Text1, "0")= =1 The n Text1 ==Right(Text1, 1)Case 6If Not clearFlag The nText1.Text =Text1.Text & 6ElseText1.Text =6clearFlag = End IfFalseIf Len(Text1.Text) = 2 And InStr(1, Text1, "0")= =1 The n Text1= =Right(Text1, 1)Case 7If Not clearFlag The n Text1.Text = Text1.Text & 7 ElseTextl.Text = 7 clearFlag = False End IfIf Len(Textl.Text) = 2 And InStr(1, Textl, "0") = 1 Then Textl = Right(Text1, 1) Case 8If Not clearFlag The n Textl.Text = Textl.Text & 8 ElseTextl.Text = 8 clearFlag = False End IfIf Len(Textl.Text) = 2 And InStr(1, Textl, "0") = 1 Then Textl = Right(Text1, 1) Case 9If Not clearFlag The n Text1.Text = Text1.Text & 9 ElseText1.Text = 9 clearFlag = False End IfIf Len(Text1.Text) = 2 And InStr(1, Text1, "0") = 1 Then Text1 = Right(Text1, 1) Case 0定义加号按钮 //////////////////////////////////////If Not clearFlag Then Textl.Text = Textl.Text & 0 ElseText1.Text = 0 clearFlag = False End IfIf Len(Text1.Text) = 2 And InStr(1, Text1, "0") = 1 Then Text1 = Right(Text1, 1) Case 10If Not clearFlag The n Text1.Text = Text1.Text + "." ElseText1.Text ="" clearFlag = False End If7///////////////////////////////////////////////// 对输入点 的个数进行约 束///////////////////////////////////////////////////////////If (In Str(Text1.Text, ".")= =1) The n 对点的处理,检查是否有点,有点再输入的话为空,否则加入其中Textl.Text =""End IfIf InStr(Text1.Text, ".") < Len(Textl.Text) Then Textl.Text = Left(Text1.Text, Len(Textl.Text) - 1) End IfEnd SelectText1.SetFocus End Sub '////////////////////////Private Sub comma nd2_click() If Text1.Text = "" The n End IfIf Trim(Text1.Text) <> "" Then sum = Text1.Text Text1.Text ="" k = "+" Else Exit Sub End If End Sub 7////////////////////////定义减号按钮 ////////////////////////////////////// Private Sub comma nd3_click()定义除号按钮If Text1.Text = "" Then End IfIf Trim(Text1.Text) <> "" The n sum = Text1.Text ' 第一个数字填入方框里 Text1.Text =""' 方框的值置空k ="-" Else Exit Sub End If End Sub '///////////////////////定义乘号按钮 ////////////////////////////////////////Private Sub comma nd4_click()If Text1.Text = "" The n End IfIf Trim(Text1.Text) <> "" Then sum = Text1.Text Text1.Text ="" k = "*" Else Exit Sub End If End Sub '/////////////////////////////////////////////////////////////////// Private Sub comma nd5_click() If Text1.Text = "" Then End IfIf Trim(Text1.Text) <> "" Then sum = Text1.Text Text1.Text ="" k = "/" Else Exit Sub End If End Sub'IIIIIIIIIIIIIIIIIIIIIIIIIIII 定 义sin号 按钮/////////////////////////////////////Private Sub comma nd1O_click() If Text1.Text = "" Then End IfIf Trim(Text1.Text) <> "" The n sum = Text1.TextElseExit SubEnd IfEnd Sub'///////////////////////////// 定义cos 按钮/////////////////////////////////////Private Sub comma nd11_click()If Text1.Text = "" The nEnd IfIf Trim(Text1.Text) <> "" The nsum = Text1.Textk ="cos"ElseExit SubEnd IfEnd Sub'///////////////////////////// 定义tan 按钮////////////////////////////////////Private Sub comma nd12_click()If Text1.Text = "" The nEnd IfIf Trim(Text1.Text) <> "" The nsum = Text1.Textk = "tan"ElseExit SubEnd IfEnd Sub'////////////////////////// 定义cot 按钮//////////////////////////////////////Private Sub comma nd13_click()If Text1.Text = "" The nEnd IfIf Trim(Text1.Text) <> "" Then sum = Textl.Textk = "cot"ElseExit SubEnd IfEnd Sub7////////////////////////// 定义人按钮///////////////////////////////////////Private Sub Comma nd14_click()If Textl.Text = "" The nExit SubIf Trim(Textl.Text) <> "" The nsum = Textl.TextTextl.Text =""k = "A"ElseExit SubEnd IfEnd Sub'///////////////////////// 定义2A ///////////////////////////////////////////Private Sub comma nd15_click()If Textl.Text = "" The nExit SubEnd IfIf Trim(Textl.Text) <> "" The nsum = Textl.Textk = "2人"ElseExit SubEnd IfEnd Sub'/////////////////////// 定义3A 按钮/////////////////////////////////////////// Private Sub comma nd16_click()If Textl.Text = "" The nExit SubEnd IfIf Trim(Textl.Text) <> "" The nsum = Textl.Textk = "3A"ElseExit SubEnd IfEnd Sub'IIIIIIIIIIIIIIIIIIII定义X!按钮///////////////////////////////////////////// Private Sub comma nd17_click()If Text1.Text = "" The nExit SubEnd IfIf Trim(Text1.Text) <> "" The nsum = Text1.Textk = "x!"ElseExit SubEnd If'////////////////// 定义sqrt 按钮////////////////////////////////////////////// Private Sub comma nd18_click()If Text1.Text = "" The nExit SubEnd IfIf Trim(Text1.Text) <> "" The nsum = Text1.Textk = "sqrt"ElseExit SubEnd IfEnd Sub'///////////////////////////// 定义阶乘的函数////////////////////////////////// Private Fu nction Ni(N As Long) As Boolea n 'Ni = FalseDim Mm As Long 'Dim F1 As Lo ngF = 1For Mm = 1 To NIf Mm > 12 The n 'If F1 > The n Ni = False Exit Fun cti on End IfF = F * MmNext MmNi = TrueEnd FunctionPrivate Sub equalbut_click() clearFlag = FalseEnd SubPrivate Sub comma nd6_click()If Trim(Text1.Text) <> "" Then 'Dim F As DoubleDim a As DoubleDim pi As DoubleDim i, j As In tegerpi = 3.14159265358979If Text1.Text > 2147483647 ThenMsgBox "溢出,请确认", vbExclamation, Me.CaptionExit SubEnd If a = Text1.Text clearFlag = TrueIf k = "+" Then sum = sum + a Text1.Text = sumElseIf k = "s in" The n sum = Sin(a * pi / 180) Textl.Text = sumElseIf k = "cos" ThenDim t As In tegert = a Mod 180 - 90If t = 0 ThenText1.Text = 0If m = 0 The n MsgBox " ElseElsesum = Cos(a * pi / 180) Textl.Text = sum End IfElseIf k = "tan" Then Dim m As In teger Dim N As In teger m = a Mod 180 - 90(90+180*n )度的正切值无意义,请重新输入 sum = (Tan(a * pi / 180)) Textl.Text = sum End IfElseIf k = "cot" The n If a = 0 The n MsgBox "0 度余切没有意义!请重新输入!Elsesum = 1 / (Tan(a * pi / 180)) Textl.Text = sum End If'ElseIf k = "x!" The n 'If Text1.Text > 0 The n 'Call fact(Text1.Text, F) ' Text1.Text = F'ElseIf Text1.Text = 0 The n 'sum = 1' Text1.Text = sum'ElseIf Text1.Text < 0 The n 'MsgBox " 负数没有阶乘! ”'End If减法运算Elself k = "x!" The nIf Ni(Textl.Text) = False The nMsgBox "溢出", vbExclamation, Me.CaptionText1.Text =""Exit SubElseText1.Text = FEnd IfElself k = "2A " The nTextl.Text = Val(Textl.Text) * Val(Textl.Text)Elself k = "3A" The nTextl.Text = Val(Textl.Text) * Val(Textl.Text) * Val(Textl.Text) Elself k = "sqr" The nIf a >= 0 The nsum = Math.Sqr(a)Textl.Text = sumElseMsgBox "开方数不能为负数!”End If Elself k = "A" The n sum =sum A a Textl.Text = sum Elself k = "-" The n sum = sum - a Textl.Text = sum resl = 1Elself k = "*" The n sum = sum * a Textl.Text = sum Elself k = "/" The nIf Textl.Text = 0 The nMsgBox "除数不能为零!请重新输入Textl.Text =""Elsesum = sum / aTextl.Text = sumIf Len( Textl.Text) > 14 The nMsgBox "溢出,请确认", vbExclamation, Me.Caption Exit SubEnd IfExit SubEnd IfEnd IfEnd IfEnd SubPrivate Sub Comma nd9_Click()If Len(Text1.Text) >= 2 ThenText1.Text = Left(Text1.Text, Len(Text1.Text) - 1) ElseText1.Text =""End IfEnd SubPrivate Sub form」。
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不同方式代码

Dim x As LongDim y As LongPrivate Sub Command1_Click() x = x * 10 + 1Text1.Text = xEnd SubPrivate Sub Command10_Click() x = x * 10Text1.Text = xEnd SubPrivate Sub Command11_Click() Text1.Text = x + yy = Val(Text1.Text)x = 0End SubPrivate Sub Command12_Click() Text1.Text = x - yy = Val(Text1.Text)x = 0End SubPrivate Sub Command13_Click() Text1.Text = x / yy = Val(Text1.Text)x = 0End SubPrivate Sub Command14_Click()Text1.Text = x * yy = Val(Text1.Text)x = 0End SubPrivate Sub Command16_Click() Text1.Text = ""x = 0y = 0End SubPrivate Sub Command2_Click() x = x * 10 + 2Text1.Text = xEnd SubPrivate Sub Command3_Click() x = x * 10 + 3Text1.Text = xEnd SubPrivate Sub Command4_Click() x = x * 10 + 4Text1.Text = xEnd SubPrivate Sub Command5_Click() x = x * 10 + 5Text1.Text = xEnd SubPrivate Sub Command6_Click() x = x * 10 + 6Text1.Text = xEnd SubPrivate Sub Command7_Click() x = x * 10 + 7Text1.Text = xEnd SubPrivate Sub Command8_Click() x = x * 10 + 8Text1.Text = xEnd SubPrivate Sub Command9_Click() x = x * 10 + 9Text1.Text = xEnd SubPrivate Sub Form_Load()x = 0y = 0End Sub这个计算器程序中,你的按钮设置为按钮数组比较合理:数字0-9设置为Command1(0) - Command1(9)+、-、*、/ 设置为Command2(0) - Command2(3)= 设置为Command3. 设置为Command1(10)C 设置为Command4代码如下:Dim X As Single, Y As Single, FH As StringPrivate Sub Command1_Click(Index As Integer)Dim K As IntegerK = Command1(Index).IndexIf K <> 10 ThenIf Right(Text1.Text, 1) = "." ThenText1.Text = Text1.Text & KElseIf Int(Val(Text1.Text)) = Val(Text1.Text) And Right(Text1.Text, 1) <> "." Then Text1.Text = Val(Text1.Text) * 10 + KElseIf Int(Val(Text1.Text)) <> Val(Text1.Text) And Right(Text1.Text, 1) <> "." Then Text1.Text = Text1.Text & KEnd IfElseText1.Text = Text1.Text & "."End IfIf FH = "" ThenX = Val(Text1.Text)ElseY = Val(Text1.Text)End IfEnd SubPrivate Sub Command2_Click(Index As Integer)Dim K1 As IntegerK1 = Command2(Index).IndexText1.Text = ""If Command2(K1).Caption = "+" ThenFH = "+"ElseIf Command2(K1).Caption = "-" ThenFH = "-"ElseIf Command2(K1).Caption = "*" ThenFH = "*"ElseIf Command2(K1).Caption = "/" ThenFH = "/"End IfEnd SubPrivate Sub Command3_Click()If X <> 0 And Y <> 0 ThenIf FH = "+" ThenText1.Text = X + YElseIf FH = "-" ThenText1.Text = X - YElseIf FH = "*" ThenText1.Text = X * YElseIf FH = "/" ThenIf Y = 0 ThenMsgBox "除数不能为0", 16, "错误!"Exit SubEnd IfText1.Text = X / YEnd IfX = Val(Text1.Text)ElseMsgBox "操作数不足!", 16, "错误!" End IfEnd SubPrivate Sub Command4_Click()Text1.Text = "": X = 0: Y = 0: FH = ""End SubPrivate Sub Form_Load()Text1.Text = "": X = 0: Y = 0: FH = ""End SubDim a As Boolean, ysf%, jsjg#, xsd1 As Boolean, xsd2 As Boolean Private Sub Command1_Click(Index As Integer)Dim x$, i%Label4.Caption = "输入"Select Case IndexCase 0x = Chr(48)Case 1x = Chr(49)Case 2x = Chr(50)Case 3x = Chr(51)Case 4x = Chr(52)Case 5x = Chr(53)Case 6x = Chr(54)Case 7x = Chr(55)Case 8x = Chr(56)Case 9x = Chr(57)Case 10x = Chr(45)Case 11x = Chr(46)End SelectIf a = False ThenIf Index = 11 ThenIf xsd1 = False ThenText1 = Text1 & xxsd1 = TrueEnd IfElseIf Index = 10 ThenIf Text1 = "" Then Text1 = "-" ElseText1 = Text1 & xEnd IfElseIf a ThenIf Index = 11 ThenIf xsd2 = False ThenText2 = Text2 & xxsd2 = TrueEnd IfElseIf Index = 10 ThenIf Text2 = "" Then Text2 = "-"ElseText2 = Text2 & xEnd IfEnd IfEnd SubPrivate Sub Command2_Click(Index As Integer) Dim cs1#, cs2#If Index = 0 ThenText1.Text = ""Text2.Text = ""Label4.Caption = "清空"ElseIf Index = 1 Thencs1 = Text1cs2 = Text2Label4.Caption = "计算"Select Case ysfCase 0jsjg = cs1 + cs2Case 1jsjg = cs1 - cs2Case 2jsjg = cs1 * cs2Case 3If Text2 > 0 Thenjsjg = cs1 / cs2Elsel = MsgBox("除数不能少于或者等于0!请重新在“参数二”中输入", 65, "错误提示:")If l = 2 Then EndEnd IfCase 4If Text2 > 0 Thenjsjg = cs1 / cs2 * 100Elsel = MsgBox("除数不能少于或者等于0!请重新在“参数二”中输入", 65, "错误提示:")If l = 2 Then EndEnd IfCase 5jsjg = cs1 ^ cs2End SelectEnd IfText3 = jsjgEnd SubPrivate Sub Command3_Click(Index As Integer) a = TrueSelect Case IndexCase 0ysf = 0Label4.Caption = " 加"Case 1ysf = 1Label4.Caption = " 减"Case 2ysf = 2Label4.Caption = " 乘"Case 3Label4.Caption = " 除"ysf = 3Case 4ysf = 4Label4.Caption = "百分"Case 5ysf = 5Label4.Caption = " 幂"End SelectEnd SubPrivate Sub Command4_Click()Text1 = Text3End SubPrivate Sub Command5_Click()Text2 = Text3End SubPrivate Sub Command6_Click()Text1 = Asc("/")End SubPrivate Sub Command7_Click()Text1 = ""End SubPrivate Sub Command8_Click()Text2 = ""End SubPrivate Sub Text1_KeyPress(KeyAscii As Integer)Dim i%Label4.Caption = "输入" If KeyAscii = 8 ThenKeyAscii = 8ElseIf KeyAscii = 37 Then KeyAscii = 0Command3(4) = TrueText2.SetFocusElseIf KeyAscii = 42 Then KeyAscii = 0Command3(2) = TrueText2.SetFocusElseIf KeyAscii = 94 Then KeyAscii = 0Command3(5) = TrueText2.SetFocusElseIf KeyAscii = 43 Then KeyAscii = 0Command3(0) = True Text2.SetFocus ElseIf KeyAscii = 45 Then If Text1 = "" Thenkeyaseii = 45ElseIf Len(Text1) = 1 And Text1 = "-" ThenKeyAscii = 0l = MsgBox("不能只有一个负数,否则无法进行下一步运算,请重新在“参数一”中输入", 65, "错误提示:")If l = 2 Then EndElseIf Len(Text1) > 2 ThenKeyAscii = 0Command3(1) = TrueText2.SetFocusEnd IfElseIf KeyAscii = 47 ThenKeyAscii = 0Command3(3) = TrueText2.SetFocusElseIf KeyAscii = 46 ThenIf xsd2 = False ThenKeyAscii = 46xsd2 = TrueElseIf xsd2 ThenKeyAscii = 0End IfElseIf KeyAscii < 48 Or KeyAscii > 57 ThenKeyAscii = 0End IfEnd SubPrivate Sub Text2_KeyPress(KeyAscii As Integer) Label4.Caption = "输入"If KeyAscii = 8 ThenKeyAscii = 8ElseIf KeyAscii = 13 ThenKeyAscii = 0Command2(1) = TrueElseIf KeyAscii = 45 ThenIf Text2 = "" Thenkeyaseii = 45ElseIf Len(Text2) >= 1 ThenKeyAscii = 0End IfElseIf KeyAscii = 46 ThenIf xsd2 = False ThenKeyAscii = 46xsd2 = TrueElseIf xsd2 ThenKeyAscii = 0End IfElseIf KeyAscii < 48 Or KeyAscii > 57 Then KeyAscii = 0End IfEnd Sub。
VB简易计算器代码

VB简易计算器代码以下是一个简单的VB计算器代码:```Public Class Form1Dim firstNum As Double ' 第一个数字Dim secondNum As Double ' 第二个数字Dim operation As Integer ' 1-加法,2-减法,3-乘法,4-除法Private Sub Button0_Click(sender As Object, e As EventArgs) Handles Button0.ClickTextBoxResult.Text = TextBoxResult.Text & "0"End SubPrivate Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.ClickTextBoxResult.Text = TextBoxResult.Text & "1"End SubPrivate Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.ClickTextBoxResult.Text = TextBoxResult.Text & "2"End SubHandles Button3.ClickTextBoxResult.Text = TextBoxResult.Text & "3"End SubPrivate Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.ClickTextBoxResult.Text = TextBoxResult.Text & "4"End SubPrivate Sub Button5_Click(sender As Object, e As EventArgs) Handles Button5.ClickTextBoxResult.Text = TextBoxResult.Text & "5"End SubPrivate Sub Button6_Click(sender As Object, e As EventArgs) Handles Button6.ClickTextBoxResult.Text = TextBoxResult.Text & "6"End SubPrivate Sub Button7_Click(sender As Object, e As EventArgs) Handles Button7.ClickTextBoxResult.Text = TextBoxResult.Text & "7"End SubHandles Button8.ClickTextBoxResult.Text = TextBoxResult.Text & "8"End SubPrivate Sub Button9_Click(sender As Object, e As EventArgs) Handles Button9.ClickTextBoxResult.Text = TextBoxResult.Text & "9"End SubPrivate Sub ButtonDot_Click(sender As Object, e As EventArgs) Handles ButtonDot.ClickIf Not TextBoxResult.Text.Contains(".") ThenTextBoxResult.Text = TextBoxResult.Text & "."End IfEnd SubPrivate Sub ButtonAdd_Click(sender As Object, e As EventArgs) Handles ButtonAdd.ClickfirstNum = Double.Parse(TextBoxResult.Text)TextBoxResult.Text = ""operation = 1End SubPrivate Sub ButtonSubtract_Click(sender As Object, e As EventArgs) Handles ButtonSubtract.ClickfirstNum = Double.Parse(TextBoxResult.Text)TextBoxResult.Text = ""operation = 2End SubPrivate Sub ButtonMultiply_Click(sender As Object, e As EventArgs) Handles ButtonMultiply.ClickfirstNum = Double.Parse(TextBoxResult.Text)TextBoxResult.Text = ""operation = 3End SubPrivate Sub ButtonDivide_Click(sender As Object, e As EventArgs) Handles ButtonDivide.ClickfirstNum = Double.Parse(TextBoxResult.Text)TextBoxResult.Text = ""operation = 4End SubPrivate Sub ButtonClear_Click(sender As Object, e As EventArgs) Handles ButtonClear.ClickTextBoxResult.Text = ""End SubPrivate Sub ButtonEquals_Click(sender As Object, e As EventArgs) Handles ButtonEquals.ClickDim result As DoublesecondNum = Double.Parse(TextBoxResult.Text)Select Case operationCase 1result = firstNum + secondNumCase 2result = firstNum - secondNumCase 3result = firstNum * secondNumCase 4result = firstNum / secondNumEnd SelectTextBoxResult.Text = result.ToStringEnd SubEnd Class```这个计算器包括数字按钮0-9、小数点按钮、加法、减法、乘法、除法和等于按钮。
计算器VB代码

Public p As String Public f As DoublePublic s As DoubleDim XDim aDim bDim cDim dPrivate Sub cmdAbs_Click()If txtScreen.Text = "" ThenMsgBox "无数字,无法计算"ElseIf txtScreen.Text <> 0 Thenf = txtScreen.Texts = fs = Abs(s)txtScreen.Text = stxtScreen2.Text = sEnd IfEnd SubPrivate Sub cmdAtn_Click()If txtScreen.Text = "" Then MsgBox "无数字,无法计算"ElseIf txtScreen.Text <> "" Then f = txtScreen.Texts = fs = Atn(s)txtScreen.Text = stxtScreen2.Text = sEnd IfEnd SubPrivate Sub cmdClean_Click() txtScreen = ""f = 0s = 0End SubPrivate Sub cmdClear_Click() txtScreen.Text = ""f = 0s = 0txtScreen2.Text = ""txtGH.Text = ""cmdSpr.Enabled = True cmdXmY.Enabled = True cmdCos.Enabled = True cmdSin.Enabled = True cmdMi.Enabled = True cmdAbs.Enabled = True cmdds.Enabled = True cmdAtn.Enabled = True cmdeN.Enabled = True cmdLog.Enabled = TruecmdTan.Enabled = Truecmdspr2.Enabled = TruecmdGHH.Enabled = TrueEnd SubPrivate Sub cmdCos_Click()If txtScreen.Text = "" ThenMsgBox "无数字,无法计算"ElseIf txtScreen.Text <> "" Thenf = txtScreen.Texts = fs = Cos(s)txtScreen.Text = stxtScreen2.Text = sEnd IfEnd SubPrivate Sub cmdDigit_Click(Index As Integer)txtScreen.Text = txtScreen.Text & cmdDigit(Index).Captions = txtScreen.TextEnd SubPrivate Sub cmdDivide_Click(Index As Integer) If p = "" ThentxtScreen.Text = ""p = "/"f = ss = 0End IfEnd SubPrivate Sub cmdds_Click()If txtScreen.Text = "" ThenMsgBox "无数字,无法计算"ElseIf txtScreen.Text <> 0 Thenf = txtScreen.Texts = fs = 1 / ftxtScreen.Text = stxtScreen2.Text = sElseMsgBox "分子不能为零。
计算器(用vb)代码

计算器(用vb)代码Dim dflag As IntegerDim I As IntegerDim opnre As IntegerDim prev As DoubleDim oflag As IntegerDim ind As IntegerDim soundbz As BooleanDim ProgramPath As StringPrivate Declare Function SetWindowPos Lib "user32" (ByVal hwnd As Long, ByVal hWndInsertAfter As Long, ByVal X As Long, ByVal Y As Long, ByVal Cx As Long, ByVal Cy As Long, ByVal wFlags As Long) As LongPublic Sub SetOnTop(ByVal IsOnTop As Integer)Dim rtn As LongIf IsOnTop = 1 Then'将窗口置于最上面rtn = SetWindowPos(Me.hwnd, -1, 0, 0, 0, 0, 3)Elsertn = SetWindowPos(Me.hwnd, -2, 0, 0, 0, 0, 3)End IfEnd SubPrivate Sub Check1_Click()If Check1.Value = 1 ThenSetOnTop 1ElseSetOnTop 0End IfEnd SubPrivate Sub Command1_Click(Index As Integer)text1 = Trim(text1)If Len(text1) > 20 ThenBeepExit SubEnd Ifsoundbz = Falsemand = "Close"MMControl1.FileName = ProgramPath & "/声音文件/" & Index & ".wav"mand = "Open"mand = "Play"If ind = 4 Thenprev = 0text1.Caption = " "ind = 0End Ifopnre = 0If oflag = 0 Thentext1.Caption = " "End Ifoflag = 1If Command1(Index).Caption <> "." ThenIf text1.Caption <> " 0" Thentext1.Caption = text1.Caption & Command1(Index).CaptionText = Mid(text1, 1, 1)If Text = "." Thentext1 = "0" & text1End IfElsetext1.Caption = " " & Command1(Index).CaptionEnd IfElseIf dflag = 0 Thentext1.Caption = text1.Caption & "."dflag = 1ElseCommand6.SetFocusExit SubEnd IfEnd IfCommand6.SetFocusEnd SubPrivate Sub Command1_KeyPress(Index As Integer, KeyAscii As Integer) If KeyAscii = 13 Then KeyAscii = 0End SubPrivate Sub Command2_Click(Index As Integer)soundbz = Falsemand = "Close"If Index = 0 Then MMControl1.FileName = ProgramPath & "/声音文件/加.wav"If Index = 1 Then MMControl1.FileName = ProgramPath & "/声音文件/减.wav"If Index = 3 Then MMControl1.FileName = ProgramPath & "/声音文件/乘.wav"If Index = 2 Then MMControl1.FileName = ProgramPath & "/声音文件/除.wav"If Index = 4 ThenMMControl1.FileName = ProgramPath & "/声音文件/等于.wav"soundbz = TrueEnd Ifmand = "Open"mand = "Play"If opnre = 0 Or Index = 4 ThenIf ind = 0 Thenprev = prev + Val(text1.Caption)ElseIf ind = 1 Thenprev = prev - Val(text1.Caption)ElseIf ind = 2 ThenIf Val(text1.Caption) = 0 Thentext1 = "错误!"BeepCommand6.SetFocusExit SubElseprev = prev / Val(text1.Caption)End IfElseIf ind = 3 Thenprev = prev * Val(text1.Caption)End Iftext1.Caption = Str(prev)oflag = 0End Ifopnre = 1ind = Indexdflag = 0Command6.SetFocusIf Index = 4 ThenIf Option1.Value = True Then text1 = Trim(Round(text1, 2))If Option2.Value = True Then text1 = Trim(Round(text1, 3))If Option3.Value = True Then text1 = Trim(Round(text1, 4))If Option4.Value = True Then text1 = Trim(text1)If text1 <> 0 ThenText = Mid(text1, 1, 1)If Text = "." Thentext1 = "0" & text1End IfEnd Ifls = Len(text1)If ls <> 0 ThenFor I = 1 To lst = Mid(text1, I, 1)DoEventsDo While (MMControl1.Mode <> 525) And soundbzDoEventsLoopmand = "Close"If t <> "." ThenIf t = "-" ThenMMControl1.FileName = ProgramPath & "/声音文件/负.wav"ElseMMControl1.FileName = ProgramPath & "/声音文件/" & t & ".wav"End IfElseMMControl1.FileName = ProgramPath & "/声音文件/10.wav"End Ifmand = "Open"mand = "Play"Next IEnd IfEnd IfEnd SubPrivate Sub Command2_KeyPress(Index As Integer, KeyAscii As Integer) If KeyAscii = 13 Then KeyAscii = 0End SubPrivate Sub Command3_Click()soundbz = Falsemand = "Close"MMControl1.FileName = ProgramPath & "/声音文件/归零.wav"mand = "Open"mand = "Play"text1.Caption = " 0"Command6.SetFocusEnd SubPrivate Sub Command3_KeyPress(KeyAscii As Integer)If KeyAscii = 13 Then KeyAscii = 0End SubPrivate Sub Command4_Click()soundbz = Falsemand = "Close"MMControl1.FileName = ProgramPath & "/声音文件/清除.wav"mand = "Open"mand = "Play"dflag = 0prev = 0oflag = 0ind = 0opnre = 0text1.Caption = " 0"Command6.SetFocusEnd SubPrivate Sub Command4_KeyPress(KeyAscii As Integer)If KeyAscii = 13 Then KeyAscii = 0End SubPrivate Sub Command5_Click()soundbz = FalseSaveSetting App.EXEName, "保留", "n1", Option1.Value SaveSetting App.EXEName, "保留", "n2", Option2.Value SaveSetting App.EXEName, "保留", "n3", Option3.Value SaveSetting App.EXEName, "保留", "n4", Option4.Value SaveSetting App.EXEName, "置顶", "yorn", Check1.Value ' mand = "Close"Unload MeEnd SubPrivate Sub Command5_KeyPress(KeyAscii As Integer) If KeyAscii = 13 Then KeyAscii = 0End SubPrivate Sub Command6_Click()Command2_Click (4)End SubPrivate Sub Form_KeyPress(KeyAscii As Integer)If text1 = "错误!" ThenCommand4_ClickEnd IfIf KeyAscii = Asc(".") ThenI = 10Command1_Click (I)ElseIf KeyAscii = Asc("0") ThenI = 0Command1_Click (I)ElseIf KeyAscii = Asc("1") ThenI = 1Command1_Click (I)ElseIf KeyAscii = Asc("2") ThenI = 2Command1_Click (I)ElseIf KeyAscii = Asc("3") ThenI = 3Command1_Click (I)ElseIf KeyAscii = Asc("4") ThenI = 4Command1_Click (I)ElseIf KeyAscii = Asc("5") ThenI = 5Command1_Click (I)ElseIf KeyAscii = Asc("6") ThenI = 6Command1_Click (I)ElseIf KeyAscii = Asc("7") ThenI = 7Command1_Click (I)ElseIf KeyAscii = Asc("8") ThenI = 8Command1_Click (I)ElseIf KeyAscii = Asc("9") ThenI = 9Command1_Click (I)ElseIf KeyAscii = Asc("0") ThenI = 0Command1_Click (I)ElseIf KeyAscii = Asc("+") ThenI = 0Command2_Click (I) ElseIf KeyAscii = Asc("+") ThenCommand2_Click (I)ElseIf KeyAscii = Asc("-") ThenI = 1Command2_Click (I)ElseIf KeyAscii = Asc("/") ThenI = 2Command2_Click (I)ElseIf KeyAscii = Asc("*") ThenI = 3Command2_Click (I)ElseIf KeyAscii = Asc("=") Or KeyAscii = 13 ThenI = 4Command2_Click (I)ElseIf KeyAscii = Asc("c") Or KeyAscii = Asc("C") Or KeyAscii = 27 Then Command4_ClickElseIf KeyAscii = Asc("d") Or KeyAscii = Asc("D") ThenCommand3_ClickEnd IfEnd SubPrivate Sub Form_Load()If App.PrevInstance = True ThenUnload MeMsgBox "程序正在运行,不可重复运行多个!", 64, "系统提示"EndEnd Ifsoundbz = Truedflag = 0prev = 0oflag = 0ind = 0Clipboard.ClearMMControl1.DeviceType = "WaveAudio"ProgramPath = App.PathOption1.Value = GetSetting(App.EXEName, "保留", "n1", True) Option2.Value = GetSetting(App.EXEName, "保留", "n2", False) Option3.Value = GetSetting(App.EXEName, "保留", "n3", False) Option4.Value = GetSetting(App.EXEName, "保留", "n4", False) Check1.Value = GetSetting(App.EXEName, "置顶", "yorn", 0)End SubPrivate Sub Form_Unload(Cancel As Integer)mand = "Close"EndEnd SubPrivate Sub text1_Click()End 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程序设计要求:1)设计一个可以进行四则运算的简单计算器。
该计算器可以进行加、减、乘、除、求模(取余)等简单的四则运算,并具有符合计算器日常使用习惯的容错纠错功能。
具体步骤如下:①在界面上建立按钮控件数组:首先在窗体中置入一个命令按钮控件后,将其激活并点击右键通过“复制”、“粘贴”的方法依次产生19个一样的命令按钮控件,其中在创建第一个“粘贴”控件时VB会询问“是否要创建控件数组?”回答“是”即可开始依次创建该控件数组。
②按钮属性的设置:将各按钮的caption属性分别设置为0,1,2……9,+、-、×,/,Mod,=,cls,Exit,注意在设置这些属性时其值与按钮控件的Index属性的对应性。
③其它控件的属性设置:文本框作为显示操作数和结果的控件,应遵循一般计算器的显示习惯,将其Alignment即对齐属性设置为“Right”,此外,将各控件相关的字体、字号等设置为统一风格。
④在程序的通用区定义四个窗体层变量:num1、num2、sum、act、前三个为双精度、act为整型变量。
⑤编写进行四则运算所需的程序作为命令按钮的单击事件过程。
实验三参考代码一:•Dim num1 As Double, num2 As Double•Dim sum As Double•Dim act As Integer•Private Sub Form_Load()•num1 = 0•num2 = 0•sum = 0•End Sub•Private Sub Command1_Click(Index As Integer)•Select Case Index•Case 0• If Text1.Text = "" Then• Text1.Text = "0"• Else• Text1.Text = Text1.Text + "0"• End If•Case 1• If Text1.Text = "" Then• Text1.Text = "1"• Else• Text1.Text = Text1.Text + "1" End If•Case 2• If Text1.Text = "" Then• Text1.Text = "2"• Else• Text1.Text = Text1.Text + "2"• End If•Case 3• If Text1.Text = "" Then• Text1.Text = "3"• Else• Text1.Text = Text1.Text + "3"• End If•Case 4• If Text1.Text = "" Then• Text1.Text = "4"• Else• Text1.Text = Text1.Text + "4" • End If•Case 5• If Text1.Text = "" Then• Text1.Text = "5"• Else• Text1.Text = Text1.Text + "5" •End If•Case 6• If Text1.Text = "" Then• Text1.Text = "6"• Else• Text1.Text = Text1.Text + "6" • End If•Case 7• If Text1.Text = "" Then• Text1.Text = "7"• Else• Text1.Text = Text1.Text + "7"•Case 8• If Text1.Text = "" Then• Text1.Text = "8"• Else• Text1.Text = Text1.Text + "8"• End If•Case 9• If Text1.Text = "" Then• Text1.Text = "9"• Else• Text1.Text = Text1.Text + "9"• End If•Case 10• If Text1.Text = "" Then• Text1.Text = "."• Else• Text1.Text = Text1.Text + "."• End If•Case 11• num1 = CDbl(Text1.Text) ‘强制转换双精度型• Text1.Text = ""•Case 12• num1 = CDbl(Text1.Text) • Text1.Text = ""• act = 2•Case 13• num1 = CDbl(Text1.Text) • Text1.Text = ""• act = 3•Case 14• num1 = CDbl(Text1.Text) • Text1.Text = ""• act = 4•Case 15• num1 = CDbl(Text1.Text) • Text1.Text = ""• act = 5•Case 16• num1 = 0• num2 = 0• sum = 0• Text1.Text = ""•Case 17• num2 = CDbl(Text1.Text) •Select Case act• Case 1• sum = num1 + num2 • Case 2• sum = num1 - num2 •Case 3• sum = num1 * num2 • Case 4• sum = num1 / num2 • Case 5• sum = num1 Mod num2 • End Select• Text1.Text = ""• Text1.Text = CStr(sum) •Case 18• End•End Select•End Sub。
风铃科学计算器程序代码(vb)

风铃计算器程序代码青春风铃西南交通大学Dim sum As Double, Expr, A, B, D, Cha As String Dim Time As IntegerDim leftbracket, rbracket As IntegerDim Bo1, Bo2, Sto As BooleanPublic Function Fact(n As Long) As DoubleIf n > 0 ThenIf n = 1 ThenFact = 1ElseFact = n * Fact(n - 1)End IfElseIf n = 0 ThenFact = 1ElseIf n = -1 ThenFact = -1ElseFact = n * Fact(n + 1)End IfEnd IfEnd FunctionPrivate Function leftfind(ByV al Expr As String, Where As Long) As String Dim i, leftbracket, rbracket As IntegerDim numl As StringIf Mid(Expr, Where - 1, 1) = ")" Then '-------------左有括号For i = Where To 1 Step -1If Mid(Expr, i, 1) = ")" Thenrbracket = rbracket + 1ElseIf Mid(Expr, i, 1) = "(" Thenlbracket = lbracket + 1End IfIf lbracket = rbracket And lbracket <> 0 Thennuml = Mid(Expr, i, Where - i)Exit ForEnd IfNext iElse '-------------无括号For i = Where - 1 To 1 Step -1numl = Mid(Expr, i, 1)If numl = "+" Or numl = "-" Or numl = "*" Or numl = "/" Or numl = "(" Then numl = Mid(Expr, i + 1, Where - i - 1)Exit ForEnd IfIf i = 1 Thennuml = Mid(Expr, 1, Where - 1)Exit ForEnd IfNext iEnd Ifleftfind = numlEnd FunctionPrivate Function rightfind(ByV al Expr As String, Where As Long) As String Dim i, leftbracket, rbracket As IntegerDim numr As StringIf Mid(Expr, Where + 1, 1) = "(" Then '-------------右有括号For i = Where + 1 To Len(Expr)If Mid(Expr, i, 1) = ")" Thenrbracket = rbracket + 1ElseIf Mid(Expr, i, 1) = "(" Thenlbracket = lbracket + 1End IfIf lbracket = rbracket And lbracket <> 0 Thennumr = Mid(Expr, Where + 1, i - Where)Exit ForEnd IfNext iElse '-------------无括号For i = Where + 1 To Len(Expr)numr = Mid(Expr, i, 1)If numr = "+" Or numr = "-" Or numr = "*" Or numr = "/" Or numl = "(" Then numr = Mid(Expr, Where + 1, i - Where - 1)Exit ForEnd IfIf i = Len(Expr) Thennumr = Mid(Expr, Where + 1, i - Where)Exit ForEnd IfNext iEnd Ifrightfind = numrEnd FunctionPrivate Sub jingdian_Click(Index As Integer)Frame1.BackColor = &H8080FFFrame2.BackColor = &H80FF80Frame3.BackColor = &HFF80FFText2.BackColor = &H80FF80For i = 0 To 11Label1(i).BackColor = &HFF80FFNext ijingdian(0).Enabled = Falsechuantong(1).Enabled = Truepinhong(2).Enabled = TrueEnd SubPrivate Sub chuantong_Click(Index As Integer)Frame1.BackColor = &H8000000FFrame2.BackColor = &H8000000FFrame3.BackColor = &H8000000FText2.BackColor = &H8000000FFor i = 0 To 11Label1(i).BackColor = &H8000000FNext ijingdian(0).Enabled = Truechuantong(1).Enabled = Falsepinhong(2).Enabled = TrueEnd SubPrivate Sub pinhong_Click(Index As Integer)Frame1.BackColor = &HFF80FFFrame2.BackColor = &HFF80FFFrame3.BackColor = &HFF80FFText2.BackColor = &HFF80FFFor i = 0 To 11Label1(i).BackColor = &HFF80FFNext ijingdian(0).Enabled = Truechuantong(1).Enabled = Truepinhong(2).Enabled = FalseEnd SubPrivate Sub Form_Load()A = "0":B = "0": D = "0"Sto = False: Bo = FalseText1.Text = "0"Text2.Text = "青春风铃欢迎您的使用!"jingdian(0).Enabled = FalseEnd Sub'-------------------------------------------------状态栏代码--------------------------------------------------------Private Sub Frame1_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single) StatusBar1.Panels(2).Text = "数字键"End SubPrivate Sub Frame2_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single) StatusBar1.Panels(2).Text = "运算符"End SubPrivate Sub Frame3_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single) StatusBar1.Panels(2).Text = "功能区,选中Shift时执行附加功能"End SubPrivate Sub Command4_MouseMove(Index As Integer, Button As Integer, Shift As Integer, X As Single, Y As Single)Select Case IndexCase 0StatusBar1.Panels(2).Text = "退格"Case 1StatusBar1.Panels(2).Text = "清除"Case 2StatusBar1.Panels(2).Text = "左括号"Case 3StatusBar1.Panels(2).Text = "右括号"Case 4StatusBar1.Panels(2).Text = "等于号"End SelectEnd SubPrivate Sub Text1_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single) StatusBar1.Panels(2).Text = "风铃计算表达式"End SubPrivate Sub Text2_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single) StatusBar1.Panels(2).Text = "风铃计算结果"End SubPrivate Sub Check1_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single) StatusBar1.Panels(2).Text = "功能转换键"End SubPrivate Sub Check2_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single) StatusBar1.Panels(2).Text = "选中为角度模式,否则为弧度模式"End Sub'---------------------数字键的输入----------------------------Private Sub Command1_Click(Index As Integer)If Time <> 1 ThenText1.Text = "" '清空表达式Time = 1End IfIf Index <= 9 ThenText1.Text = Text1.Text & IndexElseIf Index = 10 ThenText1.Text = Text1.Text & "."ElseText1.Text = Text1.Text & "pi"End IfEnd Sub'---------------------运算符的输入----------------------------Private Sub Command2_Click(Index As Integer)If Time = 0 ThenText1.Text = ""ElseIf Time = 2 ThenText1.Text = "Ans"End IfTime = 1Select Case IndexCase 0Text1.Text = Text1.Text & "+"Case 1Text1.Text = Text1.Text & "-"Case 2Text1.Text = Text1.Text & "*"Case 3Text1.Text = Text1.Text & "/"End SelectEnd Sub'---------------------函数功能的输入----------------------------Private Sub Command3_Click(Index As Integer)'------前处理-------If Index <= 2 Or (Index <= 11 And Index >= 8 And Check1.V alue = 0) Then If Time = 2 ThenText1.Text = "Ans" '引用结果End IfElseIf Time <> 1 ThenText1.Text = "" '清空表达式End IfEnd If'------附加功能-------If Check1.V alue = 0 ThenSelect Case IndexCase 8 '1/xText1.Text = Text1.Text & "^-1"Case 9 'ncrText1.Text = Text1.Text & "C"Case 10 'nprText1.Text = Text1.Text & "P"Case 11 'x!Text1.Text = Text1.Text & "!"End SelectElseIf Check1.V alue = 1 And Sto = False Then If Time <> 1 And Index = 11 ThenText1.Text = "Ans"End IfSelect Case IndexCase 8If Time <> 1 ThenText1.Text = "A="Text2.Text = AElseText1.Text = Text1.Text & "A"End IfCase 9If Time <> 1 ThenText1.Text = "B="Text2.Text = BElseText1.Text = Text1.Text & "B"End IfCase 10If Time <> 1 ThenText1.Text = "D="Text2.Text = DElseText1.Text = Text1.Text & "D"End IfCase 11If Time <> 1 ThenText1.Text = "Ans→"ElseText1.Text = Text1.Text & "→"End IfSto = TrueEnd SelectElse 'check1.value=1 and sto=1Select Case IndexCase 8Text1.Text = Text1.Text & "A"Case 9Text1.Text = Text1.Text & "B"Case 10Text1.Text = Text1.Text & "D"End SelectBo = TrueEnd IfIf Bo = True ThenBo = FalseCommand4_Click (4)End If'------基本功能输入-------If Check1.V alue = 0 ThenSelect Case IndexCase 0 '幂运算Text1.Text = Text1.Text & "^"Case 1 '平方Text1.Text = Text1.Text & "^2"Case 2 '立方Text1.Text = Text1.Text & "^3"Case 3 'logText1.Text = Text1.Text & "ln("Case 4 'sinText1.Text = Text1.Text & "sin("Case 5 'cosText1.Text = Text1.Text & "cos("Case 6 'tanText1.Text = Text1.Text & "tan("Case 7 'lgText1.Text = Text1.Text & "lg("End SelectElseSelect Case IndexCase 0 '根式运算Text1.Text = Text1.Text & "Rn("Case 1 '平方根Text1.Text = Text1.Text & "^(1/2)"Case 2 '立方根Text1.Text = Text1.Text & "^(1/3)"Case 3 'e^xText1.Text = Text1.Text & "e^("Case 4 'asinText1.Text = Text1.Text & "asin("Case 5 'acosText1.Text = Text1.Text & "acos("Case 6 'tanText1.Text = Text1.Text & "atn("Case 7 'lnText1.Text = Text1.Text & "10^("End SelectEnd IfTime = 1End Sub'---------------------常用按钮及等号的代码---------------------------- Private Sub Command4_Click(Index As Integer)Dim Where As LongDim numl, numr, str As StringDim n, r As DoubleDim i, j, lbracket, rbracket As IntegerSelect Case IndexCase 0 '<-- 退格If Len(Text1.Text) >= 2 ThenText1.Text = Left(Text1.Text, Len(Text1.Text) - 1)Time = 1ElseText1.Text = "0"Time = 0End IfCase 1 'AC 清零Text1.Text = "0"Text2.Text = "0"Time = 0sum = 0Case 2 '( 号If Time <> 1 ThenText1.Text = "" '清空表达式Time = 1End IfText1.Text = Text1.Text & "("Case 3 ') 号If Time = 0 ThenText1.Text = "" '清空表达式Time = 1End IfText1.Text = Text1.Text & ")"Case 4 '= 号Expr = Replace(Text1.Text, "pi", "3.14159265358979323846264338327950288419716939937510")Expr = Replace(Expr, "Ans", Text2.Text)Expr = Replace(Expr, " ", "")Expr = Replace(Expr, "=", "")Where = InStr(Expr, "→")If Where <> 0 ThenCha = Right(Expr, 1)Expr = Left(Expr, Len(Expr) - 2)End IfExpr = Replace(Expr, "A", A)Expr = Replace(Expr, "B", B)Expr = Replace(Expr, "D", D)'-------处理括号不足问题----------For i = 1 To Len(Expr)If Mid(Expr, i, 1) = ")" Thenrbracket = rbracket + 1ElseIf Mid(Expr, i, 1) = "(" Thenlbracket = lbracket + 1End IfNext iIf lbracket < rbracket ThenExpr = String(rbracket - lbracket, "(") & ExprElseIf lbracket > rbracket ThenExpr = Expr & String(lbracket - rbracket, ")")End IfSet Sc = CreateObject("ScriptControl")nguage = "VBScript"'---------------------处理acos----------------------------For j = 1 To Len(Expr)Where = InStr(Expr, "acos")If Where <> 0 ThenWhere = Where + 3numr = rightfind(Expr, Where)str = "acos" & numrOn Error GoTo eh1r = CDbl(Sc.Eval(numr))If Check2.V alue = 1 Thenr = (Atn(-r / Sqr(-r * r + 1)) + 2 * Atn(1)) * 45 / Atn(1)ElseIf Check2.V alue = 0 Thenr = Atn(-r / Sqr(-r * r + 1)) + 2 * Atn(1)End IfExpr = Replace(Expr, str, CStr(r))i = 0: numl = "": n = 0: r = 0: Where = 0: str = ""ElseExit ForEnd IfNext j'---------------------处理asin----------------------------For j = 1 To Len(Expr)Where = InStr(Expr, "asin")If Where <> 0 ThenWhere = Where + 3numr = rightfind(Expr, Where)str = "asin" & numrOn Error GoTo eh1r = CDbl(Sc.Eval(numr))If Check2.V alue = 1 Thenr = Atn(r / Sqr(-r * r + 1)) * 45 / Atn(1)ElseIf Check2.V alue = 0 Thenr = Atn(r / Sqr(-r * r + 1))End IfExpr = Replace(Expr, str, CStr(r))i = 0: numl = "": n = 0: r = 0: Where = 0: str = ""ElseExit ForEnd IfNext j'----------------------处理阶乘!--------------------------------------For j = 1 To Len(Expr)Where = InStr(Expr, "!")If Where <> 0 Then '-------------有阶乘numl = leftfind(Expr, Where)str = numl & "!"On Error GoTo eh1n = CDbl(Sc.Eval(numl))n = Fact(Fix(n))Expr = Replace(Expr, str, CStr(n))i = 0: numl = "": n = 0: r = 0: Where = 0: str = ""ElseExit ForEnd IfNext j'----------------------处理排列--------------------------------------For j = 1 To Len(Expr)Where = InStr(Expr, "P")If Where <> 0 Then '-------------有排列numl = leftfind(Expr, Where)numr = rightfind(Expr, Where)str = numl & "P" & numrOn Error GoTo eh1n = CDbl(Sc.Eval(numl))r = CDbl(Sc.Eval(numr))If r > n ThenGoTo eh1End Ifn = Fact(Fix(n)) / Fact(Fix(n - r))Expr = Replace(Expr, str, CStr(n))i = 0: numl = "": n = 0: r = 0: Where = 0: str = ""ElseExit ForEnd IfNext j'----------------------处理组合--------------------------------------For j = 1 To Len(Expr)Where = InStr(Expr, "C")If Where <> 0 Then '-------------有组合numl = leftfind(Expr, Where)numr = rightfind(Expr, Where)str = numl & "C" & numrOn Error GoTo eh1n = CDbl(Sc.Eval(numl))r = CDbl(Sc.Eval(numr))If r > n ThenGoTo eh1End Ifn = Fact(Fix(n)) / Fact(Fix(r)) / Fact(Fix(n - r))Expr = Replace(Expr, str, CStr(n))i = 0: numl = "": n = 0: r = 0: Where = 0: str = ""ElseExit ForEnd IfNext j'----------------------处理其他情况--------------------------------------Expr = Replace(Expr, "Rn(", "^(1/")If Check2.V alue = 1 ThenExpr = Replace(Expr, "atn(", "1/atn(1)*45*atn(")Expr = Replace(Expr, "sin(", "sin(atn(1)/45*")Expr = Replace(Expr, "cos(", "cos(atn(1)/45*")Expr = Replace(Expr, "tan(", "tan(atn(1)/45*")End IfExpr = Replace(Expr, "e^(", "exp(1)^(")Expr = Replace(Expr, "ln(", "log(")Expr = Replace(Expr, "lg(", "1/log(10)*log(")On Error GoTo eh1sum = Sc.Eval(Expr)If Sto = True ThenSelect Case ChaCase "A"A = CDbl(sum)Case "B"B = CDbl(sum)Case "D"D = CDbl(sum)Case "B"End SelectSto = FalseEnd IfText2.Text = sumTime = 2End Select '对应=Exit Subeh1:Text1.Text = "^_^:运行错误,风铃计算表达式不合语法规则!" End Sub。
简单科学计算器vb程序

’//本人写的原始程序'注意没法实现如下功能:10+===然后输入10再=。
此程序会结果出错Dim denghao, fenghao As Boolean 'denghao判断是否按下“=”,fenghao判断是否按下“1/X”Dim xiaoshu, biaoji As Boolean 'xiaoshu判断是否已按“.”,biaoji第一次判断前面是否按过“+”“-”等运算符按钮Dim fuhao As Byte '运算符类型标号Dim leixin As Byte '进制转换类型Dim fistnumber, last As Double 'fistnumber第一个数据,最后一个数据Dim panduan, jinzhi As Boolean 'panduan判断前面是否连续按“+”“-”等运算符按钮Dim denglianxu As Boolean 'denglianxu判断前面是否连续安"="Dim m, n, i, sum As DoubleDim pi As Double 'sin,cosDim kuahao As Boolean '判断是否按下“(”Dim si As StringDim shilu, bajin As Boolean '十六进制下才能输入A-F;八进制下8-9不能输入Dim h, d, o, b As Integer '进制Dim se, s As DoubleDim ejin As Boolean '判断2-9,在二进制下不能输入Private Sub baifeng_Click() '1/X的程序If Not biaoji Thenfuhao = 7If Val(Text1.Text) = "0" ThenText1.Text = "除数不能为零"ElseCall jieguoEnd IfElseCall jieguofuhao = 7Call jieguoEnd Iffenghao = Truexiaoshu = FalseEnd SubPrivate Sub clear_Click()If clear.Value = 1 Then Text1.Text = "0"xiaoshu = Falseclear.Value = 0sum = 1biaoji = Falsepanduan = Falsedenghao = Falselast = 0fistnumber = 0denglianxu = FalseX = 0si = ""i = 0sum = 0shilu = Falsebajin = Falseejin = FalseEnd SubPrivate Sub Command11_Click() '+/-号Text1.Text = -1 * Val(Text1.Text)If Left(Val(Text1.Text), 1) = "." Then Text1.Text = "0" + Text1.Text If Left(Val(Text1.Text), 2) = "-." ThenText1.Text = -1 * Val(Text1.Text)Text1.Text = "-0" + Text1.TextEnd Ifpanduan = Truedenglianxu = Falsedenghao = TrueEnd SubPrivate Sub Command0_Click()If Text1.Text = "0" ThenText1.Text = 0ElseIf Not denghao And Not fenghao ThenIf Not kuahao Then Text1.Text = Text1.Text + "0"ElseText1.Text = "0"denglianxu = Falsefenghao = Falsedenghao = Falsepanduan = FalseEnd IfEnd SubPrivate Sub Command1_Click()If Text1.Text = "0" Then Text1.Text = ""If Not denghao And Not fenghao ThenIf Not kuahao Then Text1.Text = Text1.Text + "1" ElseText1.Text = "1"denglianxu = Falsefenghao = Falsedenghao = Falsepanduan = Falsekuahao = FalseEnd IfEnd SubPrivate Sub Command2_Click()If Text1.Text = "0" Then Text1.Text = ""If ejin ThenMsgBox "请输入二进制数"Text1.Text = 0Exit SubEnd IfIf Not denghao And Not fenghao ThenIf Not kuahao Then Text1.Text = Text1.Text + "2" ElseText1.Text = "2"fenghao = Falsedenghao = Falsepanduan = Falsedenglianxu = Falsekuahao = FalseEnd IfEnd SubPrivate Sub Command3_Click()If Text1.Text = "0" Then Text1.Text = ""If ejin ThenMsgBox "请输入二进制数"Text1.Text = 0Exit SubEnd IfIf Not denghao And Not fenghao ThenIf Not kuahao Then Text1.Text = Text1.Text + "3" ElseText1.Text = "3"fenghao = Falsedenghao = Falsepanduan = Falsedenglianxu = Falsekuahao = FalseEnd IfEnd SubPrivate Sub Command4_Click()If Text1.Text = "0" Then Text1.Text = ""If ejin ThenMsgBox "请输入二进制数"Text1.Text = 0Exit SubEnd IfIf Not denghao And Not fenghao ThenIf Not kuahao Then Text1.Text = Text1.Text + "4" ElseText1.Text = "4"fenghao = Falsedenghao = Falsepanduan = Falsekuahao = FalseEnd Ifdenglianxu = FalseEnd SubPrivate Sub Command5_Click()If Text1.Text = "0" Then Text1.Text = ""If ejin ThenMsgBox "请输入二进制数"Exit SubEnd IfIf Not denghao And Not fenghao ThenIf Not kuahao Then Text1.Text = Text1.Text + "5" ElseText1.Text = "5"denghao = Falsefenghao = Falsepanduan = Falsekuahao = FalseEnd Ifdenglianxu = FalseEnd SubPrivate Sub Command6_Click()If Text1.Text = "0" Then Text1.Text = ""If ejin ThenMsgBox "请输入二进制数"Text1.Text = 0Exit SubEnd IfIf Not denghao And Not fenghao ThenIf Not kuahao Then Text1.Text = Text1.Text + "6" ElseText1.Text = "6"denghao = Falsefenghao = Falsepanduan = Falsekuahao = FalseEnd Ifdenglianxu = FalseEnd SubPrivate Sub Command7_Click()If Text1.Text = "0" Then Text1.Text = ""If ejin ThenMsgBox "请输入二进制数"Text1.Text = 0Exit SubEnd IfIf Not denghao And Not fenghao ThenIf Not kuahao Then Text1.Text = Text1.Text + "7" Elsedenghao = Falsefenghao = Falsepanduan = Falsekuahao = FalseEnd Ifdenglianxu = FalseEnd SubPrivate Sub Command8_Click()If Text1.Text = "0" Then Text1.Text = ""If bajin ThenMsgBox "请输入八进制数"Text1.Text = 0Exit SubEnd IfIf ejin ThenMsgBox "请输入二进制数"Text1.Text = 0Exit SubEnd IfIf Not denghao And Not fenghao ThenIf Not kuahao Then Text1.Text = Text1.Text + "8" ElseText1.Text = "8"denghao = Falsefenghao = Falsepanduan = Falsekuahao = FalseEnd Ifdenglianxu = FalseEnd SubPrivate Sub Command9_Click()If Text1.Text = "0" Then Text1.Text = ""If bajin ThenMsgBox "请输入八进制数"Text1.Text = 0Exit SubEnd IfIf ejin ThenMsgBox "请输入二进制数"Text1.Text = 0Exit SubIf Not denghao And Not fenghao ThenIf Not kuahao Then Text1.Text = Text1.Text + "9" ElseText1.Text = "9"denghao = Falsefenghao = Falsepanduan = Falsekuahao = FalseEnd Ifdenglianxu = FalseEnd SubPrivate Sub Command17_Click(Index As Integer) If shilu ThenSelect Case IndexCase 0If Text1.Text = "0" Then Text1.Text = ""If Not denghao And Not fenghao ThenText1.Text = Text1.Text + "A"ElseText1.Text = "A"denghao = Falsefenghao = Falsepanduan = FalseEnd IfCase 1If Text1.Text = "0" Then Text1.Text = ""If Not denghao And Not fenghao ThenText1.Text = Text1.Text + "B"ElseText1.Text = "B"denghao = Falsefenghao = Falsepanduan = FalseEnd IfCase 2If Text1.Text = "0" Then Text1.Text = ""If Not denghao And Not fenghao ThenText1.Text = Text1.Text + "C"ElseText1.Text = "C"denghao = Falsefenghao = Falsepanduan = FalseCase 3If Text1.Text = "0" Then Text1.Text = "" If Not denghao And Not fenghao Then Text1.Text = Text1.Text + "D"ElseText1.Text = "D"denghao = Falsefenghao = Falsepanduan = FalseEnd IfCase 4If Text1.Text = "0" Then Text1.Text = "" If Not denghao And Not fenghao Then Text1.Text = Text1.Text + "E"ElseText1.Text = "E"denghao = Falsefenghao = Falsepanduan = FalseEnd IfCase 5If Text1.Text = "0" Then Text1.Text = "" If Not denghao And Not fenghao Then Text1.Text = Text1.Text + "F"ElseText1.Text = "F"denghao = Falsefenghao = Falsepanduan = FalseEnd IfEnd SelectElseMsgBox "无效函数"End IfEnd SubPrivate Sub eee_Click() 'e为底的指数fuhao = 5Call jieguoxiaoshu = FalsePrivate Sub lne_Click() 'e为底的对数If Text1.Text > 0 Thenfuhao = 6Call jieguoElseMsgBox "函数输入无效", vbRetryCancel, "信息提示" End Ifxiaoshu = FalseEnd SubPrivate Sub exit_Click()EndEnd SubPrivate Sub Form_Load()biaoji = FalseText1.Text = 0sum = 1si = ""xiaoshu = Falsepaiduan = Falsedenghao = Falsedenglianxu = Falseerjin = Falsed = 1i = 0bajin = Falseejin = False'x = 0End SubPrivate Sub jiahao_Click()If Not biaoji Thenfistnumber = Val(Text1.Text)ElseIf Not panduan ThenCall jieguofistnumber = Val(Text1.Text)ElseText1.Text = Val(Text1.Text)End Iffuhao = 1xiaoshu = Falsebiaoji = Truedenghao = Truepanduan = Truedenglianxu = FalseEnd SubPrivate Sub jianhao_Click()If Not biaoji Thenfistnumber = Val(Text1.Text)ElseIf Not panduan ThenCall jieguofistnumber = Val(Text1.Text)ElseText1.Text = Val(Text1.Text)End Iffuhao = 2xiaoshu = Falsedenghao = Truepanduan = Truebiaoji = Truedenglianxu = FalseEnd SubPrivate Sub chenghao_Click()If Not biaoji Then '没有按下运算符fistnumber = Val(Text1.Text)ElseIf Not panduan Then '没有连续按运算按钮Call jieguofistnumber = Val(Text1.Text)ElseText1.Text = Val(Text1.Text)End Iffuhao = 3xiaoshu = Falsebiaoji = Truedenghao = Truepanduan = Truedenglianxu = FalseEnd SubPrivate Sub chuhao_Click()If Not biaoji Thenfistnumber = Val(Text1.Text)ElseIf Not panduan ThenCall jieguofistnumber = Val(Text1.Text)ElseText1.Text = Val(Text1.Text)End Iffuhao = 4xiaoshu = Falsebiaoji = Truedenghao = Truepanduan = Truedenglianxu = FalseEnd SubPrivate Sub Command10_Click() '加“(”括号m = fuhaon = fistnumberbiaoji = FalseText1.Text = "("kuahao = FalseEnd SubPrivate Sub Com_Click() '加“)”括号Call jieguofistnumber = nfuhao = mpanduan = Falsefenghao = Truexiaoshu = FalseEnd SubPrivate Sub result_Click()If panduan Then '有两个以上连续的运算符If Not denglianxu Then '只按了一个等号Text1.Text = Val(Text1.Text)Call jieguoElse '按两个以上的等号Call jianchuEnd IfElse '没有连续运算符(运算符在两个数中间)'Call xxxdenglianxu = FalseCall jieguoEnd IfEnd SubPrivate Sub point_Click()If Not denghao And Not fenghao ThenIf Not kuahao ThenIf Text1.Text = "." Then Text1.Text = "0."End IfIf Not xiaoshu ThenText1.Text = Text1.Text + "."xiaoshu = TrueEnd IfElseText1.Text = "0."denghao = Falsefenghao = FalseEnd IfEnd SubPublic Sub jianchu() '多个等号连续时调用last = Val(Text1.Text)Text1.Text = fistnumberIf fuhao = 1 Then Text1.Text = last + Text1.TextIf fuhao = 2 Then Text1.Text = last - Text1.TextIf fuhao = 3 Then Text1.Text = last * Text1.TextIf fuhao = 4 ThenIf Val(Text1.Text) = "0" ThenText1.Text = "除数不能为零"ElseText1.Text = last / Val(Text1.Text)End IfEnd IfIf Left(Val(Text1.Text), 1) = "." Then Text1.Text = "0" + Text1.TextEnd SubPrivate Sub jiecheng_Click() '阶乘Dim n As Integer, Y As Double, i As Integer 'double和variant变量最多算到170!long最多算到12!If Val(Text1.Text) < 0 ThenText1.Text = "函数输入无效"ElseIf Val(Text1.Text) >= 171 ThenText1.Text = "超出本计算器计算范围"Elsen = Val(Text1.Text)Y = 1For i = 1 To nY = Y * iNext iText1.Text = Str(Y)End IfEnd SubPrivate Sub duishu_Click() '对数If Val(Text1.Text) <= 0 Or V al(Text1.Text) = 1 ThenText1.Text = "函数输入无效"ElseIf Not biaoji Thenfistnumber = Val(Text1.Text)ElseIf Not panduan ThenCall jieguofistnumber = Val(Text1.Text)ElseText1.Text = Val(Text1.Text)End Iffuhao = 9xiaoshu = Falsebiaoji = Truedenghao = Truepanduan = Truedenglianxu = FalseEnd SubPrivate Sub zhishu_Click() '指数If Not biaoji Thenfistnumber = Val(Text1.Text) ElseIf Not panduan ThenCall jieguofistnumber = Val(Text1.Text)ElseText1.Text = Val(Text1.Text)End Iffuhao = 8xiaoshu = Falsebiaoji = Truedenghao = Truepanduan = Truedenglianxu = FalseEnd SubPrivate Sub yuxuan_Click() '余弦fuhao = 10Call jieguoEnd SubPrivate Sub zhengxuan_Click() '正弦fuhao = 11Call jieguoEnd SubPrivate Sub Option1_Click() '16 leixin = 1shilu = Truebajin = Falseejin = FalseCall zhuanhuand = 0o = 0b = 0denghao = TrueEnd SubPrivate Sub Option2_Click() '10 leixin = 2shilu = Falsebajin = Falseejin = FalseCall zhuanhuand = 1h = 0o = 0b = 0denghao = TrueEnd SubPrivate Sub Option3_Click() '8 bajin = Trueleixin = 3shilu = Falseejin = FalseCall zhuanhuano = 1h = 0d = 0b = 0denghao = TrueEnd SubPrivate Sub Option4_Click() '2 leixin = 4ejin = Trueshilu = Falsebajin = FalseCall zhuanhuanb = 1h = 0o = 0denghao = TrueEnd SubPublic Sub zhuanhuan() '进制转换If leixin = 1 ThenIf d = 1 Then Text1.Text = Hex(Text1.Text) '十进制转换成十六进制If o = 1 Then Text1.Text = Hex("&o" & Text1.Text) '八进制转换成十六进制If b = 1 ThenCall ezsText1.Text = Hex(Text1.Text) '二进制转换成十六进制End IfEnd IfIf leixin = 2 ThenIf h = 1 Then Text1.Text = CLng("&h" & Text1.Text) '十六进制转换成十进制If o = 1 Then Text1.Text = CLng("&o" & Text1.Text) '八进制转换成十进制If b = 1 Then Call ezs '二进制转换成十进制End IfIf leixin = 3 Then'If Val(Text1.Text) > 7 Then'MsgBox "请输入八进制数"'Text1.Text = 0'Exit Sub'End If'If Val(Text1.Text) >= 0 And Val(Text1.Text) <= 32767 ThenIf h = 1 Then Text1.Text = Oct("&h" & Text1.Text) '十六进制转换成八进制If d = 1 Then Text1.Text = Oct$(Text1.Text) '十进制转换成八进制If b = 1 ThenCall ezsText1.Text = Oct$(Text1.Text) '二进制转换成八进制End If'Else'MsgBox "请输Integer类型的正数"'Text1.Text = 0'End IfEnd IfIf leixin = 4 ThenIf h = 1 Then '十六进制转换成二进制Text1.Text = CLng("&h" & Text1.Text)Call szeEnd IfIf d = 1 Then Call sze '十进制转换成二进制If o = 1 Then '八进制转换成二进制Text1.Text = CLng("&o" & Text1.Text)Call szeEnd IfIf b = 1 Then '二进制End IfEnd IfEnd SubPublic Sub jieguo() '计算过程和结果If fuhao = 1 ThenText1.Text = fistnumber + V al(Text1.Text)End IfIf fuhao = 2 ThenText1.Text = fistnumber - Val(Text1.Text)End IfIf fuhao = 3 ThenText1.Text = fistnumber * Val(Text1.Text)End IfIf fuhao = 4 ThenIf Val(Text1.Text) = "0" ThenText1.Text = "除数不能为零"ElseText1.Text = fistnumber / Val(Text1.Text)End IfEnd IfIf fuhao = 5 Then Text1.Text = Exp(Val(Text1.Text))If fuhao = 6 Then Text1.Text = Str(Log(Val(Text1.Text))) If fuhao = 7 Then Text1.Text = 1 / Val(Text1.Text)If fuhao = 8 Then '指数Text1.Text = fistnumber ^ Val(Text1.Text)End IfIf fuhao = 9 Then '对数If Val(Text1.Text) = 1 ThenText1.Text = 0Elsesum = Log(Val(Text1.Text)) / Log(fistnumber)Text1.Text = sumEnd IfEnd IfIf fuhao = 10 Thenpi = Val(Text1.Text) / 90If Val(Text1.Text) Mod 90 = 0 And pi Mod 2 <> 0 Then '判断输入值是否为90的奇数倍数,使cos90直接赋为0Text1.Text = 0ElseText1.Text = Cos(Val(Text1.Text) * 3.14159265368979 / 180)End IfEnd IfIf fuhao = 11 Thenpi = Val(Text1.Text) / 90If Val(Text1.Text) Mod 90 = 0 And pi Mod 2 = 0 ThenText1.Text = 0ElseText1.Text = Sin(Val(Text1.Text) * 3.14159265368979 / 180)End IfEnd IfIf Left(Val(Text1.Text), 1) = "." Then Text1.Text = "0" + Text1.TextIf Left(Val(Text1.Text), 2) = "-." ThenText1.Text = -1 * Val(Text1.Text)Text1.Text = "-0" + Text1.TextEnd Ifdenghao = Truexiaoshu = Falsebiaoji = Falsedenglianxu = TrueEnd SubPublic Sub sze() '十进制转换成二进制si = "" '不初始化将出现错误Do Until Val(Text1.Text) = 0s = Val(Text1.Text) Mod 2Text1.Text = Val(Text1.Text) \ 2si = si + Str$(s) 'str$()数值转换成字符串LoopText1.Text = StrReverse(si) '将字符串反序End SubPublic Sub ezs() '二进制转换成十进制sum = 0i = 0 '不赋为零,会出现错误Do Until Val(Text1.Text) = 0se = Val(Text1.Text) Mod 10Text1.Text = Val(Text1.Text) \ 10i = i + 1sum = sum + se * 2 ^ (i - 1)LoopText1.Text = sumEnd Sub。
VB计算器程序代码

Public Class Form1Public zt As IntegerPublic fh As IntegerPublic x, y As SinglePrivate Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Loadzt = 1fh = 0TextBox1.Text = 0x = 0y = 0End SubPrivate Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.ClickIf zt = 1 ThenTextBox1.Text = TextBox1.Text * 10 + 1zt = 2ElseIf zt = 2 ThenTextBox1.Text = TextBox1.Text * 10 + 1ElseIf zt = 3 ThenTextBox1.Text = 1zt = 4ElseIf zt = 4 ThenTextBox1.Text = TextBox1.Text * 10 + 1End IfEnd SubPrivate Sub Button2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button2.ClickIf zt = 1 ThenTextBox1.Text = TextBox1.Text * 10 + 2zt = 2ElseIf zt = 2 ThenTextBox1.Text = TextBox1.Text * 10 + 2ElseIf zt = 3 ThenTextBox1.Text = 2zt = 4ElseIf zt = 4 ThenTextBox1.Text = TextBox1.Text * 10 + 2End IfEnd SubPrivate Sub Button3_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button3.ClickIf zt = 1 ThenTextBox1.Text = TextBox1.Text * 10 + 3zt = 2ElseIf zt = 2 ThenTextBox1.Text = TextBox1.Text * 10 + 3ElseIf zt = 3 ThenTextBox1.Text = 3zt = 4ElseIf zt = 4 ThenTextBox1.Text = TextBox1.Text * 10 + 3End IfEnd SubPrivate Sub Button11_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button11.ClickIf zt = 2 Thenx = TextBox1.Textfh = 1zt = 3ElseIf zt = 3 Thenfh = 1End IfEnd SubPrivate Sub Button13_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button13.ClickIf zt = 2 Thenx = TextBox1.Textfh = 3zt = 3ElseIf zt = 3 Thenfh = 3End IfEnd SubPrivate Sub Button12_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button12.ClickIf zt = 2 Thenx = TextBox1.Textfh = 2zt = 3ElseIf zt = 3 Thenfh = 2End IfEnd SubPrivate Sub Button14_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button14.ClickIf zt = 2 Thenx = TextBox1.Textfh = 4zt = 3ElseIf zt = 3 Thenfh = 4End IfEnd SubPrivate Sub Button15_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button15.Clickx = 0y = 0TextBox1.Text = 0fh = 0zt = 1End SubPrivate Sub Button16_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button16.ClickIf zt = 4 Theny = TextBox1.TextIf fh = 1 ThenTextBox1.Text = x + yElseIf fh = 2 ThenTextBox1.Text = x - yElseIf fh = 3 ThenTextBox1.Text = x * yElseIf fh = 4 ThenTextBox1.Text = x / yEnd IfEnd IfPrivate Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.ClickIf zt = 1 ThenTextBox1.Text = TextBox1.Text * 10 + 4zt = 2ElseIf zt = 2 ThenTextBox1.Text = TextBox1.Text * 10 + 4ElseIf zt = 3 ThenTextBox1.Text = 4zt = 4ElseIf zt = 4 ThenTextBox1.Text = TextBox1.Text * 10 + 4End IfEnd SubPrivate Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.ClickIf zt = 1 ThenTextBox1.Text = TextBox1.Text * 10 + 5zt = 2ElseIf zt = 2 ThenTextBox1.Text = TextBox1.Text * 10 + 5ElseIf zt = 3 ThenTextBox1.Text = 5zt = 4ElseIf zt = 4 ThenTextBox1.Text = TextBox1.Text * 10 + 5End IfEnd SubPrivate Sub Button6_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button6.ClickIf zt = 1 ThenTextBox1.Text = TextBox1.Text * 10 + 6zt = 2ElseIf zt = 2 ThenTextBox1.Text = TextBox1.Text * 10 + 6ElseIf zt = 3 ThenTextBox1.Text = 6zt = 4ElseIf zt = 4 ThenTextBox1.Text = TextBox1.Text * 10 + 6End SubPrivate Sub Button7_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button7.ClickIf zt = 1 ThenTextBox1.Text = TextBox1.Text * 10 + 7zt = 2ElseIf zt = 2 ThenTextBox1.Text = TextBox1.Text * 10 + 7ElseIf zt = 3 ThenTextBox1.Text = 7zt = 4ElseIf zt = 4 ThenTextBox1.Text = TextBox1.Text * 10 + 7End IfEnd SubPrivate Sub Button8_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button8.ClickIf zt = 1 ThenTextBox1.Text = TextBox1.Text * 10 + 8zt = 2ElseIf zt = 2 ThenTextBox1.Text = TextBox1.Text * 10 + 8ElseIf zt = 3 ThenTextBox1.Text = 8zt = 4ElseIf zt = 4 ThenTextBox1.Text = TextBox1.Text * 10 + 8End IfEnd SubPrivate Sub Button9_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button9.ClickIf zt = 1 ThenTextBox1.Text = TextBox1.Text * 10 + 9zt = 2ElseIf zt = 2 ThenTextBox1.Text = TextBox1.Text * 10 + 9ElseIf zt = 3 ThenTextBox1.Text = 9zt = 4ElseIf zt = 4 ThenTextBox1.Text = TextBox1.Text * 10 + 9End IfEnd SubPrivate Sub Button10_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button10.ClickIf zt = 1 ThenTextBox1.Text = TextBox1.Text * 10 + 0zt = 2ElseIf zt = 2 ThenTextBox1.Text = TextBox1.Text * 10 + 0ElseIf zt = 3 ThenTextBox1.Text = 0zt = 4ElseIf zt = 4 ThenTextBox1.Text = TextBox1.Text * 10 + 0End IfEnd SubEnd Class。
计算器的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计算器程序代码

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代码

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
简单的VB计算器代码

简单的VB计算器代码:'变量定义说明部分Dim x1 As Single, x2 As SingleDim Y As SingleDim n As Integer '标记使用运算符类型'接收数据输入Private Sub Command1_Click(Index As Integer)Text1.Text = Text1.Text + CStr(Index)End Sub'按运算符时的处理方法Private Sub Command3_Click(Index As Integer)x1 = Val(Text1.Text)Text1.Text = ""n = IndexEnd Sub'输入小数点时的处理办法Private Sub Command2_Click()Text1.Text = Text1.Text + "."End Sub'单击退格按钮时的处理方法Private Sub Command4_Click()Text1.Text = Mid(Text1.Text, 1, Len(Text1.Text) - 1) End Sub'单击清除按钮时的处理方法Private Sub Command5_Click() x1 = 0: x2 = 0: Y = 0Text1.Text = ""End Sub'单击退出按钮时的处理方法Private Sub Command6_Click() Unload MeEnd Sub'单击"="按钮时的处理方法Private Sub Command7_Click() x2 = Val(Text1.Text)On Error GoTo err1Select Case nCase 0Y = x1 + x2Case 1Y = x1 - x2Case 2Y = x1 * x2Case 3Y = x1 / x2End SelectText1.Text = CStr(Y)Exit Suberr1:MsgBox "您输入的数据有误,请重新输入!", vbOKOnly + vbCritical, "错误!"Text1.Text = ""x1 = 0: x2 = 0: Y = 0Exit SubEnd Sub运行界面如下:。
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
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
Index = 5
Left = 840
TabIndex = 32
Top = 1455
Width = 630
End
Begin Cmd_Exp
Begin Frm_Scientific
Caption = "计算器"
ClientHeight = 2925
ClientLeft = 165
ClientTop = 735
ClientWidth = 5595
LinkTopic = "Form1"
LockControls = -1 'True
MaxButton = 0 'False
End
Begin Cmd_CE
Caption = "CE"
Height = 405
Left = 3975
TabIndex = 14
Top = 405
Width = 750
End
Begin Cmd_Backspace
Caption = "Backspace"
Height = 405
Left = 2820
Caption = "-"
End
Begin Menu_Exit
Caption = "退出"
End
End
Begin Menu_Help
Caption = "帮助(&H)"
Begin Menu_HelpSubject
Caption = "帮助主题(&H)"
End
Begin Menu_About
Caption = "关于计算器"
ScaleHeight = 2925
ScaleWidth = 5595
StartUpPosition = 3 '窗口缺省
Begin Cmd_Rnd
Caption = "Rnd"
Height = 390
Left = 5025
TabIndex = 39
Top = 1455
Width = 510
End
Begin Cmd_PI
TabIndex = 25
Top = 210
Width = 795
End
Begin Otn_Deg
Caption = "角度"
Height = 180
Left = 135
TabIndex = 24
Top = 210
Value = -1 'True
Width = 675
End
End
Begin Cmd_Operator
Begin Command1
Caption = "1"
Height = 390
Index = 1
Left = 2820
TabIndex = 1
Top = 1935
Width = 510
End
Begin Txt_Result
Alignment = 1 'Right Justify
Height = 315
Index = 3
Left = 3915
TabIndex = 3
Top = 1935
Width = 510
End
Begin Command1
Caption = "2"
Height = 390
Index = 2
Left = 3360
TabIndex = 2
Top = 1935
Width = 510
End
End
End
End
Attribute VB_Name = "Frm_Scientific"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Left = 90
Locked = -1 'True
TabIndex = 0
Text = "0."
Top = 0
Width = 5385
End
Begin Menu_Edit
Caption = "编辑(&E)"
Begin Menu_Copy
Caption = "复制"
Shortcut = ^C
End
Begin Menu_Paste
Top = 1935
Width = 630
End
Begin Cmd_Atan
Caption = "Atan"
Height = 390
Left = 120
TabIndex = 27
Top = 2400
Width = 630
End
Begin Frame1
Height = 510
Left = 90
TabIndex = 23
Top = 960
Width = 510
End
Begin Command1
Caption = "9"
Height = 390
Index = 9
Left = 3915
TabIndex = 9
Top = 960
Width = 510
End
Begin Command1
Caption = "8"
Height = 390
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
Caption = "Sqrt"
Height = 390
Left = 5025
TabIndex = 16
Top = 960
Width = 510
End
Begin Cmd_C
Caption = "C"
Height = 405
Left = 4785
TabIndex = 15
Top = 405
Width = 750
Caption = "6"
Height = 390
Index = 6
Left = 3915
TabIndex = 6
Top = 1455
Width = 510
End
Begin Command1
Caption = "5"
Height = 390
Index = 5
Left = 3360
TabIndex = 5
End
Begin Cmd_Log
Caption = "Log"
Height = 390
Left = 1575
TabIndex = 36
Top = 1455
Width = 630
End
Begin Cmd_Ln
Caption = "Ln"
Height = 390
Left = 1575
TabIndex = 35
Dim i As Integer
Dim opnre As Integer
Dim prev As Double
Dim oflag As Integer