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

合集下载

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简易计算器代码以下是一个简单的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、小数点按钮、加法、减法、乘法、除法和等于按钮。

用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 点击文件→生成工程
生成成功! 上面框内输入乘数,点击按钮,计算出结果显 示在下面框内!
注意事项 千万注意代码中的空格

VBNET简易计算器制作与实现

VBNET简易计算器制作与实现

简易计算器的实现:图形及代码如下:Option Strict OffOption Explicit OnImports VB = Microsoft.VisualBasicPublic Class Form1Inherits System.Windows.Forms.FormDim x, y, z As DoubleDim oper As CharDim i As IntegerPrivate Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Loadx = 0y = 0z = 0oper = ""i = 0End Sub'数字Private Sub chiffre(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles B0.Click, B1.Click, B2.Click, B3.Click, B4.Click, B5.Click, B6.Click, B7.Click, B8.Click, B9.ClickIf Label1.Text = "0"ThenLabel1.Text = CStr(sender.text)Exit SubElseIf i = 1 ThenLabel1.Text = CStr(sender.text)i = 2ElseIf i = 3 ThenLabel1.Text = CStr(sender.text)Label2.Text = ""i = 0ElseLabel1.Text = Label1.Text & CStr(sender.text)End IfEnd IfEnd IfEnd Sub'小数点Private Sub deci_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles deci.ClickIf i = 0 ThenIf InStr(Label1.Text, ".") = 0 ThenLabel1.Text = Label1.Text & "."End IfElseIf i = 1 ThenLabel1.Text = "0"i = 2ElseIf i = 2 ThenIf InStr(Label1.Text, ".") = 0 ThenLabel1.Text = Label1.Text & "."End IfElseIf i = 3 ThenLabel1.Text = "0"Label2.Text = " "i = 0End IfEnd IfEnd IfEnd IfEnd Sub'正负号Private Sub pm_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles pm.Click If Val(Label1.Text) > 0 ThenLabel1.Text = "-" + Label1.TextElseLabel1.Text = -Val(Label1.Text)End IfEnd Sub'运算符Private Sub operation(ByVal sender As System.Object, ByVal e As System.EventArgs) Handlesmult.Click, div.Click, plus.Click, minus.Clickx = Val(Label1.Text)oper = CStr(sender.text)i = 1Label2.Text = Label1.Text + CStr(sender.text)End Sub'加减乘除运算Private Sub egale_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles egale.ClickIf y = 0 Theny = Val(Label1.Text)End IfIf oper = "/"Thenz = x / yx = zLabel1.Text = zElseIf oper = "-"Thenz = x - yx = zLabel1.Text = zElseIf oper = "*"Thenz = x * yx = zLabel1.Text = zElseIf oper = "+"Thenz = x + yx = zLabel1.Text = zEnd IfIf oper = "/"And y = 0 ThenLabel1.Text = "除数不能为零"End IfLabel1.Text = zLabel2.Text = " "i = 3End Sub'开平方根Private Sub sqrt_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles sqrt.ClickLabel2.Text = "sqrt(" & Label1.Text & ")"Label1.Text = System.Math.Sqrt(Val(Label1.Text))i = 3End Sub'百分号%Private Sub perc_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles pec.ClickIf i = 0 ThenLabel1.Text = "0"ElseIf i = 2 Theny = Val(Label1.Text) / 100 * xLabel1.Text = yLabel2.Text = x & oper & yEnd IfEnd IfEnd Sub'倒数Private Sub pe_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles pe.Click y = Val(Label1.Text)Label2.Text = "reciproc(" & Label1.Text & ")"If Label1.Text > 0 Theny = 1 / yLabel1.Text = CStr(y)ElseLabel1.Text = "除数不能为零"End Ifi = 3End Sub'退格Private Sub back_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BS.ClickIf Len(Label1.Text) <> 1 ThenLabel1.Text = Microsoft.VisualBasic.Left(Label1.Text, Len(Label1.Text) - 1) ElseLabel1.Text = "0"End IfEnd Sub'重新输入CEPrivate Sub dele_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CE.ClickLabel1.Text = "0"If i = 1 Theni = 2End IfEnd Sub'全部清空CPrivate Sub clear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles C.ClickLabel1.Text = "0"Label2.Text = ""x = 0y = 0z = 0oper = ""i = 0End SubEnd Class。

VB制作简单计算器的过程

VB制作简单计算器的过程

三、还有需要加强改进的地方?
If Left(Text1.Textd1(Index).Caption
Else
Text1.Text = Text1.Text & Command1(Index).Caption
例如:
依次按下“100”、“÷”、“5”,然后再按下“C”,再依次按下“2”、“=”,显示为2。这说明将前面的100÷5都清降掉了.
而依次按下“100”、“÷”、“5”,再按下“CE”,再依次按下“2”、“=”,显示为50,即清除了5,执行了100÷2的操作。
10、前导零不显示
例如:"00123","00.123"
Abs(shu1):取绝对值
len(shu1):测量字符串shu1的长度
left(shu1,n):返回字符shu1中左边n个字符
right(shu1,n):返回字符shu1中右边n个字符
Instr(n,shu1,”.”):找出小数点在shu1中第n次出现的位置
End If
If InStr(1, Text1.Text, ".") = 0 Then
Text1.Text = Text1.Text & "."
End If
If InStr(1, Text1.Text, ".") = 1 Then
Text1.Text = "0" & Text1.Text
9、纠错
说明:C与CE的区别
C键为清除键(CLEAR的缩写) 功能清除所有操作从头再来
CE键为纠错键(CLEAR ERROR的缩写)功能当前操作数归零可以重新输入操作。

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编写计算器程序

2、单击数组命令按钮Command1触发事件
Private Sub Command2_Click(Index As Integer)
Text1.Text = jsq.Eval(Tex事件,调用脚本控制器部件
脚本控制器部件Scriptcontrol改名为jsq,利用它的Eval值
Text1.Text = (Text1.Text) ^ 2
End Sub 进行平方的运算 (Text1.Text) ^ 2在Visual Basic中表示平方运算
Private Sub Command4_Click()
Text1.Text = “ " End Sub 文本框Text1.text为空表示计算器清除
用vb编写计算器程序用vb编写计算器用vb编写简单计算器用vb编写科学计算器用vb编写简易计算器vb编写计算器vb编写简易计算器vb简易计算器程序vb计算器程序如何用vb制作计算器
Private Sub Command1_Click(Index As Integer)
Text1.Text = Text1.Text + Command1(Index).Caption End Sub 解读程序: 1、数组命令按钮Command1(index)
进行加减乘除的运算,例如:jsq. Eval(8X9)=72
Private Sub Command3_Click()
Text1.Text = Sqr(Text1.Text) End Sub 进行开平方的运算 Sqr在Visual Basic中表示开平方运算
Private Sub Command5_Click()
Private Sub Command6_Click()
Unload Me End Sub
表示退出计算器

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程序设计:计算器

2.3属性设置窗体和各控件的属性设置如下表所示2.4 系统功能本系统的主要功能及任务是1.简单计算2.科学计算2.5 系统功能模块图一.界面设计。

对象属性设置第三章详细设计3.1窗体3.2程序代码Public sum As DoublePublic k As StringPublic pi As DoublePublic c As DoublePublic d As DoublePublic n As DoubleFunction Log10(X)Log10 = Log(X) / Log(10)End FunctionFunction Logn(X)Logn = Log(X) / Log(n)End FunctionPrivate Sub check_Click()Shell "C:\WINDOWS\explorer.exe /263885058/blog/1322900561" End SubPrivate Sub Command1_Click(Index As Integer)Select Case IndexCase 1Text1.Text = Text1.Text & 1Case 2Text1.Text = Text1.Text & 2Case 3Text1.Text = Text1.Text & 3Case 4Text1.Text = Text1.Text & 4Case 5Text1.Text = Text1.Text & 5Case 6Text1.Text = Text1.Text & 6Case 7Text1.Text = Text1.Text & 7Case 8Text1.Text = Text1.Text & 8Case 9Text1.Text = Text1.Text & 9Case 0Text1.Text = Text1.Text & 0Case 10Text1.Text = Text1.Text &"."End SelectEnd SubPrivate Sub Command2_Click(Index As Integer)Select Case IndexCase 0sum = Text1.TextText1.Text = ""k = "+"Case 1sum = Text1.TextText1.Text = ""k = "-"Case 2sum = Text1.TextText1.Text = ""k = "*"Case 3sum = Text1.TextText1.Text = ""k = "/"End SelectEnd SubPrivate Sub Command3_Click()Dim a As Doublea = Text1.TextIf k = "+" Thensum = sum + aText2.Text = sumElseIf k = "-" Thensum = sum - aText2.Text = sumElseIf k = "*" Thensum = sum * aText2.Text = sumElseIf k = "/" Thensum = sum / aText2.Text = sumElseIf k = "sin" Then'等号的三角计算sum = Sin(a * pi / 180)Text2.Text = sumElseIf k = "cos" Thensum = Cos(a * pi / 180)Text2.Text = sumElseIf k = "tan" Thensum = Tan(a * pi / 180)Text2.Text = sumElseIf k = "cot" Thensum = 1 / (Tan(a * pi / 180))Text2.Text = sumElseIf k = "lg" Then'等号的lg计算sum = Log(Val(Text1.Text))Text2.Text = sumElseIf k = "ln" Then'等号的ln计算sum = Log10(Val(Text1.Text))Text2.Text = sumElseIf k = "logn" Then'等号的log n a计算sum = Logn(Val(Text1.Text))Text2.Text = sumElseIf k = "2" Then'等号的幂函数计算sum = (Val(Text1.Text)) ^ 2Text2.Text = sumElseIf k = "c" Thensum = Val(Text1.Text) ^ cText2.Text = sumElseIf k = "0.5" Then'等号的开方运算sum = (Val(Text1.Text)) ^ 0.5Text2.Text = sumElseIf k = "d" Thensum = (Val(Text1.Text)) ^ (1 / d)Text2.Text = sumEnd IfEnd SubPrivate Sub Command4_Click(Index As Integer)Dim b As Integerb = Len(Text1.Text)If b > 1 ThenText1.Text = Val(Str(Left(Text1.Text, Len(Text1.Text) - 1))) ElseText1.Text = ""End IfEnd SubPrivate Sub Command5_Click()Text1.Text = ""Text2.Text = ""Label2.Caption = ""Label3.Caption = ""a = 0b = 0k = ""End SubPrivate Sub Command6_Click(Index As Integer)Select Case IndexCase 0Label2.Caption = "sin"k = "sin"Case 1Label2.Caption = "cos"k = "cos"Case 2Label2.Caption = "tan"k = "tan"Case 3Label2.Caption = "cot"k = "cot"Case 4'对数计算Label2.Caption = "lg"k = "lg"Case 5Label2.Caption = "ln"k = "ln"Case 6n = Val(InputBox("请输入对数运算的底数", 请输入数字)) Label2.Caption = "log" '普通对数计算Label4.Caption = nk = "logn"Case 7'幂运算Label3.Caption = "2"k = "2"Case 8c = Val(InputBox("请输入运算幂指数", 输入数字))Label3.Caption = ck = "c"Case 9'幂运算Label2.Caption = "√"k = "0.5"Case 10'开方运算d = Val(InputBox("请输入需要开多少次方", 输入数字))Label2.Caption = d &"√"k = "d"End SelectEnd SubPrivate Sub Command7_Click()Shell "C:\WINDOWS\explorer.exe " End SubPrivate Sub Command8_Click()Text1.Text = sumEnd SubPrivate Sub Command9_Click()EndEnd SubPrivate Sub end_Click()EndEnd SubPrivate Sub explain_Click()End SubPrivate Sub Form_Load()pi = 4 * Atn(1)End SubPrivate Sub help_Click()End SubPrivate Sub Text1_Change()End Sub。

vb计算器

vb计算器

1 需求分析可实现以下功能:1简易计算器。

2判断是否为闰。

3判断m是否为素数.。

4显示n行的杨辉三角。

5求500以内的水仙花数。

6用下三角显示二维数组a。

7求最大公约数和最小公倍。

8三个数比较大小从小到大排列。

9随机产生一个三位数逆向显示。

10判断a、b、c能否组成三角形。

11随机生成4×4矩阵求出最小值。

12编辑一个子过程求出数组a最小值。

13在数组a()中插入数,并使a依然有序。

14已知数组a()编程删除a中第5个元素。

15求200以内能被11或5整除的所有数之和。

16随机产生20个学生的成绩统计各分数段人数。

17随机产生15个小写或大写字母,放在字符数组中。

18从键盘输入任意长度的字符串,要求将字符顺序倒置。

19随机产生10个[30,100]内整数求最大值最小值和平均值。

2 运行环境.WINDOWS XP系统;VB6.0;3开发工具和编程语言Visual Basic6.0语言;4 系统设计本次试验是有一个工程和多个窗体组合而成,其中主界面中的每一个按钮都对应一个分界面,每一个分界面又都对应一项特殊的功能,并且每一个界面上都有一个返回主界面的按钮,即“返回”按钮,在主界面上有一个结束按钮可以结束程序的运行,通过这些按钮及界面可以实现不同的功能,其具体的功能如下:(1)主界面:在窗体上设计不同的按钮,通过每个按钮command _click事件执行不同的功能。

(2)简易计算器:本计算器可以实现一些基本加、减、乘、除、正弦、余弦及乘方等数学运算。

(3)判断是否为闰年,通过inputbox输入年份判断。

(4)判断m是否为素数,输入一个数通过command的单击事件判断,并且素数在picture中打印出来。

(5)显示n行的杨辉三角,通过输入数n,用text的单击事件显示具有n的杨辉三角,并且以下三角显示。

(6)求500以内的水仙花数,通过form的单击事件显示水仙花数。

(7)用下三角显示二维数组a,通过form的单击事件产生。

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

(完整word)图文教程—VB计算器设计

(完整word)图文教程—VB计算器设计

VB计算器设计—图文教程界面设计——代码设计——测试运行——保存文件界面设计:一、启动VB,新建一个“标准EX E”工程,将“Form1”的“Caption属性”改为“我的计算器”,“Height属性”改为6100,“Width属性”改为7100。

二、添加一个“Frame”,“Caption属性”改为“数字区”,Height为3615,Width为2655。

添加一个“Frame”,“Caption属性”改为“功能区”,Height为1815,Width为3135。

三、添加一个“TextBox”,“名称”改为t4,“Text属性”改为空白,Height为6255,Width为615。

用同样的方法,添加一个“TextBox”,“名称”改为t1,“Text属性”改为空白,Height为975,Width为495。

添加一个“TextBox”,“名称”改为t2,“Text属性”改为空白,Height为495,Width为495。

添加一个“TextBox”,“名称”改为t3,“Text属性”改为空白,Height为975,Width为495。

摆放位置如图所示:t4t1 t2 t3四、添加一个“CommandButton”,“名称”改为cmd0,“Caption属性”改为0,Height和Width都为495。

摆放位置如图所示:按照同样的方法,在数字区添加其他的CommandButton,并根据下表更改CommandButton的名称、Caption属性,Height和Width。

类型名称Caption属性Height高度Width宽度CommandButton Cmd0 0 495 495CommandButton Cmd小数点. 495 495CommandButton 正负+/- 495 495CommandButton Cmd1 1 495 495CommandButton Cmd2 2 495 495CommandButton Cmd3 3 495 495CommandButton Cmd4 4 495 495CommandButton Cmd5 5 495 495CommandButton Cmd6 6 495 495CommandButton Cmd7 7 495 495CommandButton Cmd8 8 495 495CommandButton Cmd9 9 495 495注意:可以用复制-粘贴的方法,但是在询问“是否创建一个控件数组?”时,请选择“否”!最后,数字区的效果如下图所示:五、添加一个“CommandButton”,“名称”改为加,“Caption属性”改为+,Height和Width都为495。

VB程序设计:计算器

VB程序设计:计算器

2.3属性设置窗体和各控件的属性设置如下表所示2.4 系统功能本系统的主要功能及任务是1.简单计算2.科学计算2.5 系统功能模块图一.界面设计。

对象属性设置第三章详细设计3.1窗体3.2程序代码Public sum As DoublePublic k As StringPublic pi As DoublePublic c As DoublePublic d As DoublePublic n As DoubleFunction Log10(X)Log10 = Log(X) / Log(10)End FunctionFunction Logn(X)Logn = Log(X) / Log(n)End FunctionPrivate Sub check_Click()Shell "C:\WINDOWS\explorer.exe /263885058/blog/1322900561" End SubPrivate Sub Command1_Click(Index As Integer)Select Case IndexCase 1Text1.Text = Text1.Text & 1Case 2Text1.Text = Text1.Text & 2Case 3Text1.Text = Text1.Text & 3Case 4Text1.Text = Text1.Text & 4Case 5Text1.Text = Text1.Text & 5Case 6Text1.Text = Text1.Text & 6Case 7Text1.Text = Text1.Text & 7Case 8Text1.Text = Text1.Text & 8Case 9Text1.Text = Text1.Text & 9Case 0Text1.Text = Text1.Text & 0Case 10Text1.Text = Text1.Text &"."End SelectEnd SubPrivate Sub Command2_Click(Index As Integer)Select Case IndexCase 0sum = Text1.TextText1.Text = ""k = "+"Case 1sum = Text1.TextText1.Text = ""k = "-"Case 2sum = Text1.TextText1.Text = ""k = "*"Case 3sum = Text1.TextText1.Text = ""k = "/"End SelectEnd SubPrivate Sub Command3_Click()Dim a As Doublea = Text1.TextIf k = "+" Thensum = sum + aText2.Text = sumElseIf k = "-" Thensum = sum - aText2.Text = sumElseIf k = "*" Thensum = sum * aText2.Text = sumElseIf k = "/" Thensum = sum / aText2.Text = sumElseIf k = "sin" Then'等号的三角计算sum = Sin(a * pi / 180)Text2.Text = sumElseIf k = "cos" Thensum = Cos(a * pi / 180)Text2.Text = sumElseIf k = "tan" Thensum = Tan(a * pi / 180)Text2.Text = sumElseIf k = "cot" Thensum = 1 / (Tan(a * pi / 180))Text2.Text = sumElseIf k = "lg" Then'等号的lg计算sum = Log(Val(Text1.Text))Text2.Text = sumElseIf k = "ln" Then'等号的ln计算sum = Log10(Val(Text1.Text))Text2.Text = sumElseIf k = "logn" Then'等号的log n a计算sum = Logn(Val(Text1.Text))Text2.Text = sumElseIf k = "2" Then'等号的幂函数计算sum = (Val(Text1.Text)) ^ 2Text2.Text = sumElseIf k = "c" Thensum = Val(Text1.Text) ^ cText2.Text = sumElseIf k = "0.5" Then'等号的开方运算sum = (Val(Text1.Text)) ^ 0.5Text2.Text = sumElseIf k = "d" Thensum = (Val(Text1.Text)) ^ (1 / d)Text2.Text = sumEnd IfEnd SubPrivate Sub Command4_Click(Index As Integer)Dim b As Integerb = Len(Text1.Text)If b > 1 ThenText1.Text = Val(Str(Left(Text1.Text, Len(Text1.Text) - 1))) ElseText1.Text = ""End IfEnd SubPrivate Sub Command5_Click()Text1.Text = ""Text2.Text = ""Label2.Caption = ""Label3.Caption = ""a = 0b = 0k = ""End SubPrivate Sub Command6_Click(Index As Integer)Select Case IndexCase 0Label2.Caption = "sin"k = "sin"Case 1Label2.Caption = "cos"k = "cos"Case 2Label2.Caption = "tan"k = "tan"Case 3Label2.Caption = "cot"k = "cot"Case 4'对数计算Label2.Caption = "lg"k = "lg"Case 5Label2.Caption = "ln"k = "ln"Case 6n = Val(InputBox("请输入对数运算的底数", 请输入数字)) Label2.Caption = "log" '普通对数计算Label4.Caption = nk = "logn"Case 7'幂运算Label3.Caption = "2"k = "2"Case 8c = Val(InputBox("请输入运算幂指数", 输入数字))Label3.Caption = ck = "c"Case 9'幂运算Label2.Caption = "√"k = "0.5"Case 10'开方运算d = Val(InputBox("请输入需要开多少次方", 输入数字))Label2.Caption = d &"√"k = "d"End SelectEnd SubPrivate Sub Command7_Click()Shell "C:\WINDOWS\explorer.exe " End SubPrivate Sub Command8_Click()Text1.Text = sumEnd SubPrivate Sub Command9_Click()EndEnd SubPrivate Sub end_Click()EndEnd SubPrivate Sub explain_Click()End SubPrivate Sub Form_Load()pi = 4 * Atn(1)End SubPrivate Sub help_Click()End SubPrivate Sub Text1_Change()End Sub。

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。

【VIP专享】使用VB 设计一个简易计算器

【VIP专享】使用VB 设计一个简易计算器
输入代码完成后,应该先保存工程,再运行程序,以避免因死机等意外发生而造成损 失。一个工程由若干文件组成,保存文件的顺序应该是先保存窗体文件(F1.frm),再保存 文件工程文件(P1.vbp),而保存地点则可自由安排。 (6)测试和调试应用程序。 (7)生成可执行程序。
执行“文件”—>“生成 P1.exe”菜单命令,根据提示进行操作即可将本工程编译成能
6.培养学生观察、思考、对比及分析综合的能力。过程与方法1.通过观察蚯蚓教的学实难验点,线培形养动观物察和能环力节和动实物验的能主力要;特2征.通。过教对学观方察法到与的教现学象手分段析观与察讨法论、,实对验线法形、动分物组和讨环论节法动教特学征准的备概多括媒,体继课续件培、养活分蚯析蚓、、归硬纳纸、板综、合平的面思玻维璃能、力镊。子情、感烧态杯度、价水值教观1和.通过学理解的蛔1虫.过观适1、察于程3观阅 六蛔寄.内列察读 、虫生出蚯材 让标容生3根常蚓料 学本教活.了 据见身: 生,师的2、解 问的体巩鸟 总看活形作 用蛔 题线的固类 结雌动态业 手虫 自形练与 本雄学、三: 摸对 学动状习人 节蛔生结4、、收 一人 后物和同类 课虫活构请一蚯集 摸体 回并颜步关 重的动、学、蚓鸟 蚯的 答归色学系 点形教生生让在类 蚓危 问纳。习从 并状学理列学平的害 题线蚯四线人 归、意特出四生面体以形蚓、形类 纳大图点常、五观玻存 表及动的鸟请动文 本小引以见引、察璃现 ,预物身类 3学物明 节有言及的、导巩蚯上状 是防的体之生和历 课什根蚯环怎学固蚓和, 干感主是所列环史 学么据蚓节二样生练引牛鸟 燥染要否以举节揭 到不上适动、区回习导皮类 还的特分分蚯动晓 的同节于物让分答。学纸减 是方征节布蚓物起 一,课穴并学蚯课生上少 湿法。?广的教, 些体所居归在生蚓前回运的 润;4泛益学鸟色生纳.靠物完的问答动原 的4蛔,处目类 习和活环.近在成前题蚯的因 ?了虫以。标就 生体的节身其实端并蚓快及 触解寄上知同 物表内特动体结验和总利的慢我 摸蚯生适识人 学有容点物前构并后结用生一国 蚯蚓在于与类 的什,的端中思端线问活样的 蚓人飞技有 基么引进主的的考?形题环吗十 体生行能着 本特出要几变以动,境?大 节活的1密 方征本“特节化下物.让并为珍 近习会形理切 法。课生征有以问的小学引什稀 腹性态解的 。2课物。什游题主.结生出么鸟 面和起结蛔关观题体么戏:要利明蚯?类 处适哪构虫系察:的特的特用确蚓等 ,于些特适。蛔章形殊形征板,这资 是穴疾点于可虫我态结式。书生种料 光居病是寄的们结构,五小物典, 滑生?重生鸟内学构,学、结的型以 还活5要生类部习与.其习巩鸟结的爱 是如原活生结了功颜消固类构线鸟 粗形何因的存构腔能色化练适特形护 糙态预之结的,肠相是系习于点动鸟 ?、防一构现你动适否统。飞都物为结蛔。和状认物应与的行是。主构虫课生却为和”其结的与题、病本理不蛔扁的他构特环以生?8特乐虫形观部特8征境小理三页点观的动位点梳相组等、这;,哪物教相,理适为方引些2鸟,育同师.知应单面导鸟掌类结了;?生识的位学你握日构解2互.。办特生认线益特了通动手征观识形减点它过,抄;察吗动少是们理生报5蛔?物,与的解.参一了虫它和有寄主蛔与份解结们环些生要虫其。蚯构都节已生特对中爱蚓。会动经活征人培鸟与飞物灭相。类养护人吗的绝适这造兴鸟类?主或应节成趣的为要濒的课情关什特临?就危感系么征灭来害教;?;绝学,育,习使。我比学们它生可们理以更解做高养些等成什的良么两好。类卫动生物习。惯根的据重学要生意回义答;的3.情通况过,了给解出蚯课蚓课与题人。类回的答关:系线,形进动行物生和命环科节学动价环值节观动的物教一育、。根教据学蛔重虫点病1.引蛔出虫蛔适虫于这寄种生典生型活的线结形构动和物生。理二特、点设;置2.问蚯题蚓让的学生生活思习考性预和习适。于穴居生活的形态、结构、生理等方面的特征;3.线形动物和环节动物的主要特征。
  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、各控件组其属性设置如下:
设置效果如下图所示:
二、编写代码
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
三、测试,成功的话给个好评哦!谢谢各位下载与支持!
这个可以编写作为作业哦!。

相关文档
最新文档