2013-07-28 21:46:31
输入年、月、日、时、分、秒,输出下一秒的年、月、日、时、分、秒。
注意点:
- 加上输入合法性检查,在下面的代码中没有加;
- 注意闰年的情况;
代码:
1 #include <iostream> 2 using namespace std; 3 4 size_t MonthTable[2][12] = { {31,29,31,30, 31,30,31,31, 30,31,30,31}, 5 {31,29831,30, 31,30,31,31, 30,31,30,31} }; 6 7 typedef struct data 8 { 9 size_t year; 10 size_t month; 11 size_t day; 12 size_t hour; 13 size_t minute; 14 size_t second; 15 }Data; 16 17 bool IsLeapYear(const size_t year) 18 { 19 return ( (year % 4 == 0 && year % 100 != 0 ) || (year % 400 == 0 ) ); 20 } 21 22 void GetNextDay(Data ¤tDate,Data &nextDate) 23 { 24 size_t maxDayInMonth = MonthTable[ ( IsLeapYear(currentDate.year) )][currentDate.month - 1]; 25 26 27 nextDate.day = (currentDate.day + 1) % maxDayInMonth; 28 29 if (0 == nextDate.day) 30 { 31 nextDate.day = maxDayInMonth; 32 } 33 34 if (1 == nextDate.day) 35 { 36 nextDate.month = (currentDate.month + 1) % 12; 37 38 if (0 == nextDate.month) 39 { 40 nextDate.month = 12; 41 } 42 43 if (1 == nextDate.month) 44 { 45 nextDate.year += 1; 46 } 47 } 48 } 49 50 void DateCopy(const Data ¤tDate,Data &nextDate) 51 { 52 nextDate.year = currentDate.year; 53 nextDate.month = currentDate.month; 54 nextDate.day = currentDate.day; 55 nextDate.hour = currentDate.hour; 56 nextDate.minute = currentDate.minute; 57 nextDate.second = currentDate.second; 58 } 59 60 void GetNextSecond(Data ¤tDate,Data &nextDate) 61 { 62 DateCopy(currentDate,nextDate); 63 nextDate.second = (currentDate.second + 1) % 60; 64 if (0 == nextDate.second) 65 { 66 nextDate.minute = (currentDate.minute + 1) % 60; 67 if (0 == nextDate.minute) 68 { 69 nextDate.hour = (currentDate.hour + 1) % 24; 70 71 if (0 == nextDate.hour) 72 { 73 GetNextDay(currentDate,nextDate); 74 } 75 } 76 } 77 } 78 79 void TestDriver() 80 { 81 Data currentDate; 82 Data nextDate; 83 84 cout<<"please enter a data(year month day hour minute second),end with ctrl+z"<<endl; 85 86 while (cin>>currentDate.year>>currentDate.month>>currentDate.day 87 >>currentDate.hour>>currentDate.minute>>currentDate.second) 88 { 89 cout<<"the date of the current second is: "<<currentDate.year<<" "<<currentDate.month<<" "<<currentDate.day<<" "<< 90 currentDate.hour<<" "<<currentDate.minute<<" "<<currentDate.second<<endl; 91 GetNextSecond(currentDate,nextDate); 92 cout<<"the date of the next second is: "<<nextDate.year<<" "<<nextDate.month<<" "<<nextDate.day<<" "<< 93 nextDate.hour<<" "<<nextDate.minute<<" "<<nextDate.second<<endl; 94 cout<<"please enter a data(year month day hour minute second),end with ctrl+z"<<endl; 95 } 96 } 97 98 int main() 99 { 100 TestDriver(); 101 return 0; 102 }
运行结果:
please enter a data(year month day hour minute second),end with ctrl+z 2007 12 31 23 59 59 the date of the current second is: 2007 12 31 23 59 59 the date of the next second is: 2008 1 1 0 0 0 please enter a data(year month day hour minute second),end with ctrl+z 2007 12 30 23 59 59 the date of the current second is: 2007 12 30 23 59 59 the date of the next second is: 2007 12 31 0 0 0 please enter a data(year month day hour minute second),end with ctrl+z 2007 12 31 23 59 56 the date of the current second is: 2007 12 31 23 59 56 the date of the next second is: 2007 12 31 23 59 57 please enter a data(year month day hour minute second),end with ctrl+z ^Z 请按任意键继续. . .