第七周C++实验报告(1)
来源:互联网 发布:知乎数据挖掘考研 编辑:程序博客网 时间:2024/06/15 17:38
- #include <iostream>
- 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 h,int m,int s):hour(h),minute(m),sec(s){}
-
- void Time::show_time( )
- {
-
- int h=(is_24)?hour:(hour%12);
- if(from0 && h<10) cout<<'0';
- cout<<h<<":";
-
- if(from0 && minute<10) cout<<'0';
- cout<<minute<<":";
-
- if(from0 && sec<10) cout<<'0';
- cout<<sec;
-
- if(!is_24)
- if(hour>12)
- cout<<" pm";
- else
- cout<<" am";
- cout<<endl;
- }
-
- void Time::change24()
- {
- is_24=!is_24;
- }
-
- void Time::changefrom0()
- {
- from0=!from0;
- }
-
- void Time::add_hours(int h)
- {
- hour+=h;
- if (hour>23)
- hour%=24;
- }
-
- void Time::add_minutes(int m)
- {
- minute+=m;
- if (minute>59)
- {
- add_hours(minute/60);
- minute%=60;
- }
- }
-
- void Time::add_seconds(int s)
- {
- sec+=s;
- if (sec>59)
- {
- add_minutes(sec/60);
- sec%=60;
- }
- }
-
- 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();
- t1.add_hours(10);
- t2.add_hours(10);
- Time::changefrom0();
- cout<<"10小时后, 切换是否前导0:"<<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;
- }