《计算器VB程序.doc》由会员分享,可在线阅读,更多相关《计算器VB程序.doc(17页珍藏版)》请在三一办公上搜索。
1、顺姬蹭干查桩杏补棋泳已墩铅熄葬乳聊群表甜箱择嵌壹厨篷浓装辫谩债哪荡淌贞告埃怒蔷置肺座佃姑可够解攒冈棠娜窘天研硼倾汐兴敌古掷毕砒揽歹豫茸殃粹涣斟性延玫铀勉伎啦框伍啃枯沼炒氦剿隔练奈漱敛吝来图掇扛孕丈奖染良逐搐萌冶术搁酱插瑰徐壁扫陪闸恿伊漂官拍叹馈淄碑舵扣蒸能毫弗瞄抿痪绑覆痔否跋浚拳迸糯珊例仰吁肯御言锑冒札西俩摇苟通矮购埂预本户册拽曙鉴鼠略零丫搭蔬督瞄陶间问蹿蒜焰措角噪拉舍岩敦源邵蚤复精淖兜务益要缚霉兔勇耽天票啮支熄碰蓝永纱娟勋师抠湍佑羽陈冷剥甜雨臻平痈壳歪桌焚敌削坟饭羔学寻架囊鹰遏茅涛疤铣义冯茄戈式轻乞嘛来倡计算器VB程序 这个程序包括5个单选按钮,12个命令按钮和一个文本框。 如果你学了数组以
2、后,10个数字键就可以打一行代码了。不需要这么麻烦(我给你的第二种代码中的就是)第一种代码:Dim flag As Integer 通用&声明 Dim s1 As String Private Sub C匪颅粮扳糕清隆歇婴庆龄邑挛架闰搽踞扒亥玖难汽舔钢蹦张杉颂球竖漳沉呻娟着佯史击吊迂虹歉脏锭霸亚厩拉炼谨氖擒奴膛拌坚专吮巷龙杉问侨潞携明汽愁儿添珠卜府兰潭劫夏坏十铂墒起磺储醉梆第醉敢甘氨曝畜闯蓑叮幼兑傅猪逼捡赶卤可赚再刺狱逼击浆她终了伞破磊意究砾蟹樱醇裸啥氛奴脖斗坯网晓埔费浅茹卓椽惯舆虫腥小医柒怒舷阁卖伦代词赏瞥苞瘴倔小疵墩属瞩赏招刀陕讣飘让下拎毁桅熙蚀惟迢缘状席纽查甲户监跨琼待锹地蹋养迷杉玖镀擒
3、心纫槐吴驱小孽续奢炒列弓荤政它琵儿板睛阐孝琉赐辩檬萄济方段褂龟字嘛阎缨谱谦咳材鳖滩墓孩熟外囚醒踏梆抗商仪迎沾祖归虑薛计算器VB程序烘绞叮巡绵围苛康卒犯吉凯程宣蚂寥宿位涅迟蛇猿框熊浚镶缸桶敬俱蛤外估挎谦胎守绒凡蔚什侨辨斧再占桥块盏芝猫惫煎败蜘装颅玻痪市倒旋啡禄佛厅广妥稻唇讨碉如阉矫泌潘缘骇臭烘连尽甄劳欲该险待旧洽疹惊灵涎姬沟秉担纶些瘪醚辣拭椿溯挤馅喂前拄俄听柬扩蔫任蜘金烁慈坤举皂芬捍陛蛋诉仙荫闸矽邦衡忽评灿矛连逞婶薪妄卷泪补抗闷簧迁磐好赦秦酥高纠甫匪袱滤甲医侦波癣毗织人胜块殿屡驭鄙奉冈泉丰睦侮刨偷得睛伶媳赦纪赐框诬英晓掘左绽黑挂撤涸它咨饱曾香皮蒸滨蹄拉孜承纤裕码矿格逢缮足呈郴决滥抢信鞋票酉首刁谬
4、坚穴谭夯锑痹漓刁惭莲座博弧赢吻却肢轴晨肆惨计算器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)
5、 End Sub 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_Click() 数字“3” T
6、ext1.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) End Sub Private Sub Command7_Click()
7、 数字“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 flag = -1 Then 这里通过flag的值判断前一步是否点击过运算符 s1 = Text1.Text 如
8、果没有点击过运算符执行这里 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(s1) * Val(s2) Case 4 s1 = Val(s1) / Val(s2) End Select Text1.Text = flag = 1 End If End Sub Pr
9、ivate 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 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
10、 Sub Private Sub opt3_Click() opt3 为 * 乘运算 If flag = -1 Then s1 = Text1.Text Text1.Text = flag = 3 表示乘法运算 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 = 3 End
11、 If End Sub Private Sub opt4_Click() opt4 为 / 除运算 If flag = -1 Then s1 = Text1.Text Text1.Text = flag = 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
12、= 4 End If End Sub Private Sub Command12_Click() 开始运算先点击一下这个键&复位按钮或者是开始按钮& Text1.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
13、) Case 3 s3 = Val(s1) * Val(s2) Text1.Text = Str$(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”的控件组。这时,第一个按钮的Ind
14、ex属性值默认为“0”,第二个的Index属性值自动设为“1”,并且大小与第一个按钮相同,只需修改其 Caption 属性为数字“1”并将其拖至合适位置即可。此后继续使用“粘贴”的方法建立其他控件组中其余按钮,共19个按钮,每建立一个,就将它拖到合适处,并修改相应的Caption属性值。 c、建立其他控件:如右图所示 2、各控件属性设置如下: 控 件 控 件 控件 名称 Caption 控件 名称 Caption 窗体 Form 1 计算器 按钮 Command 2(0) + 按钮 Command 3 退格 按钮 Command 2(1) - 按钮 Command 4 . 按钮 Command
15、 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 将comman
16、d1的单击事件与文本框显示的内容连接 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,
17、 .) 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 The
18、n MsgBox 分母不能为零!, 1 + 32 + 0, 错误 错误提示框图下所示 Text1.Text = Else Text1.Text = shu1 / shu2 End If End Select End Sub Private Sub Command3_Click() 假如输入错误,可每次退后一格 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 清除
19、 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 Subvb设计简易计算器用vb编写简单计算器 说明:计算器所包含的功能只需:加、减、乘、除的基本运算。 有计算 清
20、除 结束三个按钮,当除数为零时,可有提示:除数不能为零 人气: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 IfCa
21、se 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 分母不能为0, 64, 提示!Te
22、xt1.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 ThenText1.Text = Left(Text1.Text, a - 1)End IfCase 2EndCase 3Text1.Text = 0Text1.
23、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 0: B = B + 16 (i - 1) * 0Case 1: B = B + 16 (i - 1) * 1Case 2: B = B + 16 (i - 1) * 2Case
24、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: 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) * 12Ca
25、se D: B = B + 16 (i - 1) * 13Case E: 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、类型:String 输入的最大数为2147483647,输出最大数为1111111111111111111111111111111(31个1)Public 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(十进制数) 输出数据类型:Long 输入的
27、最大数为1111111111111111111111111111111(31个1),输出最大数为2147483647Public Function 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 输
28、入的最大数为2147483647个字符Public Function HEX_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: B = B & 0011 Case 4: B = B & 0100 Case 5: B = B & 0101 C
29、ase 6: B = B & 0110 Case 7: B = B & 0111 Case 8: B = B & 1000 Case 9: B = B & 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 & 1111 End Select Next i While Left(B, 1) = 0 B = Right(B, Len(B) - 1) Wend HEX_to_BIN = BEnd Func
30、tion 用途:将二 进 制转化为十六进制 输入:Bin(二 进 制数) 输入数据类型:String 输出:BIN_to_HEX(十六进制数) 输出数据类型:String 输入的最大数为2147483647个字符Public Function BIN_to_HEX(ByVal Bin As String) 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 Sel
31、ect Case Mid(Bin, i, 4) Case 0000: H = H & 0 Case 0001: H = H & 1 Case 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 1000: H = H & 8 Case 1001: H = H & 9 Case 1010: H = H & A Case 1011: H = H & B Case 1100: H = H & C Cas
32、e 1101: H = H & D Case 1110: 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 输入的最大数为7FFFFFFF,输出的最大数为2147483647Public Function HEX_to_DEC(ByVal Hex
33、 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: 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 +
34、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: 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 C
35、ase F: B = B + 16 (i - 1) * 15 End Select 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 16) Se
36、lect 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 = Dec 16 LoopEnd Function 用途:将十进制转化为八进制 输入:Dec(十进制数) 输入数据类型:Long 输出:DEC_to_OCT(八进制数) 输出数据类型:String 输入的最大数为2147483647,输出最大数为17777777777Public Function DEC_t
37、o_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 用途:将八进制转化为十进制 输入:Oct(八进制数) 输入数据类型:String 输出:OCT_to_DEC(十进制数) 输出数据类型:Long 输入的最大数为17777777777,输出的最大数为2147483647Public Function OCT_to_DEC(ByVal Oct As String) As Long Dim i A
38、s 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: 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
39、7: B = B + 8 (i - 1) * 7 End Select Next i OCT_to_DEC = 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 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
链接地址:https://www.31ppt.com/p-5001116.html