Entwickler-Ecke

Delphi Language (Object-Pascal) / CLX - case xxx of var_string es geht um varstring


Matthias R - Mo 04.10.10 13:10
Titel: case xxx of var_string es geht um varstring
Hi ich bin Matthias, neu hier, 35 Jahre alt und hobbymässig mit Delphi 7 unterwegs.
Das ist mein erstes posting, ich bitte daher um nachsicht, (Wahrscheinlich werde ich zig Links zu dem Thema in diesem Forum kassieren :shock: )
Hab mir ein function gebastelt:
ermittelt die Anzahl der zwischen zwei Datumswerten liegenden
Tage, die auf Montag, Mittwoch, Donnerstag oder Freitag fallen

Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
   function get_offen(anfang,ende:tdate): integer;
      begin
        result:=0;
        while anfang <= ende do
          begin
           case DayOfWeek(anfang) of 2,4,5,6:   // Montag, Mittwoch,Donnerstag und Freitag werden berücksichtigt
             begin
              inc(result);
             end;
           end;
          anfang := anfang+1;
        end;

Wie kann ich die Tage, (Mo, Mi, Do, Fr) bzw (2,4,5,6) als Variable übergeben???
ich hab es mir so gedacht

Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
function get_offen(anfang,ende:tdate): integer;
  var
   var_tage: string;
     begin
        var_tage:= '1,2,3,4';
        result:=0;
        while anfang <= ende do
          begin
           case DayOfWeek(anfang) of var_tage:
           .....
         end;

Das klappt so natürlich nicht.
Auch nach stundenlanger Suche bin ich einfach nicht fündig geworden
Sämtliche von mir gefunden Antworten durch Tante Google behandeln nur das Thema vor
dem of und nicht die Statements dahinter.
Hoffe hier wird mir geholfen.
Danke schon mal im voraus für die vielen hilfreichen Antworten.


Nersgatt - Mo 04.10.10 13:20

Du brauchst ein Set:


Delphi-Quelltext
1:
2:
3:
type
  TNumbers = 0..9;
  TNumbersSet = Set of TNumbers;


Dann kannst Du einen Parameter so definieren:

Delphi-Quelltext
1:
2:
3:
4:
Function TForm1.Test(ANumber : Integer; ANumbers : TNumbersSet) : Boolean;
begin
  Result := ANumber in ANumbers;
end;


Und der Aufruf z.B. so:

Delphi-Quelltext
1:
2:
  if Test(StrToInt(edt1.Text), [1,2,3]) then
    ShowMessage(edt1.Text + 'ist in 1,2,3');


MaxWurzel - Mo 04.10.10 13:28

user profile iconMatthias R hat folgendes geschrieben Zum zitierten Posting springen:



Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
function get_offen(anfang,ende:tdate): integer;
  var
   var_tage: string;
     begin
        var_tage:= '1,2,3,4';
        result:=0;
        while anfang <= ende do
          begin
           case DayOfWeek(anfang) of var_tage:
           .....
         end;

.

Du vergleichst hier integer mit string. Das kann nicht klappen. Definiere für jeden Tag einen Integerwert. Den kannst du dann mit case vergleichen.


Gerd Kayser - Mo 04.10.10 14:42

1. Statt DayOfWeek (mit Sonntag = 1) würde ich DayOfTheWeek (mit Montag = 1) verwenden.
2. Entweder ein Set oder eine Bitmaske verwenden.
Beispiel:

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:
type
  TWochentage = Set of 1 .. 7;
...
procedure TMainform.Button1Click(Sender: TObject);
var
  Wochentage : TWochentage;
begin
  Wochentage := [67];     // 1 = Montag !!!
  Label1.Caption := IntToStr(GetOffen(4045540465, Wochentage));
end;

function TMainform.GetOffen(Anfang, Ende: TDate; Wochentage: TWochentage): integer;
var
  Start: TDate;
  Temp : integer;
begin
  Result := 0;
  Start := Anfang;
  while Start <= Ende do
    begin
      Temp := DayOfTheWeek(Start) ;
      case Temp of
        1 : if Temp in Wochentage then      // 1 = Montag
              Inc(Result);
        2 : if Temp in Wochentage then
              Inc(Result);
        3 : if Temp in Wochentage then
              Inc(Result);
        4 : if Temp in Wochentage then
              Inc(Result);
        5 : if Temp in Wochentage then
              Inc(Result);
        6 : if Temp in Wochentage then
              Inc(Result);
        7 :  if Temp in Wochentage then
              Inc(Result);
      end;
      Start := Start + 1;
    end;
end;

procedure TMainform.Button2Click(Sender: TObject);
var
  Wochentage : byte;
begin
  Wochentage := 64 + 128;   // 64 = 2^6, 128 = 2^7   ---> hier: Sa. und So.
  Label1.Caption := IntToStr(GetOffenNeu(4045540465, Wochentage));
end;

function TMainform.GetOffenNeu(Anfang, Ende: TDate; Wochentage: byte): integer;
var
  Start: TDate;
  Temp : integer;
begin
  Result := 0;
  Start := Anfang;
  while Start <= Ende do
    begin
      Temp := DayOfTheWeek(Start);
      case Temp of
        1 : if Wochentage and 2 = 2 then    // 1 = Montag
              Inc(Result);
        2 : if Wochentage and 4 = 4 then
              Inc(Result);
        3 : if Wochentage and 8 = 8 then
              Inc(Result);
        4 : if Wochentage and 16 = 16 then
              Inc(Result);
        5 : if Wochentage and 32 = 32 then
              Inc(Result);
        6 : if Wochentage and 64 = 64 then
              Inc(Result);
        7 : if Wochentage and 128 = 128 then
              Inc(Result);
      end;
      Start := Start + 1;
    end;
end;


Matthias R - Mo 04.10.10 15:19

Hi und vielen Dank für die Antworten.
Anscheinend versteh ich dies Case Geschichte nicht so ganz.

Habe mir jetzt diese Lösung ausgedacht:


Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
var regeltage:string// z.b 1234 oder 2456

function get_offen(anfang,ende:tdate): integer;
  var i:Word;
  s:string;
      begin
        result:=0;
        while anfang <= ende do
          begin
            for i := 1 to Length(regeltage) do
             begin
               if DayOfWeek(anfang) = strtoint(Copy(regeltage,i,1)) then
                 begin
                   inc(result);
                 end;
             end;
            anfang := anfang+1;
          end;
      end;


baka0815 - Mo 04.10.10 17:04

user profile iconGerd Kayser hat folgendes geschrieben Zum zitierten Posting springen:


Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
  while Start <= Ende do
    begin
      Temp := DayOfTheWeek(Start) ;
      case Temp of
        1 : if Temp in Wochentage then      // 1 = Montag
              Inc(Result);
        2 : if Temp in Wochentage then
              Inc(Result);
        3 : if Temp in Wochentage then
              Inc(Result);
        4 : if Temp in Wochentage then
              Inc(Result);
        5 : if Temp in Wochentage then
              Inc(Result);
        6 : if Temp in Wochentage then
              Inc(Result);
        7 :  if Temp in Wochentage then
              Inc(Result);
      end;
      Start := Start + 1;
    end;


Das ist doch Quatsch. Da reicht doch einfach if Temp in Wochentage then, wozu das case drum herum?

user profile iconGerd Kayser hat folgendes geschrieben Zum zitierten Posting springen:


Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
  while Start <= Ende do
    begin
      Temp := DayOfTheWeek(Start);
      case Temp of
        1 : if Wochentage and 2 = 2 then    // 1 = Montag
              Inc(Result);
        2 : if Wochentage and 4 = 4 then
              Inc(Result);
        3 : if Wochentage and 8 = 8 then
              Inc(Result);
        4 : if Wochentage and 16 = 16 then
              Inc(Result);
        5 : if Wochentage and 32 = 32 then
              Inc(Result);
        6 : if Wochentage and 64 = 64 then
              Inc(Result);
        7 : if Wochentage and 128 = 128 then
              Inc(Result);
      end;
      Start := Start + 1;
    end;

Das ist genau so Quatsch, zumindest das 2 = 24 = 4, etc. Das ist immer true. Du meinst  and (Wochentage and 2) = 2.

Vergiss das, genau lesen ist wichtig.

Ich würde hier dann aber klammern, damit es übersichtlicher bleibt: if (Wochentage and 2) = 2 then.


Gerd Kayser - Mo 04.10.10 17:38

user profile iconbaka0815 hat folgendes geschrieben Zum zitierten Posting springen:
Das ist doch Quatsch. Da reicht doch einfach if Temp in Wochentage then, wozu das case drum herum?

Ganz einfach: Weil der Threadstarter in seinem Source auch case verwendet hat (aber leider nur gefolgt von "..."), so daß ich davon ausgehe, daß er in seiner Funktion unterschiedlich auf die einzelnen Wochentage reagieren möchte. Und case steht auch im Betreff drinnen.