当前位置:文档之家› 简单科学计算器vb程序

简单科学计算器vb程序

’//本人写的原始程序'注意没法实现如下功能:10+===然后输入10再=。

此程序会结果出错Dim denghao, fenghao As Boolean 'denghao判断是否按下“=”,fenghao判断是否按下“1/X”Dim xiaoshu, biaoji As Boolean 'xiaoshu判断是否已按“.”,biaoji第一次判断前面是否按过“+”“-”等运算符按钮Dim fuhao As Byte '运算符类型标号Dim leixin As Byte '进制转换类型Dim fistnumber, last As Double 'fistnumber第一个数据,最后一个数据Dim panduan, jinzhi As Boolean 'panduan判断前面是否连续按“+”“-”等运算符按钮Dim denglianxu As Boolean 'denglianxu判断前面是否连续安"="Dim m, n, i, sum As DoubleDim pi As Double 'sin,cosDim kuahao As Boolean '判断是否按下“(”Dim si As StringDim shilu, bajin As Boolean '十六进制下才能输入A-F;八进制下8-9不能输入Dim h, d, o, b As Integer '进制Dim se, s As DoubleDim ejin As Boolean '判断2-9,在二进制下不能输入Private Sub baifeng_Click() '1/X的程序If Not biaoji Thenfuhao = 7If Val(Text1.Text) = "0" ThenText1.Text = "除数不能为零"ElseCall jieguoEnd IfElseCall jieguofuhao = 7Call jieguoEnd Iffenghao = Truexiaoshu = FalseEnd SubPrivate Sub clear_Click()If clear.Value = 1 Then Text1.Text = "0"xiaoshu = Falseclear.Value = 0sum = 1biaoji = Falsepanduan = Falsedenghao = Falselast = 0fistnumber = 0denglianxu = FalseX = 0si = ""i = 0sum = 0shilu = Falsebajin = Falseejin = FalseEnd SubPrivate Sub Command11_Click() '+/-号Text1.Text = -1 * Val(Text1.Text)If Left(Val(Text1.Text), 1) = "." Then Text1.Text = "0" + Text1.Text If Left(Val(Text1.Text), 2) = "-." ThenText1.Text = -1 * Val(Text1.Text)Text1.Text = "-0" + Text1.TextEnd Ifpanduan = Truedenglianxu = Falsedenghao = TrueEnd SubPrivate Sub Command0_Click()If Text1.Text = "0" ThenText1.Text = 0ElseIf Not denghao And Not fenghao ThenIf Not kuahao Then Text1.Text = Text1.Text + "0"ElseText1.Text = "0"denglianxu = Falsefenghao = Falsedenghao = Falsepanduan = FalseEnd IfEnd SubPrivate Sub Command1_Click()If Text1.Text = "0" Then Text1.Text = ""If Not denghao And Not fenghao ThenIf Not kuahao Then Text1.Text = Text1.Text + "1" ElseText1.Text = "1"denglianxu = Falsefenghao = Falsedenghao = Falsepanduan = Falsekuahao = FalseEnd IfEnd SubPrivate Sub Command2_Click()If Text1.Text = "0" Then Text1.Text = ""If ejin ThenMsgBox "请输入二进制数"Text1.Text = 0Exit SubEnd IfIf Not denghao And Not fenghao ThenIf Not kuahao Then Text1.Text = Text1.Text + "2" ElseText1.Text = "2"fenghao = Falsedenghao = Falsepanduan = Falsedenglianxu = Falsekuahao = FalseEnd IfEnd SubPrivate Sub Command3_Click()If Text1.Text = "0" Then Text1.Text = ""If ejin ThenMsgBox "请输入二进制数"Text1.Text = 0Exit SubEnd IfIf Not denghao And Not fenghao ThenIf Not kuahao Then Text1.Text = Text1.Text + "3" ElseText1.Text = "3"fenghao = Falsedenghao = Falsepanduan = Falsedenglianxu = Falsekuahao = FalseEnd IfEnd SubPrivate Sub Command4_Click()If Text1.Text = "0" Then Text1.Text = ""If ejin ThenMsgBox "请输入二进制数"Text1.Text = 0Exit SubEnd IfIf Not denghao And Not fenghao ThenIf Not kuahao Then Text1.Text = Text1.Text + "4" ElseText1.Text = "4"fenghao = Falsedenghao = Falsepanduan = Falsekuahao = FalseEnd Ifdenglianxu = FalseEnd SubPrivate Sub Command5_Click()If Text1.Text = "0" Then Text1.Text = ""If ejin ThenMsgBox "请输入二进制数"Exit SubEnd IfIf Not denghao And Not fenghao ThenIf Not kuahao Then Text1.Text = Text1.Text + "5" ElseText1.Text = "5"denghao = Falsefenghao = Falsepanduan = Falsekuahao = FalseEnd Ifdenglianxu = FalseEnd SubPrivate Sub Command6_Click()If Text1.Text = "0" Then Text1.Text = ""If ejin ThenMsgBox "请输入二进制数"Text1.Text = 0Exit SubEnd IfIf Not denghao And Not fenghao ThenIf Not kuahao Then Text1.Text = Text1.Text + "6" ElseText1.Text = "6"denghao = Falsefenghao = Falsepanduan = Falsekuahao = FalseEnd Ifdenglianxu = FalseEnd SubPrivate Sub Command7_Click()If Text1.Text = "0" Then Text1.Text = ""If ejin ThenMsgBox "请输入二进制数"Text1.Text = 0Exit SubEnd IfIf Not denghao And Not fenghao ThenIf Not kuahao Then Text1.Text = Text1.Text + "7" Elsedenghao = Falsefenghao = Falsepanduan = Falsekuahao = FalseEnd Ifdenglianxu = FalseEnd SubPrivate Sub Command8_Click()If Text1.Text = "0" Then Text1.Text = ""If bajin ThenMsgBox "请输入八进制数"Text1.Text = 0Exit SubEnd IfIf ejin ThenMsgBox "请输入二进制数"Text1.Text = 0Exit SubEnd IfIf Not denghao And Not fenghao ThenIf Not kuahao Then Text1.Text = Text1.Text + "8" ElseText1.Text = "8"denghao = Falsefenghao = Falsepanduan = Falsekuahao = FalseEnd Ifdenglianxu = FalseEnd SubPrivate Sub Command9_Click()If Text1.Text = "0" Then Text1.Text = ""If bajin ThenMsgBox "请输入八进制数"Text1.Text = 0Exit SubEnd IfIf ejin ThenMsgBox "请输入二进制数"Text1.Text = 0Exit SubIf Not denghao And Not fenghao ThenIf Not kuahao Then Text1.Text = Text1.Text + "9" ElseText1.Text = "9"denghao = Falsefenghao = Falsepanduan = Falsekuahao = FalseEnd Ifdenglianxu = FalseEnd SubPrivate Sub Command17_Click(Index As Integer) If shilu ThenSelect Case IndexCase 0If Text1.Text = "0" Then Text1.Text = ""If Not denghao And Not fenghao ThenText1.Text = Text1.Text + "A"ElseText1.Text = "A"denghao = Falsefenghao = Falsepanduan = FalseEnd IfCase 1If Text1.Text = "0" Then Text1.Text = ""If Not denghao And Not fenghao ThenText1.Text = Text1.Text + "B"ElseText1.Text = "B"denghao = Falsefenghao = Falsepanduan = FalseEnd IfCase 2If Text1.Text = "0" Then Text1.Text = ""If Not denghao And Not fenghao ThenText1.Text = Text1.Text + "C"ElseText1.Text = "C"denghao = Falsefenghao = Falsepanduan = FalseCase 3If Text1.Text = "0" Then Text1.Text = "" If Not denghao And Not fenghao Then Text1.Text = Text1.Text + "D"ElseText1.Text = "D"denghao = Falsefenghao = Falsepanduan = FalseEnd IfCase 4If Text1.Text = "0" Then Text1.Text = "" If Not denghao And Not fenghao Then Text1.Text = Text1.Text + "E"ElseText1.Text = "E"denghao = Falsefenghao = Falsepanduan = FalseEnd IfCase 5If Text1.Text = "0" Then Text1.Text = "" If Not denghao And Not fenghao Then Text1.Text = Text1.Text + "F"ElseText1.Text = "F"denghao = Falsefenghao = Falsepanduan = FalseEnd IfEnd SelectElseMsgBox "无效函数"End IfEnd SubPrivate Sub eee_Click() 'e为底的指数fuhao = 5Call jieguoxiaoshu = FalsePrivate Sub lne_Click() 'e为底的对数If Text1.Text > 0 Thenfuhao = 6Call jieguoElseMsgBox "函数输入无效", vbRetryCancel, "信息提示" End Ifxiaoshu = FalseEnd SubPrivate Sub exit_Click()EndEnd SubPrivate Sub Form_Load()biaoji = FalseText1.Text = 0sum = 1si = ""xiaoshu = Falsepaiduan = Falsedenghao = Falsedenglianxu = Falseerjin = Falsed = 1i = 0bajin = Falseejin = False'x = 0End SubPrivate Sub jiahao_Click()If Not biaoji Thenfistnumber = Val(Text1.Text)ElseIf Not panduan ThenCall jieguofistnumber = Val(Text1.Text)ElseText1.Text = Val(Text1.Text)End Iffuhao = 1xiaoshu = Falsebiaoji = Truedenghao = Truepanduan = Truedenglianxu = FalseEnd SubPrivate Sub jianhao_Click()If Not biaoji Thenfistnumber = Val(Text1.Text)ElseIf Not panduan ThenCall jieguofistnumber = Val(Text1.Text)ElseText1.Text = Val(Text1.Text)End Iffuhao = 2xiaoshu = Falsedenghao = Truepanduan = Truebiaoji = Truedenglianxu = FalseEnd SubPrivate Sub chenghao_Click()If Not biaoji Then '没有按下运算符fistnumber = Val(Text1.Text)ElseIf Not panduan Then '没有连续按运算按钮Call jieguofistnumber = Val(Text1.Text)ElseText1.Text = Val(Text1.Text)End Iffuhao = 3xiaoshu = Falsebiaoji = Truedenghao = Truepanduan = Truedenglianxu = FalseEnd SubPrivate Sub chuhao_Click()If Not biaoji Thenfistnumber = Val(Text1.Text)ElseIf Not panduan ThenCall jieguofistnumber = Val(Text1.Text)ElseText1.Text = Val(Text1.Text)End Iffuhao = 4xiaoshu = Falsebiaoji = Truedenghao = Truepanduan = Truedenglianxu = FalseEnd SubPrivate Sub Command10_Click() '加“(”括号m = fuhaon = fistnumberbiaoji = FalseText1.Text = "("kuahao = FalseEnd SubPrivate Sub Com_Click() '加“)”括号Call jieguofistnumber = nfuhao = mpanduan = Falsefenghao = Truexiaoshu = FalseEnd SubPrivate Sub result_Click()If panduan Then '有两个以上连续的运算符If Not denglianxu Then '只按了一个等号Text1.Text = Val(Text1.Text)Call jieguoElse '按两个以上的等号Call jianchuEnd IfElse '没有连续运算符(运算符在两个数中间)'Call xxxdenglianxu = FalseCall jieguoEnd IfEnd SubPrivate Sub point_Click()If Not denghao And Not fenghao ThenIf Not kuahao ThenIf Text1.Text = "." Then Text1.Text = "0."End IfIf Not xiaoshu ThenText1.Text = Text1.Text + "."xiaoshu = TrueEnd IfElseText1.Text = "0."denghao = Falsefenghao = FalseEnd IfEnd SubPublic Sub jianchu() '多个等号连续时调用last = Val(Text1.Text)Text1.Text = fistnumberIf fuhao = 1 Then Text1.Text = last + Text1.TextIf fuhao = 2 Then Text1.Text = last - Text1.TextIf fuhao = 3 Then Text1.Text = last * Text1.TextIf fuhao = 4 ThenIf Val(Text1.Text) = "0" ThenText1.Text = "除数不能为零"ElseText1.Text = last / Val(Text1.Text)End IfEnd IfIf Left(Val(Text1.Text), 1) = "." Then Text1.Text = "0" + Text1.TextEnd SubPrivate Sub jiecheng_Click() '阶乘Dim n As Integer, Y As Double, i As Integer 'double和variant变量最多算到170!long最多算到12!If Val(Text1.Text) < 0 ThenText1.Text = "函数输入无效"ElseIf Val(Text1.Text) >= 171 ThenText1.Text = "超出本计算器计算范围"Elsen = Val(Text1.Text)Y = 1For i = 1 To nY = Y * iNext iText1.Text = Str(Y)End IfEnd SubPrivate Sub duishu_Click() '对数If Val(Text1.Text) <= 0 Or V al(Text1.Text) = 1 ThenText1.Text = "函数输入无效"ElseIf Not biaoji Thenfistnumber = Val(Text1.Text)ElseIf Not panduan ThenCall jieguofistnumber = Val(Text1.Text)ElseText1.Text = Val(Text1.Text)End Iffuhao = 9xiaoshu = Falsebiaoji = Truedenghao = Truepanduan = Truedenglianxu = FalseEnd SubPrivate Sub zhishu_Click() '指数If Not biaoji Thenfistnumber = Val(Text1.Text) ElseIf Not panduan ThenCall jieguofistnumber = Val(Text1.Text)ElseText1.Text = Val(Text1.Text)End Iffuhao = 8xiaoshu = Falsebiaoji = Truedenghao = Truepanduan = Truedenglianxu = FalseEnd SubPrivate Sub yuxuan_Click() '余弦fuhao = 10Call jieguoEnd SubPrivate Sub zhengxuan_Click() '正弦fuhao = 11Call jieguoEnd SubPrivate Sub Option1_Click() '16 leixin = 1shilu = Truebajin = Falseejin = FalseCall zhuanhuand = 0o = 0b = 0denghao = TrueEnd SubPrivate Sub Option2_Click() '10 leixin = 2shilu = Falsebajin = Falseejin = FalseCall zhuanhuand = 1h = 0o = 0b = 0denghao = TrueEnd SubPrivate Sub Option3_Click() '8 bajin = Trueleixin = 3shilu = Falseejin = FalseCall zhuanhuano = 1h = 0d = 0b = 0denghao = TrueEnd SubPrivate Sub Option4_Click() '2 leixin = 4ejin = Trueshilu = Falsebajin = FalseCall zhuanhuanb = 1h = 0o = 0denghao = TrueEnd SubPublic Sub zhuanhuan() '进制转换If leixin = 1 ThenIf d = 1 Then Text1.Text = Hex(Text1.Text) '十进制转换成十六进制If o = 1 Then Text1.Text = Hex("&o" & Text1.Text) '八进制转换成十六进制If b = 1 ThenCall ezsText1.Text = Hex(Text1.Text) '二进制转换成十六进制End IfEnd IfIf leixin = 2 ThenIf h = 1 Then Text1.Text = CLng("&h" & Text1.Text) '十六进制转换成十进制If o = 1 Then Text1.Text = CLng("&o" & Text1.Text) '八进制转换成十进制If b = 1 Then Call ezs '二进制转换成十进制End IfIf leixin = 3 Then'If Val(Text1.Text) > 7 Then'MsgBox "请输入八进制数"'Text1.Text = 0'Exit Sub'End If'If Val(Text1.Text) >= 0 And Val(Text1.Text) <= 32767 ThenIf h = 1 Then Text1.Text = Oct("&h" & Text1.Text) '十六进制转换成八进制If d = 1 Then Text1.Text = Oct$(Text1.Text) '十进制转换成八进制If b = 1 ThenCall ezsText1.Text = Oct$(Text1.Text) '二进制转换成八进制End If'Else'MsgBox "请输Integer类型的正数"'Text1.Text = 0'End IfEnd IfIf leixin = 4 ThenIf h = 1 Then '十六进制转换成二进制Text1.Text = CLng("&h" & Text1.Text)Call szeEnd IfIf d = 1 Then Call sze '十进制转换成二进制If o = 1 Then '八进制转换成二进制Text1.Text = CLng("&o" & Text1.Text)Call szeEnd IfIf b = 1 Then '二进制End IfEnd IfEnd SubPublic Sub jieguo() '计算过程和结果If fuhao = 1 ThenText1.Text = fistnumber + V al(Text1.Text)End IfIf fuhao = 2 ThenText1.Text = fistnumber - Val(Text1.Text)End IfIf fuhao = 3 ThenText1.Text = fistnumber * Val(Text1.Text)End IfIf fuhao = 4 ThenIf Val(Text1.Text) = "0" ThenText1.Text = "除数不能为零"ElseText1.Text = fistnumber / Val(Text1.Text)End IfEnd IfIf fuhao = 5 Then Text1.Text = Exp(Val(Text1.Text))If fuhao = 6 Then Text1.Text = Str(Log(Val(Text1.Text))) If fuhao = 7 Then Text1.Text = 1 / Val(Text1.Text)If fuhao = 8 Then '指数Text1.Text = fistnumber ^ Val(Text1.Text)End IfIf fuhao = 9 Then '对数If Val(Text1.Text) = 1 ThenText1.Text = 0Elsesum = Log(Val(Text1.Text)) / Log(fistnumber)Text1.Text = sumEnd IfEnd IfIf fuhao = 10 Thenpi = Val(Text1.Text) / 90If Val(Text1.Text) Mod 90 = 0 And pi Mod 2 <> 0 Then '判断输入值是否为90的奇数倍数,使cos90直接赋为0Text1.Text = 0ElseText1.Text = Cos(Val(Text1.Text) * 3.14159265368979 / 180)End IfEnd IfIf fuhao = 11 Thenpi = Val(Text1.Text) / 90If Val(Text1.Text) Mod 90 = 0 And pi Mod 2 = 0 ThenText1.Text = 0ElseText1.Text = Sin(Val(Text1.Text) * 3.14159265368979 / 180)End IfEnd IfIf Left(Val(Text1.Text), 1) = "." Then Text1.Text = "0" + Text1.TextIf Left(Val(Text1.Text), 2) = "-." ThenText1.Text = -1 * Val(Text1.Text)Text1.Text = "-0" + Text1.TextEnd Ifdenghao = Truexiaoshu = Falsebiaoji = Falsedenglianxu = TrueEnd SubPublic Sub sze() '十进制转换成二进制si = "" '不初始化将出现错误Do Until Val(Text1.Text) = 0s = Val(Text1.Text) Mod 2Text1.Text = Val(Text1.Text) \ 2si = si + Str$(s) 'str$()数值转换成字符串LoopText1.Text = StrReverse(si) '将字符串反序End SubPublic Sub ezs() '二进制转换成十进制sum = 0i = 0 '不赋为零,会出现错误Do Until Val(Text1.Text) = 0se = Val(Text1.Text) Mod 10Text1.Text = Val(Text1.Text) \ 10i = i + 1sum = sum + se * 2 ^ (i - 1)LoopText1.Text = sumEnd Sub。

相关主题