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大作业报告程序功能介绍:计算器可以进行简单的。

加、减、乘、除、乘方、开方、阶乘、取余和清零。

主要源程序:设置程序从form2启动。

首先出来一个展示界面,内容包括自己的姓名、班级和所做的课题。

等待时间是3秒。

Private Sub Form_Load()Label4.Caption = "3"End SubPrivate Sub Timer1_Timer() “倒计时,当倒计时为0时,一界面隐藏弹出二界面”Label4.Caption = Label4.Caption - 1If (Label4.Caption = "0") ThenForm1.HideForm2.ShowEnd IfEnd Sub解释:定时器以一定的时间间隔产生Timer事件从而执行相应的时间过程。

当form2消失后,form3开始运行。

用户名与密码正确时form3消失form出现。

进入计算器的主要界面。

Private Sub Command1_Click()If Text1.Text <> "******" ThenMsgBox "用户名错误,请重新输入"Text1.SetFocusEnd IfIf Text2.Text <> "123" ThenMsgBox "密码不正确,请重新输入"Text2.SetFocusElseIf Text1 = "******" And Text2 = "123" ThenForm2.HideForm3.ShowEnd IfEnd SubPrivate Sub Text1_Change()End Sub解释:利用Msgbox判断输入数据的正确性,否则不允许登录。

然后进入计算器界面:Private Sub Command1_Click()Text3.Text = Str(Val(Text1.Text) + Val(Text2.Text)) “加法运算”End SubPrivate Sub Command2_Click()Text3.Text = Str(Val(Text1.Text) - Val(T ext2.Text))“减法运算”End SubPrivate Sub Command3_Click()Text3.Text = Str(Val(Text1.Text) * Val(Text2.Text))“乘法运算”End SubPrivate Sub Command4_Click()Text3.Text = Str(Val(Text1.Text) / Val(T ext2.Text))“除法运算”End SubPrivate Sub Command6_Click()Text3.Text = Str(Val(Sqr(Text1.Text)))“开方运算”End SubPrivate Sub Command5_Click()Text1.Text2 = Str(Val(Text1.Text) * Val(Text1.Text)) “乘方运算”End SubPrivate Sub Command7_Click()Text1 = ""Text2 = "" “清零”Text3 = ""End SubPrivate Sub Command8_Click()Dim a#, s#s = 1a = Val(Text1)For i = 1 To a “阶乘运算”s = s * iText3 = sNext iEnd SubPrivate Sub Command9_Click()Text3 = Str(Val(Text1) Mod (Text2)) “除法取余”End SubPrivate Sub Text1_Change()End Sub总体解释:工程主要包括3个界面,第一个界面主要包括(课题、班级、姓名、倒计时器),第二个界面主要包括(用户名、密码,只有二者都输入正确时才能登录进入第三个界面),第三个界面主要包括(计算器的简单运算加、减、乘、除、乘方、开方、阶乘、取余和清零)。

VB简易计算器代码

VB简易计算器代码

VB简易计算器代码以下是一个简单的VB计算器代码:```Public Class Form1Dim firstNum As Double ' 第一个数字Dim secondNum As Double ' 第二个数字Dim operation As Integer ' 1-加法,2-减法,3-乘法,4-除法Private Sub Button0_Click(sender As Object, e As EventArgs) Handles Button0.ClickTextBoxResult.Text = TextBoxResult.Text & "0"End SubPrivate Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.ClickTextBoxResult.Text = TextBoxResult.Text & "1"End SubPrivate Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.ClickTextBoxResult.Text = TextBoxResult.Text & "2"End SubHandles Button3.ClickTextBoxResult.Text = TextBoxResult.Text & "3"End SubPrivate Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.ClickTextBoxResult.Text = TextBoxResult.Text & "4"End SubPrivate Sub Button5_Click(sender As Object, e As EventArgs) Handles Button5.ClickTextBoxResult.Text = TextBoxResult.Text & "5"End SubPrivate Sub Button6_Click(sender As Object, e As EventArgs) Handles Button6.ClickTextBoxResult.Text = TextBoxResult.Text & "6"End SubPrivate Sub Button7_Click(sender As Object, e As EventArgs) Handles Button7.ClickTextBoxResult.Text = TextBoxResult.Text & "7"End SubHandles Button8.ClickTextBoxResult.Text = TextBoxResult.Text & "8"End SubPrivate Sub Button9_Click(sender As Object, e As EventArgs) Handles Button9.ClickTextBoxResult.Text = TextBoxResult.Text & "9"End SubPrivate Sub ButtonDot_Click(sender As Object, e As EventArgs) Handles ButtonDot.ClickIf Not TextBoxResult.Text.Contains(".") ThenTextBoxResult.Text = TextBoxResult.Text & "."End IfEnd SubPrivate Sub ButtonAdd_Click(sender As Object, e As EventArgs) Handles ButtonAdd.ClickfirstNum = Double.Parse(TextBoxResult.Text)TextBoxResult.Text = ""operation = 1End SubPrivate Sub ButtonSubtract_Click(sender As Object, e As EventArgs) Handles ButtonSubtract.ClickfirstNum = Double.Parse(TextBoxResult.Text)TextBoxResult.Text = ""operation = 2End SubPrivate Sub ButtonMultiply_Click(sender As Object, e As EventArgs) Handles ButtonMultiply.ClickfirstNum = Double.Parse(TextBoxResult.Text)TextBoxResult.Text = ""operation = 3End SubPrivate Sub ButtonDivide_Click(sender As Object, e As EventArgs) Handles ButtonDivide.ClickfirstNum = Double.Parse(TextBoxResult.Text)TextBoxResult.Text = ""operation = 4End SubPrivate Sub ButtonClear_Click(sender As Object, e As EventArgs) Handles ButtonClear.ClickTextBoxResult.Text = ""End SubPrivate Sub ButtonEquals_Click(sender As Object, e As EventArgs) Handles ButtonEquals.ClickDim result As DoublesecondNum = Double.Parse(TextBoxResult.Text)Select Case operationCase 1result = firstNum + secondNumCase 2result = firstNum - secondNumCase 3result = firstNum * secondNumCase 4result = firstNum / secondNumEnd SelectTextBoxResult.Text = result.ToStringEnd SubEnd Class```这个计算器包括数字按钮0-9、小数点按钮、加法、减法、乘法、除法和等于按钮。

vb计算器 简单程序(包含 进制转换 )

vb计算器 简单程序(包含 进制转换 )

Option ExplicitDim s1 As Integer, N As Single, r As IntegerPrivate Sub Command1_Click(Index As Integer) Text1.Text = Text1.Text & IndexEnd Sub'CEPrivate Sub Command11_Click()Text1.Text = ""End SubPrivate Sub Command12_Click()Text1.Text = (Text1.Text) ^ 2End SubPrivate Sub Command17_Click()Text1.Text = " "End SubPrivate Sub Command18_Click()Text1.Text = (Text1.Text) ^ 3End SubPrivate Sub Command20_Click()If Text1.Text = "" ThenMsgBox ("²Ù×÷´íÎó")ElseText1.Text = Val(Text1.Text) * (-1)End IfEnd SubPrivate Sub Command21_Click()If InStr(Text1.Text, ".") ThenMsgBox ("СÊýµãÒÑ´æÔÚ")ElseText1.Text = Text1 & Chr(46)End IfEnd SubPrivate Sub Command23_Click()If N = 1 ThenIf Text1.Text = 0 ThenMsgBox "³ýÊý²»ÄÜΪÁã"ElseText1 = s1 / Text1.TextEnd IfElseIf N = 2 ThenText1 = s1 * Text1.TextElseIf N = 3 ThenText1 = s1 - Text1.TextElseIf N = 4 ThenText1 = s1 + Text1.TextEnd IfEnd SubPrivate Sub Command24_Click()Text1.Text = 1 / Text1.TextEnd SubPrivate Sub Command25_Click(Index As Integer) Select Case IndexCase 0Text1.Text = Text1.Text & "A"Case 1Text1.Text = Text1.Text & "B"Case 2Text1.Text = Text1.Text & "C"Case 3Text1.Text = Text1.Text & "D"Case 4Text1.Text = Text1.Text & "E"Case 5Text1.Text = Text1.Text & "F"End SelectEnd SubPrivate Sub Command4_Click(Index As Integer) Select Case IndexCase 0N = 1s1 = Text1.TextText1.Text = ""Case 1N = 2s1 = Text1.TextText1.Text = ""Case 2N = 3s1 = Text1.TextText1.Text = ""Case 3N = 4s1 = Text1.TextText1.Text = ""End SelectEnd SubPrivate Sub Command5_Click()Text1.Text = Left(Text1.Text, Len(Text1.Text) - 1) End SubPrivate Sub Command6_Click()Text1.Text = Sqr(Text1.Text)End SubPrivate Sub Form_Load()r = 10End Sub'Ê®Áù½øÖÆPrivate Sub Option1_Click()Command25(1).Enabled = TrueCommand25(0).Enabled = TrueCommand25(2).Enabled = TrueCommand25(3).Enabled = TrueCommand25(4).Enabled = TrueCommand25(5).Enabled = TrueCommand1(6).Enabled = TrueCommand1(9).Enabled = TrueCommand1(2).Enabled = TrueCommand1(3).Enabled = TrueCommand1(4).Enabled = TrueCommand1(5).Enabled = TrueCommand1(7).Enabled = TrueIf r = 10 ThenText1.Text = trandec(Val(Text1.Text), 16) ElseText1.Text = Two16(Text1.Text)End Ifr = 16End Sub'Ê®½øÖÆPrivate Sub Option2_Click()Command1(6).Enabled = TrueCommand1(9).Enabled = TrueCommand1(2).Enabled = TrueCommand1(3).Enabled = TrueCommand1(4).Enabled = TrueCommand1(5).Enabled = TrueCommand1(8).Enabled = TrueCommand1(7).Enabled = TrueCommand25(0).Enabled = FalseCommand25(1).Enabled = FalseCommand25(2).Enabled = FalseCommand25(3).Enabled = FalseCommand25(4).Enabled = FalseCommand25(5).Enabled = FalseText1.Text = Convert(Text1.Text, r)r = 10End Sub'¶þ½øÖÆPrivate Sub Option3_Click()Command1(6).Enabled = FalseCommand1(9).Enabled = FalseCommand1(2).Enabled = FalseCommand1(3).Enabled = FalseCommand1(4).Enabled = FalseCommand1(5).Enabled = FalseCommand1(8).Enabled = FalseCommand1(7).Enabled = FalseCommand25(0).Enabled = FalseCommand25(1).Enabled = FalseCommand25(2).Enabled = FalseCommand25(3).Enabled = FalseCommand25(4).Enabled = FalseIf r = 10 ThenText1.Text = trandec(Val(Text1.Text), 2)ElseText1.Text = HEX_to_BIN(Text1.Text)End Ifr = 2End SubPrivate Sub Text1_Change()If Mid(Text1.Text, 1, 1) = "." ThenText1.Text = 0 & Text1.TextEnd IfEnd Sub'Ê®½øÖÆ×ª¶þºÍÊ®Áù½øÖÆPublic Function trandec$(ByVal m%, ByVal r%)Dim strdtor$Dim iB%, mr%strdtor = ""Do While m <> 0mr = m Mod rm = m \ rIf mr >= 10 Thenstrdtor = Chr(mr - 10 + 65) & strdtorElsestrdtor = mr & strdtorEnd IfLooptrandec = strdtorEnd FunctionPublic Function Convert(ByVal S As String, ByVal N As Integer) As Double 'ÈÎÒâ½øÖÆ×ª»»³É10½øÖÆDim L As StringDim r() As StringDim i As IntegerDim j As IntegerL = "0|1|2|3|4|5|6|7|8|9|A|B|C|D|E|F|G|H|I|J|K|L|M|N|O|P|Q|R|S|T|U|V|W |X|Y|Z" '½øÖÆ×Ö·û´®×Öµär = Split(L, "|")For i = 1 To Len(S)For j = 0 To UBound(r)If UCase(Mid(S, i, 1)) = r(j) ThenConvert = Convert * N + jEnd IfNext jNext iEnd FunctionPrivate Function Two16(ByVal X As String) As String '°Ñ¶þ½øÖÆÊýת»¯ÎªÊ®Áù½øÖÆÊýDo While Len(X) Mod 4 <> 0X = "0" + XLoopDo While Len(X) > 0Select Case Right(X, 4)Case "0000"Two16 = "0" + Two16Case "0001"Two16 = "1" + Two16Case "0010"Two16 = "2" + Two16Case "0011"Two16 = "3" + Two16Case "0100"Two16 = "4" + Two16Case "0101"Two16 = "5" + Two16Case "0110"Two16 = "6" + Two16Case "0111"Two16 = "7" + Two16Case "1000"Two16 = "8" + Two16Case "1001"Two16 = "9" + Two16Case "1010"Two16 = "A" + Two16Case "1011"Two16 = "B" + Two16Case "1100"Two16 = "C" + Two16Case "1101"Two16 = "D" + Two16Case "1110"Two16 = "E" + Two16Case "1111"Two16 = "F" + Two16End SelectX = Left(X, Len(X) - 4)LoopEnd Function'Ê®Áùת¶þPublic Function HEX_to_BIN(ByVal Hex As String) As String Dim i As LongDim B As StringHex = 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"Case "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 SelectNext iDo While Left(B, 1) = "0"B = Right(B, Len(B) - 1)LoopHEX_to_BIN = BEnd Function。

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

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计算器 简单程序(包含 进制转换 )

Option ExplicitDim s1 As Integer, N As Single, r As IntegerPrivate Sub Command1_Click(Index As Integer) Text1.Text = Text1.Text & IndexEnd Sub'CEPrivate Sub Command11_Click()Text1.Text = ""End SubPrivate Sub Command12_Click()Text1.Text = (Text1.Text) ^ 2End SubPrivate Sub Command17_Click()Text1.Text = " "End SubPrivate Sub Command18_Click()Text1.Text = (Text1.Text) ^ 3End SubPrivate Sub Command20_Click()If Text1.Text = "" ThenMsgBox ("²Ù×÷´íÎó")ElseText1.Text = Val(Text1.Text) * (-1)End IfEnd SubPrivate Sub Command21_Click()If InStr(Text1.Text, ".") ThenMsgBox ("СÊýµãÒÑ´æÔÚ")ElseText1.Text = Text1 & Chr(46)End IfEnd SubPrivate Sub Command23_Click()If N = 1 ThenIf Text1.Text = 0 ThenMsgBox "³ýÊý²»ÄÜΪÁã"ElseText1 = s1 / Text1.TextEnd IfElseIf N = 2 ThenText1 = s1 * Text1.TextElseIf N = 3 ThenText1 = s1 - Text1.TextElseIf N = 4 ThenText1 = s1 + Text1.TextEnd IfEnd SubPrivate Sub Command24_Click()Text1.Text = 1 / Text1.TextEnd SubPrivate Sub Command25_Click(Index As Integer) Select Case IndexCase 0Text1.Text = Text1.Text & "A"Case 1Text1.Text = Text1.Text & "B"Case 2Text1.Text = Text1.Text & "C"Case 3Text1.Text = Text1.Text & "D"Case 4Text1.Text = Text1.Text & "E"Case 5Text1.Text = Text1.Text & "F"End SelectEnd SubPrivate Sub Command4_Click(Index As Integer) Select Case IndexCase 0N = 1s1 = Text1.TextText1.Text = ""Case 1N = 2s1 = Text1.TextText1.Text = ""Case 2N = 3s1 = Text1.TextText1.Text = ""Case 3N = 4s1 = Text1.TextText1.Text = ""End SelectEnd SubPrivate Sub Command5_Click()Text1.Text = Left(Text1.Text, Len(Text1.Text) - 1) End SubPrivate Sub Command6_Click()Text1.Text = Sqr(Text1.Text)End SubPrivate Sub Form_Load()r = 10End Sub'Ê®Áù½øÖÆPrivate Sub Option1_Click()Command25(1).Enabled = TrueCommand25(0).Enabled = TrueCommand25(2).Enabled = TrueCommand25(3).Enabled = TrueCommand25(4).Enabled = TrueCommand25(5).Enabled = TrueCommand1(6).Enabled = TrueCommand1(9).Enabled = TrueCommand1(2).Enabled = TrueCommand1(3).Enabled = TrueCommand1(4).Enabled = TrueCommand1(5).Enabled = TrueCommand1(7).Enabled = TrueIf r = 10 ThenText1.Text = trandec(Val(Text1.Text), 16) ElseText1.Text = Two16(Text1.Text)End Ifr = 16End Sub'Ê®½øÖÆPrivate Sub Option2_Click()Command1(6).Enabled = TrueCommand1(9).Enabled = TrueCommand1(2).Enabled = TrueCommand1(3).Enabled = TrueCommand1(4).Enabled = TrueCommand1(5).Enabled = TrueCommand1(8).Enabled = TrueCommand1(7).Enabled = TrueCommand25(0).Enabled = FalseCommand25(1).Enabled = FalseCommand25(2).Enabled = FalseCommand25(3).Enabled = FalseCommand25(4).Enabled = FalseCommand25(5).Enabled = FalseText1.Text = Convert(Text1.Text, r)r = 10End Sub'¶þ½øÖÆPrivate Sub Option3_Click()Command1(6).Enabled = FalseCommand1(9).Enabled = FalseCommand1(2).Enabled = FalseCommand1(3).Enabled = FalseCommand1(4).Enabled = FalseCommand1(5).Enabled = FalseCommand1(8).Enabled = FalseCommand1(7).Enabled = FalseCommand25(0).Enabled = FalseCommand25(1).Enabled = FalseCommand25(2).Enabled = FalseCommand25(3).Enabled = FalseCommand25(4).Enabled = FalseIf r = 10 ThenText1.Text = trandec(Val(Text1.Text), 2)ElseText1.Text = HEX_to_BIN(Text1.Text)End Ifr = 2End SubPrivate Sub Text1_Change()If Mid(Text1.Text, 1, 1) = "." ThenText1.Text = 0 & Text1.TextEnd IfEnd Sub'Ê®½øÖÆ×ª¶þºÍÊ®Áù½øÖÆPublic Function trandec$(ByVal m%, ByVal r%)Dim strdtor$Dim iB%, mr%strdtor = ""Do While m <> 0mr = m Mod rm = m \ rIf mr >= 10 Thenstrdtor = Chr(mr - 10 + 65) & strdtorElsestrdtor = mr & strdtorEnd IfLooptrandec = strdtorEnd FunctionPublic Function Convert(ByVal S As String, ByVal N As Integer) As Double 'ÈÎÒâ½øÖÆ×ª»»³É10½øÖÆDim L As StringDim r() As StringDim i As IntegerDim j As IntegerL = "0|1|2|3|4|5|6|7|8|9|A|B|C|D|E|F|G|H|I|J|K|L|M|N|O|P|Q|R|S|T|U|V|W |X|Y|Z" '½øÖÆ×Ö·û´®×Öµär = Split(L, "|")For i = 1 To Len(S)For j = 0 To UBound(r)If UCase(Mid(S, i, 1)) = r(j) ThenConvert = Convert * N + jEnd IfNext jNext iEnd FunctionPrivate Function Two16(ByVal X As String) As String '°Ñ¶þ½øÖÆÊýת»¯ÎªÊ®Áù½øÖÆÊýDo While Len(X) Mod 4 <> 0X = "0" + XLoopDo While Len(X) > 0Select Case Right(X, 4)Case "0000"Two16 = "0" + Two16Case "0001"Two16 = "1" + Two16Case "0010"Two16 = "2" + Two16Case "0011"Two16 = "3" + Two16Case "0100"Two16 = "4" + Two16Case "0101"Two16 = "5" + Two16Case "0110"Two16 = "6" + Two16Case "0111"Two16 = "7" + Two16Case "1000"Two16 = "8" + Two16Case "1001"Two16 = "9" + Two16Case "1010"Two16 = "A" + Two16Case "1011"Two16 = "B" + Two16Case "1100"Two16 = "C" + Two16Case "1101"Two16 = "D" + Two16Case "1110"Two16 = "E" + Two16Case "1111"Two16 = "F" + Two16End SelectX = Left(X, Len(X) - 4)LoopEnd Function'Ê®Áùת¶þPublic Function HEX_to_BIN(ByVal Hex As String) As String Dim i As LongDim B As StringHex = 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"Case "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 SelectNext iDo While Left(B, 1) = "0"B = Right(B, Len(B) - 1)LoopHEX_to_BIN = BEnd Function。

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

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

三、程序截图:四、源程序清单: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 Command3Caption = "1/x"Height = 495Left = 4470TabIndex = 27Top = 2520Width = 735EndBegin mandButton Command2Height = 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 = 735EndBegin mandButton cmbSign Caption = "+"Height = 495Left = 3690TabIndex = 22Top = 3060Width = 735EndBegin mandButton cmbSign Caption = "-"Height = 495Index = 2Left = 3690TabIndex = 21Top = 2520Width = 735EndBegin mandButton cmbSign Caption = "*"Height = 495Index = 1Left = 3690TabIndex = 20Top = 1980Width = 735EndBegin mandButton cmbSign Caption = "/"Height = 495Index = 0Left = 3690TabIndex = 19Top = 1440Width = 735EndBegin mandButton cmbNUMHeight = 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 = 16Top = 1440Width = 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 = 3Left = 2910TabIndex = 12Top = 2520Width = 735EndBegin mandButton cmbNUM Caption = "2"Height = 495Index = 2Left = 2130TabIndex = 11Width = 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+"Height = 495Left = 150TabIndex = 8Top = 3060Width = 975EndBegin mandButton cmbMS Caption = "MS"Height = 495Left = 150TabIndex = 7Width = 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 = 4020TabIndex = 4Top = 690Width = 1155 EndBegin mandButton cmbCE Caption = "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 'FalseEndPropertyHeight = 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 = FalseAttribute 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()'标示点击了点“。

相关文档
最新文档