第六周项目4-静态成员应用
2016-04-07 19:41
274 查看
问题及代码:
运行结果:
#include<iostream> #include<cstdio> using namespace std; class time { public: time(int=0,int=0,int=0); void show_time();//根据is_24和from0,输出合适形式-20:23:5/8:23:5pm/08:23:05pm void add_seconds(int x);//增加n秒钟 void add_minutes(int y);//增加n分钟 void add_hours(int z);//增加n小时 static void change24();//改变静态成员is_24,在12和24时制之间转换 static void changefrom0();//改变静态成员from0,切换是否前导0 private: static bool is_24;//为true时,24小时制,如20:23:5;为false,12小时制,显示为8:23:5pm static bool from0;//为true 时,前导0,8:23:5显示为08:23:05 int hour; int minute; int sec; }; bool time::is_24=true; bool time::from0=false; time::time(int h,int m,int s) { hour=h; minute=m; sec=s; } void time::show_time() { if(from0) { if(hour<10) cout<<"0"<<hour<<":"; else cout<<hour<<":"; if(minute<10) cout<<"0"<<minute<<":"; else cout<<minute<<":"; if(sec<10) cout<<"0"<<sec<<endl; else cout<<sec<<endl; } } void time::add_hours(int z) { hour+=z; if(hour>=24) { hour=hour%24; } } void time::add_minutes(int y) { minute+=y; if(minute>=60) { hour+=minute/60; minute=minute%60; } } void time::add_seconds(int x) { sec+=x; if(sec>=60) { minute+=sec/60; sec=sec%60; } } void time::changefrom0() { is_24=!is_24; } void time::change24() { from0=!from0; } int main() { time t1(23,14,25); time t2(8,45,6); cout<<"24时制,不前导0:\n"; t1.show_time(); t2.show_time(); cout<<"10小时后,切换是否前导0:\n"; t1.add_hours(10); t2.add_hours(10); time::change24(); cout<<"t1: "; t1.show_time(); cout<<"t2: "; t2.show_time(); cout<<"换一种时制:\n"; time::changefrom0(); cout<<"t1: "; t1.show_time(); cout<<"t2: "; t2.show_time(); }
运行结果:
相关文章推荐
- 关于二分查找算法(也叫折半查找)
- 驱动程序直接编译进内核
- 问卷调查
- 字符串的全排列和组合算法
- 安卓5.0新特性
- 使用移位运算和加减法实现乘除法
- Largest Rectangle in Histogram
- 深入理解 JavaScript 中的 replace 方法
- 本周学习心得
- 虚拟机VM tools的安装实现虚拟机中内容能够拷贝到本机上
- lintcode: Subsets II
- HistCite学习
- 字符串匹配:KMP算法的实现以及理解
- 学习进度表
- 第七周总结
- 结对编程黄金点游戏
- 积极暗示不仅励志而且助健康!
- Codeforces Round #346 (Div. 2) A. Round House
- 创建CALayer
- System Verilog学习笔记(一)