Contar a repetição em um txt
14/09/2004
0
Ex: o número 5 se repetiu 10 vz, o 8 repetiu 6.
Agradeço a ajuda.
Fabiano.
Fabiano_aprendiz
Posts
14/09/2004
Rômulo Barros
procedure TfrmOpe5EstrelasMais.Button1Click(Sender: TObject); Var strLstTeste : TStrings; intC : Integer; vetValores : Arrayy[0..9] Of Integer; begin inherited; Try strLstTeste := TStringList.Create; strLstTeste.Add(´0´); strLstTeste.Add(´1´); strLstTeste.Add(´2´); strLstTeste.Add(´2´); strLstTeste.Add(´3´); strLstTeste.Add(´3´); strLstTeste.Add(´3´); strLstTeste.Add(´4´); strLstTeste.Add(´4´); strLstTeste.Add(´4´); strLstTeste.Add(´4´); strLstTeste.Add(´5´); strLstTeste.Add(´5´); strLstTeste.Add(´5´); strLstTeste.Add(´5´); strLstTeste.Add(´5´); For intC := 0 To strLstTeste.Count -1 Do Begin vetValores[StrToInt(strLstTeste[intC])] := vetValores[StrToInt(strLstTeste[intC])] + 1; End; For intC := 0 To Length(vetValores) -1 Do ShowMessage(´Total de "´ + IntToStr(intC) + ´" no TStrings: ´ + IntToStr(vetValores[intC])); Finally FreeAndNil(strLstTeste); End;
:wink: :wink: :wink: :wink: :wink:
15/09/2004
Fabiano_aprendiz
Estou tentando testar sua solução, mas quando rodo a aplicação, o vetor já icializa com um monte de lixo (creio q seja da memória), como fazer para zerar um vetor ao iniciar a aplicação?
15/09/2004
Rômulo Barros
:wink: :wink: :wink: :wink: :wink:
15/09/2004
Fabiano_aprendiz
[]´s
Fabiano
15/09/2004
Sandra
:wink: :wink: :wink: :wink: :wink:[/quote:cfea99187c]
Undeclared,
Não seria o caso de inicializar o vetor, antes da utilização? :roll:
Mais ou menos assim:
begin inherited; For IntC := 0 To 9 do vetValores[IntC] := 0; // zerar os elementos do array vetValores Try . . .
PS: Aproveitei o IntC do tipo inteiro. :mrgreen:
15/09/2004
Beppe
Uma alternativa, sem precisar de loop´s é FillChar(Sua_Variavel, SizeOf(Sua_Variavel), 0)
15/09/2004
Fabiano_aprendiz
begin
Try
Lista:= TStringList.Create;
Lista.LoadFromFile (ExtractFileName(´numeros.txt´));
For C := 0 To Lista.Count -1 Do
Begin
vetValores[StrToInt(Lista[C])] := vetValores[StrToInt(Lista[C])] + 1;
End;
For C := 0 to Length(vetValores) -1 Do
Begin
ListBox1.Items.Add(´Total de ´´ + IntToStr(C) + ´´ sorteados: ´ + IntToStr(vetValores[C]));
end;
Finally
FreeAndNil(Lista);
End;
end;
Da forma como foi implementado, o resultado está assim:
Total de ´1´ sorteados: 40
Total de ´2´ sorteados: 48
Total de ´3´ sorteados: 35
Agora to tentando ordenar isso por ordem decrescente, digo ordenar pelos números q sairam mais vz. Ex:
Total de ´13´ sorteados: 70
Total de ´20´ sorteados: 68
Total de ´48´ sorteados: 67
Alguem sabe como me ajudar?
Agradeço a todos,
Fabiano.
Clique aqui para fazer login e interagir na Comunidade :)