标签:als iostream 索引 else foo ati getch 返回 tab
1 #include <iostream> 2 #include <assert.h> 3 4 5 //判断是否闰年 6 bool IsLeapYear(unsigned int year) 7 { 8 if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) 9 { 10 return true; 11 } 12 else 13 { 14 return false; 15 } 16 } 17 18 //根据一年中的第几天(0起始),返回月份(1起始),并且置天数为其在本月的索引(0起始) 19 unsigned int GetMonthByDay(unsigned int &day, bool leap) 20 { 21 assert(day < (leap ? 366 : 365)); 22 static unsigned int monthtable[] = { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; 23 monthtable[1] = leap ? 29 : 28; 24 25 unsigned int month = 0; 26 unsigned int count = 0; 27 unsigned int n = day; 28 29 do 30 { 31 n = day - count; 32 count += monthtable[month++]; 33 } while (day >= count); 34 day = n; 35 return month; 36 } 37 38 //距离1900年1月1日的天数(0起始),返回年,并且置天数为其在本年的索引(0起始) 39 unsigned int GetYearByDay(unsigned int &day) 40 { 41 unsigned int Y = day / 365; 42 int D = day % 365; 43 int YY = Y - 1; 44 D -= YY / 4; 45 D += YY / 100; 46 D -= (YY + 300) / 400; 47 while (D < 0) 48 { 49 Y--; 50 D += IsLeapYear(Y + 1900) ? 366 : 365; 51 } 52 day = D; 53 return Y + 1900; 54 } 55 56 //距离1900年1月1日的天数(0起始),计算这一天的日期(年,月,日)(1起始)。 57 void foo(unsigned int &day, unsigned int &year, unsigned int &month) 58 { 59 year = GetYearByDay(day); 60 month = GetMonthByDay(day, IsLeapYear(year)); 61 day += 1; 62 } 63 64 int main() 65 { 66 unsigned int day = 42696; 67 unsigned int year = 0; 68 unsigned int month = 0; 69 foo(day, year, month); 70 std::cout << year << "/" << month << "/" << day << std::endl; 71 getchar(); 72 return 0; 73 }
标签:als iostream 索引 else foo ati getch 返回 tab
原文地址:http://www.cnblogs.com/csqtech/p/6142186.html