Autor Beitrag
colaka
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 200
Erhaltene Danke: 4

Win XP, W7
Delphi 2005 Prof.
BeitragVerfasst: So 27.07.08 08:48 
Hallo,

ich habe eine Listbox, in der in jeder Zeile ein Cardinalwert steht. Diese Werte sind weder fortlaufend noch lückenlos. Auf Knopfdruck möchte ich nun die kleinstmögliche Zahl finden, die noch nicht in der Listbox steht und diese dann darin eintragen.
Da ich kein Profi bin, scheine ich mit dieser Aufgabe überfordert zu sein. Jedenfalls probiere ich schon 3 Tage herum und ende meist in einer Endlosschleife. Vielleicht kann mir hier jemand sagen, wie man so ein Problem strukturiert angeht.

Danke Ebi
Blackheart666
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 2195

XP
D3Prof, D6Pers.
BeitragVerfasst: So 27.07.08 09:42 
Wie sehen denn deine Versuche aus ?

_________________
Blackheart666
Der Irrsinn ist bei Einzelnen etwas Seltenes, - aber bei Gruppen, Parteien, Völkern, Zeiten die Regel. (Friedrich Nietzsche)
Grenzgaenger
Ehemaliges Mitglied
Erhaltene Danke: 1



BeitragVerfasst: So 27.07.08 10:36 
hier mal ein kleiner ansatz:
ausblenden volle Höhe Delphi-Quelltext
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:
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;

type
  TForm1 = class(TForm)
    bSuche: TButton;
    bInit: TButton;
    lKleinsteZahl: TLabel;
    eKleinsteZahl: TEdit;
    ListBox1: TListBox;
    procedure bInitClick(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure bSucheClick(Sender: TObject);
  private
    { Private-Deklarationen }
  public
    { Public-Deklarationen }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.bInitClick(Sender: TObject);
var
 i, x: integer;
begin
 listbox1.Clear; //listbox initialisieren
 for i := 0 to 20 do
 begin
  x := random(100); //zahlen von 0 bis 99 erstellen
  listbox1.AddItem(inttostr(x), pointer(x));  //in listbox aufnehmen
 end;
end;

procedure TForm1.bSucheClick(Sender: TObject);
var
 i: integer;
 min: integer;
begin
 min := MaxInt; //minimum initialisieren
 for i := 0 to listbox1.Count - 1 do //listbox durchlaufen;
  if min > integer(listbox1.Items.Objects[i]) then  //wenn min > zahl
   min := integer(listbox1.Items.Objects[i]);       //min auf neuen min setzen

 if min = maxint then //wenn nix gefunden
  eKleinsteZahl.Text := '' //ausgabe initialisieren
 else
  eKleinsteZahl.text := inttostr(min); //kleinste zahl ausgeben
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
 randomize;
end;

end.


das problem ist etwas tricky, da du von oben nach unten suchen musst...
matox
Hält's aus hier
Beiträge: 4
Erhaltene Danke: 1

XP
Delphi 7.0 Personal
BeitragVerfasst: So 27.07.08 10:44 
Titel: In einer Listbox eine nicht vorhandene kleinste Zahl einsetz
ausblenden volle Höhe Delphi-Quelltext
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:
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;

type
  TForm1 = class(TForm)
    ListBox1: TListBox;
    Button1: TButton;
    Button2: TButton;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);

  private
    { Private-Deklarationen }
  public
    { Public-Deklarationen }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.FormCreate(Sender: TObject);
begin
  Listbox1.Clear;
  Listbox1.Items.Add('8'); //Listbox mit Zahlen füllen
  Listbox1.Items.Add('18');
  Listbox1.Items.Add('15');
  Listbox1.Items.Add('12');
  Listbox1.Items.Add('6');
  Listbox1.Items.Add('17');
end;
//Function um die Einträge der Listbox aufsteigend zu sortieren
function CompareInt(List:TStringlist; Index1, Index2:Integer):Integer;
var d1, d2: Integer;
    r1, r2: Boolean;
   function IsInt(AString : string; var AInteger : Integer): Boolean;
  var
    Code: Integer;
  begin
    Val(AString, AInteger, Code);
    Result := (Code = 0);
  end;
  begin
  r1 :=  IsInt(List[Index1], d1);
  r2 :=  IsInt(List[Index2], d2);
  Result := ord(r1 or r2);
  if Result <> 0 then
  begin
    if d1 < d2 then
      Result := -1
    else if d1 > d2 then
      Result := 1
    else
     Result := 0;
  end else
   Result := lstrcmp(PChar(List[Index1]), PChar(List[Index2]));
end;

procedure TForm1.Button1Click(Sender: TObject);
// Zahlen sortieren
var
  sl: TStringList;
  begin
  sl := TStringList.Create;
  try
    listbox1.Sorted := False;
    sl.Assign(listbox1.Items);
    sl.CustomSort(CompareInt);
    listbox1.Items.Assign(sl);
  finally
   sl.Free;
 end;
end;

procedure TForm1.Button2Click(Sender: TObject);
var n:String;
    m:Integer;
begin
  n := Listbox1.Items.Strings[0];
  m := StrToInt(n)-1;
  Listbox1.Items.Add(IntToStr(m));
  Button1.Click; // um die ermittelte Zahl an Stelle [0] zu setzen
end;
end.