当前位置:文档之家› 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 事件过程'重新设置并初始化变量。

Num1 = 0Num2 = 0Text1.Text = "0"OptType = -1LastInput = "Nul"Command1(0).SetFocusFirstNum = TrueEnd SubPrivate Sub Command4_Click()'单击"OFF"键退出程序EndEnd SubPrivate Sub Command5_Click(Index As Integer)'单击运算符(+、-、*、/)按钮的事件过程'按下运算符后,单击"-","-"作为负号使用If LastInput = "Opt" And Command5(Index).Caption = "-" Then Text1.Text = "-"LastInput = "Neg"Exit SubEnd If'按下"C"(取消)键后,单击"-","-"作为负号使用If LastInput = "Nul" And Command5(Index).Caption = "-" Then Text1.Text = "-"LastInput = "Neg"OptType = -1Exit SubEnd If'按下运算符键后,获取第一个操作数Num1,并记录按下的运算符Num1 = Val(Text1.Text)If shuzhi = 2 Then Num1 = n2to10(Num1)Text1.Text = ""LastInput = "Opt"OptType = IndexCommand1(0).SetFocusFirstNum = FalseEnd SubPrivate Sub Command6_Click()'单击"="按钮时,计算并显示结果Num2 = Val(Text1.Text)If shuzhi = 2 Then Num2 = n2to10(Num2)Select Case OptTypeCase 0 '加法Result = Num1 + Num2Case 1 '减法Result = Num1 - Num2Case 2 '乘法Result = Num1 * Num2Case 3 '除法If Num2 = 0 ThenText1.Text = "EEEE.EEEE"ElseResult = Num1 / Num2End IfEnd SelectIf shuzhi = 2 Then Result = n10to2(Result)If Text1.Text <> "EEEE.EEEE" Then Text1.Text = Trim(Str(Result)) '显示结果Num1 = 0Num2 = 0LastInput = "Eqv"OptType = -1Command1(0).SetFocusFirstNum = TrueEnd SubPrivate Sub Command7_Click(Index As Integer)On Error Resume NextNum1 = Val(Text1.Text)If shuzhi = 2 Then Num1 = n2to10(Num1)Select Case IndexCase 0 'Sin(x)Text1.Text = Sin(angle(Num1))Case 1 'Cos(x)Text1.Text = Cos(angle(Num1))Case 2 'Tan(x)Text1.Text = Tan(angle(Num1))Case 3 'ASin(x)If Abs(Num1) <= 1 ThenText1.Text = ArcSin(Num1)ElseText1.Text = "EEEE.EEEE"End IfCase 4If Abs(Num1) <= 1 ThenText1.Text = ArcCos(Num1)ElseText1.Text = "EEEE.EEEE"End IfCase 5Text1.Text = Atn(Num1)If Option1.Value Then Text1.Text = Atn(Num1) * 180 / 3.1415926 End SelectIf Option3.Value Then Text1.Text = n10to2(Text1.Text)Num1 = 0Num2 = 0LastInput = "Eqv"OptType = -1Command1(0).SetFocusFirstNum = TrueEnd SubPrivate Sub Command8_Click(Index As Integer)Num1 = Val(Text1.Text)If shuzhi = 2 Then Num1 = n2to10(Num1)Select Case IndexCase 0 'E^xText1.Text = 2.7182 ^ Num1Case 1 'LN(X)If Num1 <= 0 ThenText1.Text = "EEEE.EEEE"ElseText1.Text = Log(Num1)End IfCase 2 'LOG(X)If Num1 <= 0 ThenText1.Text = "EEEE.EEEE"ElseText1.Text = Log(Num1) / Log(10)End IfCase 3 'Sqr(x)If Num1 < 0 ThenText1.Text = "EEEE.EEEE"ElseText1.Text = Sqr(Num1)End IfCase 4 '1/XIf Num1 = 0 ThenText1.Text = "EEEE.EEEE"ElseText1.Text = 1 / Num1End IfCase 5 'N!If Num1 < 0 ThenText1.Text = "EEEE.EEEE"ElseText1.Text = jiecheng(Int(Num1))End IfEnd SelectIf shuzhi = 2 Then Text1.Text = n10to2(Val(Text1.Text))Num1 = 0Num2 = 0LastInput = "Eqv"OptType = -1Command1(0).SetFocusFirstNum = TrueEnd SubPrivate Sub Form_Load()'加载窗体,变量初始化LastInput = "Nul"Num1 = 0Num2 = 0OptType = -1Text1.Text = "0"Text1.Locked = Trueshuzhi = 10Option1.Value = TrueOption4.Value = TrueText1.Locked = TrueEnd SubPrivate Sub Option1_Click()Static i As IntegerIf i > 0 Then Command1(0).SetFocus i = i + 1End SubPrivate Sub Option2_Click() Command1(0).SetFocusEnd SubPrivate Sub Option3_Click()Dim i As Integershuzhi = 2For i = 2 To 9Command1(i).Enabled = False Next iIf FirstNum ThenNum1 = Val(Text1.Text)Text1.Text = n10to2(Num1)ElseNum2 = Val(Text1.Text)Text1.Text = n10to2(Num2)End IfCommand1(0).SetFocusEnd SubPrivate Sub Option4_Click()Dim i As IntegerStatic j As IntegerIf shuzhi = 2 ThenFor i = 2 To 9Command1(i).Enabled = TrueNext ishuzhi = 10End IfIf FirstNum ThenNum1 = Val(Text1.Text)Text1.Text = n2to10(Num1)ElseNum2 = Val(Text1.Text)Text1.Text = n2to10(Num2)End IfFirstNum = TrueIf j > 0 Then Command1(0).SetFocus j = j + 1End Sub。

相关主题