If mit mehreren Möglichkeiten abkürzen?
-
Hallo, ich habe eine if-Abfrage wo gefrage wird ob der Monat 31 oder 30 Tage hat (also 1,3,5,7,8,10,12 haben 31 Tage, die anderen 30 (bis auf Februar, den ignorier ich einfach mal wegen Schaltjahr... Unwichtig für meine Frage).
Gibt es eine kürzere Möglichkeit als:if(monat==1||monat==3||monat==5||.....)wie z.B.
if(monat==1,3,5,7,8,10,12)Was ja leider nicht funktioniert?
Gruß
-
Mit switch:
bool IsLeapyear(int year); int DaysPerMonth(int month, int year) { switch(month) { case 1: case 3: case 5: case 7: case 8: case 10: case 12: return(31); case 4: case 6: case 9: case 11: return(30); case 2: return(28+IsLeapyear(year)); } // hier ein assert und 0 zurückgeben, Ausnahme werfen oder Ähnliches }
-
Danke für deine Antwort. Die Möglichkeit mit switch habe ich jedoch nicht in betracht gezogen, da es ja noch länger wird. Mich würde nur Interessieren ob es mit if auch einfacher geht als:
if(monat==1||monat==3||monat==5||.....)Gruß
-
Nein! (Es sei denn du führst da Boolesche Algebra durch, irgendwelche Optimierung halt)
-
Danke, mehr wollte ich nicht wissen

-
Switch ist aber imho in diesem Fall das überschaubarste, sauberste und daher beste Mittel.
Kürzer kannst du es auch schreibenswitch(month) { case 1:case 3:case 5:case 7:case 8:case 10:case 12: return(31); case 4:case 6:case 9:case 11: return(30); case 2: return(28+IsLeapyear(year)); }:xmas2: :xmas1: :xmas2:
-
Stimmt, danke!
-
Oder so:
#define MASK(X) ((X)&6) if (monat == 2) return 28; if (MASK(monat) == 1 || MASK(monat) == 8) return 31; if (MASK(monat) == 0 || MASK(monat) == 12) return 30; /* respektive: */ if (monat == 2) return 28; switch (MASK(monat)) { case 1: case 8: return 31; case 0: case 12: return 30; } #undef MASKWichtig ist dabei den Februar vorher auszuwerten, weil du sonst 30 Tage erhältst.
-
#define C(a) case a: switch(month) { C(1)C(3)C(5)C(8)C(10)C(12) return(31); C(4)C(6)C(9)C(11) return(30); C(2) return(28+IsLeapyear(year)); }
-
Korrektur:
Bei 30 Tagen muss es natürlich heißen 0 und 11.
-
Ist das schon minimal, Paul?
-
Nein, man könnte jetzt auf Anzahl der gesetzten Bits testen, aber keine Ahnung wie.
31 Tage ungerade
30 Tage gerade
-
int wieviele_tage(int monat) { return monat == 2 ? 28 : (30 + ((1 << (monat - 1)) & 2773)); }
-
Hast du dir das eben gerade mal so ausgedacht?^^Ich komm hier gerade auf:
return monat==2 ? 28 : 30 + ((monat ^ monat>>3)&1);Total fehlerhaft

Edit, jetzt müsste es passen!
-
void days(int month, int year) { static int lut[2][24] = { {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}, {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31} }; return lut[(year % 4 == 0) && (year % 400 != 0)][month]; }:p
-
Ich mach auch mit:
"\x1f\x1c\x1f\x1e\x1f\x1e\x1f\x1f\x1e\x1f\x1e\x1f"[month-1]PS: Ich vermisse den WPC.
-
Bashar, jetzt hab ich wieder was gelernt

-
return "OLONONOONONO"[month-1]-48+(month==2&&!(year%4)&&year%400);
-
Die Spaßvögel sollen sich hier mal registrieren, wär gut für die Stimmung

-
OLONONOONONO schrieb:
return "OLONONOONONO"[month-1]-48+(month==2&&!(year%4)&&year%400);Hübsch, aber klappt noch nicht ganz.
"OLONONOONONO"[month-1]-48+(month==2&&(!(year%4)&&year%100||!(year%400)))
-
Ui ui ui, da hat sich ja mal was entwickelt.
Ich fühle mich dennoch genötigt, den _korrekten_ Spießercode abzuliefern.bool IsLeapyear(int year) { if(year<1) return false; if(year%4) return false; if(year<=1582) return true; if(year%100) return true; if(year%400) return false; return true; } int DaysPerMonth(int month, int year) { switch(month) { case 1: case 3: case 5: case 7: case 8: case 10: case 12: return 31; case 4: case 6: case 9: case 11: return 30; case 2: return(28+IsLeapyear(year)); default: return 0; } }