编程小练习

来源:互联网 发布:js让一个div显示 编辑:程序博客网 时间:2024/05/17 09:08

  1. *Copyright(c) 2016.烟台大学计算机与控制工程学院 
  2.  
  3. *ALL rights  reserved. 
  4.  
  5. *文件名称:test.cpp 
  6.  
  7. *作者:李落才
  8.  
  9. *完成日期:2016年4月11日
  10.  
  11. *问题描述:设计含有静态数据成员和成员函数的time类。 
  12.  
  13. *程序输出:time的不同表示方法以及加n小时,n分钟,n秒后的时间 
  14.  
  15. */  
  16. #include <iostream>  
  17. using namespace std;  
  18. class Time  
  19. {  
  20. public:  
  21.     Time(int=0,int=0,int=0);  
  22.     void show_time( ); //根据is_24和from0,输出适合形式的时间:23:5/8:23:5 pm/08:23:05 pm  
  23.     void add_seconds(int); //增加n秒钟  
  24.     void add_minutes(int); //增加n分钟  
  25.     void add_hours(int); //增加n小时  
  26.     static void change24();  //改变静态成员is_24,在和时制之间转换  
  27.     static void changefrom0();   //改变静态成员from0,转换是否前导  
  28. private:  
  29.     static bool is_24; //为true时,小时制,如:23:5;为flase,小时制,显示为:23:5 pm  
  30.     static bool from0; //为true时,前导,:23:5显示为:23:05  
  31.     int hour;  
  32.     int minute;  
  33.     int sec;  
  34. };  
  35. bool Time::is_24=true;  
  36. bool Time::from0=false;  
  37. Time::Time(int h,int m,int s): hour(h), minute(m), sec(s){}  
  38. void Time::show_time( )  
  39. {  
  40.     int h=(is_24)?hour:hour%12;  
  41.     //if(is_24)  
  42.     //  h=hour;  
  43.     //else  
  44.     //  h=hour%12;  
  45.     if (h<10&&from0) cout<<'0';  
  46.     cout<<h<<':';  
  47.     if(minute<10&&from0) cout<<'0';  
  48.     cout<<minute<<':';  
  49.     if(sec<10&&from0) cout<<'0';  
  50.     cout<<sec;  
  51.     if(!is_24)  
  52.         cout<<((hour>12)? " pm":" am");  
  53.         //if (hour>12)  
  54.         //  cout<<" pm";  
  55.         //else  
  56.         //  cout<<" am";  
  57.     cout<<endl;  
  58. }  
  59. void Time::add_seconds(int n) //增加n秒钟  
  60. {  
  61.     sec+=n;  
  62.     if (sec>59)  
  63.     {  
  64.         add_minutes(sec/60);  
  65.         sec%=60;  
  66.     }  
  67. }  
  68. void Time::add_minutes(int n) //增加n分钟  
  69. {  
  70.     minute+=n;  
  71.     if (minute>59)  
  72.     {  
  73.         add_hours(minute/60);  
  74.         minute%=60;  
  75.     }  
  76. }  
  77. void Time::add_hours(int n) //增加n小时  
  78. {  
  79.     hour+=n;  
  80.     if (hour>23)  
  81.         hour%=24;  
  82. }  
  83. void Time::change24()  
  84. {  
  85.     is_24=!is_24;  
  86. }  
  87. void Time::changefrom0()  
  88. {  
  89.     from0=!from0;  
  90. }  
  91. int main( )  
  92. {  
  93.     Time t1(23,14,25),t2(8,45,6);  
  94.     cout<<"24时制, 不前导:"<<endl;  
  95.     cout<<"    t1是:";  
  96.     t1.show_time();  
  97.     cout<<"    t2是:";  
  98.     t2.show_time();  
  99.     t1.add_hours(10);  
  100.     t2.add_hours(10);  
  101.     Time::changefrom0(); //注意此处调用静态成员  
  102.     cout<<"10小时后, 切换是否前导:"<<endl;  
  103.     cout<<"    t1是:";  
  104.     t1.show_time();  
  105.     cout<<"    t2是:";  
  106.     t2.show_time();  
  107.     t1.change24();  
  108.     cout<<"换一种制式:"<<endl;  
  109.     cout<<"    t1是:";  
  110.     t1.show_time();  
  111.     cout<<"    t2是:";  
  112.     t2.show_time();  
  113.     return 0;  
  114. }  
0 0