2012年11月5日 星期一

第一次儲備選手選拔試卷第五題

數字系統轉換:從指定目錄"in.txt"讀取一正整數,將它轉為16進位後輸出至指定目錄"out.txt"。
(不可使用內定含數轉換)

輸入範例:168

輸出範例:A8

5 則留言:

  1. Dim num As Integer
    Dim ans(10) As String
    Private Sub Form_Load()

    Open App.Path & "\in.txt" For Input As #1
    Input #1, num
    Close #1

    countt = 0

    Do

    ans(countt) = Str(num Mod 16)
    num = num \ 16
    countt = countt + 1

    Loop Until num = 0

    Open App.Path & "\out.txt" For Output As #2
    For i = 10 To 0 Step -1
    If ans(i) = Str(10) Then ans(i) = "A"
    If ans(i) = Str(11) Then ans(i) = "B"
    If ans(i) = Str(12) Then ans(i) = "C"
    If ans(i) = Str(13) Then ans(i) = "D"
    If ans(i) = Str(14) Then ans(i) = "E"
    If ans(i) = Str(15) Then ans(i) = "F"
    If ans(i) <> "" And ans(i) <> "0" Then Print #2, ans(i);
    Next
    Close #2
    End Sub

    回覆刪除
  2. Private Sub Form_Load()
    Me.Hide
    Open App.Path & "\in.txt" For Input As #1
    Open App.Path & "\out.txt" For Output As #2
    a = Split("0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F", ",")
    Input #1, x
    Do
    y = x Mod 16
    x = x \ 16
    z = a(y) & z
    Loop Until x = 0
    Print #2, z
    Close #2
    Close #1
    End
    End Sub

    回覆刪除
  3. Private Sub Form_Load()
    Me.Hide
    Open App.Path & "\in.txt" For Input As #1
    Open App.Path & "\out.txt" For Output As #2
    Input #1, x
    Do
    a = x Mod 16
    x = x \ 16
    Select Case a
    Case 10
    a = "A"
    Case 11
    a = "B"
    Case 12
    a = "C"
    Case 13
    a = "D"
    Case 14
    a = "E"
    Case 15
    a = "F"
    End Select
    b = a & b
    Loop Until x = 0
    Print #2, b
    Close
    Close
    End
    End Sub

    回覆刪除
  4. Dim num As Integer
    Dim RP, okRP, 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
    Input #1, num
    RP = "0 1 2 3 4 5 6 7 8 9 A B C D E F"
    okRP = Split(RP, " ")
    Do
    x = num Mod 16
    num = num \ 16
    ans = okRP(x) & ans
    Loop Until num = 0
    Print #2, ans
    Close #2
    Close #1
    End
    End Sub

    回覆刪除
  5. Private Sub Form_Load()
    Me.Hide
    Open App.Path & "\in.txt" For Input As #1
    Open App.Path & "\out.txt" For Output As #2
    Input #1, n
    Do
    k = n Mod 16
    Select Case k
    Case Is = 10
    k = "A"
    Case 11
    k = "B"
    Case 12
    k = "C"
    Case 13
    k = "D"
    Case 14
    k = "E"
    Case 15
    k = "F"
    End Select
    a = k & a
    n = n \ 16
    Loop Until n = 0
    Print #2, a
    Close
    Close
    End
    End Sub

    回覆刪除