1)将文本框Text1中的文章保存到字符串变量s中。
2)从左往右扫描字符串s,用字符串word存储文章中依次出现的单词,将新出现的单词自动加入单词队列dic(i)中,单词出现的频次记录到对应的num(i)中。
如下方法扫描:
①从左往右扫描,当扫描到第i个字符时,如果是字母,继续扫描,否则,跳转到②,直至扫描结束;
②将b到i-1组成一个单词word,查询单词字典dic判断是否存在,如果不存在,将新单词插入单词字字典尾部dic(k),并且记录单词频次num(k)为1,如果dic字典中存在单词word,那么该单词对应频次增加1,再跳转到①。
Private Sub Command1_Click()
Dim dic(1 To 10000) As String '存储自建单词字典
Dim num(1 To 10000) As Integer '存储单词出现的频率
Dim word As String, c As String, s As String, slen As Integer
Dim i As Integer, j As Integer, di As Integer, k As Integer
Dim nummax As Integer '记录最高的单词频率
Dim result As String '存储出现频率最高的单词串
s = Text1.Text: slen = Len(s)
j = 0 '存储每个单词的长度
k = 1 'k-1 为当前单词字典长度
nummax = 0
For i = 1 To 10000 '初始化单词字典频次
num(i) = 0
Next i
i = 1
Do While i <= slen
If c >= "a" And c <= "z" Or c >= "A" And c <= "Z" Then j = j + 1
Else
If j <> 0 Then
word = LCase(Mid(s, i - j, j)) 'LCase()函数功能:单词统一为小写
di = 1
Do While word <> dic(di) And di < k
di = di + 1
Loop
If di = k Then '单词字典插入新单词
dic(k) = word
num(k) = 1
Else
End If
'单词在单词字典中已存在,对应频次加1
If num(di) > nummax Then nummax = num(di)
j = 0
End If
End If
i = i + 1
Loop
result = ""
For j = 1 To k - 1
List1.AddItem dic(j) + "" + Str(num(j))
If Then
If result = "" Then
result = result + dic(j) Else
result = result + "," + dic(j) '如出现频率最高的单词有多个,则用逗号分隔
End If
End If
Next j
Label2.Caption = "出现频率最高的单词是:" + result
End Sub