Autor Beitrag
personX
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 19

WIN 2000
Delphi 2006
BeitragVerfasst: So 22.10.06 13:08 
Hallo,
wie der Titel schon sagt, will ich die Anzahl der Elemente in einer Menge bestimmen. Gibt's dafür irgendeinen Befehl? Wenn ja: wie lautet der; wenn nein: wie kann ich die Anzahl sonst bestimmen? :?:
Hab schon im Forum gesucht, aber nichts passendes gefunden. Wäre schön, wenn mir jemand helfen könnte!
wulfskin
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 1349
Erhaltene Danke: 1

Win XP
D5 Pers (SSL), D2005 Pro, C, C#
BeitragVerfasst: So 22.10.06 13:16 
Mhh, du hättest schon dazu sagen können, was mit Menge meinst.
Du meinst:
  • Sets:
    ausblenden Delphi-Quelltext
    1:
    2:
    3:
    type
      TTyp = (ta, tb, tc);
      TMenge = set of TTyp;
    Dann kannst du dir eine Funktion schreiben, welche überprüft, ob der Typ vorhanden ist und dann zählt.
  • Arrays: Length.
  • TList: Count
Gruß Hape!

_________________
Manche antworten um ihren Beitragszähler zu erhöhen, andere um zu Helfen.
personX Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 19

WIN 2000
Delphi 2006
BeitragVerfasst: So 22.10.06 13:37 
Danke für die schnelle Antwort.
Zitat:
Mhh, du hättest schon dazu sagen können, was mit Menge meinst.

Ich meinte Set.
ausblenden Delphi-Quelltext
1:
var a:set of 1..255;					

Also bleibt mir nichts anderes übrig, als zu überprüfen, ob 1,2,3...255 in der Menge vorhanden sind und bei wahrer Aussage mitzuzählen. Schade, dass es keinen Befehl dazu gibt :( .
Danke dir!
Horst_H
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 1654
Erhaltene Danke: 244

WIN10,PuppyLinux
FreePascal,Lazarus
BeitragVerfasst: So 22.10.06 14:42 
Hallo,

da war mal was population count, bitcount ich finde es nicht, aber ist ja nicht so schwer.
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)
    Button1: TButton;
    Memo1: TMemo;
    procedure Button1Click(Sender: TObject);
  private
    { Private-Deklarationen }
  public
    { Public-Deklarationen }
  end;
  TSet = set of 0..255;
  pSet = ^Tset;
  pInteger = ^integer;

var
  Form1: TForm1;
  Menge : Tset;
  Zeiger : pInteger;

implementation

{$R *.dfm}
function BitCount(Zahl:integer):integer;
begin
  //Maske %10101010101010101010101010101010
  //Maske %01010101010101010101010101010101
  result := (Zahl AND $AAAAAAAAshr 1 + (Zahl AND $55555555);
  //Maske %11001100110011001100110011001100
  //Maske %00110011001100110011001100110011
  result := (result AND $CCCCCCCCshr 2 + (result AND $33333333);
  //Maske %11110000111100001111000011110000
  //Maske %00001111000011110000111100001111
  result := (result AND $F0F0F0F0shr 4 + (result AND $0F0F0F0F);
  //Maske %11111111000000001111111100000000
  //Maske %00000000111111110000000011111111
  result := (result AND $FF00FF00shr 8 + (result AND $00FF00FF);
  //Maske %11111111111111110000000000000000
  //Maske %00000000000000001111111111111111
  result := (result AND $FFFF0000shr 16 + (result AND $0000FFFF);
end;
procedure TForm1.Button1Click(Sender: TObject);
var
 i,j,sum,gessum : integer;
 s : string;
begin
  Menge := [];
  Zeiger :=@Menge;

  for i := 0 to 255 do
    begin
    include(Menge,i);
    Zeiger :=@Menge;
    s := '';
    gessum := 0;
    For j := 1 to 8 do
      begin
      sum := BitCount(Zeiger^);
      s := s+Format(' %.2d',[sum]);
      inc(gessum,sum);
      inc(Zeiger);
      end;
    s := s+Format('  %.3d',[gessum]);
    Memo1.lines.add(s);
  end;
 for i := 0 to 255 do
    begin
    exclude(Menge,i);
    Zeiger :=@Menge;
    s := '';
    gessum := 0;
    //1 bis 8 beiMengen von 0 bis 255 sonst testen
    For j := 1 to 8 do
      begin
      sum := BitCount(Zeiger^);
      s := s+Format(' %.2d',[sum]);
      inc(gessum,sum);
      inc(Zeiger);
      end;
    s := s+Format('  %.3d',[gessum]);
    Memo1.lines.add(s);
  end;

end;

end.


Gruss Horst
personX Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 19

WIN 2000
Delphi 2006
BeitragVerfasst: So 22.10.06 15:40 
Ich hab mir jetzt schon eine Funktion geschrieben:
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
type TOrder = set of 1..255;


function Elementanzahl(m:TOrder; a:integer):integer;
var i:integer;
begin
  result:=0;
  for i:=1 to a do
    if (i in m) then result:=result+1;
end;


Ich wollte den Programmtext eigentlich so kurz wie möglich halten, aber trotzdem danke!
Horst_H
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 1654
Erhaltene Danke: 244

WIN10,PuppyLinux
FreePascal,Lazarus
BeitragVerfasst: So 22.10.06 16:30 
Hallo,

falls es etwas schneller sein soll:

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:
unit SetCount;

interface

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

type
  TForm1 = class(TForm)
    Button1: TButton;
    Memo1: TMemo;
    procedure Button1Click(Sender: TObject);
  private
    { Private-Deklarationen }
  public
    { Public-Deklarationen }
  end;
  TSet = set of 0..255;
  pSet = ^Tset;
  pInteger = ^integer;

var
  Form1: TForm1;
  Menge : Tset;

implementation

{$R *.dfm}
function BitCount(Zahl:integer):integer;
begin
  result := (  Zahl AND $AAAAAAAAshr 1 + (  Zahl AND $55555555);
  result := (result AND $CCCCCCCCshr 2 + (result AND $33333333);
  result := (result AND $F0F0F0F0shr 4 + (result AND $0F0F0F0F);
  result := (result AND $FF00FF00shr 8 + (result AND $00FF00FF);
  result := (result AND $FFFF0000shr 16 +(result AND $0000FFFF);
end;

function Elementanzahl(const m:TSet; a:integer):integer;
var
  i,j : integer;
  Zeiger : pInteger;
begin
  result := 0;
  Zeiger := @m;
  For j := 0 to ((a+1shr 5)-1 do
    begin
    i := BitCount(Zeiger^);
    inc(result,i);
    inc(Zeiger);
    end;
  IF ((a+1and 31) <> 0 then
    begin
    i := Zeiger^ AND (1 shl ((a+1and 31)-1);
    i := BitCount(i);
    inc(result,i);
    end;
end;


procedure TForm1.Button1Click(Sender: TObject);
var
 i : integer;
begin
  Menge := [1..255];
  for i := 0 to 255 do
    begin
//    if i <> Elementanzahl(Menge,i) then
      Memo1.lines.add(Format('%d %.2d',[i,Elementanzahl(Menge,i)]));
    end;
  Memo1.lines.add('Fertig');
end;

end.


Gruss Horst
personX Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 19

WIN 2000
Delphi 2006
BeitragVerfasst: So 22.10.06 18:03 
Danke Horst,
der Teil ist schon wesentlich kürzer und übersichtlicher. Ich probier's mal damit aus!