2011年7月1日 星期五

找出文章中使用的英文單字字數

  現在網際網路盛行,網路使用者可以利用搜尋引擎找出特定的網路資訊。在搜尋技術中,關鍵字搜尋是最常見的方法。建立關鍵字有很多種不同的方法,其中一種方法是找出使用的單字來當作關鍵字。本題就是要請你寫一個程式,可以在一段英文文章中,找出使用的英文單字字數。

輸入說明:
第一行是要建立關鍵字的英文文章篇數,第二行開始為英文文章的內容。每篇文章之間,
以一行空白作為區隔。在建立關鍵字時,我們簡化一些文法上的規則,每個英文單字與英文
單字之間,扣除標點符號之後,以空白作為區別,稱之為一個單字,大小寫視為相同。使用
到的標點符號則包括下列三個:『,』,『.』,以及『:』。

輸出說明:
對輸入的每篇文章,分別以一行輸出使用的英文字字數。

輸入範例:
2
He works hard from morning till night. He is a hard worker.

I once heard him speaking in English. He is a very good speaker of English.

輸出範例:
10
14

來自 全國高級中等學校98 學年度商業類科學生技藝競賽 
【程式設計】職種術科試卷 第一題

7 則留言:

  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
    Input #1, N
    Do
    List1.Clear
    Input #1, X
    X = LCase(X)
    If ABC(X) <> 0 Then Print #2, ABC(X): N = N - 1
    Loop Until N = 0
    End
    End Sub

    Function ABC(A)
    ans = ""
    For i = 1 To Len(A)
    test = Mid(A, i, 1)
    If Mid(A, i, 1) <= "z" And Mid(A, i, 1) >= "a" Then
    ans = ans & Mid(A, i, 1)
    Else
    If re(ans) = False And ans <> "" Then List1.AddItem ans
    ans = ""
    End If
    Next i
    ABC = List1.ListCount
    End Function

    Function re(A) As Boolean
    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. 佑好,
    這一題的題目又是有點兒長,你看到了他的輸入了嗎?
    一篇文章是不是可以有好多行的呢? 還是只能一行?

    If ABC(X) <> 0 Then Print #2, ABC(X)

    這一行指令中,會不會太奢侈了啊,前面反正已經做了函數abc了,後面為什麼要重新再做一次呢? 用個變數記住,就只要做一次函數就行了。
    y=abc(x)
    if y <> 0 then print #2,y

    回覆刪除
  3. 熊掌好,
    我懂您的意思了,

    題目說:(每篇文章之間,以一行空白作為區隔。)

    所以有可能輸入為
    He works hard from morning till night.
    He is a hard worker.
    His name is Peter.

    I once heard him speaking in English.He is a very good speaker of English.

    回覆刪除
  4. 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
    If Not EOF(1) Then Input #1, X Else X = ""
    X = LCase(X)
    Call ABC(X)
    If Len(X) = 0 Then
    Print #2, List1.ListCount
    Total = 0
    N = N - 1
    List1.Clear
    Else
    End If
    Loop Until N = 0
    End
    End Sub

    Sub ABC(A)
    ans = ""
    For i = 1 To Len(A)
    test = Mid(A, i, 1)
    If Mid(A, i, 1) <= "z" And Mid(A, i, 1) >= "a" Then
    ans = ans & Mid(A, i, 1)
    Else
    If re(ans) = False And ans <> "" Then List1.AddItem ans
    ans = ""
    End If
    Next i

    End Sub

    Function re(A) As Boolean
    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



    ----------------------
    in.txt
    2
    He works hard from morning till night.
    He is a hard worker.
    His name is Peter.

    I once heard him speaking in English.He is a very good speaker of English.
    ------------------------------
    out.txt
    13
    14

    回覆刪除
  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 s, a As String, m As String, ans As Integer, ch



    Input #1, Ti

    For times = 1 To (Ti * 2) - 1

    List1.Clear
    ans = 0
    Input #1, a

    s = Split(a)

    For i = 0 To UBound(s)
    m = s(i)

    If Right(m, 1) = "." Or Right(m, 1) = ":" Or Right(m, 1) = "," Then m = Left(m, Len(m) - 1)


    ch = 0
    For k = 0 To List1.ListCount - 1
    If m = List1.List(k) Then ch = 1
    Next
    If ch = 0 Then List1.AddItem m: ans = ans + 1

    Next



    If times Mod 2 <> 0 Then Print #2, ans



    Next






    Close
    Close

    End

    End Sub

    回覆刪除
  6. 佑好,
    你的程式應該正確了,很好。
    arro好,
    你的程式有兩個地方要注意,1是前面講的,這段文字可能是多行的。2是你的文字的分法是不完全的,有些字之間的空白多了些,有些用,逗點分開的,可能不會有空白,這些你的程式中都分不到。

    回覆刪除
  7. 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 s, a As String, m As String, ans As Integer, ch



    Input #1, ti

    For times = 1 To ti

    Do

    List1.Clear
    List1.AddItem ""
    ans = 0
    Input #1, a
    a = LCase(a)
    For i = 1 To Len(a)

    If Mid(a, i, 1) <= "z" And Mid(a, i, 1) >= "a" Then
    m = m & Mid(a, i, 1)
    Else
    ch = 0
    For k = 0 To List1.ListCount - 1
    If m = List1.List(k) Then ch = 1
    Next
    If ch = 0 Then List1.AddItem m: ans = ans + 1
    m = ""
    End If
    Next




    If Len(a) <> 0 Then Print #2, ans

    Loop Until Len(a) = 0 Or EOF(1)

    Next






    Close
    Close

    End

    End Sub

    回覆刪除