以前写的一道二级题..
二级VB题,测试文件中出现最多的字母及次数
Option Explicit
Option Base 1
Private s As String
Private Sub Command1_Click()
Open App.Path & "\dj5.txt" For Input As #1
s = Input$(LOF(1), #1)
'读入数据到s变量里
Close
Command1.Enabled = False
Command2.Enabled = True
End Sub
Private Sub Command2_Click()
Dim i As Long
Dim ArrayN(26) As Integer
Dim ArrayIndex As Integer
Dim MaxColumn As Integer
Dim Max As Integer
Dim MidStringAscii As Integer
Dim MidString As String
Dim UcaseString As String
UcaseString = UCase(s)
'转换成大写,方便判断它的Ascii码
For i = 1 To Len(UcaseString)
MidString = Mid(UcaseString, i, 1)
MidStringAscii = Asc(MidString)
'得到这个字符的Ascii码
If (MidStringAscii >= Asc("A") And MidStringAscii <= Asc("Z")) Then
'保证这个字符是26个字母,因为不用判断逗号,感叹号!这些
ArrayIndex = MidStringAscii - Asc("A") + 1
'得到下标
ArrayN(ArrayIndex) = ArrayN(ArrayIndex) + 1
End If
Next
Max = ArrayN(1)
MaxColumn = 1
For i = 2 To 26
If (ArrayN(i) > Max) Then
Max = ArrayN(i)
MaxColumn = i
End If
Next
Text1.Text = CStr(Chr(MaxColumn + Asc("A") - 1))
Text2.Text = CStr(ArrayN(MaxColumn))
End Sub