谢谢Nothing!
上面这段代码对我很有启发。尤其是关于进位的那一段,
j = 0
Do Until j > intR
intA(j) = intT(j) Mod 10
If intT(j) >= 10 Then intT(j + 1) = intT(j + 1) + intT(j) \ 10
j = j + 1
Loop
在启发之下,初步写出以下代码来实现数组乘法:
Option Explicit
Private Sub Command1_Click()
Dim fac1(100) As Integer '保存被乘数各位数字
Dim fac2(100) As Integer '保存乘数各位数字
Dim result(200) As Integer '保存结果
Dim lenOfFac1 As Integer '保存被乘数的字符长度
Dim lenOfFac2 As Integer '保存乘数的字符长度
Dim i As Integer, j As Integer, p As Integer, q As Integer
Dim pos As Integer, m As Integer, n As Integer
'Debug.Print "result(100) = " & result(100)
lenOfFac1 = Len(Text1.Text)
lenOfFac2 = Len(Text2.Text)
'*********************************************
'*读取并保存被乘数和乘数到相应的数组
'
For i = 0 To lenOfFac1 - 1
fac1(100 - i) = Val(Mid(Text1.Text, lenOfFac1 - i, 1))
'Debug.Print "fac1(" & 100 - i & ")=" & fac1(100 - i),
Next
For j = 0 To lenOfFac2 - 1
fac2(100 - j) = Val(Mid(Text2.Text, lenOfFac2 - j, 1))
'Debug.Print "fac2(" & 100 - j & ")=" & fac2(100 - j),
Next
'************************************************
'*此处关键是要理解乘数m位与被乘数n位的数字相乘,其结果对应于"十进制数"的m+n-1
'*之后,再转换到数组的相应元素index中
For m = 1 To lenOfFac2 '控制乘数各位
For n = 1 To lenOfFac1 '控制被乘数各位
result(201 - (m + n - 1)) = result(201 - (m + n - 1)) + fac1(100 - n + 1) * fac2(100 - m + 1)
If result(201 - (m + n - 1)) >= 10 Then result(201 - (m + n - 1) - 1) = result(201 - (m + n - 1) - 1) + result(201 - (m + n - 1)) \ 10
result(201 - (m + n - 1)) = result(201 - (m + n - 1)) Mod 10
Debug.Print "result(201-(" & m & "+" & n & "-1))=" & result(201 - (m + n - 1)), _
"result(201-(" & m & "+" & n & "-1)-1)=" & result(201 - (m + n - 1) - 1)
Next n
Next m
For p = 0 To 200
If result(p) <> 0 Then
pos = p
Exit For
End If
Next
Debug.Print "pos=" & pos
Text3.Text = ""
For q = pos To 200
Text3.Text = Text3.Text & result(q)
Next
Debug.Print "len(text3)=" & Len(Text3.Text)
End Sub
Private Sub Text1_KeyPress(KeyAscii As Integer)
'Debug.Print KeyAscii,
If KeyAscii <> 8 And (KeyAscii < 48 Or KeyAscii > 57) Then
KeyAscii = 0
End If
End Sub
Private Sub Text2_KeyPress(KeyAscii As Integer)
'*只能输入数字和BackSpace键.Text1与此相同
If KeyAscii <> 8 And (KeyAscii < 48 Or KeyAscii > 57) Then
KeyAscii = 0
End If
End Sub
以上可以实现不超过100位的整数的相乘.当然,暂时还没有进行错误处理