Autor Beitrag
Wolfgang
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 34



BeitragVerfasst: Di 10.01.06 12:12 
Hi!

Ich arbeite erst seit kurzem mit Delphi und wollte nur frage, ob mir jemand einen Befehl nennen kann, mit dem man überprüfen kann, ob in einer ListBox eine gerade oder eine ungerade Anzahl von Items enthalten sind.

Danke im Vorhinein!
Gausi
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 8554
Erhaltene Danke: 481

Windows 7, Windows 10
D7 PE, Delphi XE3 Prof, Delphi 10.3 CE
BeitragVerfasst: Di 10.01.06 12:14 
Hallo und :welcome: in der Entwickler-Ecke!
Das geht einfach so:
ausblenden Delphi-Quelltext
1:
2:
3:
4:
if (Listbox1.Count MOD 2) = 0 then 
  showmessage('Gerade Anzahl')
else
  showmessage('Ungerade Anzahl')

_________________
We are, we were and will not be.
BenBE
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 8721
Erhaltene Danke: 191

Win95, Win98SE, Win2K, WinXP
D1S, D3S, D4S, D5E, D6E, D7E, D9PE, D10E, D12P, DXEP, L0.9\FPC2.0
BeitragVerfasst: Di 10.01.06 18:31 
Da MOD lahm ist: Gerade {: Boolean} := Listbox1.Items.Count AND 1 = 0;

_________________
Anyone who is capable of being elected president should on no account be allowed to do the job.
Ich code EdgeMonkey - In dubio pro Setting.
GTA-Place
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
EE-Regisseur
Beiträge: 5248
Erhaltene Danke: 2

WIN XP, IE 7, FF 2.0
Delphi 7, Lazarus
BeitragVerfasst: Di 10.01.06 19:03 
Alternativ:
ausblenden Delphi-Quelltext
1:
2:
3:
4:
if not (odd(Listbox1.Items.Count)) then
  // Gerade
else
  // Ungerade

odd(); gibt zurück ob die Zahl ungerade ist.

_________________
"Wer Ego-Shooter Killerspiele nennt, muss konsequenterweise jeden Horrorstreifen als Killerfilm bezeichnen." (Zeit.de)
BenBE
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 8721
Erhaltene Danke: 191

Win95, Win98SE, Win2K, WinXP
D1S, D3S, D4S, D5E, D6E, D7E, D9PE, D10E, D12P, DXEP, L0.9\FPC2.0
BeitragVerfasst: Di 10.01.06 19:21 
Da sei mal noch Even(XY) erwähnt, die angibt, ob eine Zahl gerade ist ;-)

_________________
Anyone who is capable of being elected president should on no account be allowed to do the job.
Ich code EdgeMonkey - In dubio pro Setting.
GTA-Place
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
EE-Regisseur
Beiträge: 5248
Erhaltene Danke: 2

WIN XP, IE 7, FF 2.0
Delphi 7, Lazarus
BeitragVerfasst: Di 10.01.06 19:25 
Mh..., danke für den Hinweis. Die Funktion kannte ich noch gar nicht.

_________________
"Wer Ego-Shooter Killerspiele nennt, muss konsequenterweise jeden Horrorstreifen als Killerfilm bezeichnen." (Zeit.de)
Wolfgang Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 34



BeitragVerfasst: Mi 11.01.06 12:58 
Ich danke euch für eure Lösungen.

wolfgang
alzaimar
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 2889
Erhaltene Danke: 13

W2000, XP
D6E, BDS2006A, DevExpress
BeitragVerfasst: Mi 11.01.06 13:08 
user profile iconBenBE hat folgendes geschrieben:
Da MOD lahm ist: Gerade {: Boolean} := Listbox1.Items.Count AND 1 = 0;

Da MOD 2 aber widerum schnell ist, kann man auch MOD 2 nehmen. Der Delphi-Compiler ist ja nicht blöd. :wink:

_________________
Na denn, dann. Bis dann, denn.
Spaceguide
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 552


(D3/D7/D8) Prof.
BeitragVerfasst: Mi 11.01.06 13:43 
Also da Items.Count ein integer ist, ist "and 1" tatsächlich schneller als "mod 2".

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
a := b mod 2; (a,b = integer)

mov ebx,eax
and ebx,$80000001
jns +$05
dec ebx
or ebx,-$02
inc ebx


ausblenden Delphi-Quelltext
1:
2:
3:
4:
a := b mod 2; (a,b = dword)

mov ebx,eax
and ebx,$01
Tastaro
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 414
Erhaltene Danke: 23



BeitragVerfasst: Mi 11.01.06 14:00 
"and 1" - "mod 2" *grusel*

Also leserlich wird das Programm durch sowas aber nicht. Und die Geschindigkeitsvorteile dabei sind sowas von egal.

Nebenbei bemerkt: Die Funktion "even" gibt es nicht (jedenfalls nicht bis Delphi 7). Wohl aber die Funktion "odd".

Beste Grüße
Tastaro
alzaimar
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 2889
Erhaltene Danke: 13

W2000, XP
D6E, BDS2006A, DevExpress
BeitragVerfasst: Mi 11.01.06 14:22 
user profile iconSpaceguide hat folgendes geschrieben:
Also da Items.Count ein integer ist, ist "and 1" tatsächlich schneller als "mod 2".

Ach Spaceguide, alter CPU-Zyklenzähler.. Mir gings ums MOD. Bei MOD 2 wird eben nur ein AND generiert, keine Division...
Tastaro: Am hübschesten ists natürlich mit odd(*), aber alte Säcke (so wie ich) verstehen auch das MOD 2... Aber Recht hast Du! Und die Geschwindigkeitsvorteile sind nur in innersten Schleifen spürbar, die Milliardenfach durchlaufen werden.

_________________
Na denn, dann. Bis dann, denn.