?? basmath.bas
字號:
Attribute VB_Name = "basMath"
'****************************************
'漢化: 小聰明 coolzm@sohu.com
'小聰明的主頁VB版: http://coolzm.533.net
'****************************************
Option Explicit
'把一個nB進制的數s轉換為10進制數
'注意:此函數并不能判斷給定的數s和nB是否為有效的數
Public Function Base2Long(s As String, ByVal nB As Integer) As Long
Dim s2 As String
Dim i As Long
Dim j As Long
Dim X As Long
Dim n As Boolean
Dim s3 As String
If Len(s) < 1 Then
Base2Long = 0
Exit Function
End If
s2 = UCase(s)
If Left$(s2, 1) = "-" Then
n = True
s2 = Right$(s2, Len(s2) - 1)
Else
n = False
End If
j = 1
X = 0
For i = Len(s2) To 1 Step -1
s3 = Mid$(s2, i, 1)
Select Case s3
Case "0" To "9":
X = X + j * (Asc(s3) - 48)
Case "A" To "Z":
X = X + j * (Asc(s3) - 55)
End Select
j = j * nB
Next i
If n Then
X = -X
End If
Base2Long = X
End Function
'把10進制數n轉換為nB進制的數,nB在2到36之間
'例如ret=Long2Base(10,16)就是把10轉換為16進制數,ret= A
Public Function Long2Base(ByVal n As Long, ByVal nB As Integer) As String
Dim s As String
Dim nD As Integer
Dim Negative As Boolean
Negative = n < 0
n = Abs(n)
Do
nD = n Mod nB
If nD > 9 Then
nD = nD + 7
End If
s = Chr$(48 + nD) & s
n = n \ nB
Loop Until n = 0
If Negative Then
s = "-" & s
End If
Long2Base = s
End Function
'判斷一個數是否為素數,如果是則返回值為真(True),否則為假(False)
Public Function IsPrime(ByVal n As Long) As Boolean
Dim i As Long
IsPrime = False
If n <> 2 And (n And 1) = 0 Then Exit Function 'test if div 2
If n <> 3 And n Mod 3 = 0 Then Exit Function 'test if div 3
For i = 6 To Sqr(n) Step 6
If n Mod (i - 1) = 0 Then Exit Function
If n Mod (i + 1) = 0 Then Exit Function
Next
IsPrime = True
End Function
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -