|
帮楼主把代码改写成自定义函数,并补足了变量定义。- Function Tm1$(a$, b$) 'pengyx
- Dim a1&, b1&, ab&, bb&, i&, j&, jw&, k&, m&, s$, t$, w&, x$, y$, z&, zs$
- m = 7 '7位与7位相乘
-
- If Len(a) + Len(b) < 16 Then '积的长度小于16位直接相乘
- s = Val(a) * Val(b)
- Else
- a1 = Len(a) Mod m: b1 = Len(b) Mod m
- If a1 <> 0 Then
- For i = 1 To m - a1
- ab = ab + 1
- a = a & "0"
- Next
- End If
- If b1 <> 0 Then
- For i = 1 To m - b1
- bb = bb + 1
- b = b & "0"
- Next
- End If 'a b长度补成M的倍数
- For i = 1 To Len(a) + Len(b) '初始化积
- s = s & "0"
- w = Len(s)
- Next
- a1 = Len(a) / m: b1 = Len(b) / m
- For i = 1 To a1
- x = Mid(a, (i - 1) * m + 1, m) '从数a中取m位
- For j = 1 To b1
- y = Mid(b, (j - 1) * m + 1, m) '从数a中取m位
- t = x * y '计算前m位的积t
- For k = 1 To (i + j) * m - Len(t)
- t = "0" & t
- Next '对t定位
- For k = 1 To Len(a) + Len(b) - Len(t)
- t = t & "0"
- Next '在t后面加0,将t补成a的长度+b的长度位数
- For k = Len(a) + Len(b) To 1 Step -1
- z = Val(Mid(s, k, 1)) + Val(Mid(t, k, 1)) + jw
- If z > 9 Then
- z = z - 10
- jw = 1
- Else
- jw = 0
- End If
- zs = z & zs
- Next '将每个t从右到左依次相加,将和的个位连成字符串,jw逢10进1
- s = zs '字符串就是积
- zs = "" '初始化字符串
- Next
- Next
- If Left(s, 1) = "0" Then '判断积的位数
- s = Mid(s, 2, Len(s) - ab - bb - 1)
- Else
- s = Mid(s, 1, Len(s) - ab - bb)
- End If
- End If
- Tm1 = s
- End Function
复制代码 |
评分
-
查看全部评分
|