|
楼主 |
发表于 2012-4-18 20:55
|
显示全部楼层
[原创]大整数的乘法
哈,这会改过来了,学精通不易,学会几条语句还是不难,当你搞通一条语句时,不是很快乐吗?你会觉得好玩!楼上yehe老师的中间缺0的问题解决了,就是把数组变量声明为字符,计算数值补够0,就是这个Dim A() As String
和A(C, R) = String(4 - Len(CJ Mod 10000), "0") & CJ Mod 10000 ';本位这个
程序如下
Private Sub Command1_Click()
D1 = Text1.Text
D2 = Text2.Text
Text3.Text = MbC(Text1.Text, Text2.Text)
End Sub
Public Function MbC(D1 As String, D2 As String) As String
Dim X, Y ';两数长度
X = Len(D1) \ 4: Y = Len(D2) \ 4
If Len(D1) Mod 4 = 0 Then
X = X
Else
X = X + 1
If Len(D2) Mod 4 = 0 Then
Y = Y
Else
Y = Y + 1
End If
End If
D3 = String(4 * X - Len(D1), "0") & D1
D4 = String(4 * Y - Len(D2), "0") & D2
Dim A() As String
ReDim A(4 To 4 * X + 4 * Y, 4 To 4 * Y)
Dim I, J, C1, C2, CJ, JW, s, t
For J = 4 * Y To 4 Step -4 ';D2
JW = 0 ';进位清0
C2 = Mid$(D4, J - 3, 4) ';每位数
For I = 4 * X To 4 Step -4 ';D1
C1 = Mid$(D3, I - 3, 4) ';每位数
CJ = Val(C1) * Val(C2) + JW ';计算乘积
C = I + J: R = 4 * Y + 4 - J
A(C, R) = String(4 - Len(CJ Mod 10000), "0") & CJ Mod 10000 ';本位
JW = CJ \ 10000 ';进位
Next
A(C - 4, R) = JW
Next
Dim B() As String
ReDim B(1 To X + Y)
JW = 0
For s = X + Y To 1 Step -1
Bit = JW
For t = 1 To Y
Bit = Bit + Val(A(4 * s, 4 * t))
Next
B(s) = String(4 - Len(Bit Mod 10000), "0") & Bit Mod 10000
JW = Bit \ 10000
Next
If B(1) > 0 Then
MbC = Val(Left(MbC, 5)) & Mid(MbC, 6) & B(1)
Else
MbC = Val(Left(MbC, 5)) & Mid(MbC, 6)
End If
For s = 2 To X + Y
MbC = Val(Left(MbC, 5)) & Mid(MbC, 6) & B(s)
Next
End Function
Private Sub Command2_Click()
Text1.Text = ""
Text2.Text = ""
Text3.Text = ""
End Sub
实验
乘数1000001
乘数3
积3000003 |
|