Autor Beitrag
The_Tremendous
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 47

Win XP SP2
Borland Delphi 6
BeitragVerfasst: Di 07.06.05 11:01 
Ich habe euch vorher ja schonmal mit meinem Datumsprog genervet, und hoffe, ihr könnt mir nochmal helfen. Ich bin Jetz mit dem Prog fas fertig, und muss nur noch den Wochentag von einem Taum bestimmen.

Die Delphifunktionen wie Dayof the week usw darf ich nicht nutzen. Die berechnung soll mit dem Zellerschen Algoritmus gelöst werden.

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
procedure TFrmDatum.WochentagBestimmen(WochenTag : String);
var ergebnis, day, month, year, century: integer;
begin
  day :=  StrToInt(copy(WochenTag,1,2));
  month := StrToInt(copy(WochenTag,4,2));
  century := StrToInt(copy(WochenTag,7,2));
  year := StrToInt(copy(WochenTag,9,2));
  ergebnis := (day +((13*month-1)div 5) + year + (year div 4) + (century div 4)
                -2*century) mod 7;
  if ergebnis < 0 then
  ergebnis := ergebnis * -1;

  ShowMessage('Das Datum ' +WochenTag+ ' liegt auf einem' + IntToStr(ergebnis));
end;


Weenn ich als Datum (in WochenTag enthalten) den 03.01.2005 übergebe, dann kommt da ein Mittwoch anstatt einem Montag raus.

Warum kommt bei der Variable meist ein negativer wert raus?? kann ja eigentlich auch net sein, oder??

Wäre gut, wenn mir jemand helfen könnte.
Narses
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Administrator
Beiträge: 10185
Erhaltene Danke: 1261

W11x64
TP3 .. D7pro .. D10.2CE
BeitragVerfasst: Di 07.06.05 12:01 
Moin!

Lass dir doch mal anzeigen (z.b. ShowMessage), was in den Variablen "angekommen" ist:
ausblenden Delphi-Quelltext
1:
2:
3:
4:
  day :=  StrToInt(copy(WochenTag,1,2));
  month := StrToInt(copy(WochenTag,4,2));
  century := StrToInt(copy(WochenTag,7,2));
  year := StrToInt(copy(WochenTag,9,2));


cu
Narses
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 07.06.05 12:13 
Änder Zeile 11 mal in Ergebnis := Ergebnis + 7.

Ansonsten prüf wirklich mal die Teilergebnisse und evtl. auch die Klammernsetzung.

_________________
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.
The_Tremendous Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 47

Win XP SP2
Borland Delphi 6
BeitragVerfasst: Di 07.06.05 13:47 
Also alle variablen ausser month sind komischerweise leer, nur month hat den richtigen Teil des Strings drinne. Keine Ahnung warum.
smiegel
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 992
Erhaltene Danke: 1

WIN 7
D7 Prof., C#, RAD XE Prof.
BeitragVerfasst: Di 07.06.05 13:51 
Hallo,

@The_Tremendous:

Du hast den Algorithmus falsch implementiert!!! Richtiger müsste er wie folgt lauten:
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
procedure TFrmDatum.WochentagBestimmen(aDatum:String);  
const WoTagStr:array[0..6] of String=('So.', 'Mo.', 'Di.', 'Mi.', 'Do.', 'Fr.', 'Sa.');
var WoTag, day, month, year, century:Integer;  
begin  
  day:=StrToInt(Copy(aDatum, 1, 2));  
  month:=StrToInt(Copy(aDatum, 4, 2));  
  year:=StrToInt(copy(aDatum, 7, 4));  
  // Monatskennzahlen: 1 = März .. 10 = Dezember; Jan. und Feb. sind 11. und 12. Monat des Vorjahres
  if (month<=2) then
  begin
    Inc(month, 10);
    Dec(year, 1);
  end else Dec(month, 2);
  // Jahrhundert = Jahr div 100; Jahr entspr. Jahr mod 100
  century:=year div 100;
  year:=year mod 100;
  WoTag:=(day+((13*month-1) div 5)+year+(year div 4)+(century div 4)-2*century) mod 7;  
  if (WoTag<0) then Inc(WoTag, 7);
 
  ShowMessage('Das Datum %s liegt auf einem %s.', [aDatum, WoTagStr[WoTag]]);  
end;

_________________
Gruß Smiegel
Ich weiß, daß ich nichts weiß, aber ich weiß mehr als die, die nicht wissen, daß sie nichts wissen. (Sokrates)
The_Tremendous Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 47

Win XP SP2
Borland Delphi 6
BeitragVerfasst: Di 07.06.05 14:52 
@ Smiegel: Danke für deine Hilfe. Jetzt funzt es!! :lol:

Falls Jemand mal das gleiche Problem haben sollte, ist hier eine weitere funktionierende Lösung:

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:
procedure TFrmDatum.WochentagBestimmen(WochenTag : String);
var ergebnis, day, month, year, century: integer;
    ausgabe : string;
begin
  // Auslesen des Wochentages, des Monats, des Jahrhunderts und des Jahres
  // aus dem vorgegebenem Datum
  day :=  StrToInt(copy(WochenTag,1,2));
  month := StrToInt(copy(WochenTag,4,2));
  century := StrToInt(copy(WochenTag,7,2));
  year := StrToInt(copy(WochenTag,9,2));
  // Anpassung der Monate, da laut Zellerschem Algorithmus die Monate Januar und
  // Februar zum Vorjahr gehören
  if month < 3 then Dec(year);
    month := Succ((month + 9) mod 12);
  // Zellerscher Algorithmus
  ergebnis := (day +((13*month-1)div 5) + year + (year div 4) + (century div 4)
                -2*century) mod 7;
  // Erhöhen des Ergebnisses um 7 Tage, wenn ergebnis negativ ist
  if ergebnis < 0 then
    Inc(ergebnis, 7);
  // Belegung der Tage zu dem jeweiligen Ergebnis
  case ergebnis of
      0:  ausgabe := 'Sonntag';
      1:  ausgabe := 'Montag';
      2:  ausgabe := 'Dienstag';
      3:  ausgabe := 'Mittwoch';
      4:  ausgabe := 'Donnerstag';
      5:  ausgabe := 'Freitag';
      6:  ausgabe := 'Samstag';
   end;
  ShowMessage('Das Datum ' +WochenTag+ ' liegt auf einem ' + ausgabe);
end;
hibbert
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 1007

WinServer2003, Win XP, Linux
D6 Pers, D05
BeitragVerfasst: Do 03.11.05 16:49 
Hi,
ich habe mal ne Frage zu den soweit funktionierenden Funktionen:

Gebe ich das heutige Datum (03.11.2005) ein, dann bekomme ich den Wert Donnerstag.
Gebe ich aber den Wert 12.10.1492 ein, so erhalte ich den Wert Mittwoch, obwohl es eigentlich ein Freitag ist bzw war.

Warum liefern alle Funktionen ein anderen Wochentag zurück ?

_________________
I kunnen väl svara endast ja eller nej
Om i viljen eller nej
smiegel
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 992
Erhaltene Danke: 1

WIN 7
D7 Prof., C#, RAD XE Prof.
BeitragVerfasst: Fr 04.11.05 08:33 
Hallo,

die von mir gepostete Routine ermittelt für den 3.11.2005 korrekt den Freitag als Wochentag.

Inwiefern die Routine für Daten vor der Gregorianischen Kalenderreform (04.10. / 15.10.1582) gilt, kann ich nicht sagen, da ich die Grundbedingungen das Zellerschen-Algorithmus nicht kenne.

_________________
Gruß Smiegel
Ich weiß, daß ich nichts weiß, aber ich weiß mehr als die, die nicht wissen, daß sie nichts wissen. (Sokrates)
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: Fr 04.11.05 12:19 
Der Zellersche Algo gilt ohne eine gewisse Erweiterung NICHT für Daten vor dem 15.10.1582 ... D.h. konkret, dass Du auf Datum <15.10.1582 testen musst und dann einen gewissen Offset Addieren musst, damit der korrekte Wochentag rauskommt.

_________________
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.
hibbert
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 1007

WinServer2003, Win XP, Linux
D6 Pers, D05
BeitragVerfasst: Fr 04.11.05 16:26 
user profile iconsmiegel hat folgendes geschrieben:
Hallo,

die von mir gepostete Routine ermittelt für den 3.11.2005 korrekt den Freitag als Wochentag.

mhh der 3.11.05 ist / war aber ein Donnerstag :wink:

mhh ok

user profile iconBenBE hat folgendes geschrieben:
Der Zellersche Algo gilt ohne eine gewisse Erweiterung NICHT für Daten vor dem 15.10.1582 ...

OK, mit dieser angabe kann ich leben.

danke.

hibbert

_________________
I kunnen väl svara endast ja eller nej
Om i viljen eller nej