用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,感觉很好,写了个计算器程序,虽然花了不少时间,可也着实高兴。
其中遇到很多问题,最终也在各种资料中得到解决。
现在附上截图和全部代码,希望和大家交流一下,相互学习。
也希望能帮助到准备做计算器的同学。
计算器最终执行文件图标:计算器包括三个窗体(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教程编程实例详解VB教程编程实例详解VB(Visual Basic)是一种面向对象的编程语言,它可以用于开发Windows应用程序。
在这里,我们将详细解释一些VB 编程实例,以帮助初学者更好地理解和掌握这门语言。
编程实例1:计算器首先,让我们创建一个计算器的VB程序。
我们将使用VB的窗体和按钮来实现这个功能。
首先,我们需要在窗体上添加一些按钮,如"1"、"2"、"+"、"="等。
然后,我们需要用一个文本框来显示计算结果。
在窗体上双击"+"按钮并添加以下代码:```Private Sub ButtonPlus_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonPlus.ClickDim num1 As IntegerDim num2 As IntegerDim result As Integernum1 = Integer.Parse(TextBoxNum1.Text)num2 = Integer.Parse(TextBoxNum2.Text)result = num1 + num2TextBoxResult.Text = result.ToString()End Sub```在这个代码中,我们首先定义了三个变量:num1、num2和result,用于存储计算结果。
然后,我们使用Integer.Parse方法将文本框中输入的文本转换为整数类型,并将其赋值给num1和num2。
接下来,我们将num1和num2相加,将结果赋值给result,并使用result.ToString()方法将结果转换为字符串类型并显示在结果文本框中。
编程实例2:学生成绩管理系统接下来,让我们创建一个学生成绩管理系统的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)代码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编写简单计算器程序
Option ExplicitDim LastInput As String * 3 '记录上次按下的按键Dim Num1 As Double '第一个操作数Dim Num2 As Double '第二个操作数Dim OptType As Integer '按下哪一个操作符Dim Result As Double '表示运算结果Dim shuzhi As Integer '表示当前采用的shuzhiDim FirstNum As Boolean '是否是第一个操作数Sub keyp(keynum As Integer)Dim CHAR As String * 1CHAR = Chr(keynum)If CHAR = "+" Or keynum = 43 Then Command5(0).Value = TrueIf CHAR = "-" Or keynum = 45 Then Command5(1).Value = TrueIf CHAR = "*" Or keynum = 42 Then Command5(2).Value = TrueIf CHAR = "/" Or keynum = 47 Then Command5(3).Value = TrueIf shuzhi = 2 And CHAR >= "2" And CHAR <= "9" Thenkeynum = 0Exit SubEnd IfIf keynum >= 48 And keynum <= 57 Then Command1(keynum - 48).Value = True If keynum = 46 Then Command2.Value = TrueIf UCase(CHAR) = "C" Then Command3.Value = TrueIf keynum = 27 Then Command4.Value = TrueIf keynum = 61 Then Command6.Value = Truekeynum = 0End SubFunction angle(ByVal j1 As Integer) As Singleangle = j1If Option1.Value Then angle = j1 * 3.14 / 180End FunctionFunction ArcSin(ByVal Num As Single) As SingleIf Num = 1 ThenArcSin = 3.1415926 / 2ElseIf Num = -1 ThenArcSin = 3.1415926 * 3 / 2ElseArcSin = Atn(Num / Sqr(-Num * Num + 1))End IfIf Option1.Value Then ArcSin = ArcSin * 180 / 3.1415926End FunctionFunction ArcCos(ByVal Num As Single) As SingleIf Num = 1 ThenArcCos = 0ElseIf Num = -1 ThenArcCos = 3.1415926ElseArcCos = Atn(-Num / Sqr(-Num * Num + 1)) + 2 * Atn(1)End IfIf Option1.Value Then ArcCos = ArcCos * 180 / 3.1415926 End FunctionFunction jiecheng(ByVal n As Integer) As SingleDim COUNT As Integerjiecheng = 1For COUNT = 1 To njiecheng = jiecheng * COUNTNextEnd FunctionFunction n10to2(ByVal Number As Single) As SingleDim IntN As Long 'Number的整数部分Dim FracN As Single 'Number的小数部分Dim ModN As Integer '整数部分换算时,记录余数Dim RltN As String '换算结果Dim i As IntegerIf InStr(Number, "e") > 0 Or InStr(Number, "E") > 0 Then MsgBox "不能转换以科学记数法表示的数据!"Exit FunctionEnd IfModN = 0'Number = Val(Text1.Text)IntN = Int(Number)FracN = Number - IntN'以下代码用于将十进制的整数部分换算为二进制Do While IntN > 0ModN = IntN Mod 2IntN = IntN \ 2RltN = ModN & RltNLoopRltN = RltN & "."i = 1'以下代码用于将十进制的小数部分换算为二进制Do While i <= 7 Or FracN <> 0FracN = FracN * 2If FracN >= 1 ThenFracN = FracN - 1RltN = RltN & "1"ElseRltN = RltN & "0"End Ifi = i + 1Loopn10to2 = RltN'Option3.Value = TrueEnd FunctionFunction n2to10(ByVal Number As Double) As SingleDim i As Integer, j As IntegerDim IntN As Long, FracN As SingleDim RltN As SingleDim POS As Integer '记录小数点位置If InStr(Number, "e") > 0 Or InStr(Number, "E") > 0 ThenMsgBox "不能转换以科学记数法表示的数据!"Exit FunctionEnd IfOn Error GoTo ErrIntN = Int(Number)FracN = Number - IntNDo While IntN > 0 '换算整数部分RltN = RltN + (IntN Mod 10) * 2 ^ jj = j + 1IntN = IntN \ 10LoopPOS = InStr(1, Str(FracN), ".")j = -1For i = POS + 1 To Len(Str(FracN)) '换算小数部分RltN = RltN + 2 ^ j * Val(Mid(Str(FracN), i, 1))j = j - 1Next in2to10 = RltN'Option4.Value = TrueExit FunctionErr:Text1.Text = "数据太大,溢出!"End FunctionPrivate Sub Command1_Click(Index As Integer)'当按下数字键(0-9)时,向文本框尾部追加数据'并通过变量LastInput记录上次按键为数字键If Len(Text1.Text) > 16 Then Exit SubIf Text1.Text = "0" Or LastInput = "Eqv" Then Text1.Text = ""Text1.Text = Text1.Text & Index '追加数据LastInput = "Num"Command1(0).SetFocusEnd SubPrivate Sub Command1_KeyPress(Index As Integer, KeyAscii As Integer) Call keyp(KeyAscii)End SubPrivate Sub Command2_Click()'按下小数点按钮的处理过程'如果数据位数超出范围,或数据中已包含小数点,退出本过程If Len(Text1.Text) > 16 Or InStr(1, Text1.Text, ".") > 0 _And LastInput <> "Eqv" Then Exit Sub'如果以"."开始输入新数据,在"."前加"0";'如果在数据输入过程中按下".",直接将"."追加在数据尾部If LastInput = "Opt" Or LastInput = "Eqv" Or LastInput = "Neg" Then Text1.Text = Text1.Text + "0."ElseText1.Text = Text1.Text + "."End IfLastInput = "Num"Command1(0).SetFocusEnd SubPrivate Sub Command3_Click()'按下"C"(取消) 按钮的Click 事件过程'重新设置并初始化变量。
VB写简易计算器附图
用V B6.0编写简易计算器效果图:废话不多说,直接上步骤一、创建控件组1、创建控件组的方法??首先创建一个命令按钮,调整其大小(觉得合适就行),名称为Command1,Caption属性为数字 0 ;然后进行“复制”和“粘贴”,当选择“粘贴”时,出现对话框提示已有一个同名控件,询问是否创建控件组,选择“是”后,即创建了一个名为“Command”的控件组。
这时,第一个按钮的Index属性值默认为“0”,第二个的Index属性值自动设为“1”,并且大小与第一个按钮相同,只需修改其 Caption 属性为数字“1”并将其拖至合适位置即可。
此后继续使用“粘贴”的方法建立其他控件组中其余按钮,共20个按钮,每建立一个,就将它拖到合适处,并修改相应的Caption属性值。
二、编写代码Dim s1 As Single, s2 As Single, ysf As String'定义两个单精度数变量用与存放参与运算的数,一个字符型存放运算符Private Sub Command1_Click(Index As Integer)Text1.Text = Text1.Text & Command1(Index).Caption'将command1的单击事件与文本框显示的内容连接End SubPrivate Sub Command2_Click()Text1.Text = Text1.Text + "."If (InStr(Text1.Text, ".") = 1) Then'第一位不能为小数Text1.Text = ""End IfIf InStr(Text1.Text, ".") < Len(Text1.Text) Then '防止出现两个小数点Text1.Text = Left(Text1.Text, Len(Text1.Text) - 1)End IfEnd SubPrivate Sub Command3_Click()s2 = Val(Text1.Text) '开始加减乘除运算Select Case ysfCase "+"Text1.Text = s1 + s2Case "-"Text1.Text = s1 - s2Case "*"Text1.Text = s1 * s2Case "/"If s2 = 0 ThenMsgBox "分母不能为零!"Text1.Text = ""ElseText1.Text = s1 / s2End IfEnd SelectText1 = IIf(Left(Text1.Text, 1) = ".", 0 & Text1.Text, Text1.Text) '这个很关键,如果没有这个的话,得出小于1的小数前面没有0End SubPrivate Sub Command4_Click()If Text1.Text = "" Then '文本为空就结束Exit SubEnd IfText1.Text = Left(Text1.Text, Len(Text1.Text) - 1) '文本退一格End SubPrivate Sub Command5_Click()Text1.Text = "" '清除当前框内文本End SubPrivate Sub Command6_Click(Index As Integer)s1 = Val(Text1.Text) '将s1隐藏起来ysf = Command6(Index).CaptionText1.Text = ""End SubPrivate Sub Command7_Click()If Left(Text1.Text, 1) <> "-" Then '判断作为负数Text1.Text = "-" & Text1.TextElseText1.Text = Right(Text1.Text, Len(Text1.Text) - 1)End IfEnd SubPrivate Sub Command8_Click()Text1.Text = Text1.Text * Text1.Text '平方End Sub各位朋友,可以将红色代码复制到相应位置,不清楚的可以全选复制,但是一定要按照我的步骤和给的名称来哦!还可以再添加按钮Private Sub Command9_Click() '这是退出代码EndEnd Sub三、测试,成功的话给个好评哦!谢谢各位下载与支持!这个可以编写作为作业哦!。
使用VB制作计算器程序
多功能计算器界面如下图所示。
实现代码如下:Public b As SinglePublic flag, first As IntegerDim narray(100) As Single '存放文本框1中输入的多个数据Dim i As Integer '存放输入数组的实际长度'单次运算Dim a As Single '存放第一个操作数Dim key As String '存放运算符'以上在模块中定义变量Private Sub cmd0_Click() '单击数字键0Text1.Text = Text1.Text + cmd0.Caption '可用"&"代替"+"End SubPrivate Sub cmd1_Click() '单击数字键1Text1.Text = Text1.Text + cmd1.CaptionEnd SubPrivate Sub cmd2_Click() '单击数字键2Text1.Text = Text1.Text + cmd2.CaptionEnd SubPrivate Sub cmd3_Click() '单击数字键3Text1.Text = Text1.Text + cmd3.CaptionEnd SubPrivate Sub cmd4_Click() '单击数字键4Text1.Text = Text1.Text + cmd4.CaptionEnd SubPrivate Sub cmd5_Click() '单击数字键5Text1.Text = Text1.Text + cmd5.CaptionEnd SubPrivate Sub cmd6_Click() '单击数字键6Text1.Text = Text1.Text + cmd6.CaptionEnd SubPrivate Sub cmd7_Click() '单击数字键7Text1.Text = Text1.Text + cmd7.CaptionEnd SubPrivate Sub cmd8_Click() '单击数字键8Text1.Text = Text1.Text + cmd8.CaptionEnd SubPrivate Sub cmd9_Click() '单击数字键9Text1.Text = Text1.Text + cmd9.CaptionEnd SubPrivate Sub cmddot_Click() '连接小数点Text1.Text = Text1.Text + cmddot.CaptionIf InStr(Text1.Text, ".") < Len(Text1.Text) Then'防止出现多个小数点Text1.Text = Left(Text1.Text, Len(Text1.Text) - 1)End IfEnd SubPrivate Sub cmdcls_Click() '单击CE键Text1.Text = ""End SubPrivate Sub add_Click() '单击“+”,保存第一个操作数和运算符 a = Val(Text1.Text)key = add.CaptionText1.Text = " "End SubPrivate Sub subs_Click() '单击“-”a = Val(Text1.Text)key = subs.CaptionText1.Text = " "End SubPrivate Sub mul_Click() '单击“*”a = Val(Text1.Text)key = mul.CaptionText1.Text = " "End SubPrivate Sub div_Click() '单击“/”a = Val(Text1.Text)key = div.CaptionText1.Text = " "End SubPrivate Sub modi_Click() '单击“Mod”a = Val(Text1.Text)key = more.CaptionText1.Text = " "End SubPrivate Sub mulpi_Click() '单击“^”a = Val(Text1.Text)key = mulpi.CaptionText1.Text = " "End SubPrivate Sub sign_Click() '单击“+/-”,改变操作数符号Text1.Text = -Val(Text1.Text)End SubPrivate Sub equal_Click() '单击“=”Select Case key '判断运算符Case "+": Text1.Text = a + Val(Text1.Text)Case "-": Text1.Text = a - Val(Text1.Text)Case "*": Text1.Text = a * Val(Text1.Text)Case "/": Text1.Text = a / Val(Text1.Text)Case "\": Text1.Text = a \ Val(Text1.Text)Case "mod": Text1.Text = a Mod Val(Text1.Text)Case "^": Text1.Text = a ^ Val(Text1.Text)Case "<": Text1.Text = a < Val(Text1.Text)Case ">": Text1.Text = a > Val(Text1.Text)Case "<>": Text1.Text = a <> Val(Text1.Text)Case "Like": b = "*" & Trim(Text1.Text) & "*"If (Str(a) Like b) Then Text1.Text = True Else Text1.Text = False Case "Not": Text1.Text = Not aCase "And": Text1.Text = a And Val(Text1.Text)Case "Or": Text1.Text = a Or Val(Text1.Text)Case "Xor": Text1.Text = a Xor Val(Text1.Text)End SelectEnd SubPrivate Sub less_Click() '单击“<”a = Val(Text1.Text)key = less.CaptionText1.Text = " "End SubPrivate Sub more_Click() '单击“>”a = Val(Text1.Text)key = more.CaptionText1.Text = " "End SubPrivate Sub notequal_Click() '单击“<>”a = Val(Text1.Text)key = notequal.CaptionText1.Text = " "End SubPrivate Sub likes_Click() '单击“Likes”a = Val(Text1.Text)key = likes.CaptionText1.Text = " "End SubPrivate Sub cmdnot_Click() '单击“Not”a = Val(Text1.Text)key = cmdnot.CaptionText1.Text = " "End SubPrivate Sub cmdand_Click() '单击“And”a = Val(Text1.Text)key = cmdand.CaptionText1.Text = " "End SubPrivate Sub cmdor_Click() '单击“Or”a = Val(Text1.Text)key = cmdor.CaptionText1.Text = " "End SubPrivate Sub cmdxor_Click() '单击“Xor”a = Val(Text1.Text)key = cmdxor.CaptionText1.Text = " "End SubPrivate Sub Command3_Click(Index As Integer) '函数区中功能实现,Command3'为函数区控件数组名Select Case IndexCase 0Text2.Text = Sin(Val(Trim$(Text1.Text))) '调用内部函数sinCase 1Text2.Text = Cos(Val(Trim$(Text1.Text)))Case 2Text2.Text = Abs(Val(Trim$(Text1.Text)))Case 3Text2.Text = Sqr(Val(Trim$(Text1.Text)))Case 4Text2.Text = Hex$(Val(Trim$(Text1.Text)))Case 5Text2.Text = Oct$(Val(Trim$(Text1.Text)))Case 6Text2.Text = Asc(Trim$(Text1.Text))Case 7Text2.Text = Rnd(Val(Trim$(Text1.Text)))Case 8Text2.Text = DateCase 9Text2.Text = Len(Trim$(Text1.Text))Case 10Text2.Text = sum1() '调用自定义函数求和Case 11Text2.Text = ave() '调用自定义函数求平均值Case 12 '12为max命令按钮的控件数组Index值Dim m As SingleCall max2(m) '调用自定义子过程求最大值Text2.Text = m 'm为调用过程得到的最大值'Text2.Text = max() '调用自定义函数过程求最大值Case 13Text2.Text = min() '调用自定义函数过程求最小值Case 14Text2.Text = sort() '调用自定义函数过程排序Case 15'Text2.Text = fac() '调用自定义函数过程求阶乘Text2.Text = fac1(Val(Text1.Text))'调用自定义函数过程求阶乘,用递归实现End SelectEnd SubPrivate Function sum1()Dim k As IntegerDim s As Singles = 0For k = 1 To i - 1s = s + narray(k)Next ksum1 = sEnd FunctionPrivate Function max()Dim k As IntegerDim s As Single, m As Singlem = narray(1)For k = 2 To iIf m < narray(k) Then m = narray(k) Next kmax = mEnd FunctionPrivate Function ave() '求平均值Dim k As IntegerDim s As Single, m As SingleFor k = 1 To is = s + narray(k)Next kave = s / (i - 1)End FunctionPrivate Function min() '求最小值函数Dim k As IntegerDim s As Single, m As Singlem = narray(1)For k = 2 To i - 1If m > narray(k) Then m = narray(k) Next kmin = mEnd FunctionPrivate Function sort() '排序函数Dim k As Integer, j As IntegerDim s As String, m As SingleFor k = 1 To i - 2For j = 1 To i - 1If narray(j) < narray(j + 1) Thenm = narray(j)narray(j) = narray(j + 1)narray(j + 1) = mEnd IfNext jNext kFor k = 1 To i - 1s = s+" "+Str$(narray(k)) '将排序结果存放到字符串s,以便带回到主调程序中Next ksort = sEnd FunctionPrivate Function fac() '求阶乘函数Dim k As Integer, j As IntegerDim m As SingleIf InStr(Trim$(Text1.Text), " ") > 1 Then'如果在文本输入框中输入了多个数据,则计算一个'数据的阶乘j = first '数组元素narray()中第一个元素Else: j = Val(Trim$(Text1.Text))End Ifm = 1For k = 1 To jm = m * kNext kfac = mEnd FunctionPrivate Sub Text1_KeyPress(Keyasc As Integer)'在文本框1中输入多个数据,以空格分隔,按回'车键结束,识别数据存放到数组narray( )中Dim c As StringDim n As Integer, k As Integeri = 1k = 1If Keyasc = 13 Then '按下回车键For n = 0 To Len(Text1.Text) - 1c = Mid$(Text1.Text, n + 1, 1)If c = " " Thennarray(i) = Val(Mid$(Text1.Text, k, n - k + 1))'识别数据存放在数组narray(i)中k = k + Len(Str$(narray(i))) '下一个数据位置起点i = i + 1 '每识别一个数据数组实际长度加1End IfNext nEnd Iffirst = narray(1)End SubPrivate Sub max2(m As Single) '求数组元素的最大值Dim k As Integerm = narray(1)For k = 2 To iIf m < narray(k) Then m = narray(k)Next kEnd SubPrivate Function fac1(n As Long) '递归求阶乘If n > 1 Thenfac1 = fac1(n - 1) * nElsef ac1 = 1End IfEnd Function。
VB计算器代码
第 3 章计算器第3 章计算器3.1 开发任务在本任务中,我们要分别实现基本算术运算、累加和计算、阶乘计算、三角函数计算、排列组合计算和对数计算等多个子任务。
3.1.1 计算器的实验版本1. 程序界面设计(1)新建工程打开VB开发环境,在工程浏览器窗口中(见图3-1左),将工程名称改为“计算器”(如图3-1中);再将窗体名称改为“frmCalculator”如图3-1右)。
图3-1 改变工程和窗体名称将窗体文件保存为“frmCalculator.frm”,工程文件保存为“prjCalculator”。
(2)添加控件在本工程中,我们需要用到下列控件:1个文本框用于输入运算数和输出结果;16个按钮构成计算器键盘,其中10个用于输入10个数字字符,1个用于输入小数点,一个用于触发计算的等号,另外4个用于选择加、减、乘、除运算符。
首先在窗体上部添加一个文本框,默认名称是Text1,调整好大小和位置(如图3-2),并将属性Text的值清空,再将对齐方式Alignment 改成“1 -Right Justify”右对齐;图3-2 添加文本框控件再来制作键盘,第1步,添加第1个按钮。
在文本框下方添加一个按钮Command1,将它调整为一个按键般大小,并把Caption属性改成“1”。
第2步,添加第2个按钮。
添加外形类似的按钮,用复制的方法即可。
不过要注意,在粘贴时VB会询问“已经有一个控件为"Command1 "。
创建一个控件数组吗?”(见图3-3),一定要回答“否”。
将复制好的按钮Command2的Caption改成2,并移动到Command1的右边。
图3-3 创建控件数组询问对话框第3步,重复进行粘贴操作,依次制作其它按钮,按图3-4的布局排列。
前9个按钮的Caption改成与它们的顺序号相同,Command10的Caption改为“0”,Command11的Caption改为“.”,ommand12的Caption改为“=”,Command13~Command16的Caption依次改为“+”、“-”、“*”、“/”。
VB计算器
用VB写计算器这是我设计的计算器样式,代码如下:Dim data1 As Double‘定义变量类型’Dim data2 As DoubleDim j As IntegerDim flag As BooleanDim result As StringPrivate Sub Form_Load()flag = TrueText1 = "0"End Sub0~9的代码:Private Sub Command1_Click(Index As Integer)‘0’If flag = True ThenIf Text1 <> "0" ThenText1 = Text1 + "0"End IfElseText1 = ""Text1 = Text1 + "0"End Ifflag = TrueEnd SubPrivate Sub Command11_Click(Index As Integer)‘1’If flag = True ThenIf Text1 <> "0" ThenText1 = Text1 + "1"ElseText1 = ""Text1 = "1"End IfElseText1 = ""Text1 = Text1 + "1"End Ifflag = TrueEnd SubPrivate Sub Command12_Click(Index As Integer)‘2’If flag = True ThenIf Text1 <> "0" ThenText1 = Text1 + "2"ElseText1 = ""Text1 = "2"End IfElseText1 = ""Text1 = Text1 + "2"End Ifflag = TrueEnd SubPrivate Sub Command13_Click(Index As Integer)‘3’If flag = True ThenIf Text1 <> "0" ThenText1 = Text1 + "3"ElseText1 = ""Text1 = "3"End IfElseText1 = ""Text1 = Text1 + "3"End Ifflag = TrueEnd SubPrivate Sub Command14_Click(Index As Integer)‘4’If flag = True ThenIf Text1 <> "0" ThenText1 = Text1 + "4"ElseText1 = ""Text1 = "4"End IfElseText1 = ""Text1 = Text1 + "4"End Ifflag = TrueEnd SubPrivate Sub Command15_Click(Index As Integer)‘5’If flag = True ThenIf Text1 <> "0" ThenText1 = Text1 + "5"ElseText1 = ""Text1 = "5"End IfElseText1 = ""Text1 = Text1 + "5"End Ifflag = TrueEnd SubPrivate Sub Command16_Click(Index As Integer)‘6’If flag = True ThenIf Text1 <> "0" ThenText1 = Text1 + "6"ElseText1 = ""Text1 = "6"End IfElseText1 = ""Text1 = Text1 + "6"End Ifflag = TrueEnd SubPrivate Sub Command17_Click(Index As Integer)‘7’If flag = True ThenIf Text1 <> "0" ThenText1 = Text1 + "7"ElseText1 = ""Text1 = "7"End IfElseText1 = ""Text1 = Text1 + "7"End Ifflag = TrueEnd SubPrivate Sub Command18_Click(Index As Integer)‘8’If flag = True ThenIf Text1 <> "0" ThenText1 = Text1 + "8"ElseText1 = ""Text1 = "8"End IfElseText1 = ""Text1 = Text1 + "8"End Ifflag = TrueEnd SubPrivate Sub Command19_Click(Index As Integer)‘9’If flag = True ThenIf Text1 <> "0" ThenText1 = Text1 + "9"ElseText1 = ""Text1 = "9"End IfElseText1 = ""Text1 = Text1 + "9"End Ifflag = TrueEnd SubPrivate Sub Command2_Click()‘小数点的实现’If Text1 <> "" ThenText1 = Text1 + "."End Ifflag = TrueEnd SubPrivate Sub Command3_Click()‘正负号的转换’If Text1 > 0 And Text1 < 1 ThenText1 = Trim("-") + Trim(Text1)Exit SubEnd IfIf Text1 > -1 And Text1 < 0 ThenText1 = "0" + Trim(-Text1)Exit SubEnd IfText1 = -Text1End Sub‘+-*/加减乘除的实现’Private Sub Command4_Click()‘+’data1 = Text1j = "1"Text1 = "0"Text1.SetFocusEnd SubPrivate Sub Command5_Click()‘-‘data1 = Text1j = "2"Text1 = "0"Text1.SetFocusEnd SubPrivate Sub Command6_Click()‘*’data1 = Text1j = "3"Text1 = "0"Text1.SetFocusEnd SubPrivate Sub Command7_Click()‘/’data1 = Text1j = "4"Text1 = "0"Text1.SetFocusEnd SubPrivate Sub Command10_Click()‘=’data2 = Text1If j = 1 Thenresult = data1 + data2ElseIf j = 2 Thenresult = data1 - data2ElseIf j = 3 Thenresult = data1 * data2ElseIf j = 4 Thenresult = data1 / data2End IfIf result > 0 And result < 1 Thenresult = "0" + Trim(result)End IfIf result > -1 And result < 0 Thenresult = "0" + Trim(-result)result = "-" + resultEnd IfText1 = resultdata1 = "0"flag = FalseEnd SubPrivate Sub Command9_Click()‘C清除按钮’Text1 = "0"Text1.SetFocusEnd Sub。
用VB编写一个简单计算器
用VB编写一个简单计算器一、功能:实现简单的加减乘除功能,C归零,CE取消输入,%计算并显示第一个操作数的百分比。
二、控件:1个label,20个commandbutton。
三、计算器运行界面:四、详细代码:Option ExplicitDim Op1, Op2 '前面输入的操作数Dim DecimalFlag As Integer '小数点仍然存在吗?Dim NumOps As Integer '操作数个数Dim LastInput '指示上一次按键事件的类型Dim OpFlag '指示未完成的操作Dim TempReadout' C (取消) 按钮的Click 事件过程' 重新设置显示并初始化变量Private Sub Cancel_Click()Readout = Format(0, "0.")Op1 = 0Op2 = 0Form_LoadEnd Sub' CE (取消输入) 按钮的Click 事件过程Private Sub CancelEntry_Click()Readout = Format(0, "0.")DecimalFlag = FalseLastInput = "CE"End Sub' 小数点(.) 按钮的Click 事件过程' 如果上一次按键为运算符,初始化readout 为"0.";' 否则显示时追加一个小数点Private Sub Decimal_Click()If LastInput = "NEG" ThenReadout = Format(0, "-0.")ElseIf LastInput <> "NUMS" ThenReadout = Format(0, "0.")End IfDecimalFlag = TrueLastInput = "NUMS"End Sub' 窗体的初始化过程' 设置所有变量为其初始值Private Sub Form_Load()DecimalFlag = FalseNumOps = 0LastInput = "NONE"OpFlag = " "Readout = Format(0, "0.")'Decimal.Caption = Format(0, ".")End Sub' 数字键(0-9) 的Click 事件过程' 向显示中的数追加新数Private Sub Number_Click(Index As Integer)If LastInput <> "NUMS" ThenReadout = Format(0, ".")DecimalFlag = FalseEnd IfIf DecimalFlag ThenReadout = Readout + Number(Index).CaptionElseReadout = Left(Readout, InStr(Readout, Format(0, ".")) - 1) + Number(Index).Caption + Format(0, ".")End IfIf LastInput = "NEG" Then Readout = "-" & ReadoutLastInput = "NUMS"End Sub' 运算符(+, -, x, /, =) 的Click 事件过程' 如果接下来的按键是数字键,增加NumOps。
用VB制作计算器
用VB制作计算器在VB编程语言中,可以使用Windows Forms应用程序来制作一个简单的计算器。
Windows Forms应用程序是VB的一种可视化编程工具,可以为用户提供一个图形用户界面。
首先,我们需要创建一个新的Windows Forms应用程序项目,并打开默认生成的Form1窗体。
接下来,我们可以使用Windows Forms控件来创建一个计算器的用户界面。
在Form1窗体上,我们可以添加一个TextBox控件用于显示计算结果,并设置其属性为只读。
```vbPrivate ReadOnly resultTextBox As TextBox = New TextBox```然后,我们可以添加一些Button控件来表示计算器的数字和操作符。
我们可以使用按钮的Click事件来处理用户的点击操作。
```vbPrivate ReadOnly buttons As List(Of Button) = New List(Of Button)```接下来,我们需要实现按钮的Click事件处理程序来执行相应的计算逻辑。
我们可以使用Eval函数来计算表达式的值,并将结果显示在TextBox中。
Private Sub Button_Click(sender As Object, e As EventArgs) Dim button As Button = TryCast(sender, Button)If button IsNot Nothing ThenDim buttonText As String = button.TextIf buttonText = "=" Then'计算表达式的值Dim expression As String = resultTextBox.TextDim value As Double = Eval(expression)resultTextBox.Text = value.ToStringElseIf buttonText = "C" Then'清空文本框resultTextBox.Text = ""Else'添加数字或操作符resultTextBox.Text += buttonTextEnd IfEnd IfEnd Sub最后,我们需要在窗体的构造函数或Load事件处理程序中将按钮和TextBox添加到窗体上。
用VB编写计算器
用VB编写计算器2007-01-09 10:36一.实验目的用vb语言编写一个简易计算器二.实验要求1.能够完成浮点数的加,减,乘,除;(平方等)2.能够实现退格和清除功能;3.初始值为0.0;4.小数点不能重复输入;5.高位数的0不出现;6.应用控件数组实现。
三.控件属性列表1、创建控件组的方法a、首先创建一个命令按钮,调整其大小—宽、高为 495,名称为Command1,aption 属性为数字 0 。
b、然后进行“复制”和“粘贴”,当选择“粘贴”时,出现对话框提示已有一个同名控件,询问是否创建控件组,选择“是”后,即创建了一个名为“Command”的控件组。
这时,第一个按钮的Index属性值默认为“0”,第二个的Index属性值自动设为“1”,并且大小与第一个按钮相同,只需修改其Caption 属性为数字“1”并将其拖至合适位置即可。
此后继续使用“粘贴”的方法建立其他控件组中其余按钮,共19个按钮,每建立一个,就将它拖到合适处,并修改相应的Caption属性值。
c、建立其他控件:如右图所示2、各控件属性设置如下:控件控件控件名称 Caption 控件名称 Caption窗体 Form 1 计算器按钮 Command 2(0) +按钮 Command 3 退格按钮 Command 2(1) -* 按钮 Command 4 . 按钮 Command 2(2)按钮 Command 5 = 按钮 Command 2(3) /按钮 command 6 + 按钮Command 7 ±按钮 Command 1(0)~Command1(9) Caption 0 ~ 9 各个属性修改后得到如图所示的界面四程序如下Dim shu1 As Single, shu2 As Single, suanfu As String'定义两个单精度数变量用与存放参与运算的数,一个字符型存放运算符Private Sub Command1_Click(Index As Integer)Text1.Text = Text1.Text & Command1(Index).Caption'将command1的单击事件与文本框显示的内容连接End SubPrivate Sub Command2_Click(Index As Integer)shu1 = Val(Text1.Text) '将shu1隐藏起来suanfu = Command2(Index).CaptionText1.Text = ""End SubPrivate Sub Command4_Click()Text1.Text = Text1.Text + "."If (InStr(Text1.Text, ".") = 1) Then '第一位不能为小数Text1.Text = ""End IfIf InStr(Text1.Text, ".") < Len(Text1.Text) Then'防止出现两个小数点Text1.Text = Left(Text1.Text, Len(Text1.Text) - 1)End IfEnd SubPrivate Sub Command5_Click() '开始加减乘除的运算shu2 = Val(Text1.Text)Select Case suanfuCase "+"Text1.Text = shu1 + shu2Case "-"Text1.Text = shu1 - shu2Case "*"Text1.Text = shu1 * shu2Case "/"If shu2 = 0 ThenMsgBox "分母不能为零!", 1 + 32 + 0,"错误" '错误提示框图下所示Text1.Text = ""ElseText1.Text = shu1 / shu2End IfEnd SelectEnd SubPrivate Sub Command3_Click() '假如输入错误,可每次退后一格If Text1.Text = "" ThenExit SubEnd IfText1.Text = Left(Text1.Text, Len(Text1.Text) - 1)End SubPrivate Sub Command6_Click()Text1.Text = "" '清除End SubPrivate Sub Command7_Click() '平方运算Text1.Text = Text1.Text * Text1.TextEnd SubPrivate Sub Command8_Click()If Left(Text1.Text, 1) <> "-" ThenText1.Text = "-" & Text1.TextElseText1.Text = Right(Text1.Text, Len(Text1.Text) - 1) End IfEnd SubPrivate Sub Form_Click()a = Int(Rnd() * 255)b = Int(Rnd() * 255)c = Int(Rnd() * 255)Form1.BackColor = RGB(a, b, c)End Sub。
vb简易计算器
vb简易计算器本⼈⼀觉醒来闲得⽆聊,正在学习VB,便⽤VB写个简易的计算器吧!巩固基础。
代码如下:1 /**2 *Author:乌鸟heart3 *Version:1.04 */5Dim IntX As Double'全局变量,⽤于存储计算的数值6Dim IntOperation As Double'标记运算类型7Dim isBegin As Boolean'标记是否已经给IntX赋值8Public Sub Clear() '清空命令函数910 screen.Caption = ""11End Sub12Public Sub SavaToIntX()1314Select Case IntOperation1516Case1'加法17If isBegin = False Then18 IntX = Val(screen.Caption)19 isBegin = True20Else21 IntX = IntX + Val(screen.Caption)22End If2324Case2'减法25If isBegin = False Then26 IntX = Val(screen.Caption)27 isBegin = True28Else29 IntX = IntX - Val(screen.Caption)30End If3132Case3'乘法33If isBegin = False Then34 IntX = Val(screen.Caption)35 isBegin = True36Else37 IntX = IntX * Val(screen.Caption)38'screen.Caption = IntX39End If4041Case4'除法42If isBegin = False Then43 IntX = Val(screen.Caption)44 isBegin = True45Else46 IntX = IntX / Val(screen.Caption)47End If4849End Select5051End Sub5253Private Sub Command0_Click()54 screen.Caption = screen.Caption & 055End Sub56Private Sub Command1_Click()57 screen.Caption = screen.Caption & 158End Sub59Private Sub Command2_Click()60 screen.Caption = screen.Caption & 261End Sub62Private Sub Command3_Click()63 screen.Caption = screen.Caption & 364End Sub65Private Sub Command4_Click()66 screen.Caption = screen.Caption & 467End Sub68Private Sub Command5_Click()69 screen.Caption = screen.Caption & 570End Sub71Private Sub Command6_Click()72 screen.Caption = screen.Caption & 673End Sub74Private Sub Command7_Click()75 screen.Caption = screen.Caption & 776End Sub77Private Sub Command8_Click()78 screen.Caption = screen.Caption & 879End Sub80Private Sub Command9_Click()81 screen.Caption = screen.Caption & 982End Sub8384Private Sub CommandClear_Click() '清空命令85 isBegin = False86 IntOperation = 087 IntX = 088 screen.Caption = ""89End Sub9091Private Sub CommandEqual_Click() '等号运算 9293If IntOperation <> 0Then'有运算标记的情况94Call SavaToIntX95 IntOperation = 096 isBegin = False97 screen.Caption = IntX98End If99100End Sub101102Private Sub CommandMinus_Click() '减法运算103104If IntOperation <> 0Then'有运算标记的情况105Call SavaToIntX106 IntOperation = 2107Call Clear108109Else110 IntOperation = 2111Call SavaToIntX112Call Clear113114End If115End Sub116117Private Sub CommandMultiple_Click() '乘法运算118If IntOperation <> 0Then'有运算标记的情况119Call SavaToIntX120 IntOperation = 3121Call Clear122123Else124 IntOperation = 3125Call SavaToIntX126Call Clear127128End If129130End Sub131132Private Sub CommandPlus_Click() '加法运算133134If IntOperation <> 0Then'有运算标记的情况135Call SavaToIntX136 IntOperation = 1137Call Clear138139Else140 IntOperation = 1141Call SavaToIntX142Call Clear143144End If145146End Sub147148Private Sub CommandSlash_Click() '除法运算149150If IntOperation <> 0Then'有运算标记的情况151Call SavaToIntX152 IntOperation = 4153Call Clear154155Else156 IntOperation = 4157Call SavaToIntX158Call Clear159160End If161End SubOK,DONE!。
使用VB设计一个简易计算器
使用VB设计一个简易计算器下面是使用VB设计一个简易计算器的代码示例:```vbPublic Class FrmCalculatorDim operand1 As DoubleDim operand2 As DoubleDim operatorFlag As StringPrivate Sub BtnNumber_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.ClickDim button As Button = CType(sender, Button)TxtResult.Text += button.TextEnd SubPrivate Sub BtnOperator_Click(sender As Object, e As EventArgs) Handles BtnPlus.Click, BtnMinus.Click,BtnMultiply.Click, BtnDivide.ClickDim button As Button = CType(sender, Button)operatorFlag = button.Textoperand1 = Val(TxtResult.Text)TxtResult.Text = ""End SubPrivate Sub BtnEquals_Click(sender As Object, e As EventArgs) Handles BtnEquals.Clickoperand2 = Val(TxtResult.Text)Select Case operatorFlagCase "+"TxtResult.Text = (operand1 + operand2).ToStringCase "-"TxtResult.Text = (operand1 - operand2).ToStringCase "*"TxtResult.Text = (operand1 * operand2).ToStringCase "/"If operand2 <> 0 ThenTxtResult.Text = (operand1 / operand2).ToStringElseTxtResult.Text = "Cannot divide by zero"End IfEnd SelectEnd SubPrivate Sub BtnClear_Click(sender As Object, e As EventArgs) Handles BtnClear.ClickTxtResult.Text = ""End SubEnd Class```在上述代码中,Form上放置了一些按钮和一个文本框用于显示计算结果。
使用VB制作计算器程序
使用VB制作计算器程序计算器程序是一种用于进行数学计算的工具。
最常见的计算器是电子计算器,它可以实现简单的加减乘除等基本计算。
而现在,我们可以使用VB(即Visual Basic)编程语言制作一个计算器程序,使其在计算能力上有所提升。
首先,我们需要创建一个VB Windows Forms应用程序。
在创建项目时,我们可以选择桌面应用程序(Windows Forms)模板。
接下来,我们需要设计计算器的用户界面。
可以使用按钮、文本框等控件来实现基本的数字输入和计算。
假设我们的计算器有一个文本框用于显示输入和结果,并且有数字按钮和运算符按钮来输入表达式,并且还有一个等号按钮用于计算结果。
我们可以使用VB代码来实现各种按钮的功能。
以下是一个简单的计算器程序示例:```vbPublic Class Form1Dim num1 As Double ' 第一个操作数Dim num2 As Double ' 第二个操作数Dim result As Double ' 结果Dim operator As String ' 运算符'数字按钮的点击事件处理函数Private Sub NumberButton_Click(sender As Object, e As EventArgs) Handles Button1.Click, Button2.Click, Button3.Click, Button4.Click, Button5.Click, Button6.Click, Button7.Click, Button8.Click, Button9.Click, Button0.ClickDim number As Button = CType(sender, Button)TextBox1.Text += number.TextEnd Sub'运算符按钮的点击事件处理函数Private Sub OperatorButton_Click(sender As Object, e As EventArgs) Handles PlusButton.Click, MinusButton.Click, MultiplyButton.Click, DivideButton.ClickDim op As Button = CType(sender, Button)num1 = CDbl(TextBox1.Text)TextBox1.Clear[operator] = op.TextEnd Sub'等号按钮的点击事件处理函数Private Sub EqualsButton_Click(sender As Object, e As EventArgs) Handles EqualsButton.Clicknum2 = CDbl(TextBox1.Text)Select Case [operator]Case "+"result = num1 + num2Case "-"result = num1 - num2Case "*"result = num1 * num2Case "/"result = num1 / num2End SelectTextBox1.Text = result.ToStringEnd Sub'清除按钮的点击事件处理函数Private Sub ClearButton_Click(sender As Object, e As EventArgs) Handles ClearButton.ClickTextBox1.ClearEnd SubEnd Class```以上代码实现了简单的加减乘除功能,并将结果显示在文本框中。
用VB6.0编写一个小计算器
04
测试和调试
功能测试
测试所有基本功能
加、减、乘、除、平方、平方根等。
测试运算符优先级
确保遵循数学规则,如先乘除后加减。
测试数字范围
包括整数、小数和科学记数法表示的数字。
错误处理
输入验证
确保输入是数字,防止非法字符输入。
异常处理
在VB6.0中,可以使用"/"运算符进行除法 运算。为了实现除法运算,可以在按钮的 点击事件中编写相应的代码,例如: `result = num1 / num2`,其中`num1`和 `num2`是要相除的两个数,`result`是存储 结果的变量。需要注意的是,如果除数为0 会导致程序出错,因此需要进行除数为0的 判断。
谢谢观看
用VB6.0编写一个小计算器
目录
• 引言 • 设计计算器界面 • 实现计算器功能 • 测试和调试 • 总结和展望
01
引言
目的和背景
提高编程技能和实践能力
练习开发一个简单的计算 器应用程序
掌握VB6.0编程语言的基本 语法和控件
01
03 02
计算器概述
一个基本的计算器应 用程序应具备加、减、 乘、除运算功能
优化用户界面
虽然现在的用户界面已经比较友好,但我们可以 进一步优化它,使其更加美观和易用。例如,可 以调整按钮大小、颜色和布局,使其更加符合用 户审美和使用习惯。
扩展平台兼容性
为了使更多人能够使用这个小计算器,我们可以 考虑将其移植到其他平台,如手机或平板电脑。 这需要我们使用跨平台的开发框架和技术来实现 。
平方和平方根运算
总结词
实现平方和平方根运算的基本功能
计算器的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上数字的运算可以随便修改,与日常中的计算器存在很大差异。
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
用VB编写计算器
2007-01-09 10:36
一.实验目的
用vb语言编写一个简易计算器
二.实验要求
1.能够完成浮点数的加,减,乘,除;(平方等)
2.能够实现退格和清除功能;
3.初始值为0.0;
4.小数点不能重复输入;
5.高位数的0不出现;
6.应用控件数组实现。
三.控件属性列表
1、创建控件组的方法
a、首先创建一个命令按钮,调整其大小—宽、高为 495,名称为Command1,caption 属性为数字 0 。
b、然后进行“复制”和“粘贴”,当选择“粘贴”时,出现对话框提示已有一个同名控件,询问是否创建控件组,选择“是”后,即创建了一个名为“Command”的控件组。
这时,第一个按钮的Index属性值默认为“0”,第二个的Index属性值自动设为“1”,并且大小与第一个按钮相同,只需修改其 Caption 属性为数字“1”并将其拖至合适位置即可。
此后继续使用“粘贴”的方法建立其他控件组中其余按钮,共19个按钮,每建立一个,就将它拖到合适处,并修改相应的Caption属性值。
c、建立其他控件:如右图所示
2、各控件属性设置如下:
控件控件
控件名称 Caption 控件名称 Caption
窗体 Form 1 计算器按钮 Command 2(0) +
按钮 Command 3 退格按钮 Command 2(1) -
* 按钮 Command 4 . 按钮 Command 2(2)
按钮 Command 5 = 按钮 Command 2(3) /
按钮 command 6 + 按钮Command 7 ±
按钮 Command 1(0)~Command1(9) Caption 0 ~ 9 各个属性修改后得到如图所示的界面
四程序如下
Dim shu1 As Single, shu2 As Single, suanfu As String
'定义两个单精度数变量用与存放参与运算的数,一个字符型存放运算符
Private Sub Command1_Click(Index As Integer)
Text1.Text = Text1.Text & Command1(Index).Caption
'将command1的单击事件与文本框显示的内容连接
End Sub
Private Sub Command2_Click(Index As Integer)
shu1 = Val(Text1.Text) '将shu1隐藏起来
suanfu = Command2(Index).Caption
Text1.Text = ""
End Sub
Private Sub Command4_Click()
Text1.Text = Text1.Text + "."
If (InStr(Text1.Text, ".") = 1) Then '第一位不能为小数 Text1.Text = ""
End If
If InStr(Text1.Text, ".") < Len(Text1.Text) Then
'防止出现两个小数点
Text1.Text = Left(Text1.Text, Len(Text1.Text) - 1) End If
End Sub
Private Sub Command5_Click() '开始加减乘除的运算
shu2 = Val(Text1.Text)
Select Case suanfu
Case "+"
Text1.Text = shu1 + shu2
Case "-"
Text1.Text = shu1 - shu2
Case "*"
Text1.Text = shu1 * shu2
Case "/"
If shu2 = 0 Then
MsgBox "分母不能为零!", 1 + 32 + 0,
"错误" '错误提示框图下所示
Text1.Text = ""
Else
Text1.Text = shu1 / shu2
End If
End Select
End Sub
Private Sub Command3_Click() '假如输入错误,可每次退后一格If Text1.Text = "" Then
Exit Sub
End If
Text1.Text = Left(Text1.Text, Len(Text1.Text) - 1)
End Sub
Private Sub Command6_Click()
Text1.Text = "" '清除
End Sub
Private Sub Command7_Click() '平方运算
Text1.Text = Text1.Text * Text1.Text
End Sub
Private Sub Command8_Click()
If Left(Text1.Text, 1) <> "-" Then
Text1.Text = "-" & Text1.Text
Else
Text1.Text = Right(Text1.Text, Len(Text1.Text) - 1) End If
End Sub
Private Sub Form_Click()
a = Int(Rnd() * 255)
b = Int(Rnd() * 255)
c = Int(Rnd() * 255)
Form1.BackColor = RGB(a, b, c)
End Sub。