判断一个时期时间是否正确,输出下一秒的时间
2015-10-31 00:00
330 查看
摘要: 判断一个时期时间是否正确,输出下一秒的时间
判断一个时期时间是否正确,输出下一秒的时间
运行结果
判断一个时期时间是否正确,输出下一秒的时间
#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; }
运行结果
相关文章推荐
- CentOS7 网络设置
- Update java version in windows 7
- OC学习之--字符串类&数组类&值对象
- tiny4412 uboot
- Swift-导航控制器UINavigationController的用法示例
- 报错-Unknown class in Interface Builder file
- 内存池设计与实现
- 怎样在Linux环境编译支持C11
- java基本数据类型
- 【Unity】用Unity加载电脑C盘中的一张图片
- 下载文件中文乱码问题+路径斜杠问题
- 认识与入门 Markdown,Markdown教程
- Java 类的实例变量初始化的过程 静态块、非静态块、构造函数的加载顺序
- Android View和ViewGroup
- 解决android中Layout文件下的xml文件配好后,R类中不能自动生成相应代码
- No resource found that matches the given name 'Theme.AppCompat.Light 的完美解决方案
- Android常见的按钮监听器实现方式
- Navicat for MySQL连接MYSQL出错,错误代码1045的解决方法
- eclipse 快捷键
- Android LayoutInflater.inflate(int resource, ViewGroup root, boolean attachToRoot)的参数理解