AGB  ·  Datenschutz  ·  Impressum  







Anmelden
Nützliche Links
Registrieren
Thema durchsuchen
Ansicht
Themen-Optionen

LottoTippErstellen

Ein Thema von YXCVBNM1 · begonnen am 1. Sep 2011 · letzter Beitrag vom 2. Sep 2011
Antwort Antwort
Seite 2 von 2     12   
Bjoerk

Registriert seit: 28. Feb 2011
Ort: Mannheim
1.384 Beiträge
 
Delphi 10.4 Sydney
 
#11

AW: LottoTippErstellen

  Alt 2. Sep 2011, 01:19
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.
  Mit Zitat antworten Zitat
Benutzerbild von DeddyH
DeddyH

Registriert seit: 17. Sep 2006
Ort: Barchfeld
27.542 Beiträge
 
Delphi 11 Alexandria
 
#12

AW: LottoTippErstellen

  Alt 2. Sep 2011, 09:18
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;
Detlef
"Ich habe Angst vor dem Tag, an dem die Technologie unsere menschlichen Interaktionen übertrumpft. Die Welt wird eine Generation von Idioten bekommen." (Albert Einstein)
Dieser Tag ist längst gekommen
  Mit Zitat antworten Zitat
Antwort Antwort
Seite 2 von 2     12   


Forumregeln

Es ist dir nicht erlaubt, neue Themen zu verfassen.
Es ist dir nicht erlaubt, auf Beiträge zu antworten.
Es ist dir nicht erlaubt, Anhänge hochzuladen.
Es ist dir nicht erlaubt, deine Beiträge zu bearbeiten.

BB-Code ist an.
Smileys sind an.
[IMG] Code ist an.
HTML-Code ist aus.
Trackbacks are an
Pingbacks are an
Refbacks are aus

Gehe zu:

Impressum · AGB · Datenschutz · Nach oben
Alle Zeitangaben in WEZ +1. Es ist jetzt 23:41 Uhr.
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