优质解答
参考资料:
Dim a(),b(),c() As Byte '定义输入和输出数组
Sub Main()
ts = "整数乘法"
xx = InputBox("请输入被乘数:",ts)
If xx = "" Then End
yy = InputBox("请输入乘数",ts)
If yy = "" Then End
a1 = Len(xx) '取xx的位数
b1 = Len(yy) '取yy的位数
c1 = a1 + b1 '确定结果的最大位数
ReDim a(a1),b(b1),c(c1) '重定义数组
For i = 1 To a
a(i) = Mid(xx,a1 - i + 1,1) '给数组a的每一位赋值
If Not IsNumeric(a(i)) Then '检查输入的合法性
MsgBox "被乘数输入错误",,ts
End
End If
Next i
For i = 1 To b1
b(i) = Mid(yy,b1 - i + 1,1) '给数组b的每一位赋值
If Not IsNumeric(b(i)) Then '检查输入的合法性
MsgBox "乘数输入错误",,ts
End
End If
Next i
For i = 1 To c1 '结果数组c初始化
c(i) = 0
Next i
For i = 1 To b1 '取b中的每一位
For j = 1 To a1 '取a中的每一位
c(j + i - 1) = c(j + i - 1) + b(i) * a(j) '相乘结果累加到c中的相应位
zz = c(j + i - 1)
k = 0
Do While zz >= 10 'c中的每一位进位检查
c(j + i + k - 1) = zz Mod 10 '本位处理
c(j + i + k) = c(j + i + k) + zz \ 10 '进位处理
zz = c(j + i + k)
k = k + 1
Loop
Next j
Next i
zz = ""
For k = c1 To 1 Step -1
If c(k) 0 Then Exit For '去掉结果中的前导0
Next k
k = IIf(k < 1,1,k)
For i = k To 1 Step -1
zz = zz + Trim(Str(c(i)))
Next i
zz = xx + " X " + yy + " = " + zz
ts = "计算结果:" + Str(a1) + "位 X " + Str(b1) + "位 结果为" + Str(k) + "位"
MsgBox zz,,ts '算式和结果输出
End
End Sub