2010年3月23日 星期二

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

現在網際網路盛行,網路使用者可以利用搜尋引擎找出特定的網路資訊。在搜尋技術中,

關鍵字搜尋是最常見的方法。建立關鍵字有很多種不同的方法,其中一種方法是找出使用的

單字來當作關鍵字。本題就是要請你寫一個程式,可以在一段英文文章中,找出使用的英文

單字字數。

輸入說明:

第一行是要建立關鍵字的英文文章篇數,第二行開始為英文文章的內容。每篇文章之間,

以一行空白作為區隔。在建立關鍵字時,我們簡化一些文法上的規則,每個英文單字與英文

單字之間,扣除標點符號之後,以空白作為區別,稱之為一個單字,大小寫視為相同。使用

到的標點符號則包括下列三個:『,』,『.』,以及『:』。

輸出說明:

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

輸入範例:

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

7 則留言:

  1. Dim N, X, A(100), B(100) As String
    Private Sub Form_Load()
    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
    X = Replace(X, ".", " ")
    X = Split(X)
    A(0) = X(0)
    Ai = 0
    For i = 1 To UBound(X)
    Ap = True
    For j = 0 To Ai
    If X(i) = "" Or X(i) = A(j) Then Ap = False
    Next j
    If Ap = True Then Ai = Ai + 1: A(Ai) = X(i)
    Next i
    Print #2, Ai + 1
    For i = 1 To UBound(A)
    A(i) = ""
    Next i
    Loop
    Close #2
    Close #1
    End Sub

    回覆刪除
  2. 高仔好,
    1.題目中,說的標點符號,不是只有一種哦。
    2.你的B陣列要做啥?
    3.如果你的A,B陣列都是要宣告成string的話,下面的寫法比較好。
    dim a(100) as string, b(100) as string
    不然,你的a(100)可能是個不定變數的陣列。(variant)

    回覆刪除
  3. Private Sub Form_Load()
    Open App.Path & "\in.txt" For Input As #1
    Open App.Path & "\out.txt" For Output As #2
    Line Input #1, x
    x = Replace(x, ",", "")
    x = Replace(x, ".", "")
    x = Replace(x, ":", "")
    en = Split(UCase(x))
    num = UBound(en) + 1
    For i = 0 To (UBound(en) - 1)
    ans = True
    For j = i + 1 To UBound(en)
    If en(i) = en(j) Then
    ans = True
    num = num - 1
    End If
    Next j
    Next i
    Print #2, num
    Close #1
    Close #2
    End Sub

    回覆刪除
  4. Y揚好,
    你的程式原則ok,除了一個小錯誤,也許是po上網頁上的小問題而已。
    x = Replace(x, ",", "")
    那個後面的雙引號中間,是要有個空格的。這樣split才會分得對吧?

    回覆刪除
  5. Private Sub Form_Load()
    Open App.Path & "\in.txt" For Input As #1
    Open App.Path & "\out.txt" For Output As #2
    Line Input #1, x
    x = Replace(x, ",", "")
    x = Replace(x, ".", "")
    x = Replace(x, ":", "")
    a = Split(x, " ")
    For i = 0 To UBound(a)
    For j = 0 To UBound(a)
    If a(i) = a(j) Then
    ans = ans - 1
    End If
    Next j
    ans = ans + 1
    If ans = -2 Then
    ans = ans + 1
    End If
    Next i
    Print #2, UBound(a) + ans
    Close #2
    Close #1
    End Sub

    回覆刪除
  6. 阿瑋好,
    1.你這題沒考慮到大小寫,所以程式會錯。
    2.你這題的算ans,的算法是錯的吧。例如,字串中有3個he,有4個is的話,你的計數如何呢?

    回覆刪除
  7. Dim X As String
    Dim XX(100) As String
    Private Sub Form_Load()
    Open App.Path & "\in.txt" For Input As #1
    Open App.Path & "\out.txt" For Output As #2
    Input #1, qq
    For i = 1 To qq
    Line Input #1, X
    X = Replace(X, ",", "")
    X = Replace(X, ":", "")
    X = Replace(X, ".", "")
    A = Split(X, " ")
    For j = 0 To UBound(A)
    XX(j + 1) = LCase(A(j))
    ans = j + 1
    Print XX(j + 1), ans
    Next j
    q = ans
    For j = 1 To q
    For k = j To q
    For s = 1 To Len(XX(j))
    A1 = Mid(XX(j), 1, Len(XX(j)))
    Next s
    For s = 1 To Len(XX(k))
    A2 = Mid(XX(k), 1, Len(XX(k)))
    Next s
    If A1 = A2 And k <> j Then
    ans = ans - 1
    End If
    Next k
    Next j
    Print ans
    Next i
    Close #2
    Close #1
    End Sub

    回覆刪除