2010年9月27日 星期一

小白加法

Dim ansS As String, c As String
Private Sub Form_Load()
Open App.Path & "\in.txt" For Input As #1
Input #1, A
Input #1, Sy
Input #1, B
Call ans1(A, B)
End Sub

Public Sub ans1(ByVal x As String, ByVal y As String)
A = Len(x)
B = Len(y)
If A < B Then M = A: Max = B: c = y Else M = B: Max = A: c = x
ReDim sA(A) As Integer, sB(B) As Integer, ans(Max + 1) As Integer
For i = 1 To M
    sA(i) = Mid(x, A - i + 1, 1)
    sB(i) = Mid(y, B - i + 1, 1)
    ans(i) = sA(i) + sB(i) + ans(i)
    If ans(i) > 9 Then
    ans(i + 1) = ans(i) \ 10
    ans(i) = ans(i) Mod 10
    End If
Next i

For i = M + 1 To Max
    ans(i) = ans(i) + Mid(c, Max - i + 1, 1)
    If ans(i) > 9 Then
    ans(i + 1) = ans(i) \ 10
    ans(i) = ans(i) Mod 10
    End If
Next i

For i = Max + 1 To 1 Step -1
ansS = ansS & ans(i)
Next i
If Mid(ansS, 1, 1) = 0 Then ansS = Mid(ansS, 2, Len(ansS) - 1)
Print ansS
End Sub


阿揚有幫我看過了,應該是OK

1 則留言:

  1. 小白好,
    這樣子的程式,還是要重寫。
    將加法寫成函數。就是簡單地將被加數放進去,
    然後,傳回加起來的值。
    function myadd(byval a as string ,byval b as string) as string
    再試一次吧。

    回覆刪除