|
本帖最后由 lolmuta 于 2014-4-2 11:14 编辑
二个巨大数的+-x 除还在研究中
虽然原理只是小学的数学,但也弄了好一阵子......
- Sub test()
- Dim sgg As String
- sgg = BigNumMultiplication("1230981204973453434534510732", "64569845695683295334534534534586")
- Debug.Print sgg '79484266459422178553419202408729802785262572401684842176952
- sgg = BigNumAdd("1230981204973453434534510732", "64569845695683295334534534534586")
- Debug.Print sgg '64571076676888268787969069045318
- sgg = BigNumSubtract("1230981204973453434534510732", "64569845695683295334534534534586")
- Debug.Print sgg '-64568614714478321881100000023854
- End Sub
复制代码 '相乘 A>0 B>0-
- Public Function BigNumMultiplication(A As String, B As String) As String
- Dim Result() As String
- ReDim Result(0 To Len(A) - 1)
-
-
- Dim i As Integer, j As Integer
-
-
- Dim NextNo As Integer
- For i = Len(A) To 1 Step -1
- Dim t As Integer
- t = CInt(Mid(A, i, 1))
-
- Dim Dig As Integer
- Dig = Len(A) - i
- For k = 1 To Dig
- Result(Dig) = Result(Dig) & "0"
- Next
- For j = Len(B) To 1 Step -1
-
- Dim s As Integer
- s = CInt(Mid(B, j, 1))
- 'Debug.Print t & " " & s
- '保留各位數
-
- Dim s_t As Integer
- s_t = s * t + NextNo
- If j > 1 Then
- Dim S_digits As String
- S_digits = s_t Mod 10
- '進位要給下一個
- NextNo = s_t \ 10
- Result(Dig) = S_digits & Result(Dig)
- Else
- Result(Dig) = s_t & Result(Dig)
- End If
-
-
- Next
- NextNo = 0
-
-
- Next
-
-
- Dim AddAllNo As String
- AddAllNo = "0"
- For i = LBound(Result) To UBound(Result)
-
- AddAllNo = BigNumAdd(AddAllNo, Result(i))
-
- Next
- BigNumMultiplication = AddAllNo
-
-
- End Function
复制代码 '相加
- Public Function BigNumAdd(A As String, B As String) As String
- Dim i As Integer, j As Integer
- Dim NoI As Integer
- Dim NextNo As Integer
-
- Dim s As Integer, t As Integer
- Dim Result As String
- Do
-
- NoI = NoI + 1
-
- If (Len(A) - NoI) < 0 And (Len(B) - NoI) < 0 Then
- If NextNo > 0 Then
- Result = NextNo & Result
- End If
- Exit Do
- End If
- If (Len(A) - NoI) < 0 Then
- s = 0
- Else
- s = Mid(A, Len(A) - NoI + 1, 1)
-
- End If
- If (Len(B) - NoI) < 0 Then
- t = 0
- Else
- t = Mid(B, Len(B) - NoI + 1, 1)
- End If
- Dim s_t As Integer
- s_t = s + t + NextNo
- NextNo = s_t \ 10
- Dim S_digits As String
- S_digits = CStr(s_t Mod 10)
-
- Result = S_digits & Result
-
- Loop
- BigNumAdd = Result
-
- End Function
复制代码 '相減 |
|