编写VB程序,实现上述功能。运行程序,在列表框List1中显示n个英文单词(单词仅由小写字母组成且字母个数至少为1),单击“统计”按钮Command1,在列表框List2升序显示全部单词的按键组合,在文本框Text1中显示最频繁使用的按键组合,若有多个,则全部输出。程序运行界面如图b所示。
'n为常量,代码略
Dim a(1 To n) As String '数组a存储所有单词
Dim b(1To2*n) As String '数组b存储转换后的按键组合
Private Sub Form_ Load()
'读取所有单词存入数组a,并显示在列表框List1中
'初始化数组b各元素为空字符串,代码略
End Sub
Private Sub Command1_Click()
Dim i As Integer, j As Integer, k As Integer, p As Integer, max As Integer
Dim m As Integer, key As String, t As String, s1 As String, s2 As String
key= 222334455566677889999 'key 表示a-z各个字母所对应的键盘数字
max=0
'以下代码实现将数组a中的每个单词转化成按键组合,并依次存入数组b中
For i= 1 To n
t= ""
For j= 1 To Len(a(i))
m = Asc(Mid(a(i), j, 1))- Asc("a")
t=
Next j
b(i)= t
Next i
'以下代码实现对数组元素b( 1)~b(n)升序排序
Do While i>=1
For j= 1 To i
If b(j+ 1)< b(j) Then
t=b(j+ 1): b(j+ 1)= b(j): b(j)=t
End If
Next j
i=i- 1
Loop
'将排序后数组元素b(1)~b(n)显示在列表框List2中,代码略
s1= b(1)
p=n+ 1
i= 1:j= 2
Do While j<= n '从数组元素b(n+1)开始存储最高频次的按键组合
s2 = b(j)
If s1 < s2 Then
If Then
max=j-i
p=n+ 1
b(p)=s1
Else If max=j- i Then
p=p+ 1
b(p)= sl
End If
s1= s2
End If
j=j+ 1
Loop
t=""
If max<j- i Then
t= s1
Else
For k=n+ 1 To p
t=t+ b(k)+" "
Next k
If max=j- I Then t=t+" "+s1
End If
Text1.Text= "最频繁按键组合为: " &t
End Sub