2011年8月12日 星期五

文章的字母統計及單字的分離(99模擬) -2

子題2(9%):請列出文章中使用那些「A」或「a」開頭的單字?

輸入說明:
第1 行表示文章的行數(行數至多10 行,每行最多120 個字),第2 行開始為文章的內容。

輸出說明:
依單字在文章中出現之順序,輸出開頭為「A」或「a」的單字。輸出的單字大小寫需保持與
原文章相同,而每行只輸出1 個單字。若同一單字多次出現在文章中,只有該單字在文章中
首次出現時才被輸出,第2 次或以後出現時均不再輸出。

輸入範例:【檔名:in-1-2.txt】
4
Just ask a Chinese fruit farmer who now has to pay people to pollinate apple trees because there are no
longer enough bees to do the job. And it's not just the number of bees that are rapidly dwindling. As a
direct result of human activity, species are becoming extinct at a rate 1,000 times greater than the natural
average.

輸出範例:【檔名:out-1-2.txt】
ask
a
apple
are
And
As
activity
at
average

8 則留言:

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

    A = ""
    Y = ""

    Do While Not EOF(1)
    Line Input #1, X
    Y = Y & X & " "
    Loop

    Combo = False

    For i = 1 To Len(Y)
    B = Mid(Y, i, 1)

    If UCase(B) < "A" Or UCase(B) > "Z" Then
    Combo = False
    If Re(A) = False And A <> "" Then List1.AddItem A
    A = ""
    End If

    If B = "a" Or B = "A" Or Combo = True Then
    A = A & B
    Combo = True
    End If

    Next i


    For i = 0 To List1.ListCount - 1
    Print #2, List1.List(i)
    Next i

    Close #2
    Close #1
    End
    End Sub

    Function Re(A)
    P = False
    For i = 0 To List1.ListCount - 1
    If List1.List(i) = A Then P = True: Exit For
    Next i
    Re = P
    End Function

    回覆刪除
  2. 佑好,
    1.程式不正確。
    重複的檢查中,沒注意到大小寫的問題。as 和 As 只能放進去1個,但是,保持在第1次出現的那一個。
    2.程式中的那兩個if,
    If UCase(B) < "A" Or UCase(B) > "Z" Then
    Combo = False
    If Re(A) = False And A <> "" Then List1.AddItem A
    A = ""
    End If

    If B = "a" Or B = "A" Or Combo = True Then
    A = A & B
    Combo = True
    End If

    試著寫成1組if,程式才會快。
    If UCase(B) < "A" Or UCase(B) > "Z" Then
    ...(這邊combo會設成false)
    else
    ...(這邊你寫了3個條件,但是,用or連接,也就是說不執行上邊那個的,一定會執行這邊,因為combo)
    end if
    這樣子的話,兩個if只會執行1次,不是上面,就是下面的,不浪費時間。

    回覆刪除
  3. 佑好,
    還有,
    Do While Not EOF(1)
    Line Input #1, X
    Y = Y & X & " "
    Loop
    這邊有些危險,y會不會太長呢?(溢位問題)
    你沒有必要將它接在一起才處理的啊,分行處理就行了,
    以這題來說,y=y & x & " " 改成 y=x
    然後,將loop放到寫入檔案之前,就行了。

    回覆刪除
  4. 熊掌好,

    在修改過程中還發現到
    假如Change單字還會取出ange

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

    A = ""
    Y = ""

    Do While Not EOF(1)
    Line Input #1, X
    X = " " & X
    Combo = False

    For i = 2 To Len(X)
    B = Mid(X, i, 1)

    If (UCase(B) < "A" Or UCase(B) > "Z") Then
    Combo = False
    If Re(A) = False And A <> "" Then List1.AddItem A
    A = ""

    Else
    If (UCase(B) = "A" And Mid(X, i - 1, 1) = " ") Or Combo = True Then
    A = A & B
    Combo = True
    End If

    End If



    Next i
    Loop

    For i = 0 To List1.ListCount - 1
    Print #2, List1.List(i)
    Next i

    Close #2
    Close #1
    End
    End Sub

    Function Re(A)
    P = False
    For i = 0 To List1.ListCount - 1
    If UCase(List1.List(i)) = UCase(A) Then P = True: Exit For
    Next i
    Re = P
    End Function

    回覆刪除
  5. 佑好,
    1.
    UCase(B) 你用了那麼多次,也是浪費時間的。
    用個新的變數去記它,只要做一次。
    UB=UCase(B)
    2.
    將下列的部分,改成用select case
    B = Mid(X, i, 1)

    If (UCase(B) < "A" Or UCase(B) > "Z") Then
    Combo = False
    If Re(A) = False And A <> "" Then List1.AddItem A
    A = ""

    Else
    If (UCase(B) = "A" And Mid(X, i - 1, 1) = " ") Or Combo = True Then
    A = A & B
    Combo = True
    End If

    End If

    回覆刪除
  6. 熊掌好,

    感覺換湯不換藥。


    Select Case UB

    Case Is < "A", Is > "Z"

    If Len(A) <> 0 Then
    Combo = False
    If Re(A) = False Then List1.AddItem A
    A = ""
    End If

    Case Else

    If UB = "A" And Mid(X, i - 1, 1) = " " Or Combo = True Then
    A = A & B
    Combo = True
    End If

    End Select

    回覆刪除
  7. 佑好,
    你說的Chang會取出 ang,於是你用了Mid(X, i - 1, 1) = " " 去檢查前面是不是空白,那麼如果 I like banana,apple and orange.中的,apple是不是就不會被取到了呢?
    之前不是有個題目,是將字分別給取出來的嗎? 用那樣的方式,先將整篇文章全部分成字。再去看字的開頭是不是a/A,再去看有沒有重複...

    回覆刪除
  8. 熊掌好,

    對吼,
    我加上一點判斷,
    寫了一下大概是這樣。

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

    Do While Not EOF(1)
    Line Input #1, X
    A = ""

    For i = 1 To Len(X)
    B = Mid(X, i, 1)
    BU = UCase(B)

    Select Case BU
    Case "A" To "Z"
    A = A & B
    Case Else
    If UCase(Left(A, 1)) = "A" And Re(A) = False Then List1.AddItem A
    A = ""
    End Select

    Next i
    Loop

    For i = 0 To List1.ListCount - 1
    Print #2, List1.List(i)
    Next i

    Close #2
    Close #1
    End
    End Sub

    Function Re(A)
    P = False
    For i = 0 To List1.ListCount - 1
    If UCase(List1.List(i)) = UCase(A) Then P = True: Exit For
    Next i
    Re = P
    End Function

    回覆刪除