⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 basmath.bas

📁 功能强大的API
💻 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 + -