If mit mehreren Möglichkeiten abkürzen?
-
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; } }
-
Michael E. schrieb:
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)))<krümelkack>
Das ist nicht portabel, da der C++ Standard kein ASCII erzwingt. Ich glaube, die einzige Garantie, die da gemacht wird, ist, dass '0'+x mit 0<=x<=9 die richtige Dezimalziffer gibt, also dass '0', '1', '2', ..., '9' aufeinanderfolgende char-Werte sind.
</krümelkack>Ich würd's wahrscheinlich mit switch machen. Das ist einfach am leserlichsten. Ich möchte trotzdem eine Lösung zeigen, die wir hier noch nicht gesehen haben:
template<class T, usnigned N, class U> inline bool binary_search(const T(&oa)[N], U x) { return std::binary_search(oa+0,oa+N,x); } ... if (month==2) return 28+isLeapYear(year); static const int m30[] = {4,6,9,11}; return binary_search(m30,month) ? 30 : 31;@Vicious Falcon: Du weißt, dass return keine Funktion ist und die Klammern um den Ausdruck überflüssig sind, ja?