LoryOne
06-11-2003, 16.59.57
Ecco il codice:
Option Explicit
Dim Caratteri(32 To 255) As Long
Private Function Func_ContaStringa(ByVal S As String, Optional ByVal Delimiter As String = ",") As Long
'Conta quante volte una sottostringa è contenuta all'interno di una stringa
'Ogni elemento è separato da un delimitatore
Dim Ty As Long, Yt As Long, Yy As Long, Conta As Long
Ty = InStr(S, Delimiter)
Yt = Len(Delimiter)
While Ty
Conta = Conta + 1
Yy = Ty + Yt
Ty = InStr(Yy, S, Delimiter)
Wend
Func_ContaStringa = Conta
End Function
Private Function Func_Contaparole(ByVal S As String, Optional ByVal Delimiter As String = " ") As Long
'Conta le parole.
Dim Ty As Long, Yt As Long, Yy As Long, Conta As Long
S = Trim$[S]
If Len[S] Then
Ty = InStr(S, Delimiter)
Yt = Len(Delimiter)
While Ty
If Asc(Mid$(S, Ty + 1, 1)) <> 32 Then Conta = Conta + 1
Yy = Ty + Yt
Ty = InStr(Yy, S, Delimiter)
Wend
Func_Contaparole = Conta + 1
End If
End Function
Private Sub Command1_Click()
Dim A As Integer, TotCaratteri As Long, Ty As Long
Dim S As String, S1 As String * 50, S2 As String * 1
Dim T1 As Single, T2 As Single
S = RichTextBox1.Text
Screen.MousePointer = vbHourglass
T1 = Timer
With List1
.Clear
For A = 32 To 255
Select Case A
'Scarta i caratteri non testuali
Case 95, 96, 127 To 144, 147 To 159
Case Else
S2 = Chr$(A)
Ty = Func_ContaStringa(S, S2)
Caratteri(A) = Ty
TotCaratteri = TotCaratteri + Caratteri(A)
S1 = "Carattere '" & S2 & "' contenuto " & Caratteri(A) & " volte"
.AddItem S1
End Select
Next
End With
Ty = Func_Contaparole[S]
T2 = Timer
Screen.MousePointer = vbNormal
Label1 = "Vi sono " & Format$(TotCaratteri, "###,0") & " caratteri"
Label3 = "Vi sono " & Format$(Ty, "###,0") & " parole"
Label2 = "Completato in " & T2 - T1 & " secondi."
End Sub
Private Sub Command2_Click()
Dim S As String
S = InputBox("Nome file :", "Immetti il nome del file di testo")
If Len(Trim$[S]) = 0 Then S = App.Path & "\license.txt"
RichTextBox1.filename = S
End Sub
Private Sub Form_Load()
Command1.Caption = "Esegui"
Command2.Caption = "File"
End Sub
Private Sub Text1_KeyPress(KeyAscii As Integer)
MsgBox "Contenuto " & Caratteri(KeyAscii) & " volte"
End Sub
Il programmino conta il numero di parole contenute in un testo, nonchè il numero di volte che ogni singolo carattere che lo compone è presente in esso.
Avrete bisogno di
1 richtextbox
1 listbox
3 label
1 textbox
2 commandbutton
Il programma è stato compilato in codice nativo (VB5.0).
Esso è portabile (a mio avviso) con facilità sotto altri linguaggi.
Sarebbe interessante evidenziare le differenze velocistiche, secondo me, in fondo non così rilevanti.
Saluti ;)
Option Explicit
Dim Caratteri(32 To 255) As Long
Private Function Func_ContaStringa(ByVal S As String, Optional ByVal Delimiter As String = ",") As Long
'Conta quante volte una sottostringa è contenuta all'interno di una stringa
'Ogni elemento è separato da un delimitatore
Dim Ty As Long, Yt As Long, Yy As Long, Conta As Long
Ty = InStr(S, Delimiter)
Yt = Len(Delimiter)
While Ty
Conta = Conta + 1
Yy = Ty + Yt
Ty = InStr(Yy, S, Delimiter)
Wend
Func_ContaStringa = Conta
End Function
Private Function Func_Contaparole(ByVal S As String, Optional ByVal Delimiter As String = " ") As Long
'Conta le parole.
Dim Ty As Long, Yt As Long, Yy As Long, Conta As Long
S = Trim$[S]
If Len[S] Then
Ty = InStr(S, Delimiter)
Yt = Len(Delimiter)
While Ty
If Asc(Mid$(S, Ty + 1, 1)) <> 32 Then Conta = Conta + 1
Yy = Ty + Yt
Ty = InStr(Yy, S, Delimiter)
Wend
Func_Contaparole = Conta + 1
End If
End Function
Private Sub Command1_Click()
Dim A As Integer, TotCaratteri As Long, Ty As Long
Dim S As String, S1 As String * 50, S2 As String * 1
Dim T1 As Single, T2 As Single
S = RichTextBox1.Text
Screen.MousePointer = vbHourglass
T1 = Timer
With List1
.Clear
For A = 32 To 255
Select Case A
'Scarta i caratteri non testuali
Case 95, 96, 127 To 144, 147 To 159
Case Else
S2 = Chr$(A)
Ty = Func_ContaStringa(S, S2)
Caratteri(A) = Ty
TotCaratteri = TotCaratteri + Caratteri(A)
S1 = "Carattere '" & S2 & "' contenuto " & Caratteri(A) & " volte"
.AddItem S1
End Select
Next
End With
Ty = Func_Contaparole[S]
T2 = Timer
Screen.MousePointer = vbNormal
Label1 = "Vi sono " & Format$(TotCaratteri, "###,0") & " caratteri"
Label3 = "Vi sono " & Format$(Ty, "###,0") & " parole"
Label2 = "Completato in " & T2 - T1 & " secondi."
End Sub
Private Sub Command2_Click()
Dim S As String
S = InputBox("Nome file :", "Immetti il nome del file di testo")
If Len(Trim$[S]) = 0 Then S = App.Path & "\license.txt"
RichTextBox1.filename = S
End Sub
Private Sub Form_Load()
Command1.Caption = "Esegui"
Command2.Caption = "File"
End Sub
Private Sub Text1_KeyPress(KeyAscii As Integer)
MsgBox "Contenuto " & Caratteri(KeyAscii) & " volte"
End Sub
Il programmino conta il numero di parole contenute in un testo, nonchè il numero di volte che ogni singolo carattere che lo compone è presente in esso.
Avrete bisogno di
1 richtextbox
1 listbox
3 label
1 textbox
2 commandbutton
Il programma è stato compilato in codice nativo (VB5.0).
Esso è portabile (a mio avviso) con facilità sotto altri linguaggi.
Sarebbe interessante evidenziare le differenze velocistiche, secondo me, in fondo non così rilevanti.
Saluti ;)