1#include <iostream>2#include <assert.h>3 4 5 //determine if a leap year6 BOOLIsleapyear (unsignedintYear )7 {8 if(Year%4==0&& Year% -!=0) || Year% -==0)9 {Ten return true; One } A Else - { - return false; the } - } - - //returns the month (1 start) based on the day ordinal of the year (0), and the number of days it is indexed in the month (0 starts) +UnsignedintGetmonthbyday (unsignedint&day,BOOLLeap) - { +ASSERT (Day < leap?366:365)); A StaticUnsignedintMonthtable[] = { to, in, to, -, to, -, to, to, -, to, -, to }; atmonthtable[1] = leap? in: -; - -Unsignedintmonth =0; -UnsignedintCount =0; -Unsignedintn =Day ; - in Do - { ton = day-count; +Count + = monthtable[month++]; -} while(Day >=count); theDay =N; * returnmonth; $ }Panax Notoginseng - //number of days from January 1, 1900 (0 start), return year, and number of days as its index in the year (0 start) theUnsignedintGetyearbyday (unsignedint&Day ) + { AUnsignedintY = day/365; the intD = day%365; + intYY = Y-1; -D-= YY/4; $D + = YY/ -; $D-= (YY + -) / -; - while(D <0) - { they--; -D + = isleapyear (Y +1900) ?366:365;Wuyi } theDay =D; - returnY +1900; Wu } - About //number of days from January 1, 1900 (starting at 0), the date of the day (year, month, day) is calculated (1 start). $ voidFoo (unsignedint&day, unsignedint&year, unsignedint&month) - { -Year =Getyearbyday (day); -month =Getmonthbyday (Day, Isleapyear (year)); ADay + =1; + } the - intMain () $ { theUnsignedintDay =42696; theUnsignedintYear =0; theUnsignedintmonth =0; the foo (day, year, month); -Std::cout << Year <<"/"<< Month <<"/"<< Day <<Std::endl; in GetChar (); the return 0; the}
2. Give the number of days from January 1, 1900 to date