一个关于时间的静态变量的应用
2016-04-07 19:58
295 查看
/* Copyright (c) 2016
* All rights reserved
* 文件名称:3.cpp
* 作者:刘丽
* 完成日期:2016年 4 月 7日
* 版本号: v1.0
*
* 问题描述:一个关于时间类的静态成员应用
*/
* All rights reserved
* 文件名称:3.cpp
* 作者:刘丽
* 完成日期:2016年 4 月 7日
* 版本号: v1.0
*
* 问题描述:一个关于时间类的静态成员应用
*/
#include <iostream> #include<string> using namespace std; class Time { public: Time(int=0,int =0,int =0); void show_time (); void add_seconds(int ); void add_minutes(int); void add_hours(int n); static void change24(); static void changefrom(); private: static bool is_24; static bool from0; int hour; int minute; int sec; }; bool Time::is_24=true; bool Time::from0=false; Time::Time(int hour,int minute,int sec):hour(hour),minute(minute),sec(sec) { } void Time::show_time () { int t; if(from0==true) { if(is_24==true) { if(hour<10) cout<<"0"; cout<<hour<<":"; if(minute<10) cout<<"0"; cout<<minute<<":"; if(sec<10) cout<<"0"; cout<<sec<<endl; } else { if(hour>12) { t=hour%12; if(t<10) cout<<"0"; cout<<t<<":"; if(minute<10) cout<<"0"; cout<<minute<<":"; if(sec<10) cout<<"0"; cout<<sec<<endl; } else { if(hour<10) cout<<"0"; cout<<hour<<":"; if(minute<10) cout<<"0"; cout<<minute<<":"; if(sec<10) cout<<"0"; cout<<sec<<endl; } } } else //from0==false { if(is_24==true) cout<<hour<<":"<<minute<<":"<<sec<<endl; else if(hour>12) cout<<hour%12<<":"<<minute<<":"<<sec<<" pm"<<endl; else cout<<hour<<":"<<minute<<":"<<sec<<" am"<<endl; } } void Time::add_seconds(int n) { if(sec+n>60) { sec=(sec+n)%60; add_minutes((sec+n)/60); } else sec+=n; } void Time::add_minutes(int n) { if(minute+n>60) { minute=(minute+n)%60; add_hours((minute+n)/60); } else minute+=n; } void Time::add_hours(int n) { hour+=n; if(hour>=24) hour=hour%24; } void Time::change24() { if(is_24==true) is_24=false; else is_24=true; } void Time::changefrom() { if(from0==true) from0=false; else from0=true; } int main() { Time t1(23,14,25),t2(8,45,6); cout<<"24时制,不前导0:"<<endl; cout<<"t1是:"; t1.show_time(); cout<<"t2是:"; t2.show_time(); cout<<"10小时后,切换是否前导0:"<<endl; t1.add_hours(10); t1.changefrom(); t1.show_time(); t2.add_hours(10); t2.show_time(); cout<<"换一种形式"<<endl; t1.change24(); t1.show_time(); t2.show_time(); return 0; }
相关文章推荐
- 使用C++实现JNI接口需要注意的事项
- Android Native 绘图方法
- 关于指针的一些事情
- c++ primer 第五版 笔记前言
- share_ptr的几个注意点
- C#中struct和class的区别详解
- Lua中调用C++函数示例
- Lua教程(一):在C++中嵌入Lua脚本
- Lua教程(二):C++和Lua相互传递数据示例
- VBS ArrayList Class vbs中的数组类
- 大家看了就明白了css样式中类class与标识id选择符的区别小结
- C++联合体转换成C#结构的实现方法
- Erlang初学:Erlang的一些特点和个人理解总结
- C++高级程序员成长之路
- C++编写简单的打靶游戏
- C++ 自定义控件的移植问题
- C++变位词问题分析
- C/C++数据对齐详细解析
- C++基于栈实现铁轨问题
- C++中引用的使用总结