vb编写的计算器代码

合集下载

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简易计算器代码

VB简易计算器代码

VB简易计算器代码下面是一个简单的VB计算器代码,用于执行基本的加、减、乘、除运算。

```vbOption Strict OnPublic Class CalculatorPrivate Sub btnAdd_Click(sender As Object, e As EventArgs) Handles btnAdd.ClickDim num1 As Double = CDbl(txtNum1.Text)Dim num2 As Double = CDbl(txtNum2.Text)Dim result As Double = num1 + num2txtResult.Text = result.ToStringEnd SubPrivate Sub btnSubtract_Click(sender As Object, e As EventArgs) Handles btnSubtract.ClickDim num1 As Double = CDbl(txtNum1.Text)Dim num2 As Double = CDbl(txtNum2.Text)Dim result As Double = num1 - num2txtResult.Text = result.ToStringPrivate Sub btnMultiply_Click(sender As Object, e As EventArgs) Handles btnMultiply.ClickDim num1 As Double = CDbl(txtNum1.Text)Dim num2 As Double = CDbl(txtNum2.Text)Dim result As Double = num1 * num2txtResult.Text = result.ToStringEnd SubPrivate Sub btnDivide_Click(sender As Object, e As EventArgs) Handles btnDivide.ClickDim num1 As Double = CDbl(txtNum1.Text)Dim num2 As Double = CDbl(txtNum2.Text)If num2 = 0 ThenMessageBox.Show("除数不能为0!", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error)ElseDim result As Double = num1 / num2txtResult.Text = result.ToStringEnd IfEnd Sub```此代码创建了一个简单的窗体应用程序,其中包含两个文本框用于输入两个数字,四个按钮用于执行不同的计算操作,以及一个文本框用于显示结果。

VB简单计算器(截图附代码)

VB简单计算器(截图附代码)

VB程序简单计算器最近学完VB,感觉很好,写了个计算器程序,虽然花了不少时间,可也着实高兴。

其中遇到很多问题,最终也在各种资料中得到解决。

现在附上截图和全部代码,希望和大家交流一下,相互学习。

也希望能帮助到准备做计算器的同学。

计算器最终执行文件图标:计算器包括三个窗体(form):主页面form1:其中的“欢迎各位到此一游”是闪烁效果,呵呵,是自己想着无聊,就想出这么个玩意。

与计算器计算功能无关。

具体实现看下来代码。

Form1的实现代码:Public haha As BooleanPrivate Sub Command1_Click()Dim a As Integera = MsgBox("亲爱的你,真的想要退出本系统吗?", _vbYesNo + vbInformation + vbDefaultButton1, "退出系统前的询问撒(⊙o⊙)")If a = 6 Then '表示当选择“是”的时候的返回值EndEnd IfEnd SubPrivate Sub Command2_Click()Me.HideForm2.ShowEnd SubPrivate Sub Command3_Click()Me.HideForm3.ShowEnd SubPrivate Sub Form_Load()haha = FalseEnd SubPrivate Sub Timer1_Timer()haha = Not hahaIf haha ThenLabel2.ForeColor = &HFF00FFElseLabel2.ForeColor = vbWhiteEnd IfEnd Sub有些像图像等一些可见的控件属性就没在代码里写了,直接在属性里设置了。

页面(form2):此页面将鼠标点上去,还有意想不到的效果喲。

全部代码为:Dim isFocus1, isFocus2 As BooleanPrivate Sub Command1_Click()If isFocus1 Then '判断焦点在那个文本框中,便于实现按钮输入Text1.Text = Text1.Text & 0End IfIf isFocus2 ThenText2.Text = Text2.Text & 0End IfEnd SubPrivate Sub Command10_Click() If isFocus1 ThenText1.Text = Text1.Text & 9 End IfIf isFocus2 ThenText2.Text = Text2.Text & 9 End IfEnd SubPrivate Sub Command11_Click() If isFocus1 ThenText1.Text = Text1.Text & "." End IfIf isFocus2 ThenText2.Text = Text2.Text & "." End IfEnd SubPrivate Sub Command12_Click() If isFocus1 ThenText1.Text = -Val(Text1.Text)End IfIf isFocus2 ThenText2.Text = -Val(Text2.Text)End IfEnd SubPrivate Sub Command13_Click()Dim a As Integera = Val(Text1.Text) + Val(Text2.Text)Text3.Text = Val(Text1.Text) & "+" & Val(Text2.Text) & "=" & aIf Text1.Text = "" Or Text2.Text = "" ThenMsgBox "哼哼,双目运算一定要是两个数喔" & vbCrLf & vbCrLf & _ "不用说,补上去是必须的撒", vbCritical, "温馨小提醒(*^__^*)" End IfEnd SubPrivate Sub Command14_Click()Dim a As Integera = Val(Text1.Text) - Val(Text2.Text)Text3.Text = Val(Text1.Text) & "-" & Val(Text2.Text) & "=" & aIf Text1.Text = "" Or Text2.Text = "" ThenMsgBox "哼哼,双目运算一定要是两个数喔" & vbCrLf & vbCrLf & _ "不用说,补上去是必须的撒", vbCritical, "温馨小提醒(*^__^*)" End IfEnd SubPrivate Sub Command15_Click()a = Val(Text1.Text) * Val(Text2.Text)Text3.Text = Val(Text1.Text) & "×" & Val(Text2.Text) & "=" & aIf Text1.Text = "" Or Text2.Text = "" ThenMsgBox "哼哼,双目运算一定要是两个数喔" & vbCrLf & vbCrLf & _ "不用说,补上去是必须的撒", vbCritical, "温馨小提醒(*^__^*)" End IfEnd SubPrivate Sub Command16_Click() '除法的特殊性,除数不能为零If Val(Text2.Text) Thena = Val(Text1.Text) / Val(Text2.Text)Text3.Text = Val(Text1.Text) & "÷" & Val(Text2.Text) & "=" & aIf Text1.Text = "" Or Text2.Text = "" ThenMsgBox "哼哼,双目运算一定要是两个数喔" & vbCrLf & vbCrLf & _ "不用说,补上去是必须的撒", vbCritical, "温馨小提醒(*^__^*)" End IfElseText3.Text = "无穷大∞"MsgBox "亲,除数不可以为零的哟!", vbInformation, "矮油,不得了嘞(*^__^*)"Text2.Text = ""Text2.SetFocusEnd IfEnd SubPrivate Sub Command17_Click() '实现清零,并将焦点给文本框1Text1.Text = ""Text2.Text = ""Text3.Text = ""Text1.SetFocusEnd SubPrivate Sub Command18_Click()Me.Hide '进入计算器1Form1.ShowEnd SubPrivate Sub Command2_Click() If isFocus1 ThenText1.Text = Text1.Text & 1 End IfIf isFocus2 ThenText2.Text = Text2.Text & 1 End IfEnd SubPrivate Sub Command3_Click() If isFocus1 ThenText1.Text = Text1.Text & 2 End IfIf isFocus2 ThenText2.Text = Text2.Text & 2 End IfEnd SubPrivate Sub Command4_Click() If isFocus1 ThenText1.Text = Text1.Text & 3 End IfIf isFocus2 ThenText2.Text = Text2.Text & 3 End IfEnd SubPrivate Sub Command5_Click() If isFocus1 ThenText1.Text = Text1.Text & 4 End IfIf isFocus2 ThenText2.Text = Text2.Text & 4 End IfEnd SubPrivate Sub Command6_Click() If isFocus1 ThenText1.Text = Text1.Text & 5 End IfIf isFocus2 ThenText2.Text = Text2.Text & 5 End IfEnd SubPrivate Sub Command7_Click() If isFocus1 ThenText1.Text = Text1.Text & 6 End IfIf isFocus2 ThenText2.Text = Text2.Text & 6 End IfEnd SubPrivate Sub Command8_Click() If isFocus1 ThenText1.Text = Text1.Text & 7 End IfIf isFocus2 ThenText2.Text = Text2.Text & 7 End IfEnd SubPrivate Sub Command9_Click() If isFocus1 ThenText1.Text = Text1.Text & 8End IfIf isFocus2 ThenText2.Text = Text2.Text & 8End IfEnd SubPrivate Sub Form_Load()isFocus1 = False: isFocus2 = FalseEnd SubPrivate Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)Label4.ForeColor = vbBlackLabel4.FontUnderline = FalseLabel4.FontBold = FalseEnd SubPrivate Sub Label4_Click()Form3.ShowEnd SubPrivate Sub Label4_MouseMove(Button As Integer, Shift As Integer, X AsSingle, Y As Single)Label4.ForeColor = vbGreen Label4.FontUnderline = True Label4.FontBold = True End SubPrivate Sub Text1_GotFocus() isFocus1 = TrueisFocus2 = FalseEnd SubPrivate Sub Text2_GotFocus() isFocus2 = TrueisFocus1 = FalseEnd Sub页面(form3):这里面的亮点自己找哦。

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简易计算器代码

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简易计算器源码

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

计算器(用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代码

科学计算器vb代码
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
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计算器源代码

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

VB 计算器 代码

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.实验标题计算器2.目的及要求设计一个简易计算器可以进行简单的加减乘除等运算3.操作步骤⑴设置应用程序用户界面⑵设置对象属性Ⅰ.Command1(0)--Command(17)的Caption 属性分别为“0,1,2.3.4.5.6.7.8.9.小数点.+.-.*./.^.=.CLS”Ⅱ.Text1的Caption属性为“”;Locked属性为True⑶编写程序代码Private Sub Command1_Click(Index As Integer)Select Case IndexCase 0 To 15Text1 = Text1 + Command1(Index).CaptionCase 16Dim a As StringDim b As StringDim s As StringDim L As IntegerDim k As Strings = Trim(Text1)L = Len(s)For i = 1 To Lk = Mid(s, i, 1)If k = "+" Or k = "-" Or k = "*" Or k = "/" Or k = "^" Thenb = Right(s, L - i)Exit ForElsea = a + kEnd IfNextIf k = "+" Then Text1 = Str(Val(a) + Val(b))If k = "-" Then Text1 = Str(Val(a) - Val(b))If k = "*" Then Text1 = Str(Val(a) * Val(b))If k = "/" Then Text1 = Str(Val(a) / Val(b))If k = "^" Then Text1 = Str(Val(a) ^ Val(b))Case 17Text1 = ""End SelectEnd Sub4.实验中存在的问题及分析Text的Locked属性未设置,以至于text上数字的运算可以随便修改,与日常中的计算器存在很大差异。

VB计算器

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编写一个简单计算器

用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计算器程序代码

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计算器代码
Dim b,c,d 'b为输入值暂存变量,c为运算类型变量,d为保存数值变量,可忽略
Private Sub a_KeyPress(KeyAscii As Integer) '限制输入非数字,a为输入框初始值为空
If KeyAscii = 46 And Not CBool(InStr(a, ".")) Then Exit Sub '限制只能输入一个小数点
If InStr(a, "0.") = 1 Then Exit Sub '限制小数点前只能输入一个0
If InStr(a, "0") = 1 Then a.Text = "" '限制数字前输入0
End Sub
Private Sub back_Click() '退格功能
a.SetFocus
SendKeys "{7}"
End Sub
Private Sub key8_Click()
a.SetFocus
SendKeys "{8}"
End Sub
Private Sub key9_Click()
a.SetFocus
SendKeys "{9}"
End Sub
Private Sub add_Click() '按下+号功能
a.SetFocus
SendKeys "{2}"
End Sub
Private Sub key3_Click()
a.SetFocus
SendKeys "{3}"

VB实例代码-计算器

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

科学计算器vb代码
Top = 1455
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. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

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' 窗体的初始化过程' 设置所有变量为其初始值。

Private Sub Form_Load()Cancel_ClickMemNum = CDec(0)LabMem.Visible = MemNum <> 0End Sub' 输入参数转长精度Sub GetOp1()Op1 = V al(Disp)If Abs(Op1) < CC1 And Abs(Op1) > CC2 Then Op1 = CDec(Disp) End Sub' C (取消)' 重新设置显示并初始化变量。

Private Sub Cancel_Click()Disp = "0."Op1 = CDec(0)Op2 = CDec(0)DecFlag = FalseKlast = "NUL"Temp2 = CDec(0)OpFlag = ""Kedt = 0End Sub' CE (取消输入) 。

Private Sub CancelEntry_Click()Disp = "0."Op1 = CDec(0)DecFlag = FalseKedt = 0End Sub' 小数点(.)' 如果上一次按键为运算符,初始化Disp 为"0.";' 否则显示时追加一个小数点。

Private Sub Decimal_Click()If Klast = "EQU" Then Cancel_ClickIf Kedt < 2 ThenDisp = "0."Kedt = 2End IfDecFlag = TrueEnd Sub' 数字键(0-9)' 向显示中的数追加新数。

Private Sub Number_Click(Index As Integer)If Klast = "EQU" Then Cancel_ClickIf Kedt < 2 ThenDisp = "."DecFlag = FalseEnd IfIf DecFlag ThenDisp = Disp + Number(Index).CaptionElseDisp = Left(Disp, InStr(Disp, Format(0, ".")) - 1) + Number(Index).Caption + Format(0, ".")End IfKedt = 2End Sub' 执行一次运算(+,-,*,/)Private Sub Operation()Dim a As Double, b As DoubleOn Error GoTo ErrH'Temp2 = Op1a = CDbl(Op1):b = CDbl(Op2)Select Case OpFlagCase "+": a = b + aCase "-": a = b - 1Case "X": a = b * aCase "/": a = b / aEnd SelectIf Abs(a) > CC1 Or Abs(a) < CC2 ThenDisp = aOp1 = aOp2 = aExit SubEnd If' 在长精度范围内Select Case OpFlagCase "+": Op1 = Op2 + Op1Case "-": Op1 = Op2 - Op1Case "X": Op1 = Op2 * Op1Case "/": Op1 = Op2 / Op1End Select'If V arType(Op1) = vbDouble And Abs(Op1) < CC1 And Abs(Op1) > CC2 Then Op1 = CDec(Op1)Disp = Op1Op2 = Op1Exit SubErrH:Cancel_ClickDisp = "ERROR!"End Sub' 运算符(=)' 如果重复按“=”,则重复最后的运算。

Private Sub Equal_Click()Select Case KlastCase "NUL"If Kedt = 2 Then GetOp1Disp = Op1Op2 = Op1Case "EQU"If OpFlag <> "" ThenOp2 = Op1Op1 = Temp2OperationEnd IfCase "OPS"If Kedt = 2 Then GetOp1OperationEnd SelectKlast = "EQU"Kedt = 1End Sub' 运算符(+, -, x, /)' 如果有一个操作数,则设置Op1。

' 如果有两个操作数,则将Op1 设置为Op1 与当前输入字符串的运算结果,并显示结果。

Private Sub Operator_Click(Index As Integer)'If Klast = "OPS" And Kedt > 0 ThenIf Kedt = 2 Then GetOp1OperationElseIf Kedt = 2 Then GetOp1Disp = Op1Op2 = Op1End IfKlast = "OPS"Kedt = 0OpFlag = Operator(Index).CaptionEnd Sub'改变数字的正负号Private Sub negcmd_Click()If Left(Disp, 1) <> "-" ThenDisp = "-" + DispElseDisp = Right(Disp, Len(Disp) - 1)End IfGetOp1End Sub' 函数计算Private Sub Func_Click(Index As Integer)On Error GoTo ErrH'If Kedt = 2 Then GetOp1Select Case IndexCase 0: ' 开根号计算If Op1 < 0 ThenMsgBox "负数开方错误", 48ElseOp1 = sqr28(Op1)End IfCase 1: ' 平方计算If Abs(Op1) > CC3 Or Abs(Op1) < 0.0000001 Then Op1 = CDbl(Op1) ^ 2ElseOp1 = Op1 * Op1End IfCase 2: ' 取整计算Op1 = Int(Op1)Case 3: ' 倒数计算If Abs(Op1) > CC3 ThenOp1 = 1 / CDbl(Op1)ElseOp1 = 1 / Op1End IfCase 4: '百分比键If Abs(Op1) < 0.0000000000001 ThenOp1 = CDbl(Op1) / 100ElseOp1 = Op1 / 100End IfCase 5: ' 开立方计算Op1 = cur28(Op1)End Select' 双精度转长精度If V arType(Op1) = vbDouble And Abs(Op1) < CC1 And Abs(Op1) > CC2 Then Op1 = CDec(Op1)Disp = Op1Kedt = 1Exit SubErrH:Cancel_ClickDisp = "ERROR!"End Sub'。

相关文档
最新文档