2012年7月30日 星期一

文數字排序

由"in.txt"讀取一串的文字或數字,將其按數字、大寫英文字母、小寫英文字母排序。

出自  程式設計隊訓練教材  NO.9 文數字排序

輸入範例:
DoYouHave1456879DollarsToBorrow
輸出範例:
1456789BDDHTYaaelloooooorrrsuvw

3 則留言:

  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
    Line Input #1, x

    For i = 1 To Len(x)
    If Mid(x, i, 1) >= 0 And Mid(x, i, 1) <= 9 Then List1.AddItem Mid(x, i, 1)
    If Mid(x, i, 1) >= "a" And Mid(x, i, 1) <= "z" Then List2.AddItem Mid(x, i, 1)
    If Mid(x, i, 1) >= "A" And Mid(x, i, 1) <= "Z" Then List3.AddItem Mid(x, i, 1)
    Next
    For i = 0 To List1.ListCount
    ans = ans & List1.List(i)
    Next
    For i = 0 To List3.ListCount
    ans = ans & List3.List(i)
    Next
    For i = 0 To List2.ListCount
    ans = ans & List2.List(i)
    Next
    Print #2, ans
    Close
    Close
    End
    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
    Line Input #1, n
    For i = 1 To Len(n)
    If Mid(n, i, 1) >= 0 And Mid(n, i, 1) <= 9 Then List1(0).AddItem Mid(n, i, 1)
    If Mid(n, i, 1) >= "A" And Mid(n, i, 1) <= "Z" Then List1(1).AddItem Mid(n, i, 1)
    If Mid(n, i, 1) >= "a" And Mid(n, i, 1) <= "z" Then List1(2).AddItem Mid(n, i, 1)
    Next
    ans = ""
    For i = 0 To List1(0).ListCount
    ans = ans & List1(0).List(i)
    Next
    For i = 0 To List1(1).ListCount
    ans = ans & List1(1).List(i)
    Next
    For i = 0 To List1(2).ListCount
    ans = ans & List1(2).List(i)
    Next
    Print #2, ans
    Close #2
    Close #1
    End
    End Sub

    回覆刪除
  3. Dim a() 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
    Line Input #1, x
    ReDim a(Len(x))
    For i = 1 To Len(x)
    a(i) = Mid(x, i, 1)
    Next i
    For i = 1 To Len(x)
    For j = i To Len(x)
    If a(i) > a(j) Then
    z = a(j)
    a(j) = a(i)
    a(i) = z
    End If
    Next j
    Next i
    For i = 1 To Len(x)
    stra = stra & a(i)
    Next i
    Print #2, stra
    Close #2
    Close #1
    End
    End Sub

    回覆刪除