求VB统计数字出现频率的代码 20
我所说的意思就是在一个TEXT中,我已经计算出很多数,比如这样的12305078910345008912300608901034500891230507891034500...
我所说的意思就是在一个TEXT中,我已经计算出很多数,比如这样的
123050789 103450089 1230060890 103450089 123050789 103450089 1230060890 103450089 123050789
我的目的是想统计里面各个数字出现的字数。比如上边的结果是
1,3,8,9出现 9次
5出现 7次
2出现 5次
4出现 4次
7出现 3次
6出现 2次
不算里面的0 然后把数字出现的次数按从多到少的顺序排下来。 结果通过COMMAND1 显示在 TEXT2 中
希望高手朋友不会因为分少不理我 在线等答 谢谢啦 展开
123050789 103450089 1230060890 103450089 123050789 103450089 1230060890 103450089 123050789
我的目的是想统计里面各个数字出现的字数。比如上边的结果是
1,3,8,9出现 9次
5出现 7次
2出现 5次
4出现 4次
7出现 3次
6出现 2次
不算里面的0 然后把数字出现的次数按从多到少的顺序排下来。 结果通过COMMAND1 显示在 TEXT2 中
希望高手朋友不会因为分少不理我 在线等答 谢谢啦 展开
2个回答
展开全部
加点分吧!
这个写得不易啊!
Private Sub Command1_Click()
Dim FindStr(1 To 9) As Integer '存放各个数出现次数
Dim LenStr As Integer 'text1中文字的个数
Dim TStr As String '存放text1中的文字
Dim i, j As Integer '循环时用
Dim StrOut() As String '存放各项结果
Dim SltIn As String '各个数,如果出现次数相同就用“,”隔开
Dim IsAga As String '存放已进行出现次数对比的各个数
Dim TmpL As String '排序时存放当前个数最多的结果
Text2 = vbNullString '清空
TStr = Text1
IsAga = "0"
For i = 1 To 9 '循环查找各个数的出现次数
LenStr = Len(TStr)
TStr = Replace(TStr, CStr(i), vbNullString)
FindStr(i) = LenStr - Len(TStr)
Next
For i = 1 To 9 '循环处理出现次数相同的数
If InStr(1, IsAga, CStr(i), vbTextCompare) = 0 Then '如果该数没有进行出现次数对比
For j = 2 To 9 '循环找出出现次数相同的数
If FindStr(i) = FindStr(j) And i <> j Then
IsAga = IsAga & j '已进行出现次数对比就跳过
If SltIn = vbNullString Then
SltIn = i & "," & SltIn & j
Else
SltIn = SltIn & "," & j
End If
End If
Next
ReDim Preserve StrOut(k)
If SltIn = vbNullString Then SltIn = CStr(i)
StrOut(k) = SltIn & "出现 " & FindStr(i) & "次" & vbNewLine
SltIn = vbNullString '因为用到累加,所以不用时记得清空
k = k + 1
End If
Next
For i = UBound(StrOut) To 1 Step -1 '结果排序
For j = 0 To i - 1
If Val(Right(StrOut(j), Len(StrOut(j)) - InStr(1, StrOut(j), " ", vbTextCompare))) < Val(Right(StrOut(j + 1), Len(StrOut(j + 1)) - InStr(1, StrOut(j + 1), " ", vbTextCompare))) Then
TmpL = StrOut(j)
StrOut(j) = StrOut(j + 1)
StrOut(j + 1) = TmpL
End If
Next
Next
For i = 0 To UBound(StrOut) '循环输出结果
Text2 = Text2 & StrOut(i)
Next
End Sub
这个写得不易啊!
Private Sub Command1_Click()
Dim FindStr(1 To 9) As Integer '存放各个数出现次数
Dim LenStr As Integer 'text1中文字的个数
Dim TStr As String '存放text1中的文字
Dim i, j As Integer '循环时用
Dim StrOut() As String '存放各项结果
Dim SltIn As String '各个数,如果出现次数相同就用“,”隔开
Dim IsAga As String '存放已进行出现次数对比的各个数
Dim TmpL As String '排序时存放当前个数最多的结果
Text2 = vbNullString '清空
TStr = Text1
IsAga = "0"
For i = 1 To 9 '循环查找各个数的出现次数
LenStr = Len(TStr)
TStr = Replace(TStr, CStr(i), vbNullString)
FindStr(i) = LenStr - Len(TStr)
Next
For i = 1 To 9 '循环处理出现次数相同的数
If InStr(1, IsAga, CStr(i), vbTextCompare) = 0 Then '如果该数没有进行出现次数对比
For j = 2 To 9 '循环找出出现次数相同的数
If FindStr(i) = FindStr(j) And i <> j Then
IsAga = IsAga & j '已进行出现次数对比就跳过
If SltIn = vbNullString Then
SltIn = i & "," & SltIn & j
Else
SltIn = SltIn & "," & j
End If
End If
Next
ReDim Preserve StrOut(k)
If SltIn = vbNullString Then SltIn = CStr(i)
StrOut(k) = SltIn & "出现 " & FindStr(i) & "次" & vbNewLine
SltIn = vbNullString '因为用到累加,所以不用时记得清空
k = k + 1
End If
Next
For i = UBound(StrOut) To 1 Step -1 '结果排序
For j = 0 To i - 1
If Val(Right(StrOut(j), Len(StrOut(j)) - InStr(1, StrOut(j), " ", vbTextCompare))) < Val(Right(StrOut(j + 1), Len(StrOut(j + 1)) - InStr(1, StrOut(j + 1), " ", vbTextCompare))) Then
TmpL = StrOut(j)
StrOut(j) = StrOut(j + 1)
StrOut(j + 1) = TmpL
End If
Next
Next
For i = 0 To UBound(StrOut) '循环输出结果
Text2 = Text2 & StrOut(i)
Next
End Sub
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
Sub command1_click()
Dim cnt(0 To 9) As Integer,l as integer
Text2 = ""
tmp = Replace(Text1, " ", "")
For i = 0 To 9
l=len(tmp)
tmp = Replace(tmp, CStr(i), "")
cnt(i) = l - Len(tmp)
Text2 = Text2 + "数字" + CStr(i) + "出现次数为:" + CStr(cnt(i)) + vbCrLf
Next i
End Sub
Dim cnt(0 To 9) As Integer,l as integer
Text2 = ""
tmp = Replace(Text1, " ", "")
For i = 0 To 9
l=len(tmp)
tmp = Replace(tmp, CStr(i), "")
cnt(i) = l - Len(tmp)
Text2 = Text2 + "数字" + CStr(i) + "出现次数为:" + CStr(cnt(i)) + vbCrLf
Next i
End Sub
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询