Dim bb(1000) As Integer
Dim cc(1000) As Integer
Dim c As String
Private Sub Form_Load()
Open App.Path & "\in.txt" For Input As #1
Line Input #1, a
Line Input #1, b
Print a
Print b
Print myadd(a, b)
Print myfold(a, b)
Print k
Close #1
End Sub
Public Function myadd(a, b) As String
Dim c As String, q1 As Integer, q2 As Integer, nt As Integer
nt = 0: c = ""
If Len(a) < Len(b) Then
Min = Len(a): Max = Len(b): k = b
Else
Min = Len(b): Max = Len(a): k = a
End If
For i = 1 To Min
q1 = Mid(a, Len(a) - i + 1, 1)
q2 = Mid(b, Len(b) - i + 1, 1)
c = ((q1 + q2 + nt) Mod 10) & c
nt = (q1 + q2 + nt) \ 10
Next i
If nt > 0 Then
If Max = Min Then
myadd = nt & c
Else
myadd = myadd(Left(k, Max - Min), nt) & c
End If
Else
If Max = Min Then
myadd = c
Else
myadd = Left(k, Max - Min) & c
End If
End If
End Function
Public Function myfold(a, b) As String
Dim k As Integer
For i = Len(b) To 1 Step -1
t = Mid(b, i, 1)
For j = Len(a) To 1 Step -1
k = Len(a) - j + Len(b) - i
r = Mid(a, j, 1)
n = t * r
Do Until k <= 0
n = n & "0"
k = k - 1
Loop
ln = myadd(n, ln)
Next j
Next i
myfold = ln
End Function
測試資料
回覆刪除A
987654321987654321
B
14789632589632147
+
1002443954577286468
*
14607044547759654318209796785057187
阿揚好,
回覆刪除你的程式OK。
乘法中的補0的地方,想法正確。但是做太慢了。
直接將n*10^k,(或k-1,k+1要試一下),會快多了。
還是回歸到「數學」啊,加油。