2012年11月20日 星期二

化成二進位

數字(0~127)

n → 化成二進位 →加上偶同位→換算值

輸入→化成二進位→加上偶同位→換算成十進位→輸出

3 → (0000011) 2 → (00000110) 2 → 6

100 → (1100100) 2 → (11001001) 2 → 201

2 則留言:

  1. Private Sub Form_Load()
    Me.Hide
    Open App.Path & "\in.txt" For Input As #1
    Open App.Path & "\out.txt" For Output As #2
    Do
    Input #1, num
    Do
    y = num Mod 2
    num = num \ 2
    ans = y & ans
    Loop Until num = 0
    ori = Len(ans)
    temp = Replace(ans, 1, "")
    If (ori - Len(temp)) Mod 2 = 0 Then ans = ans & "0"
    If (ori - Len(temp)) Mod 2 <> 0 Then ans = ans & "1"
    For i = Len(ans) To 1 Step -1
    ansnum = ansnum + Val(Mid(ans, i, 1)) * (2 ^ (Len(ans) - i))
    Next
    Print #2, ansnum: ansnum = 0: ans = ""
    Loop Until EOF(1)
    Close #2
    Close #1
    End
    End Sub

    回覆刪除
  2. Dim x As Integer
    Dim ans As String
    Private Sub Form_Load()
    Me.Hide
    Open App.Path & "\in.txt" For Input As #1
    Open App.Path & "\out.txt" For Output As #2
    Do While Not EOF(1)
    Input #1, x
    x = Val(x): ans = ""
    Do
    y = x Mod 2
    x = x \ 2
    ans = y & ans
    Loop Until x = 0
    For i = Len(ans) + 1 To 7
    ans = "0" & ans
    Next i
    If Right(ans, 1) = "1" Then ans = ans & "0" Else ans = ans & "1"
    s = 0: All = 0
    For i = Len(ans) To 1 Step -1
    All = All + (Val(Mid(ans, i, 1)) * 2 ^ s)
    s = s + 1
    Next i
    Print #2, All
    Loop
    Close #2
    Close #1
    End
    End Sub

    回覆刪除