2008年11月28日 星期五

技藝競竇97年模擬試題參考答案 Q1


' Problem1:字數統計(14%)
' 雷迪雅被老師要求寫一份為數2000 字的報告,可是雷迪雅胸無點墨、才疏學淺,常常寫了一整個下午還不到一頁,
' 因此他每寫一段時間就開始數自己已經寫了多少個字,可是常常這樣數也是會累的,於是他希望能有一個自動字數統計
' 程式。
' 有了字數統計程式,就可以自動統計好一篇文章總共有幾行、有幾個字、有幾個字元。行被定義為用換行字元隔開
' 的連續字元,字被定義為用空白、TAB 或換行字元所隔開的連續字元,而字元除了一般可見的字元還包括TAB 字元和
' 空白字元(注意不包含換行字元)。
' 身為雷迪雅好朋友的你,常常受到他的照顧,正所謂吃人嘴軟,拿人手短,如今雷迪雅遇到了這個難題,請你義不
' 容辭地寫一個程式幫他吧!
' 輸入說明:
' 輸入檔第一行說明有幾組測試資料,第二行開始即為第一筆測試資料,每行不會超過1024 個字元,每組測試資料
' 中間用五個連續等號' =' 的一行來作分隔。每組測試資料之中絕不會有五個連續等號' =' 出現。
' 輸出說明:
' 每組測試資料輸出一行,每行有三個數字,分別代表一組測試資料中有幾行,幾個字和幾個字元,每個數字之間請
' 用一個空白隔開。
' 輸入範例:
' 2
' This is a sample input.
' HelloWorld!!
' =====
' The speech by Hunyak, translated, is:
' "What am I doing here?
' They say, the famous Hungarian police,
' that I held down my husband and chopped off his head.
' But I didn' t do it, I am not guilty.
' I can' t believe that Uncle Sam says I did it.
' They say I didit, but really I didn' t."
' 輸出範例:
' 2 7 36
' 8 55 270


Public Class Form1
Dim resultStr = ""
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Me.Hide()
Dim fileContents As String
fileContents = My.Computer.FileSystem.ReadAllText(Application.StartupPath & "\Test1.txt")
Dim recArray() = Split(fileContents, vbNewLine)
Dim articleN = Val(recArray(0))
Dim recIndex = 1
Dim lineCtr = 0
Dim wordCtr = 0
Dim charCtr = 0
While recIndex <= UBound(recArray)
If recArray(recIndex) <> "=====" And recIndex <> UBound(recArray) Then
lineCtr = lineCtr + 1
Dim wordArray() = Split(recArray(recIndex))
If recArray(recIndex) <> "" Then
wordCtr = wordCtr + UBound(wordArray) + 1
End If
charCtr = charCtr + Len(recArray(recIndex))
Else
resultStr = resultStr & lineCtr & " " & wordCtr & " " & charCtr & vbNewLine
lineCtr = 0
wordCtr = 0
charCtr = 0
End If
recIndex = recIndex + 1
End While
MsgBox(resultStr)
'My.Computer.FileSystem.WriteAllText(Application.StartupPath & "\result1.txt", resultStr, False)
End
End Sub
End Class

沒有留言: