VB编写简易计算器(附图)

合集下载

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

《VB》课内实验报告学生姓名:及学号:学院:班级: 数学101课程名称:VB实验题目:控件数组的应用指导教师姓名及职称:2012年03月28日目录一、实验目的 (1)二、实验内容 (1)三、实验要点及说明 (1)四、实现方法 (1)五、实验结果 (2)六、源程序清单 (2)七、思考及总结 (4)一、实验目的1.掌握动态数组与静态数组的使用方法。

2.掌握控件数组的添加、删除以及使用方法。

3.掌握If语句、Select Case语句以及循环语句的使用。

4.掌握控件数组中索引号(Index)的作用。

二、实验内容1.制作计算器,完成十进制的加减乘除运算。

2.随机产生15个不重复的A-Z(包括A,Z)的大写字母,存放在字符数组中。

三、实验要点及说明1.利用数组控件完成计算器的制作。

(1)设计界面及设置属性界面设计中的显示部分要求用标签实现;运算符、数字分别使用控件数组实现;(2)编写代码图3-1 设计界面程序代码的任务是单击数字按钮和运算符按钮实现十进制的常规运算,同时要求做到实现正负号的转变、结果的标准输出以及除法中除数的检查等功能。

2.界面设计部分要求能够体现出字符数组中的内容,且实现随机不重复显示的功能,具体参数不做要求。

四、实现方法运行环境:Visual Basic运行开发环境;1.VB6.0窗体的属性设置运用复制的方法建立两个建立控件数组,编写代码。

2. 设置窗体的属性,添加一个文本框MultiLine属性设置成True,编写代码。

五、实验结果1. 能够完成十进制的加减乘除运算,可以判断被除数是否为零,如果为零,弹出对话框提示错误。

2.单击窗体随机出现15个不同的字母。

六、源程序清单1、源程序:Option Explicit ‘对所有变量进行显式声明Dim Num1 As Single, Num2 As Single ‘声明Num1,Num2为单精度浮点型变量Dim Flag As Boolean ‘声明Flag为布尔变量Dim Cul As Integer ‘声明Cul为整型变量Private Sub Command1_Click(Index As Integer) ‘Command1的单击事件Num1 = Val(Text1.Text) ‘Text1中的值赋给Num1Flag = True ‘Flag为真Cul = IndexEnd SubPrivate Sub _Click() ‘Command4的单击事件If InStr(Text1.Text, ".") = 0 And Text1.Text <> "" Then ‘如果Text1中没Text1.Text = Text1.Text + "." 有"."并且不为空,就在Text1中加一个"."End IfEnd SubPrivate Sub Command2_Click(Index As Integer)If Flag Then Text1.Text = "": Flag = False ‘如果Flag为真则Text1赋为空Text1.Text = Text1.Text + Command2(Index).Caption ‘如果为假Text1加End Sub Command2(Index) Private Sub Command5_Click() ‘Command5的单击事件的单击事件Text1.Text = "" ‘清空Text1,Num1, Num2的值Num1 = 0Num2 = 0End SubPrivate Sub Command6_Click() ‘Command6的单击事件Dim result As Single ‘声明result为单精度浮点型变量Num2 = Val(Text1.Text) ‘Text1中的值赋给Num2Flag = True ‘Flag b变为真Select Case Cul ‘Select循环Case 0: result = Num1 + Num2Case 1: result = Num1 - Num2Case 2: result = Num1 * Num2Case 3:If Num2 <> 0 Then ‘若分母为零提示result = Num1 / Num2ElseMsgBox "出错!被0除!"End IfEnd SelectIf result \ 1 <> result Then ‘如果result \ 1不等于result那么在result前Text1.Text = Format(result, "0.#") 加”0.”赋给Text1.TextElseText1.Text = Str(result) ‘否则直接将result转化成字符串赋给Text1.TextEnd IfEnd SubPrivate Sub Command3_Click() ‘Command3的单击事件If Text1.Text <> "" Then ‘如果Text1.Text不是空的If InStr( Text1.Text, "-") = 0 Then ‘Text1.Text中不包含"-"Text1.Text = "-" + Text1.Text ‘把"-" Text1.Text连接后赋给Text1.Text ElseText1.Text = Right(Text1.Text, Len(Text1.Text) - 1)End If ‘取Text1.Text中长度减1的字符赋给Text1.Text End IfEnd Sub2、源程序Private Sub Form_Click() ‘Form的单击事件Dim s(1 To 15) As String * 1, c As String * 1 ‘声明s,c为定长字符串Dim Found As Boolean ‘声明Found为布尔变量s(1) = chr(Int(Rnd * 26 + 65)) ‘将一随机字符赋给s(1)n = 2Do While n <= 15 ‘循环条件n<=15c = chr(Int(Rnd * 26 + 65)) ‘将一随机字符赋给cFound = FalseFor j = 1 To n – 1 ‘For循环If s(j) = c Then Found = TrueNext jIf Not Found Then ‘Found为假,进行下两行语句s(n) = cn = n + 1End IfLoopFor i = 1 To 15 ‘循环输出s(i)到Text1.Text中Text1.Text = Text1.Text & s(i)Next iEnd Sub七、思考及总结。

用VB6.0编写一个小计算器

用VB6.0编写一个小计算器

在左侧工具栏中,选择输入框,在窗口界面中
设置三个合适大小的输入框
制作中...
修改输入框内文字:选择输入框在属性处找到
text,将右边文字修改。
添加一个按钮,名称和软件名相同改法

双击软件界面 提示:一号框(左上)×二号框(右上)=三号框 (左下),我们要输入:(注意空格)
PrivateSubCommand1_Click() OnErrorGoto1
用 VB6.0 编写一个小计算器
y2a3d 计算器在线计算 /
教大家制作一个小软件——乘法计算器软件
VB6.0 方法
打开 VB6.0 选择“标准 EXE”
首先,在属性中的 caption 处,将 Form1(软
件名)改为自己喜爱的名字
按住窗口的调整按钮,将窗口调整为合适大小
Text3.Text=Text1.Text*Text2.Text ExitSub
1: msgbox 请输入正确数据,,出错啦...
EndSub 点击文件→生成工程
生成成功! 上面框内输入乘数,点击按钮,计算出结果显 示在下面框内!
注意事项 千万注意代码中的空格

VB编写简单计算器程序

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写简易计算器附图

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制作计算器程序

使用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简单计算器编程代码(附图)

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

课程设计说明书正文一、题目:计算器的创作和相应程序的编写二、本题的主要功能:通过计算器的创作熟悉各控件的属性和练习程序的编写。

三、程序截图:四、源程序清单:Begin VB.Form Form1Caption = "计算器"ClientHeight = 3765ClientLeft = 165ClientTop = 855ClientWidth = 5355Icon = "Form1.frx":0000LinkTopic = "Form1"LockControls = -1 'TrueScaleHeight = 3765ScaleWidth = 5355StartUpPosition = 3 '窗口缺省Begin mandButton Command4Caption = "="Height = 495Left = 4470TabIndex = 28Top = 3060Width = 735EndBegin mandButton Command3 Caption = "1/x"Height = 495Left = 4470TabIndex = 27Top = 2520Width = 735EndBegin mandButton Command2 Caption = "%"Height = 495Left = 4470TabIndex = 26Top = 1980Width = 735EndBegin mandButton Command1 Caption = "sqrt"Height = 495Left = 4470TabIndex = 25Top = 1440Width = 735EndBegin mandButton cmbDOT Caption = "."Height = 495Left = 2910TabIndex = 24Top = 3060Width = 735EndBegin mandButton cmbZF Caption = "+/-"Height = 495Left = 2130TabIndex = 23Top = 3060Width = 735 EndBegin mandButton cmbSign Caption = "+"Height = 495Index = 3Left = 3690TabIndex = 22Top = 3060Width = 735 EndBegin mandButton cmbSign Caption = "-"Height = 495Index = 2Left = 3690TabIndex = 21Top = 2520Width = 735 EndBegin mandButton cmbSign Caption = "*"Height = 495Index = 1Left = 3690TabIndex = 20Top = 1980Width = 735EndBegin mandButton cmbSign Caption = "/"Height = 495Index = 0Left = 3690TabIndex = 19Top = 1440Width = 735EndBegin mandButton cmbNUM Caption = "9"Height = 495Index = 9Left = 2910TabIndex = 18Top = 1440Width = 735EndBegin mandButton cmbNUM Caption = "8"Height = 495Index = 8Left = 2130TabIndex = 17Top = 1440Width = 735EndBegin mandButton cmbNUM Caption = "7"Height = 495Index = 7Left = 1350TabIndex = 16Width = 735EndBegin mandButton cmbNUM Caption = "6"Height = 495Index = 6Left = 2910TabIndex = 15Top = 1980Width = 735EndBegin mandButton cmbNUM Caption = "5"Height = 495Index = 5Left = 2130TabIndex = 14Top = 1980Width = 735EndBegin mandButton cmbNUM Caption = "4"Height = 495Index = 4Left = 1350TabIndex = 13Top = 1980Width = 735EndBegin mandButton cmbNUM Caption = "3"Height = 495Index = 3TabIndex = 12Top = 2520Width = 735EndBegin mandButton cmbNUM Caption = "2"Height = 495Index = 2Left = 2130TabIndex = 11Top = 2520Width = 735EndBegin mandButton cmbNUM Caption = "1"Height = 495Index = 1Left = 1350TabIndex = 10Top = 2520Width = 735EndBegin mandButton cmbNUM Caption = "0"Height = 495Index = 0Left = 1350TabIndex = 9Top = 3060Width = 735EndBegin mandButton cmbMa Caption = "M+"Left = 150TabIndex = 8Top = 3060Width = 975 EndBegin mandButton cmbMS Caption = "MS"Height = 495Left = 150TabIndex = 7Top = 2520Width = 975 EndBegin mandButton cmbMR Caption = "MR"Height = 495Left = 150TabIndex = 6Top = 1980Width = 975 EndBegin mandButton cmbMC Caption = "MC"Height = 495Left = 150TabIndex = 5Top = 1440Width = 975 EndBegin mandButton cmbC Caption = "C"Height = 495Left = 4020Top = 690Width = 1155EndBegin mandButton cmbCECaption = "CE"Height = 495Left = 2670TabIndex = 3Top = 690Width = 1155EndBegin mandButton cmbbackspace Caption = "Backspace"Height = 495Left = 1380TabIndex = 2Top = 690Width = 1155EndBegin VB.TextBox Text1Alignment = 1 'Right Justify Height = 375Left = 210TabIndex = 0Text = "0."Top = 120Width = 4935EndBegin bel Label2Alignment = 2 'CenterHeight = 255Left = 360TabIndex = 29Top = 840Width = 375EndBegin bel Label1Alignment = 2 'CenterBorderStyle = 1 'Fixed SingleBeginProperty FontName = "宋体"Size = 14.25Charset = 134Weight = 400Underline = 0 'FalseItalic = 0 'FalseStrikethrough = 0 'False EndPropertyHeight = 495Left = 240TabIndex = 1Top = 690Width = 615EndBegin VB.Menu editCaption = "编辑(&E)"EndBegin VB.Menu lookCaption = "查看(&V)"EndBegin VB.Menu helpCaption = "帮助(&H)"EndEndAttribute VB_Name = "Form1"Attribute VB_GlobalNameSpace = False Attribute VB_Creatable = FalseAttribute VB_PredeclaredId = TrueAttribute VB_Exposed = FalseOption ExplicitPrivate Declare Function ShowCursor Lib "user32" (ByVal bShow As Long) As Long Dim dotflag As BooleanDim fuhao As StringDim first As DoubleDim second As DoubleDim isEqual As BooleanDim memory As Double '保存显示的数据Dim lianyong As Double '当连续按等号时使用该变量Private Declare Function ShowCursor Lib "user32" (ByVal bShow As Long) As Long Dim dotflag As BooleanDim fuhao As StringDim first As DoubleDim second As DoubleDim isEqual As BooleanDim memory As Double '保存显示的数据Dim lianyong As Double '当连续按等号时使用该变量Private Sub cmbDesign_Click(Index As Integer)End SubPrivate Sub cmbbackspace_Click()If Right(Trim(Text1.Text), 1) = "." ThenText1.Text = Mid(Text1.Text, 1, Len(Text1.Text) - 2) & "."ElseText1.Text = Mid(Text1.Text, 1, Len(Text1.Text) - 1)End IfIf Right(Text1.Text, 1) = "." Thendotflag = FalseEnd IfIf Len(Text1.Text) = 1 ThenText1.Text = "0."End IfEnd Sub 单击Backspace时删除文本框内最后一个字符Private Sub cmbC_Click()dotflag = FalseText1.Text = "0."first = 0second = 0End Sub 使文本框变成初始状态“0.”Private Sub cmbCE_Click()Text1.Text = "0."End Sub 删除文本框内的所有内容,使文本框变成初始状态“0.”Private Sub cmbDOT_Click()'标示点击了点“。

用VB制作计算器

用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编写简易计算器(附图)

For personal use only in study and research;not for commercial use用VB6.0编写简易计算器效果图:废话不多说,直接上步骤一、创建控件组1、创建控件组的方法??首先创建一个命令按钮,调整其大小(觉得合适就行),名称为Command1,Caption 属性为数字 0 ;然后进行“复制”和“粘贴”,当选择“粘贴”时,出现对话框提示已有一个同名控件,询问是否创建控件组,选择“是”后,即创建了一个名为“Command”的控件组。

这时,第一个按钮的Index属性值默认为“0”,第二个的Index属性值自动设为“1”,并且大小与第一个按钮相同,只需修改其 Caption 属性为数字“1”并将其拖至合适位置即可。

此后继续使用“粘贴”的方法建立其他控件组中其余按钮,共20个按钮,每建立一个,就将它拖到合适处,并修改相应的Caption属性值。

2、各控件组其属性设置如下:二、编写代码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三、测试,成功的话给个好评哦!谢谢各位下载与支持!这个可以编写作为作业哦!仅供个人参考仅供个人用于学习、研究;不得用于商业用途。

用VB6.0编写一个小计算器

用VB6.0编写一个小计算器
储结果的变量。需要注意的是,对于负数进行平方根运算会返回一个复数结果。
04
测试和调试
功能测试
测试所有基本功能
加、减、乘、除、平方、平方根等。
测试运算符优先级
确保遵循数学规则,如先乘除后加减。
测试数字范围
包括整数、小数和科学记数法表示的数字。
错误处理
输入验证
确保输入是数字,防止非法字符输入。
异常处理
在VB6.0中,可以使用"/"运算符进行除法 运算。为了实现除法运算,可以在按钮的 点击事件中编写相应的代码,例如: `result = num1 / num2`,其中`num1`和 `num2`是要相除的两个数,`result`是存储 结果的变量。需要注意的是,如果除数为0 会导致程序出错,因此需要进行除数为0的 判断。
谢谢观看
用VB6.0编写一个小计算器
目录
• 引言 • 设计计算器界面 • 实现计算器功能 • 测试和调试 • 总结和展望
01
引言
目的和背景
提高编程技能和实践能力
练习开发一个简单的计算 器应用程序
掌握VB6.0编程语言的基本 语法和控件
01
03 02
计算器概述
一个基本的计算器应 用程序应具备加、减、 乘、除运算功能
优化用户界面
虽然现在的用户界面已经比较友好,但我们可以 进一步优化它,使其更加美观和易用。例如,可 以调整按钮大小、颜色和布局,使其更加符合用 户审美和使用习惯。
扩展平台兼容性
为了使更多人能够使用这个小计算器,我们可以 考虑将其移植到其他平台,如手机或平板电脑。 这需要我们使用跨平台的开发框架和技术来实现 。
平方和平方根运算
总结词
实现平方和平方根运算的基本功能

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。

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

用VB6.0编写简易计算器
效果图:
废话不多说,直接上步骤
一、创建控件组
1、创建控件组的方法
首先创建一个命令按钮,调整其大小(觉得合适就行),名称为Command1,Caption 属性为数字 0 ;然后进行“复制”和“粘贴”,当选择“粘贴”时,出现对话框提示已有一个同名控件,询问是否创建控件组,选择“是”后,即创建了一个名为“Command”的控件组。

这时,第一个按钮的Index属性值默认为“0”,第二个的Index属性值自动设为“1”,并且大小与第一个按钮相同,只需修改其 Caption 属性为数字“1”并将其拖至合适位置即可。

此后继续使用“粘贴”的方法建立其他控件组中其余按钮,共20个按钮,每建立一个,就将它拖到合适处,并修改相应的Caption属性值。

2、各控件组其属性设置如下:
控件名称功能/属性
窗体Form 1 简易计算器
按钮Command1 Command1(0)~ Command1(9)即:Caption 0 ~ 9 按钮Command2 小数点“.”
按钮Command3 等于号“=”
按钮Command4 退格
按钮Command5 清除
按钮Command6 Command6(0)~ Command6(3)即:Caption +、-、*、/ 按钮Command7 负数
按钮Command8 平方
设置效果如下图所示:
二、编写代码
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 Sub
Private Sub Command2_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 Command3_Click()
s2 = Val(Text1.Text) '开始加减乘除运算
Select Case ysf
Case "+"
Text1.Text = s1 + s2
Case "-"
Text1.Text = s1 - s2
Case "*"
Text1.Text = s1 * s2
Case "/"
If s2 = 0 Then
MsgBox "分母不能为零!"
Text1.Text = ""
Else
Text1.Text = s1 / s2
End If
End Select
Text1 = IIf(Left(Text1.Text, 1) = ".", 0 & Text1.Text, Text1.Text) '这个很关键,如果没有这个的话,得出小于1的小数前面没有0
End Sub
Private Sub Command4_Click()
If Text1.Text = "" Then '文本为空就结束
Exit Sub
End If
Text1.Text = Left(Text1.Text, Len(Text1.Text) - 1) '文本退一格
End Sub
Private Sub Command5_Click()
Text1.Text = "" '清除当前框内文本
End Sub
Private Sub Command6_Click(Index As Integer)
s1 = Val(Text1.Text) '将s1隐藏起来
ysf = Command6(Index).Caption
Text1.Text = ""
End Sub
Private Sub Command7_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 Command8_Click()
Text1.Text = Text1.Text * Text1.Text '平方
End Sub
各位朋友,可以将红色代码复制到相应位置,不清楚的可以全选复制,但是一定要按照我的步骤和给的名称来哦!
还可以再添加按钮
Private Sub Command9_Click() '这是退出代码
End
End Sub
三、测试,成功的话给个好评哦!谢谢各位下载与支持!
这个可以编写作为作业哦!。

相关文档
最新文档