第五周 静态成员应用 23
来源:互联网 发布:工程进度表制作软件 编辑:程序博客网 时间:2024/06/03 22:10
- /*
- *Copyright (c) 2015,烟台大学计算机学院
- *All rights reserved.
- *文件名称:text.cpp
- *作者:李德彪
- *完成日期:2015年4月2日
- *版本号:v1.0
- *
- *问题描述:设计含有静态数据成员和成员函数的time类,
- 静态数据成员是类中所有的对象共有的数据,
- 在下面的设计中,时钟要采取12小时制,还是要采用24小时制,
- 显示时,不足两位的数字前是否前导0,都是影响全局变量的设置,
- 适合做为类中的静态数据成员。
- *输入描述:无
- *程序输出:输出是否前导0以及是否切换时间制
- */
- #include <iostream>
- #include<cstdlib>
- 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 );
- static void change24();
- static void changefrom0();
- 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 _hour;
- if(is_24)
- _hour=hour;
- else
- _hour=hour%12;
- if (_hour<10&&from0) cout<<'0';
- cout<<_hour<<':';
- if(minute<10&&from0) cout<<'0';
- cout<<minute<<':';
- if(sec<10&&from0) cout<<'0';
- cout<<sec;
- if(!is_24)
- if (hour>12)
- cout<<" pm";
- else
- cout<<" am";
- cout<<endl;
- }
- void Time::add_seconds(int n)
- {
- sec=sec+n;
- if(sec>=60)
- {
- sec=sec%60;
- minute=minute+sec/60;
- if(minute>=60)
- {
- minute=minute%60;
- hour=hour+minute/60;
- }
- if(hour>=24)
- hour=hour%24;
- }
- }
- void Time::add_minutes(int n)
- {
- minute=minute+n;
- if(minute>=60)
- {
- minute=minute%60;
- hour=hour+minute/60;
- }
- if(hour>=24)
- hour=hour%24;
- }
- void Time::add_hours(int n)
- {
- hour=hour+n;
- if(hour>=24)
- hour=hour%24;
- }
- void Time::change24()
- {
- is_24=!is_24;
- }
- void Time::changefrom0()
- {
- from0=!from0;
- }
- int main()
- {
- Time t1(23,14,25),t2(8,45,6);
- cout<<"24时制, 不前导:"<<endl;
- cout<<" t1是:";
- t1.show_time();
- cout<<" t2是:";
- t2.show_time();
- t1.add_hours(10);
- t2.add_hours(10);
- Time::changefrom0();
- cout<<"10小时后, 切换是否前导:"<<endl;
- cout<<" t1是:";
- t1.show_time();
- cout<<" t2是:";
- t2.show_time();
- t1.change24();
- cout<<"换一种制式:"<<endl;
- cout<<" t1是:";
- t1.show_time();
- cout<<" t2是:";
- t2.show_time();
- system("pause");
- return 0;
- }
0 0
- 第五周项目23-静态成员应用
- 第五周 静态成员应用 23
- 第五周项目四:静态成员应用
- 第五周项目4 静态成员应用
- 第五周 【项目4-静态成员应用】
- 第五周项目四:静态成员应用
- 第五周项目4-静态成员应用
- 第五周 项目四-静态成员应用
- 第五周项目四静态成员应用
- 第五周项目四 静态成员应用
- 第五周项目4-静态成员应用
- 第五周 项目4-静态成员应用
- 第五周项目4-静态成员应用
- 第五周 【项目4-静态成员应用】
- 第五周项目四---静态成员应用
- 第五周项目4:静态成员应用
- 第五周项目四静态成员应用
- 第五周项目4 静态成员应用
- 游戏中的角色类1
- SQL tuning 类 的面试题
- Android简化获取控件
- 第五周第四项目——长方柱类
- 关于前端开发的思考
- 第五周 静态成员应用 23
- 前端开发学习笔记01(大杂烩)
- jquery 1.11 attr("disabled" 无效
- Coverity代码静态检测工具介绍
- [BZOJ1070][SCOI2007]修车(费用流)
- Codeforces Round #251(Div. 2) 439A. Devu, the Singer and Churu, the Joker 水题
- Tixml主页上给的一个遍历方法
- 118. Pascal's Triangle
- 项目2-带武器的游戏角色