Delphi-PRAXiS
Seite 2 von 2     12   

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   Programmieren allgemein (https://www.delphipraxis.net/40-programmieren-allgemein/)
-   -   LottoTippErstellen (https://www.delphipraxis.net/162684-lottotipperstellen.html)

Bjoerk 2. Sep 2011 01:19

AW: LottoTippErstellen
 
War ein Indexfehler und Sortieren hat auch gefehlt. Die Lösung mit dem repeat/until ist übrigens nicht besonders schön, aber das soll jetzt erst mal egal sein.

Delphi-Quellcode:
unit uLotto;

interface

type
  TLotto = class(TObject)
  private
    Tipp: array of array of integer;
  public
    procedure LottoTippErstellen(Anzahl: integer);
  end;

implementation

procedure TLotto.LottoTippErstellen(Anzahl: integer);
const
  N = 6;
var
  I, J, K, T: integer;
  Result: boolean;
begin
  SetLength(Tipp, Anzahl, N);
  for K:= 0 to Anzahl-1 do
    for I:= 0 to N-1 do
      repeat
        Result:= true;
        Tipp[K, I]:= Random(49)+1;
        for J:= 0 to I-1 do
          if Tipp[K, I] = Tipp[K, J] then Result:= false;
      until Result;
  for K:= 0 to Anzahl-1 do
    for I:= 0 to N-2 do
       for J:= I+1 to N-1 do
       begin
         if Tipp[K, I] > Tipp[K, J] then
         begin
           T:= Tipp[K, I];
           Tipp[K, I]:= Tipp[K, J];
           Tipp[K, J]:= T;
         end;
       end;
end;

end.

DeddyH 2. Sep 2011 09:18

AW: LottoTippErstellen
 
Oder etwas verteilter (enthält noch Verbesserungspotential, aber ich wollte auch nicht übertreiben):
Delphi-Quellcode:
type

  TZahlen = array[0..5] of Byte;

  TLottofeld = class
  private
    FZahlen: TZahlen;
  public
    constructor Create;
    property Zahlen: TZahlen read FZahlen;
  end;

  TLottoFelder = array of TLottofeld;

  TLottotipp = class
  private
    FTipps: TLottofelder;
    function GetTipps(Index: integer): TLottofeld;
    function GetTippCount: integer;
  public
    constructor Create(Anzahl: Cardinal);
    destructor Destroy; override;
    property Tipps[Index: integer]: TLottofeld read GetTipps; default;
    property TippCount: integer read GetTippCount;
  end;

{ TLottofeld }

constructor TLottofeld.Create;
var
  Liste: TList;
  i, Index: integer;

  procedure Swap(var a, b: Byte);
  var
    tmp: Byte;
  begin
    tmp := a;
    a := b;
    b := tmp;
  end;

  procedure Bubblesort;
  var
    Done: Boolean;
    j, LastPos: integer;
  begin
    LastPos := High(FZahlen);
    repeat
      Done := true;
      for j := Low(FZahlen) to LastPos - 1 do
        if FZahlen[j] > FZahlen[j + 1] then
          begin
            Swap(FZahlen[j], FZahlen[j + 1]);
            Done := false;
          end;
      dec(LastPos);
    until Done;
  end;

begin
  Liste := TList.Create;
  try
    for i := 1 to 49 do
      Liste.Add(Pointer(i));
    for i := Low(FZahlen) to High(FZahlen) do
      begin
        Index := Random(Liste.Count);
        FZahlen[i] := integer(Liste[Index]);
        Liste.Delete(Index);
      end;
    Bubblesort;
  finally
    Liste.Free;
  end;
end;

{ TLottotipp }

constructor TLottotipp.Create(Anzahl: Cardinal);
var
  i: integer;
begin
  SetLength(FTipps, Anzahl);
  for i := 0 to Anzahl - 1 do
    FTipps[i] := TLottofeld.Create;
end;

destructor TLottotipp.Destroy;
var
  i: integer;
begin
  for i := Low(FTipps) to High(FTipps) do
    FTipps[i].Free;
  SetLength(FTipps, 0);
  inherited;
end;

function TLottotipp.GetTippCount: integer;
begin
  Result := Length(FTipps);
end;

function TLottotipp.GetTipps(Index: integer): TLottofeld;
begin
  Result := FTipps[Index];
end;


Alle Zeitangaben in WEZ +1. Es ist jetzt 00:29 Uhr.
Seite 2 von 2     12   

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024 by Thomas Breitkreuz