2011年12月26日 星期一

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

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

輸入範例:168

輸出範例:A8

6 則留言:

  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
    Dim i, g, k As Integer
    Dim h(99)
    i = 0
    k = 0
    Input #1, g

    If g > 16 Then
    Do While g > 16
    k = k + 1
    i = i + 1
    h(i) = g Mod 16
    g = g \ 16
    Loop
    End If

    If g = 10 Then g = "A": If g = 11 Then g = "B"
    If g = 12 Then g = "C": If g = 13 Then g = "D"
    If g = 14 Then g = "E": If g = 15 Then g = "F"

    For x = 1 To k
    If h(x) = 10 Then h(x) = "A": If h(x) = 11 Then h(x) = "B"
    If h(x) = 12 Then h(x) = "C": If h(x) = 13 Then h(x) = "D"
    If h(x) = 14 Then h(x) = "E": If h(x) = 15 Then h(x) = "F"
    Next

    Print #2, g & "";

    For y = i To 1 Step -1
    Print #2, h(y) & "";
    Next

    Close
    Close
    End
    End Sub

    回覆刪除
  2. 小冰程式有錯噢
    你輸入15
    正確應為F 你答案是 15

    回覆刪除
  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
    Dim i, k As Integer
    Dim h(99), g
    i = 0
    k = 0
    Input #1, g

    If g > 16 Then
    Do While g > 16
    k = k + 1
    i = i + 1
    h(i) = g Mod 16
    g = g \ 16
    Loop
    End If

    If g = 10 Then g = "A"
    If g = 11 Then g = "B"
    If g = 12 Then g = "C"
    If g = 13 Then g = "D"
    If g = 14 Then g = "E"
    If g = 15 Then g = "F"

    For x = 1 To k
    If h(x) = 10 Then h(x) = "A"
    If h(x) = 11 Then h(x) = "B"
    If h(x) = 12 Then h(x) = "C"
    If h(x) = 13 Then h(x) = "D"
    If h(x) = 14 Then h(x) = "E"
    If h(x) = 15 Then h(x) = "F"
    Next

    Print #2, g & "";

    For y = i To 1 Step -1
    Print #2, h(y) & "";
    Next

    Close
    Close
    End
    End Sub

    回覆刪除
  4. 程式對了。
    下次嘗試 select case 吧

    回覆刪除
  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
    Dim z As String
    Input #1, x
    Do
    y = x Mod 16
    Select Case y
    Case 10
    a = "A"
    Case 11
    a = "B"
    Case 12
    a = "C"
    Case 13
    a = "D"
    Case 14
    a = "E"
    Case 15
    a = "F"
    Case Else
    a = y
    End Select
    x = x \ 16
    z = a & z
    Loop Until x = 0
    Print #2, z
    Close #2
    Close #1
    End
    End Sub

    回覆刪除
  6. 哲好,
    程式正確。
    還可以更進階哦。
    用字串"0123456789ABCDEF" 直接分成陣列,
    再將餘數直接指向陣列中的相應位置,
    可以試試哦。

    (其實我是在感冒中啦,所以在巴黎的公寓中沒出門)

    回覆刪除