第七周C++实验报告(1)

来源:互联网 发布:知乎数据挖掘考研 编辑:程序博客网 时间:2024/06/15 17:38
  1. #include <iostream>       
  2. using namespace std;   
  3. class Time  
  4. {  
  5. public:  
  6.     Time(int=0,int=0,int=0);  
  7.     void show_time( ); //根据is_24和from0,输出适合形式-20:23:5/8:23:5 pm/08:23:05 pm  
  8.     void add_seconds(int); //增加n秒钟  
  9.     void add_minutes(int); //增加n分钟    
  10.     void add_hours(int); //增加n小时    
  11.     static void change24();  //改变静态成员is_24,在12和24时制之间转换  
  12.     static void changefrom0();   //改变静态成员from0,切换是否前导0  
  13. private:  
  14.     static bool is_24; //为true时,24小时制,如20:23:5;为flase,12小时制,显示为8:23:5 pm   
  15.     static bool from0; //为true时,前导0,8:23:5显示为08:23:05  
  16.     int hour;  
  17.     int minute;  
  18.     int sec;  
  19. };  
  20.   
  21. //下面写出静态成员的初始化及各成员函数的成员  
  22. bool Time::is_24=true;  
  23. bool Time::from0=false;  
  24.   
  25. Time::Time(int h,int m,int s):hour(h),minute(m),sec(s){}  
  26.   
  27. void Time::show_time( )        
  28. {  
  29.     //显示时  
  30.     int h=(is_24)?hour:(hour%12);  
  31.     if(from0 && h<10) cout<<'0';  
  32.     cout<<h<<":";  
  33.     //显示分  
  34.     if(from0 && minute<10) cout<<'0';  
  35.     cout<<minute<<":";  
  36.     //显示秒  
  37.     if(from0 && sec<10) cout<<'0';  
  38.     cout<<sec;  
  39.     //确定显示am/pm  
  40.     if(!is_24)  
  41.         if(hour>12)  
  42.             cout<<"  pm";  
  43.         else  
  44.             cout<<"  am";  
  45.         cout<<endl;  
  46. }  
  47.   
  48. void Time::change24()  
  49. {  
  50.     is_24=!is_24;  
  51. }  
  52.   
  53. void Time::changefrom0()  
  54. {  
  55.     from0=!from0;  
  56. }  
  57.   
  58. void Time::add_hours(int h) //增加n小时      
  59. {    
  60.     hour+=h;      
  61.     if (hour>23)      
  62.         hour%=24;    
  63. }   
  64.   
  65. void Time::add_minutes(int m) //增加n分钟       
  66. {    
  67.     minute+=m;      
  68.     if (minute>59)       //参见add_seconds()中的注释       
  69.     {      
  70.         add_hours(minute/60);      
  71.         minute%=60;      
  72.     }      
  73. }   
  74.   
  75. void Time::add_seconds(int s) //增加n秒钟     
  76. {    
  77.     sec+=s;         //直接加上去。此操作可能使sec超出取值范围,将在下面处理,我们只要保证此函数执行完sec的取值正确即可       
  78.     if (sec>59)      //注意:if中的两条语句不能交换顺序       
  79.     {      
  80.         add_minutes(sec/60);    //增加sec/60分钟       
  81.         sec%=60;                //秒数应该是sec%=60       
  82.     }      
  83. }    
  84.   
  85. int main( )     
  86. {  
  87.     Time t1(23,14,25),t2(8,45,6);   
  88.     cout<<"24时制, 不前导0:"<<endl;  
  89.     cout<<"    t1是:";  
  90.     t1.show_time();  
  91.     cout<<"    t2是:";  
  92.     t2.show_time();  
  93.     t1.add_hours(10);  
  94.     t2.add_hours(10);  
  95.     Time::changefrom0(); //注意此处调用静态成员  
  96.     cout<<"10小时后, 切换是否前导0:"<<endl;  
  97.     cout<<    "t1是:";  
  98.     t1.show_time();  
  99.     cout<<    "t2是:";  
  100.     t2.show_time();  
  101.     t1.change24();  
  102.     cout<<"换一种制式:"<<endl;  
  103.     cout<<"    t1是:";  
  104.     t1.show_time();  
  105.     cout<<"    t2是:";  
  106.     t2.show_time();  
  107.     system("pause");  
  108.     return 0;  


原创粉丝点击