Private Sub Command1_Click() '加法 Text3.Text = Val(Text1.Text) + Val(Text2.Text) End SubPrivate Sub Command2_Click() '减法 Text3.Text = Val(Text1.Text) - Val(Text2.Text) End SubPrivate Sub Command3_Click() '乘法 Text3.Text = Val(Text1.Text) * Val(Text2.Text) End SubPrivate Sub Command4_Click() '除法 Text3.Text = Val(Text1.Text) \ Val(Text2.Text)End SubPrivate Sub Command6_Click() text1="0"End Sub 除数不能为零吗,要怎样才能当text2为0时提示除数不能为0!
Dim que(25) As Double Public qt As Integer Public qh As Integer Public qv As Integer Public ang As DoublePublic memo As Double Dim dflag As Integer Dim i As Integer Dim opnre As Integer Dim prev As Double Dim oflag As Integer Dim ind As Integer Private Sub Command1_Click(Index As Integer) If ind = 4 Then prev = 0 Text1.Text = " " ind = 0 End If opnre = 0 ' conform operand If oflag = 0 Then ' if previous operator clear text Text1.Text = " " End If oflag = 1 If Command1(Index).Caption <> "." Then If Text1.Text <> " 0" Then Text1.Text = Text1.Text & Command1(Index).Caption Else Text1.Text = " " & Command1(Index).Caption End If Else If dflag = 0 Then Text1.Text = Text1.Text & "." dflag = 1 Else MsgBox ("ILLEGAL SAIRAM") End If End If End SubPrivate Sub Command10_Click(Index As Integer) If Index = 2 Then If Val(Text1.Text) < 700 Then Text1.Text = Str(Exp(Val(Text1.Text))) Else MsgBox (" OVERFLOW. VALUE TOO BIG ") End If ElseIf Index = 0 Then If Val(Text1.Text) > 0 Then Text1.Text = Str(Log(Val(Text1.Text))) Else MsgBox (" ILLEGAL. LOG NON POSITIVE ") End If ElseIf Index = 1 Then If Val(Text1.Text) > 0 Then Text1.Text = Str((Log(Val(Text1.Text)) / Log(10))) Else MsgBox (" ILLEGAL. LOG NON POSITIVE ") End If End If End SubPrivate Sub Command11_Click() If (Text1.Text <> "") Then Text1.Text = Mid(Text1.Text, 1, Len(Text1.Text) - 1) End If End SubPrivate Sub Command13_Click() Unload Me End SubPrivate Sub Command14_Click() Text1.Text = Str(Val(Text1.Text) * -1) End Sub Function qnext(p As Integer) As Integer On Error Resume Next qnext = (p + 1) Mod qt End FunctionFunction power(x As Double, Y As Long) As Double Dim i As Double i = 1 If (Y > 0) Then While Y > 0 Y = Y - 1 i = i * x Wend power = i ElseIf (Y = 0) Then power = 1 Else MsgBox ("ILLEGAL. POWER LESTHAN 0.") End If End Function
Function fact(num As Long) As Long If (num < 0 Or num = 0) Then MsgBox ("ILLEGAL NEAGETIVE FACTORIAL") fact = num Else If (num > 12) Then MsgBox ("VALUE TOO LARGE") fact = num Else re = 1 While (num > 0) re = re * num num = num - 1 Wend fact = re End If End If
End Function Private Sub Command2_Click(Index As Integer) Dim n As Long Dim r As Long If opnre = 0 Or Index = 4 Then If ind = 0 Then prev = prev + Val(Text1.Text) ElseIf ind = 1 Then prev = prev - Val(Text1.Text) ElseIf ind = 6 Then prev = prev Mod Val(Text1.Text) ElseIf ind = 7 Then r = Fix(Val(Text1.Text)) n = Fix(Val(prev)) If ((n > r Or n = r) And n > 0 And (r > 0 Or r = 0)) Then prev = fact(n) / (fact(n - r)) Else MsgBox ("ILLEGAL ENTRIES of N ,R ") End If ElseIf ind = 5 Then r = Fix(Val(Text1.Text)) n = Fix(Val(prev)) If ((n > r Or n = r) And n > 0 And (r > 0 Or r = 0)) Then prev = fact(n) / (fact(n - r) * fact(r)) Else MsgBox ("ILLEGAL ENTRIES of N ,R ") End If ElseIf ind = 8 Then If (Text1.Text = "" Or prev = 0) Then MsgBox ("ILLEGAL. INVALIED ENTRIES") Else prev = (power(prev, Fix(Val(Text1.Text))))
End If
ElseIf ind = 2 Then If Val(Text1.Text) <> 0 Then prev = prev / Val(Text1.Text) Else MsgBox (" ILLEGAL DIVIDE 0 ") End If ElseIf ind = 3 Then prev = prev * Val(Text1.Text) End If Text1.Text = Str(prev) oflag = 0 ' operator or operand End If opnre = 1 ' multiple operators flag ind = Index dflag = 0 End SubPrivate Sub Command3_Click(Index As Integer) Text1.Text = " 0" End SubPrivate Sub Command4_Click(Index As Integer) memo = 0 dflag = 0 prev = 0 oflag = 0 ind = 0 opnre = 0 qh = 0 qt = 0 Clipboard.Clear Text1.Text = " 0" End SubPrivate Sub Command5_Click(Index As Integer) Select Case Index Case 0 Text1.Text = Str(Sin(ang * Val(Text1.Text))) Case 1 Text1.Text = Str(Cos(ang * Val(Text1.Text))) Case 2 If (Cos(Val(Text1.Text))) <> 0 Then Text1.Text = Str(Sin(ang * Val(Text1.Text)) / Cos(ang * Val(Text1.Text))) Else MsgBox ("ILLEGAL. DIVIDE BY ZERO ") End If Case 5 Text1.Text = Str((Atn(Val(Text1.Text))) / ang) End Select End SubPrivate Sub Command6_Click(Index As Integer) Dim re As Long Dim temp As Long
temp = Val(scientific.Text1.Text) Select Case Index Case 2 ' temp = Val(Text1.Text) If temp > 0 Or temp = 0 Then scientific.Text1.Text = Str(Sqr(Val(Text1.Text))) Else MsgBox (" ILLEGAL ATTEMPTING NEGETIVE ROOT") End If
Case 0 temp = Val(Text1.Text) If temp <> 0 Then scientific.Text1.Text = Str(1 / temp) Else MsgBox (" ILLEGAL DIVIDE 0 ")
End If Case 1 If Abs(Val(Text1.Text)) < 46300 Then scientific.Text1.Text = Str((temp * temp)) Else MsgBox (" ILLEGAL DIVIDE 0 ") End If Case 8 If Abs(Val(Text1.Text)) < 1290 Then scientific.Text1.Text = Str(temp * temp * temp) Else MsgBox ("OVERFLOW. VALUE TOO LARGE ") End If Case 4 Text1.Text = Str(fact(Val(Text1.Text))) End Select
End Sub
Private Sub Command7_Click(Index As Integer) Select Case Index Case 0 memo = memo + Val(Text1.Text) Case 1 memo = memo - Val(Text1.Text) Case 2 memo = memo * Val(Text1.Text) Case 3 If Val(Text1.Text) <> 0 Then memo = memo / Val(Text1.Text) Else MsgBox ("ILLEGAL. DIVIDE 0 ERROR ") End If Case 4 Text1.Text = Str(memo) prev = Val(Text1.Text) Case 5 memo = 0 End Select End SubPrivate Sub Command8_Click(Index As Integer) Select Case Index Case 0 que(qt) = Val(Text1.Text) qt = qt + 1 Case 1 qv = qnext(qv) Label1.Caption = Str(que(qv)) ' qv = qnext(qv) Case 2 On Error GoTo anil que(qv) = que(qt - 1) Label1.Caption = Str(que(qv)) qt = qt - 1
Case 3 Text1.Text = Label1.Caption End Select anil:
End SubPrivate Sub Command9_Click(Index As Integer) Select Case Index Case 0 Text1.Text = Str(Rnd) Case 1 Text1.Text = 3.141592654 Case 2 Text1.Text = 2.718281828 Case 3 Text1.Text = 9.86 Case 4 Text1.Text = "6.625" End Select opnre = 0 ' If oflag = 0 Then ' Text1.Text = " " ' End If oflag = 1 End Sub 'Private Sub Command4_Click() ' dflag = 0 ' prev = 0 ' oflag = 0 ' ind = 0 ' opnre = 0 ' Text1.Text = " 0"'End Sub'Private Sub Command5_Click() ' Unload Me ' End 'End SubPrivate Sub ecopy_Click() Clipboard.Clear Clipboard.SetText Text1.SelText
End SubPrivate Sub ecut_Click() Clipboard.Clear Clipboard.SetText Text1.SelText Text1.SelText = ""
End SubPrivate Sub eexit_Click() Unload Me
End SubPrivate Sub epaste_Click() Text1.Text = "" Text1.SelText = Clipboard.GetText() End SubPrivate Sub eselectall_Click() Clipboard.Clear Clipboard.SetText Text1.Text End SubPrivate Sub Form_KeyPress(KeyAscii As Integer)
If KeyAscii = Asc(".") Then i = 10 Command1_Click (i) Beep ElseIf KeyAscii = Asc("0") Then i = 0 Command1_Click (i) Beep ElseIf KeyAscii = Asc("1") Then i = 1 Command1_Click (i) Beep ElseIf KeyAscii = Asc("2") Then i = 2 Command1_Click (i) Beep ElseIf KeyAscii = Asc("3") Then i = 3 Command1_Click (i) Beep ElseIf KeyAscii = Asc("4") Then i = 4 Command1_Click (i) Beep ElseIf KeyAscii = Asc("5") Then i = 5 Command1_Click (i) Beep ElseIf KeyAscii = Asc("6") Then i = 6 Command1_Click (i) Beep ElseIf KeyAscii = Asc("7") Then i = 7 Command1_Click (i) Beep ElseIf KeyAscii = Asc("8") Then i = 8 Command1_Click (i) Beep ElseIf KeyAscii = Asc("9") Then i = 9 Command1_Click (i) Beep ElseIf KeyAscii = Asc("0") Then i = 0 Command1_Click (i) Beep ElseIf KeyAscii = Asc("+") Then i = 0 Command2_Click (i) Beep ElseIf KeyAscii = Asc("+") Then i = 0 Command2_Click (i) Beep ElseIf KeyAscii = Asc("-") Then i = 1 Command2_Click (i) Beep ElseIf KeyAscii = Asc("/") Then i = 2 Command2_Click (i) Beep
ElseIf KeyAscii = Asc("*") Then i = 3 Command2_Click (i) Beep ElseIf KeyAscii = Asc("=") Then i = 4 Command2_Click (i) Beep ElseIf KeyAscii = Asc("c") Or KeyAscii = Asc("C") Then dflag = 0 prev = 0 oflag = 0 ind = 0 opnre = 0 Text1.Text = " 0" Beep Beep ElseIf KeyAscii = Asc("d") Or KeyAscii = Asc("D") Then Text1.Text = " 0" Beep End If End SubPrivate Sub Form_Load() scientific.Width = 6540 scientific.Height = 4735 memo = 0 dflag = 0 prev = 0 oflag = 0 ind = 0 opnre = 0 qh = 0 qt = 0 Clipboard.Clear Option2(0).Value = True ang = 3.14 / 180 End SubPrivate Sub habout_Click() nachelp.Show End SubPrivate Sub hcontents_Click() naccalc.ShowEnd SubPrivate Sub Option2_Click(Index As Integer) Select Case Index Case 0 ang = 3.141592654 / 180 Case 1 ang = 1 Case 2 ang = 3.141592654 / 200 End Select End SubPrivate Sub Option3_Click() standard.Show scientific.Hide scientific.Option3.Value = False End Sub Private Sub Text1_Change() Label2.Caption = Text1.TextEnd Sub
'加法
Text3.Text = Val(Text1.Text) + Val(Text2.Text)
End SubPrivate Sub Command2_Click()
'减法
Text3.Text = Val(Text1.Text) - Val(Text2.Text)
End SubPrivate Sub Command3_Click()
'乘法
Text3.Text = Val(Text1.Text) * Val(Text2.Text)
End SubPrivate Sub Command4_Click()
'除法
Text3.Text = Val(Text1.Text) \ Val(Text2.Text)End SubPrivate Sub Command6_Click()
text1="0"End Sub
除数不能为零吗,要怎样才能当text2为0时提示除数不能为0!
---------------------------------------------------------------------------------
把算数表达式传递给变量就可以了,计算顺序用不着操心。这个方法还可以计算逻辑运算
Public qt As Integer
Public qh As Integer
Public qv As Integer
Public ang As DoublePublic memo As Double
Dim dflag As Integer
Dim i As Integer
Dim opnre As Integer
Dim prev As Double
Dim oflag As Integer
Dim ind As Integer
Private Sub Command1_Click(Index As Integer)
If ind = 4 Then
prev = 0
Text1.Text = " "
ind = 0
End If
opnre = 0 ' conform operand
If oflag = 0 Then ' if previous operator clear text
Text1.Text = " "
End If
oflag = 1
If Command1(Index).Caption <> "." Then
If Text1.Text <> " 0" Then
Text1.Text = Text1.Text & Command1(Index).Caption
Else
Text1.Text = " " & Command1(Index).Caption
End If
Else
If dflag = 0 Then
Text1.Text = Text1.Text & "."
dflag = 1
Else
MsgBox ("ILLEGAL SAIRAM")
End If
End If
End SubPrivate Sub Command10_Click(Index As Integer)
If Index = 2 Then
If Val(Text1.Text) < 700 Then
Text1.Text = Str(Exp(Val(Text1.Text)))
Else
MsgBox (" OVERFLOW. VALUE TOO BIG ")
End If
ElseIf Index = 0 Then
If Val(Text1.Text) > 0 Then
Text1.Text = Str(Log(Val(Text1.Text)))
Else
MsgBox (" ILLEGAL. LOG NON POSITIVE ")
End If
ElseIf Index = 1 Then
If Val(Text1.Text) > 0 Then
Text1.Text = Str((Log(Val(Text1.Text)) / Log(10)))
Else
MsgBox (" ILLEGAL. LOG NON POSITIVE ")
End If End If
End SubPrivate Sub Command11_Click()
If (Text1.Text <> "") Then
Text1.Text = Mid(Text1.Text, 1, Len(Text1.Text) - 1)
End If
End SubPrivate Sub Command13_Click()
Unload Me
End SubPrivate Sub Command14_Click()
Text1.Text = Str(Val(Text1.Text) * -1)
End Sub
Function qnext(p As Integer) As Integer
On Error Resume Next
qnext = (p + 1) Mod qt
End FunctionFunction power(x As Double, Y As Long) As Double
Dim i As Double
i = 1
If (Y > 0) Then
While Y > 0
Y = Y - 1
i = i * x
Wend
power = i
ElseIf (Y = 0) Then
power = 1
Else
MsgBox ("ILLEGAL. POWER LESTHAN 0.")
End If
End Function
If (num < 0 Or num = 0) Then
MsgBox ("ILLEGAL NEAGETIVE FACTORIAL")
fact = num
Else
If (num > 12) Then
MsgBox ("VALUE TOO LARGE")
fact = num
Else
re = 1
While (num > 0)
re = re * num
num = num - 1
Wend
fact = re
End If
End If
End Function
Private Sub Command2_Click(Index As Integer)
Dim n As Long
Dim r As Long
If opnre = 0 Or Index = 4 Then
If ind = 0 Then
prev = prev + Val(Text1.Text)
ElseIf ind = 1 Then
prev = prev - Val(Text1.Text)
ElseIf ind = 6 Then
prev = prev Mod Val(Text1.Text)
ElseIf ind = 7 Then
r = Fix(Val(Text1.Text))
n = Fix(Val(prev))
If ((n > r Or n = r) And n > 0 And (r > 0 Or r = 0)) Then
prev = fact(n) / (fact(n - r))
Else
MsgBox ("ILLEGAL ENTRIES of N ,R ")
End If ElseIf ind = 5 Then
r = Fix(Val(Text1.Text))
n = Fix(Val(prev))
If ((n > r Or n = r) And n > 0 And (r > 0 Or r = 0)) Then
prev = fact(n) / (fact(n - r) * fact(r))
Else
MsgBox ("ILLEGAL ENTRIES of N ,R ")
End If
ElseIf ind = 8 Then
If (Text1.Text = "" Or prev = 0) Then
MsgBox ("ILLEGAL. INVALIED ENTRIES")
Else
prev = (power(prev, Fix(Val(Text1.Text))))
End If
ElseIf ind = 2 Then
If Val(Text1.Text) <> 0 Then
prev = prev / Val(Text1.Text)
Else
MsgBox (" ILLEGAL DIVIDE 0 ")
End If
ElseIf ind = 3 Then
prev = prev * Val(Text1.Text)
End If
Text1.Text = Str(prev)
oflag = 0 ' operator or operand
End If
opnre = 1 ' multiple operators flag
ind = Index
dflag = 0
End SubPrivate Sub Command3_Click(Index As Integer)
Text1.Text = " 0"
End SubPrivate Sub Command4_Click(Index As Integer)
memo = 0
dflag = 0
prev = 0
oflag = 0
ind = 0
opnre = 0
qh = 0
qt = 0
Clipboard.Clear
Text1.Text = " 0"
End SubPrivate Sub Command5_Click(Index As Integer)
Select Case Index
Case 0
Text1.Text = Str(Sin(ang * Val(Text1.Text)))
Case 1
Text1.Text = Str(Cos(ang * Val(Text1.Text)))
Case 2
If (Cos(Val(Text1.Text))) <> 0 Then
Text1.Text = Str(Sin(ang * Val(Text1.Text)) / Cos(ang * Val(Text1.Text)))
Else
MsgBox ("ILLEGAL. DIVIDE BY ZERO ")
End If
Case 5
Text1.Text = Str((Atn(Val(Text1.Text))) / ang)
End Select
End SubPrivate Sub Command6_Click(Index As Integer)
Dim re As Long
Dim temp As Long
temp = Val(scientific.Text1.Text)
Select Case Index
Case 2
' temp = Val(Text1.Text)
If temp > 0 Or temp = 0 Then
scientific.Text1.Text = Str(Sqr(Val(Text1.Text)))
Else
MsgBox (" ILLEGAL ATTEMPTING NEGETIVE ROOT")
End If
Case 0
temp = Val(Text1.Text)
If temp <> 0 Then
scientific.Text1.Text = Str(1 / temp)
Else
MsgBox (" ILLEGAL DIVIDE 0 ")
End If
Case 1
If Abs(Val(Text1.Text)) < 46300 Then
scientific.Text1.Text = Str((temp * temp))
Else
MsgBox (" ILLEGAL DIVIDE 0 ")
End If
Case 8
If Abs(Val(Text1.Text)) < 1290 Then
scientific.Text1.Text = Str(temp * temp * temp)
Else
MsgBox ("OVERFLOW. VALUE TOO LARGE ")
End If
Case 4
Text1.Text = Str(fact(Val(Text1.Text)))
End Select
End Sub
Select Case Index
Case 0
memo = memo + Val(Text1.Text)
Case 1
memo = memo - Val(Text1.Text)
Case 2
memo = memo * Val(Text1.Text)
Case 3
If Val(Text1.Text) <> 0 Then
memo = memo / Val(Text1.Text)
Else
MsgBox ("ILLEGAL. DIVIDE 0 ERROR ")
End If
Case 4
Text1.Text = Str(memo)
prev = Val(Text1.Text)
Case 5
memo = 0
End Select
End SubPrivate Sub Command8_Click(Index As Integer)
Select Case Index
Case 0
que(qt) = Val(Text1.Text)
qt = qt + 1
Case 1
qv = qnext(qv)
Label1.Caption = Str(que(qv))
' qv = qnext(qv)
Case 2
On Error GoTo anil
que(qv) = que(qt - 1)
Label1.Caption = Str(que(qv))
qt = qt - 1
Case 3
Text1.Text = Label1.Caption
End Select
anil:
End SubPrivate Sub Command9_Click(Index As Integer)
Select Case Index
Case 0
Text1.Text = Str(Rnd)
Case 1
Text1.Text = 3.141592654
Case 2
Text1.Text = 2.718281828
Case 3
Text1.Text = 9.86
Case 4
Text1.Text = "6.625"
End Select
opnre = 0
' If oflag = 0 Then
' Text1.Text = " "
' End If
oflag = 1
End Sub
'Private Sub Command4_Click()
' dflag = 0
' prev = 0
' oflag = 0
' ind = 0
' opnre = 0
' Text1.Text = " 0"'End Sub'Private Sub Command5_Click()
' Unload Me
' End
'End SubPrivate Sub ecopy_Click()
Clipboard.Clear
Clipboard.SetText Text1.SelText
End SubPrivate Sub ecut_Click()
Clipboard.Clear
Clipboard.SetText Text1.SelText
Text1.SelText = ""
End SubPrivate Sub eexit_Click()
Unload Me
End SubPrivate Sub epaste_Click()
Text1.Text = ""
Text1.SelText = Clipboard.GetText()
End SubPrivate Sub eselectall_Click()
Clipboard.Clear
Clipboard.SetText Text1.Text
End SubPrivate Sub Form_KeyPress(KeyAscii As Integer)
If KeyAscii = Asc(".") Then
i = 10
Command1_Click (i)
Beep
ElseIf KeyAscii = Asc("0") Then
i = 0
Command1_Click (i)
Beep
ElseIf KeyAscii = Asc("1") Then
i = 1
Command1_Click (i)
Beep
ElseIf KeyAscii = Asc("2") Then
i = 2
Command1_Click (i)
Beep
ElseIf KeyAscii = Asc("3") Then
i = 3
Command1_Click (i)
Beep
ElseIf KeyAscii = Asc("4") Then
i = 4
Command1_Click (i)
Beep
ElseIf KeyAscii = Asc("5") Then
i = 5
Command1_Click (i)
Beep
ElseIf KeyAscii = Asc("6") Then
i = 6
Command1_Click (i)
Beep
ElseIf KeyAscii = Asc("7") Then
i = 7
Command1_Click (i)
Beep
ElseIf KeyAscii = Asc("8") Then
i = 8
Command1_Click (i)
Beep
ElseIf KeyAscii = Asc("9") Then
i = 9
Command1_Click (i)
Beep
ElseIf KeyAscii = Asc("0") Then
i = 0
Command1_Click (i)
Beep
ElseIf KeyAscii = Asc("+") Then
i = 0
Command2_Click (i)
Beep
ElseIf KeyAscii = Asc("+") Then
i = 0
Command2_Click (i)
Beep
ElseIf KeyAscii = Asc("-") Then
i = 1
Command2_Click (i)
Beep
ElseIf KeyAscii = Asc("/") Then
i = 2
Command2_Click (i)
Beep
ElseIf KeyAscii = Asc("*") Then
i = 3
Command2_Click (i)
Beep
ElseIf KeyAscii = Asc("=") Then
i = 4
Command2_Click (i)
Beep
ElseIf KeyAscii = Asc("c") Or KeyAscii = Asc("C") Then
dflag = 0
prev = 0
oflag = 0
ind = 0
opnre = 0
Text1.Text = " 0"
Beep
Beep
ElseIf KeyAscii = Asc("d") Or KeyAscii = Asc("D") Then
Text1.Text = " 0"
Beep
End If
End SubPrivate Sub Form_Load()
scientific.Width = 6540
scientific.Height = 4735
memo = 0
dflag = 0
prev = 0
oflag = 0
ind = 0
opnre = 0
qh = 0
qt = 0
Clipboard.Clear
Option2(0).Value = True
ang = 3.14 / 180
End SubPrivate Sub habout_Click()
nachelp.Show
End SubPrivate Sub hcontents_Click()
naccalc.ShowEnd SubPrivate Sub Option2_Click(Index As Integer)
Select Case Index
Case 0
ang = 3.141592654 / 180
Case 1
ang = 1
Case 2
ang = 3.141592654 / 200
End Select
End SubPrivate Sub Option3_Click()
standard.Show
scientific.Hide
scientific.Option3.Value = False
End Sub
Private Sub Text1_Change()
Label2.Caption = Text1.TextEnd Sub