1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
| unit VMain;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm8 = class(TForm)
Edit1: TEdit;
ListBox1: TListBox;
Label1: TLabel;
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
procedure Edit1Change(Sender: TObject);
private
Words: TStringList;
public
{ Déclarations publiques }
end;
var
Form8: TForm8;
implementation
{$R *.dfm}
// ci-gît un code qui fût emporté avant d'être commenté.
type
PCharWord = ^TCharWord;
TCharWord = record // banzaii ! //
class function create(const aWord: string): TCharWord; static;
class operator equal(A, B: TCharWord): boolean;
class operator explicit(const a: string): TCharWord;
class operator implicit(const a: string): TCharWord;
case integer of
0 :(len: integer; chars: array[byte] of byte);
1 :(mem: array[0..64] of longword);
end;
const
SizeOfTCharWord = SizeOf(TCharWord);
class function TCharWord.create(const aWord: string): TCharWord;
var X, L: integer;
B: byte;
begin
for X := 0 to 64 do
result.mem[X] := 0;
result.len := Length(aWord);
L := result.len;
for X := 1 to L do
begin
B := byte(aWord[X]);
case aWord[X] of
{a} 'à','ä','â','ã' : inc(result.chars[97]);
{c} 'ç' : inc(result.chars[99]);
{e} 'é','è','ë','ê' : inc(result.chars[101]);
{i} 'ï','î','ì' : inc(result.chars[105]);
{o} 'ô','ö','ò' : inc(result.chars[111]);
{u} 'ü','û','ù' : inc(result.chars[117]);
{y} 'ÿ' : inc(result.chars[121]);
else
if (B >= 97) and (B <= (123)) then
inc(result.chars[B])
else
dec(result.len);
end;
end;
end;
class operator TCharWord.equal(A, B: TCharWord): boolean;
begin
result := A.len = B.len;
if result then
result := compareMem(@A, @B, SizeOfTCharWord);
end;
class operator TCharWord.explicit(const a: string): TCharWord;
begin
result := TCharWord.create(a);
end;
class operator TCharWord.implicit(const a: string): TCharWord;
begin
result := TCharWord.create(a);
end;
procedure TForm8.Edit1Change(Sender: TObject);
var X: integer;
A, B: TCharWord;
G: LongWord;
begin
if length(Edit1.Text) < 1 then
exit;
A := string(Edit1.Text);
ListBox1.Items.BeginUpdate;
try
G := GetTickCount;
ListBox1.Clear;
for X := 0 to Words.Count-1 do
begin
B := PCharWord(Pointer(Words.Objects[X]))^;
if A = B then
ListBox1.Items.Add(Words[X])
end;
G := GetTickCount-G;
finally
ListBox1.Items.EndUpdate;
Label1.Caption := format('%d anagrammes trouvés, pour %d mots, en %.3f secondes',[ListBox1.Count,Words.Count,G*0.001]);
end;
end;
procedure TForm8.FormCreate(Sender: TObject);
var X: integer;
P: PCharWord;
G: LongWord;
begin
Edit1.Align := alTop;
Edit1.AlignWithMargins := true;
Label1.Align := alTop;
Label1.AlignWithMargins := true;
ListBox1.Align := alClient;
ListBox1.AlignWithMargins := true;
Words := TStringList.Create;
Words.LoadFromFile(extractFilePath(ParamStr(0))+'Liste_mots.txt');
for X := 0 to Words.Count - 1 do
begin
new(P);
P^ := TCharWord.create(Words[X]);
Words.Objects[X] := TObject(P);
end;
Edit1.Text := 'atirer';
end;
procedure TForm8.FormDestroy(Sender: TObject);
var X: integer;
P: PCharWord;
begin
// ! //
for X := 0 to Words.Count - 1 do
begin
P := PCharWord(Words.Objects[X]);
dispose(P);
end;
Words.Free;
// ! //
end;
end. |
Partager