2011年3月18日 星期五

簡易大數減法

輸入兩數N1、N2,做大數的減法。 (N1、N2皆無限制)



輸入:
1000000000000000000000000
1

輸出:
999999999999999999999999

5 則留言:

  1. Private Sub Form_Load()
    Dim a As String, b As String
    Me.Hide
    Open App.Path & "\in.txt" For Input As #1
    Open App.Path & "\out.txt" For Output As #2

    Input #1, a
    Input #1, b

    Print #2, Bless(a, b)

    Close
    Close
    End
    End Sub
    Function Bless(ByVal A1 As String, ByVal A2 As String)
    Dim gets As Integer, gets2 As Integer, NewA2 As String, ANS As String, NewANS As String
    '補齊
    Do Until Len(A1) = Len(A2)
    If Len(A1) > Len(A2) Then
    A2 = "0" & A2
    Else
    A1 = "0" & A1
    End If
    Loop
    ' 9'S
    For i = 1 To Len(A2)
    gets = Mid(A2, i, 1)
    NewA2 = NewA2 & (9 - gets)
    Next
    ' 10'S
    A2 = Bplus(NewA2, "1")
    '計算
    ANS = Bplus(A1, A2)

    If Len(A2) < Len(ANS) Then
    '正數
    ANS = Right(ANS, Len(ANS) - 1)
    Do While Left(ANS, 1) = 0
    ANS = Right(ANS, Len(ANS) - 1)
    Loop
    Else

    '負數
    For i = 1 To Len(ANS)
    gets2 = Mid(ANS, i, 1)
    NewANS = NewANS & (9 - gets2)
    Next

    ANS = Bplus(NewANS, "1")

    Do While Left(ANS, 1) = 0
    ANS = Right(ANS, Len(ANS) - 1)
    Loop

    ANS = "-" & ANS
    End If

    Bless = ANS
    End Function
    Function Bplus(ByVal N1 As String, ByVal N2 As String)

    Dim NS1 As Integer, NS2 As Integer, Tmp As Integer, Plus As String, FAns As String

    Do Until Len(N1) = Len(N2)
    If Len(N1) > Len(N2) Then
    N2 = "0" & N2
    Else
    N1 = "0" & N1
    End If
    Loop


    For i = Len(N1) To 0 Step -1

    If i = 0 Then
    FAns = Tmp & FAns
    Else
    NS1 = Mid(N1, i, 1)
    NS2 = Mid(N2, i, 1)
    Plus = NS1 + NS2
    FAns = ((Plus + Tmp) Mod 10) & FAns
    Tmp = (Plus + Tmp) \ 10
    End If

    Next

    If Left(FAns, 1) = "0" Then FAns = Right(FAns, Len(FAns) - 1)

    Bplus = FAns

    End Function

    //-----

    感覺這題不難呢~

    做成函數果然方便多了

    回覆刪除
  2. arro好,
    這題做得不錯。
    既然是函數,那個補數,也做成函數好了。
    因為有兩個地方都做補數了,做成函數,在debug上容易些。(除錯)

    回覆刪除
  3. Private Sub Form_Load()
    Dim a As String, b As String
    Me.Hide
    Open App.Path & "\in.txt" For Input As #1
    Open App.Path & "\out.txt" For Output As #2

    Input #1, a
    Input #1, b

    Print #2, Bless(a, b)

    Close
    Close
    End
    End Sub
    Function Bless(ByVal A1 As String, ByVal A2 As String)
    Dim gets2 As Integer, ANS As String, NewANS As String
    '補齊
    Do Until Len(A1) = Len(A2)
    If Len(A1) > Len(A2) Then
    A2 = "0" & A2
    Else
    A1 = "0" & A1
    End If
    Loop
    ' 9'S 10'S
    A2 = Tr9(A2)
    '計算
    ANS = Bplus(A1, A2)

    If Len(A2) < Len(ANS) Then
    '正數
    ANS = Right(ANS, Len(ANS) - 1)
    Do While Left(ANS, 1) = 0
    ANS = Right(ANS, Len(ANS) - 1)
    Loop
    Else

    '負數
    ANS = Tr9(ANS)

    Do While Left(ANS, 1) = 0
    ANS = Right(ANS, Len(ANS) - 1)
    Loop

    ANS = "-" & ANS
    End If

    Bless = ANS
    End Function
    Function Bplus(ByVal N1 As String, ByVal N2 As String)

    Dim NS1 As Integer, NS2 As Integer, Tmp As Integer, Plus As String, FAns As String

    Do Until Len(N1) = Len(N2)
    If Len(N1) > Len(N2) Then
    N2 = "0" & N2
    Else
    N1 = "0" & N1
    End If
    Loop


    For i = Len(N1) To 0 Step -1

    If i = 0 Then
    FAns = Tmp & FAns
    Else
    NS1 = Mid(N1, i, 1)
    NS2 = Mid(N2, i, 1)
    Plus = NS1 + NS2
    FAns = ((Plus + Tmp) Mod 10) & FAns
    Tmp = (Plus + Tmp) \ 10
    End If

    Next

    If Left(FAns, 1) = "0" Then FAns = Right(FAns, Len(FAns) - 1)

    Bplus = FAns

    End Function

    Function Tr9(T As String)
    Dim NewT As String, gets As String
    ' 9'S
    For i = 1 To Len(T)
    gets = Mid(T, i, 1)
    NewT = NewT & (9 - gets)
    Next

    ' 10'S
    Tr9 = Bplus(NewT, "1")

    End Function



    我把9跟10的捕補數都弄再同一個func裡面
    雖說只用了兩次

    回覆刪除
  4. arro好,
    用兩次,用一次,雖然也是重點之一,但是,現階段的你(們)都還是在熟悉的地方,用用副程式和函數,還有listbox,都蠻好的。

    回覆刪除
  5. Private Sub Form_Load()
    Me.Hide
    Dim N1 As String, N2 As String
    Dim C9 As String, C10 As String


    Open App.Path & "\in.txt" For Input As #1
    Open App.Path & "\out.txt" For Output As #2

    Input #1, N1, N2

    If Len(N1) > Len(N2) Then

    For i = 1 To (Len(N1) - Len(N2))
    N2 = "0" & N2
    Next i

    Else

    For i = 1 To (Len(N2) - Len(N1))
    N1 = "0" & N1
    Next i

    End If


    X1 = Co9(N2)
    X1 = Mplus(X1, "1")
    X2 = Mplus(N1, X1)
    ans = ""
    If Len(X2) > Len(N1) Then

    For i = 2 To (Len(N1) + 1)
    ans = ans & Mid(X2, i, 1)
    Next i

    Else

    For i = 1 To Len(N1)
    ans = ans & Mid(X2, i, 1)
    Next i
    ans = Co9(ans)
    ans = Mplus(ans, "1")

    ans = "-" & ans

    End If

    If Left(ans, 1) = 0 Then ans = Replace(ans, "0", "", 1, 1)

    Print #2, ans

    Close #1
    Close #2
    End
    End Sub

    Function Co9(A)

    C = ""

    For i = 1 To Len(A)
    C = C & (9 - Val(Mid(A, i, 1)))
    Next i

    Co9 = C

    End Function


    Function Mplus(A, B)

    go = 0
    ans = ""

    If Len(B) > Len(A) Then
    C = B
    B = A
    A = C
    End If

    For i = 1 To Len(A) - Len(B)
    B = "0" & B
    Next i

    For i = Len(A) To 1 Step -1
    X = Val(Mid(A, i, 1)) + Val(Mid(B, i, 1)) + go
    go = 0
    If X >= 10 Then go = X \ 10: X = X Mod 10
    ans = CStr(X) & ans
    If i = 1 And go <> 0 Then ans = CStr(go) & ans
    Next i

    Mplus = ans

    End Function

    回覆刪除