判断一个时期时间是否正确,输出下一秒的时间
#include <stdio.h> #include <stdlib.h> #include <iostream> using namespace std; int year=0; int month=0; int day=0; int hour=0; int minute=0; int second=0; //函数声明 void inputDate(); int dayMonth(int ); int leapyear(int ); void nextScond(); int main() { while(1) { inputDate(); nextScond(); } return 0; } //输入日期,检查日期输入是否正确 void inputDate() { int loop; for(loop=0;loop<3;loop++) { cout<<"请输入年-月-日 时:分:秒"<<endl; cout<<"年:"; cin>>year; cout<<"月:"; cin>>month; cout<<"日:"; cin>>day; cout<<"时:"; cin>>hour; cout<<"分:"; cin>>minute; cout<<"秒:"; cin>>second; if(month<1||month>12) { cout<<"月份输入错误!"<<endl; continue; } else if(day<1||day>dayMonth(month)) { cout<<"日期输入错误!"<<endl; continue; } else if(hour<0||hour>23) { cout<<"小时输入错误!"<<endl; continue; } else if(minute<0||minute>59) { cout<<"分钟输入错误!"<<endl; continue; } else if(second<0||second>59) { cout<<"秒输入错误!"<<endl; continue; } else { break; } } } int dayMonth(int month) { switch(month) { case 1: case 3: case 5: case 7: case 8: case 10: case 12: return 31; case 2: if(0==(year%4 && 0!=year%100)||0== year%400) { return 29; } else return 28; case 4: case 6: case 9: case 11: return 30; } return 1; } //判断是否 int leapyear(int year) { if(0 == (year % 4 && 0 != year % 100) || 0 == year %400) return 1; else return 0; } //下一秒的日期时间 void nextScond() { if(59==second) { minute+=1; second=0; if(60==minute) { hour+=1; minute=0; if(24==hour) { day+=1; hour=0; if(day>dayMonth(month)) { month+=1; day=1; if(13==month) { year+=1; month=1; } } } } } else second+=1; cout<<year<<"-"<<month<<"-"<<day<<" "<<hour<<":"<<minute<<":"<<second; }
运行结果
原文:http://my.oschina.net/lvguidong/blog/524446