急需一個VB小程序,計算0-9數字總次數
題目:
急需一個VB小程序,計算0-9數字總次數
請不要嫌我說得囉嗦,這也是爲了節約您的時間:
設計一個VB6小程序,在文本框輸入或粘貼一段數字,有的數字後面帶括號,且無論該小括號與前面的數字有多遠,即使有換行,小括號內的數字都屬於左邊離它最近的那串數字的次數,而數字後面沒有小括號的要被視作有小括號,只不過裡面的數字是1而已.注意小括號爲英文輸入法的小括號(),不是中文輸入法的(),同時分開數字的逗號也是英文輸入法下的.點擊按鈕後將所有數字的次數計算出來,生成.txt格式的文本彈出(即使用Shell "C:\WINDOWS\NOTEPAD.EXE " & App.Path & "\數字總次數.txt",1),在文本內,數字的次數按從高到低順序排列,次數一樣的列在一起.下面的舉例包含上面所說的所有情況:
2(6),08(4),15(3),9(2),36(說明:本列數字小括號內的數字全部緊跟左側數字,2有6次,0和8 有4次,1和5都是3次,9是2次,3與6都是1次,7沒出現)
9 (8),1 (7),23458 (5),0(4),6(說明:本列數字與次數有空格)
3(9),48(8),6
(7),5
(6),09(5),1(4),2(2)(說明:本列有換行,如6有7次,5有6次)
169(6),045(5),2(3),38(2)
則最終結果,在彈出的.txt文件中顯示:
9(21),1(20),58(19),04(18),3(17),2(16),6(15),7(0),
注意上面的7沒有出現,所以次數是0次,要求自己驗證通過!不要錯誤的還發!如果難以實現,我可以輸入得規範些,如將1次的也帶括號,前面的數字與後面的括號可以沒有空格,但是換行是必要的.
怎麼還沒人回答啊,5555555555555555
解答:
這回完全符合你的題意了.
結果確實如你所給
新建一個exe工程,畫一個text文本框,一個按鈕.
text文本框的multiline=true
代碼如下
'
Option Explicit
Private Sub Command1_Click()
On Error GoTo writeFile:
Dim CacheA As String
Dim CacheB() As String
Dim CacheC() As String
Dim CounterA(0 To 9) As Long
Dim CounterCache(0 To 9) As Long
Dim ThisLenC As Long,LastLenC As Long
Dim i As Integer,j As Integer,b As Long,LastValue As Long,VBOut As String
CacheA = Text1.Text
CacheA = Replace(CacheA,Chr(32),"")
CacheA = Replace(CacheA,Chr(13),",")
CacheA = Replace(CacheA,Chr(10),",")
CacheA = Replace(CacheA," ","")
CacheA = Replace(CacheA,")",Chr(35))
CacheA = Replace(CacheA,",",Chr(35))
Do
CacheA = Replace(CacheA,Chr(35) & Chr(35),Chr(35))
Loop Until InStr(1,CacheA,Chr(35) & Chr(35),vbTextCompare) = 0
CacheA = Replace(CacheA,Chr(35) & "(","(")
CacheB = Split(CacheA,Chr(35),,vbTextCompare)
For i = LBound(CacheB) To UBound(CacheB)
If InStr(1,CacheB(i),"(",vbTextCompare) = 0 Then
CacheB(i) = CacheB(i) & "(1"
End If
CacheC = Split(CacheB(i),"(",,vbTextCompare)
j = 0
Do While Len(CacheC(0)) > 0
LastLenC = Len(CacheC(0))
CacheC(0) = Replace(CacheC(0),j,"",,,vbTextCompare)
ThisLenC = Len(CacheC(0))
CounterA(j) = (LastLenC - ThisLenC) * CLng(CacheC(1)) + CounterA(j)
LastLenC = ThisLenC
j = j + 1
Loop
Next
For i = 0 To 9
CounterCache(i) = CounterA(i)
Next
For i = 0 To 9
For j = i + 1 To 9
If CounterCache(i) > CounterCache(j) Then
b = CounterCache(i)
CounterCache(i) = CounterCache(j)
CounterCache(j) = b
End If
Next
Next
LastValue = 0
For i = 9 To 0 Step -1
If Not LastValue = CounterCache(i) Then
For j = 0 To 9
If CounterA(j) = CounterCache(i) Then
VBOut = VBOut & j
End If
Next
VBOut = VBOut & "(" & CounterCache(i) & "),"
LastValue = CounterCache(i)
End If
Next
If FileLen(App.Path & "\數字總次數.txt") > 0 Then
Kill App.Path & "\數字總次數.txt"
End If
writeFile:
Open App.Path & "\數字總次數.txt" For Output As #1
Print #1,Left(VBOut,Len(VBOut))
Close #1
Shell "C:\WINDOWS\NOTEPAD.EXE " & App.Path & "\數字總次數.txt",1
End Sub
Private Sub Form_Load()
Text1.Text = ""
End Su
添加新評論