计算器VB程序_第1页
计算器VB程序_第2页
计算器VB程序_第3页
计算器VB程序_第4页
计算器VB程序_第5页
已阅读5页,还剩11页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、计算器VB程序 这个程序包括5个单选按钮,12个命令按钮和一个文本框。 如果你学了数组以后,10个数字键就可以打一行代码了。不需要这么麻烦(我给你的第二种代码中的就是)第一种代码:Dim flag As Integer 通用&声明 Dim s1 As String Private Sub Command1_Click() '数字“1” Text1.Text = Text1.Text + Str$(1) End Sub Private Sub Command10_Click() '数字“0” Text1.Text = Text1.Text + Str$(0) End Su

2、b if isjd then m-tb-out.text=str(sin(val(m-tb-out.text)/180)*pi)elsem-tb-out.text=str(sin(val(m-tb-out.text)Private Sub Command11_Click() '点号“.” Text1.Text = Text1.Text + "." End Sub Private Sub Command2_Click() '数字“2” Text1.Text = Text1.Text + Str$(2) End Sub Private Sub Command3_

3、Click() '数字“3” Text1.Text = Text1.Text + Str$(3) End Sub Private Sub Command4_Click() '数字“4” Text1.Text = Text1.Text + Str$(4) End Sub Private Sub Command5_Click() '数字“5” Text1.Text = Text1.Text + Str$(5) End Sub Private Sub Command6_Click() '数字“6” Text1.Text = Text1.Text + Str$(6) E

4、nd Sub Private Sub Command7_Click() '数字“7” Text1.Text = Text1.Text + Str$(7) End Sub Private Sub Command8_Click() '数字“8” Text1.Text = Text1.Text + Str$(8) End Sub Private Sub Command9_Click() '数字“9” Text1.Text = Text1.Text + Str$(9) End Sub Private Sub opt1_Click() 'opt1 为 + 加运算 If f

5、lag = -1 Then '这里通过flag的值判断前一步是否点击过运算符 s1 = Text1.Text '如果没有点击过运算符执行这里 Text1.Text = "" flag = 1 '表示加法运算 Else '如果前一步没有点击过运算符则执行者里 s2 = Text1.Text Select Case flag '这里通过flag的值判断前一步的运算符,并执行相应的运算. Case 1 s1 = Val(s1) + Val(s2) Case 2 s1 = Val(s1) - Val(s2) Case 3 s1 = Val(s

6、1) * Val(s2) Case 4 s1 = Val(s1) / Val(s2) End Select Text1.Text = "" flag = 1 End If End Sub Private Sub opt2_Click() 'opt2 为 - 减运算 If flag = -1 Then s1 = Text1.Text Text1.Text = "" flag = 2 '表示减法运算 Else s2 = Text1.Text Select Case flag Case 1 s1 = Val(s1) + Val(s2) Case

7、 2 s1 = Val(s1) - Val(s2) Case 3 s1 = Val(s1) * Val(s2) Case 4 s1 = Val(s1) / Val(s2) End Select Text1.Text = "" flag = 2 End If End Sub Private Sub opt3_Click() 'opt3 为 * 乘运算 If flag = -1 Then s1 = Text1.Text Text1.Text = "" flag = 3 '表示乘法运算 Else s2 = Text1.Text Select C

8、ase flag Case 1 s1 = Val(s1) + Val(s2) Case 2 s1 = Val(s1) - Val(s2) Case 3 s1 = Val(s1) * Val(s2) Case 4 s1 = Val(s1) / Val(s2) End Select Text1.Text = "" flag = 3 End If End Sub Private Sub opt4_Click() 'opt4 为 / 除运算 If flag = -1 Then s1 = Text1.Text Text1.Text = "" flag =

9、4 '表示除法运算 Else s2 = Text1.Text Select Case flag Case 1 s1 = Val(s1) + Val(s2) Case 2 s1 = Val(s1) - Val(s2) Case 3 s1 = Val(s1) * Val(s2) Case 4 s1 = Val(s1) / Val(s2) End Select Text1.Text = "" flag = 4 End If End Sub Private Sub Command12_Click() 开始运算先点击一下这个键&复位按钮或者是开始按钮& Tex

10、t1.Text = "" flag = -1 End Sub Private Sub opt5_Click() 'command14 为 = “运算结果” s2 = Text1.Text Text1.Text = "" Select Case flag Case 1 s3 = Val(s1) + Val(s2) Text1.Text = Str$(s3) Case 2 s3 = Val(s1) - Val(s2) Text1.Text = Str$(s3) Case 3 s3 = Val(s1) * Val(s2) Text1.Text = St

11、r$(s3) Case 4 s3 = Val(s1) / Val(s2) Text1.Text = Str$(s3) End Select flag = -1 End Sub第二种代码:1、创建控件组的方法 a、首先创建一个命令按钮,调整其大小 宽、高为 495,名称为Command1,aption 属性为数字 0 。 b、然后进行“复制”和“粘贴”,当选择“粘贴”时,出现对话框提示已有一个同名控件,询问是否创建控件组,选择“是”后,即创建了一个名为“Command”的控件组。这时,第一个按钮的Index属性值默认为“0”,第二个的Index属性值自动设为“1”,并且大小与第一个按钮相同,只需

12、修改其 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) / 按钮 com

13、mand 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

14、 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 = "

15、" 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 "-&

16、quot; 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() '假如输

17、入错误,可每次退后一格 If Text1.Text = "0" Then Exit Sub End If Text1.Text = Left(Text1.Text, Len(Text1.Text) - 1) End Sub Private Sub Command6_Click() Text1.Text = "0" '清除 End Sub Private Sub Command7_Click() '平方运算 Text1.Text = Text1.Text * Text1.Text End Sub Private Sub Command8_C

18、lick() If Left(Text1.Text, 1) <> "-" Then Text1.Text = "-" & Text1.Text Else Text1.Text = Right(Text1.Text, Len(Text1.Text) - 1) End If End Subvb设计简易计算器用vb编写简单计算器 说明:计算器所包含的功能只需:加、减、乘、除的基本运算。 有"计算" "清除" "结束"三个按钮,当除数为零时,可有提示:"除数不能为零 人气:

19、13 解决时间:2008-12-13 22:31Dim a As Integer, b As Double, c As DoublePrivate Sub Command1_Click(Index As Integer)Select Case IndexCase 1, 2, 3, 4, 5, 6, 7, 8, 9, 0Text1.Text = Text1.Text & IndexCase 10If InStr(Text1.Text, ".") = flase ThenText1.Text = Text1.Text + "."End IfCase

20、12, 13, 14, 15c = Indexb = Val(Text1.Text)Text1.Text = ""Case 11If c = 12 ThenText1.Text = Str(Val(Text1.Text) + b)End IfIf c = 13 ThenText1.Text = Str(b - Val(Text1.Text)End IfIf c = 14 ThenText1.Text = Str(Val(Text1.Text) * b)End IfIf c = 15 ThenIf Val(Text1.Text) = 0 ThenMsgBox "分母

21、不能为0", 64, "提示!"Text1.Text = ""ElseText1.Text = Str(b / Val(Text1.Text)End IfEnd IfEnd SelectEnd SubPrivate Sub Command2_Click(Index As Integer)Select Case IndexCase 0Text1.Text = ""Case 1Text1.Text = ""a = Len(Text1.Text)If Len(Text1.Text) > 1 ThenTex

22、t1.Text = Left(Text1.Text, a - 1)End IfCase 2EndCase 3Text1.Text = "0"Text1.SetFocusEnd Select转载请注明本篇文章来源于 电脑知识精华() 原文出处:Private Function HEX_to_DEC(ByVal Hex As String) As LongDim i As LongDim B As LongHex = UCase(Hex)For i = 1 To Len(Hex)Select Case Mid(Hex, Len(Hex) - i + 1, 1)Case &quo

23、t;0": B = B + 16 (i - 1) * 0Case "1": B = B + 16 (i - 1) * 1Case "2": B = B + 16 (i - 1) * 2Case "3": B = B + 16 (i - 1) * 3Case "4": B = B + 16 (i - 1) * 4Case "5": B = B + 16 (i - 1) * 5Case "6": B = B + 16 (i - 1) * 6Case "7&qu

24、ot;: B = B + 16 (i - 1) * 7Case "8": B = B + 16 (i - 1) * 8Case "9": B = B + 16 (i - 1) * 9Case "A": B = B + 16 (i - 1) * 10Case "B": B = B + 16 (i - 1) * 11Case "C": B = B + 16 (i - 1) * 12Case "D": B = B + 16 (i - 1) * 13Case "E"

25、;: B = B + 16 (i - 1) * 14Case "F": B = B + 16 (i - 1) * 15End SelectNext iHEX_to_DEC = BEnd FunctionPrivate Sub Command1_Click() '将text1里面的十六进制数转化为十进制Text1.Text = HEX_to_DEC(Text1.Text)End SubVB进制转换大全' 用途:将十进制转化为二 进 制' 输入:Dec(十进制数)' 输入数据类型:Long' 输出:DEC_to_BIN(二 进 制数)&#

26、39; 输出数据类型:StringPublic Function DEC_to_BIN(Byval Dec As Long) As String DEC_to_BIN = "" Do While Dec > 0 DEC_to_BIN = Dec Mod 2 & DEC_to_BIN Dec = Dec 2 LoopEnd Function' 用途:将二 进 制转化为十进制' 输入:Bin(二 进 制数)' 输入数据类型:String' 输出:BIN_to_DEC(十进制数)' 输出数据类型:LongPublic Func

27、tion BIN_to_DEC(ByVal Bin As String) As Long Dim i As Long For i = 1 To Len(Bin) BIN_to_DEC = BIN_to_DEC * 2 + Val(Mid(Bin, i, 1) Next iEnd Function' 用途:将十六进制转化为二 进 制' 输入:Hex(十六进制数)' 输入数据类型:String' 输出:HEX_to_BIN(二 进 制数)' 输出数据类型:String' 输入的最大数为2147483647个字符Public Function HEX_

28、to_BIN(ByVal Hex As String) As String Dim i As Long Dim B As String Hex = UCase(Hex) For i = 1 To Len(Hex) Select Case Mid(Hex, i, 1) Case "0": B = B & "0000" Case "1": B = B & "0001" Case "2": B = B & "0010" Case "3":

29、 B = B & "0011" Case "4": B = B & "0100" Case "5": B = B & "0101" Case "6": B = B & "0110" Case "7": B = B & "0111" Case "8": B = B & "1000" Case "9": B = B

30、 & "1001" Case "A": B = B & "1010" Case "B": B = B & "1011" Case "C": B = B & "1100" Case "D": B = B & "1101" Case "E": B = B & "1110" Case "F": B = B &

31、 "1111" End Select Next i While Left(B, 1) = "0" B = Right(B, Len(B) - 1) Wend HEX_to_BIN = BEnd Function' 用途:将二 进 制转化为十六进制' 输入:Bin(二 进 制数)' 输入数据类型:String' 输出:BIN_to_HEX(十六进制数)' 输出数据类型:String' 输入的最大数为2147483647个字符Public Function BIN_to_HEX(ByVal Bin As Str

32、ing) As String Dim i As Long Dim H As String If Len(Bin) Mod 4 <> 0 Then Bin = String(4 - Len(Bin) Mod 4, "0") & Bin End If For i = 1 To Len(Bin) Step 4 Select Case Mid(Bin, i, 4) Case "0000": H = H & "0" Case "0001": H = H & "1" Ca

33、se "0010": H = H & "2" Case "0011": H = H & "3" Case "0100": H = H & "4" Case "0101": H = H & "5" Case "0110": H = H & "6" Case "0111": H = H & "7" Case &qu

34、ot;1000": H = H & "8" Case "1001": H = H & "9" Case "1010": H = H & "A" Case "1011": H = H & "B" Case "1100": H = H & "C" Case "1101": H = H & "D" Case "111

35、0": H = H & "E" Case "1111": H = H & "F" End Select Next i While Left(H, 1) = "0" H = Right(H, Len(H) - 1) Wend BIN_to_HEX = HEnd Function' 用途:将十六进制转化为十进制' 输入:Hex(十六进制数)' 输入数据类型:String' 输出:HEX_to_DEC(十进制数)' 输出数据类型:Long' 输入的

36、最大数为7FFFFFFF,输出的最大数为2147483647Public Function HEX_to_DEC(ByVal Hex As String) As Long Dim i As Long Dim B As Long Hex = UCase(Hex) For i = 1 To Len(Hex) Select Case Mid(Hex, Len(Hex) - i + 1, 1) Case "0": B = B + 16 (i - 1) * 0 Case "1": B = B + 16 (i - 1) * 1 Case "2":

37、 B = B + 16 (i - 1) * 2 Case "3": B = B + 16 (i - 1) * 3 Case "4": B = B + 16 (i - 1) * 4 Case "5": B = B + 16 (i - 1) * 5 Case "6": B = B + 16 (i - 1) * 6 Case "7": B = B + 16 (i - 1) * 7 Case "8": B = B + 16 (i - 1) * 8 Case "9"

38、: B = B + 16 (i - 1) * 9 Case "A": B = B + 16 (i - 1) * 10 Case "B": B = B + 16 (i - 1) * 11 Case "C": B = B + 16 (i - 1) * 12 Case "D": B = B + 16 (i - 1) * 13 Case "E": B = B + 16 (i - 1) * 14 Case "F": B = B + 16 (i - 1) * 15 End Select

39、Next i HEX_to_DEC = BEnd Function' 用途:将十进制转化为十六进制' 输入:Dec(十进制数)' 输入数据类型:Long' 输出:DEC_to_HEX(十六进制数)' 输出数据类型:String' 输入的最大数为2147483647,输出最大数为7FFFFFFFPublic Function DEC_to_HEX(Dec As Long) As String Dim a As String DEC_to_HEX = "" Do While Dec > 0 a = CStr(Dec Mod

40、16) Select Case a Case "10": a = "A" Case "11": a = "B" Case "12": a = "C" Case "13": a = "D" Case "14": a = "E" Case "15": a = "F" End Select DEC_to_HEX = a & DEC_to_HEX Dec =

41、 Dec 16 LoopEnd Function' 用途:将十进制转化为八进制' 输入:Dec(十进制数)' 输入数据类型:Long' 输出:DEC_to_OCT(八进制数)' 输出数据类型:StringPublic Function DEC_to_OCT(Byval Dec As Long) As String DEC_to_OCT = "" Do While Dec > 0 DEC_to_OCT = Dec Mod 8 & DEC_to_OCT Dec = Dec 8 LoopEnd Function' 用途

42、:将八进制转化为十进制' 输入:Oct(八进制数)' 输入数据类型:String' 输出:OCT_to_DEC(十进制数)' 输出数据类型:LongPublic Function OCT_to_DEC(ByVal Oct As String) As Long Dim i As Long Dim B As Long For i = 1 To Len(Oct) Select Case Mid(Oct, Len(Oct) - i + 1, 1) Case "0": B = B + 8 (i - 1) * 0 Case "1":

43、B = B + 8 (i - 1) * 1 Case "2": B = B + 8 (i - 1) * 2 Case "3": B = B + 8 (i - 1) * 3 Case "4": B = B + 8 (i - 1) * 4 Case "5": B = B + 8 (i - 1) * 5 Case "6": B = B + 8 (i - 1) * 6 Case "7": B = B + 8 (i - 1) * 7 End Select Next i OCT_to_D

44、EC = BEnd Function' 用途:将二 进 制转化为八进制' 输入:Bin(二 进 制数)' 输入数据类型:String' 输出:BIN_to_OCT(八进制数)' 输出数据类型:String' 输入的最大数为2147483647个字符Public Function BIN_to_OCT(ByVal Bin As String) As String Dim i As Long Dim H As String If Len(Bin) Mod 3 <> 0 Then Bin = String(3 - Len(Bin) Mod

45、3, "0") & Bin End If For i = 1 To Len(Bin) Step 3 Select Case Mid(Bin, i, 3) Case "000": H = H & "0" Case "001": H = H & "1" Case "010": H = H & "2" Case "011": H = H & "3" Case "100": H = H & "4" Case "101": H = H & "5" Ca

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论