Autor Beitrag
Matthias R
Hält's aus hier
Beiträge: 2



BeitragVerfasst: Mo 04.10.10 13:10 
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
ausblenden 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
ausblenden 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
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 1581
Erhaltene Danke: 279


Delphi 10 Seattle Prof.
BeitragVerfasst: Mo 04.10.10 13:20 
Du brauchst ein Set:

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


Dann kannst Du einen Parameter so definieren:
ausblenden 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:
ausblenden Delphi-Quelltext
1:
2:
  if Test(StrToInt(edt1.Text), [1,2,3]) then
    ShowMessage(edt1.Text + 'ist in 1,2,3');

_________________
Gruß, Jens
Zuerst ignorieren sie dich, dann lachen sie über dich, dann bekämpfen sie dich und dann gewinnst du. (Mahatma Gandhi)
MaxWurzel
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 77
Erhaltene Danke: 10



BeitragVerfasst: Mo 04.10.10 13:28 
user profile iconMatthias R hat folgendes geschrieben Zum zitierten Posting springen:


ausblenden 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
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 632
Erhaltene Danke: 121

Win 7 32-bit
Delphi 2006/XE
BeitragVerfasst: 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:
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:
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 Threadstarter
Hält's aus hier
Beiträge: 2



BeitragVerfasst: 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:

ausblenden 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
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 489
Erhaltene Danke: 14

Win 10, Win 8, Debian GNU/Linux
Delphi 10.1 Berlin, Java, C#
BeitragVerfasst: Mo 04.10.10 17:04 
user profile iconGerd Kayser hat folgendes geschrieben Zum zitierten Posting springen:

ausblenden 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:

ausblenden 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
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 632
Erhaltene Danke: 121

Win 7 32-bit
Delphi 2006/XE
BeitragVerfasst: 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.