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 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199
| unit Loto;
interface
uses
Windows, SysUtils, Classes;
const
cLoto = $01;
cEuroMillion = $02;
type
PLoto = ^TLoto;
TLoto = packed record
LotoType : byte;
case byte of
0:(Numbers : array[0..6] of byte);
1:(N1,N2,N3,N4,N5,N6,N7: byte);
end;
const
SizeOfLoto = SizeOf(TLoto);
LotoDefault: TLoto = (LotoType:cLoto; Numbers:(0,0,0,0,0,0,0));
type
TLotoFile = class
private
fLotos : array of TLoto;
fStream: TFileStream;
function getLoto(index: integer): TLoto;
procedure setLoto(index: integer; const Value: TLoto);
public
property loto[index: integer]: TLoto read getLoto write setLoto; default;
function count: integer;
function Push(aLoto: TLoto): integer; overload;
function Pop: TLoto;
function IndexOf(aLoto: TLoto): integer;
procedure Exchange(IndexA, IndexB: integer);
constructor Create(aFileName: string); reintroduce;
destructor Destroy; override;
end;
function LotoToString(aLoto : TLoto): string;
function LotoCompare(A, B: TLoto): boolean;
function LotoCreate(aType,aN1,aN2,aN3,aN4,aN5,aN6,aN7: byte): TLoto;
function LotoValid(aLoto: TLoto): boolean;
implementation
function LotoValid(aLoto: TLoto): boolean;
begin
result := false;
case aLoto.LotoType of
cLoto : result :=
(aLoto.N1>0 and aLoto.N1<=45) and
(aLoto.N2>0 and aLoto.N2<=45) and
(aLoto.N3>0 and aLoto.N3<=45) and
(aLoto.N4>0 and aLoto.N4<=45) and
(aLoto.N5>0 and aLoto.N5<=45) and
(aLoto.N6>0 and aLoto.N6<=45);
cEuroMillion :
(aLoto.N1>0 and aLoto.N1<=50) and
(aLoto.N2>0 and aLoto.N2<=50) and
(aLoto.N3>0 and aLoto.N3<=50) and
(aLoto.N4>0 and aLoto.N4<=50) and
(aLoto.N5>0 and aLoto.N5<=50) and
(aLoto.N6>0 and aLoto.N6<=11) and
(aLoto.N7>0 and aLoto.N7<=11);
end;
end;
function LotoCreate(aType,aN1,aN2,aN3,aN4,aN5,aN6,aN7: byte): TLoto;
begin
result.LotoType = aType;
result.N1 := aN1;
result.N2 := aN2;
result.N3 := aN3;
result.N4 := aN4;
result.N5 := aN5;
result.N6 := aN6;
result.N7 := aN7;
end;
function LotoCompare(A, B: TLoto): boolean;
begin
result := compareMem(@A, @B, SizeOfLoto);
end;
function LotoToString(aLoto : TLoto): string;
begin
result := '';
case aLoto.LotoType of
cLoto : result := format('%d, %d, %d, %d, %d et %d',
[ aLoto.Numbers[0],
aLoto.Numbers[1],
aLoto.Numbers[2],
aLoto.Numbers[3],
aLoto.Numbers[4],
aLoto.Numbers[5],
]);
cEuroMillion : result := format('%d, %d, %d, %d, %d et %d, %d',
[ aLoto.Numbers[0],
aLoto.Numbers[1],
aLoto.Numbers[2],
aLoto.Numbers[3],
aLoto.Numbers[4],
aLoto.Numbers[5],
aLoto.Numbers[6],
]);
end;
end;
{ TLotoFile }
function TLotoFile.count: integer;
begin
result := fStream.Size div SizeOfLoto;
end;
constructor TLotoFile.Create(aFileName: string);
begin
inherited Create;
if FileExists(aFileName) then
fStream := TFileStream.Create(aFileName, fmOpenReadWrite)
else
fStream := TFileStream.Create(aFileName, fmCreate);
end;
destructor TLotoFile.Destroy;
begin
fStream.free;
inherited;
end;
procedure TLotoFile.Exchange(IndexA, IndexB: integer);
var A,B: TLoto;
begin
A := getLoto(IndexA);
B := getLoto(IndexB);
setLoto(IndexA, B);
setLoto(IndexB, A);
end;
function TLotoFile.getLoto(index: integer): TLoto;
begin
if (Index >= 0) and (Index < Count) then
begin
fStream.Seek(Index * SizeOfLoto, soFromBeginning);
fStream.Read(result, SizeOfLoto);
end
else
assert(false, format('%d is out of Indexs [0..%d]',[Index, Count-1]));
end;
function TLotoFile.IndexOf(aLoto: TLoto): integer;
var X: integer;
begin
result := -1;
for X := 0 to Count - 1 do
if LotoCompare(getLoto(X), aLoto) then
begin
result := X;
break;
end;
end;
function TLotoFile.Pop: TLoto;
begin
fStream.Seek(fStream.Size-SizeOfLoto, soFromBeginning);
fStream.Size := fStream.Size - SizeOfLoto;
end;
function TLotoFile.Push(aLoto: TLoto): integer;
begin
result := Count;
fStream.Seek(0, soFromEnd);
fStream.Write(aLoto, SizeOfLoto);
end;
procedure TLotoFile.setLoto(index: integer; const Value: TLoto);
begin
if (Index >= 0) and (Index < Count) then
begin
fStream.Seek(Index * SizeOfLoto, soFromBeginning);
fStream.Write(Value, SizeOfLoto);
end
else
assert(false,format('%d is out of Indexs [0..%d]',[Index, Count-1]));
end;
end. |
Partager