子題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
Dim s
回覆刪除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, t
For i = 1 To t
Line Input #1, n
s = Split(n)
Call A1
Next
Close
Close
End
End Sub
Sub A1()
For i = 0 To UBound(s)
If Left(s(i), 1) = "A" Or Left(s(i), 1) = "a" Then
If Right(s(i), 1) < "a" Or Right(s(i), 1) > "z" Then s(i) = Left(s(i), Len(s(i)) - 1)
Dim ch As Boolean
ch = False
For x = 0 To List1.ListCount - 1
If LCase(s(i)) = LCase(List1.List(x)) Then ch = True
Next
If ch = False Then Print #2, s(i): List1.AddItem s(i)
End If
Next
End Sub
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
For ii = 1 To N
Line Input #1, X
temp = ""
For I = 1 To Len(X)
A = Mid(X, I, 1)
If UCase(A) >= "A" And UCase(A) <= "Z" Then
temp = temp & A
Else
If UCase(Left(temp, 1)) = "A" Then If re(temp) = False Then List1.AddItem temp
temp = ""
End If
Next I
Next ii
For I = 0 To List1.ListCount - 1
Print #2, List1.List(I)
Next I
Close #2
Close #1
End
End Sub
Function re(A) As Boolean
P = False
For I = 0 To List1.ListCount - 1
If UCase(A) = UCase(List1.List(I)) Then P = True
Next I
re = P
End Function