VB程序设计:统计文本文件中各字母出现的次数(不区分大小写),显示出现过的字母和出现的次数。 要求:(1
2个回答
展开全部
Private Sub Form_Load()
Dim file_name As String, zs As Long, zd() As Long, zs1() As Long, strline As String, tp As String
CommonDialog1.Filter = "文本文件(.txt)|*.txt|*.*|*.*"
CommonDialog1.ShowOpen
file_name = CommonDialog1.FileName
lngHandle = FreeFile()
zs = 0 ''回车数
ReDim zd(0) As Long
ReDim zs1(0) As Long
zd(0) = "-1"
Open file_name For Input As lngHandle
Do While Not EOF(lngHandle)
zs = zs + 1
Line Input #lngHandle, strline
For i = 1 To Len(strline)
tp = Asc(LCase(Right(Left(strline, i), 1)))
gjz = -1
For k = 0 To UBound(zd) - 1
If zd(k) = tp Then gjz = k
Next
If gjz <> -1 Then
zs1(gjz) = zs1(gjz) + 1
Else
ReDim Preserve zd(UBound(zd) + 1) As Long
ReDim Preserve zs1(UBound(zs1) + 1) As Long
zd(UBound(zd) - 1) = tp
zs1(UBound(zs1) - 1) = 1
End If
Next
Loop
List1.Clear
List1.AddItem "回车 " & ": " & zs & "个"
For k = 0 To UBound(zd) - 1
List1.AddItem Chr(zd(k)) & ": " & zs1(k) & "个"
Next
Close lngHandle
End Sub
修改了一下,不然对中文支持不了!
Dim file_name As String, zs As Long, zd() As Long, zs1() As Long, strline As String, tp As String
CommonDialog1.Filter = "文本文件(.txt)|*.txt|*.*|*.*"
CommonDialog1.ShowOpen
file_name = CommonDialog1.FileName
lngHandle = FreeFile()
zs = 0 ''回车数
ReDim zd(0) As Long
ReDim zs1(0) As Long
zd(0) = "-1"
Open file_name For Input As lngHandle
Do While Not EOF(lngHandle)
zs = zs + 1
Line Input #lngHandle, strline
For i = 1 To Len(strline)
tp = Asc(LCase(Right(Left(strline, i), 1)))
gjz = -1
For k = 0 To UBound(zd) - 1
If zd(k) = tp Then gjz = k
Next
If gjz <> -1 Then
zs1(gjz) = zs1(gjz) + 1
Else
ReDim Preserve zd(UBound(zd) + 1) As Long
ReDim Preserve zs1(UBound(zs1) + 1) As Long
zd(UBound(zd) - 1) = tp
zs1(UBound(zs1) - 1) = 1
End If
Next
Loop
List1.Clear
List1.AddItem "回车 " & ": " & zs & "个"
For k = 0 To UBound(zd) - 1
List1.AddItem Chr(zd(k)) & ": " & zs1(k) & "个"
Next
Close lngHandle
End Sub
修改了一下,不然对中文支持不了!
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询