静态成员的应用

来源:互联网 发布:数据透视表显示合计 编辑:程序博客网 时间:2024/06/05 23:50
  1. #include <iostream>  
  2. #include <cstdio>  
  3. using std::cout;  
  4. using std::cin;  
  5. using std::endl;  
  6.   
  7. class Time  
  8. {  
  9. public:  
  10.     Time(int h = 0,int m = 0,int s = 0):hour(h),minute(m),sec(s){};  
  11.     void set_time();//设置时间  
  12.     void show_time();//显示时间  
  13.     void add_seconds(int);//增加秒  
  14.     void add_minutes(int);//增加分钟  
  15.     void add_hours(int);//增加小时  
  16.     void check();//检查时间格式  
  17.     static void change24();//用于切换12小时制和24小时制,ok  
  18.     static void changefrom0();//根据from0切换是否前导0,ok  
  19. private:  
  20.     bool is_time(int ,int ,int);  
  21.     static bool is_24;//为true,24小时制,否则12小时制  
  22.     static bool from0;//为true,前导0,否则,不前导0  
  23.     int hour = 0;  
  24.     int minute = 0;  
  25.     int sec = 0;  
  26. };  
  27. bool Time::is_24 = true;  
  28. bool Time::from0 = false;  
  29.   
  30. void Time::check()  
  31. {  
  32.     if(!is_24 && hour > 11)  
  33.         hour = hour%12;  
  34. }  
  35.   
  36. void Time::change24()  
  37. {  
  38.     int flag;  
  39.     cout << "1:24小时制\n2:12小时制" << endl;  
  40.     cin >> flag;  
  41.     is_24 = (flag == 1 ? true:false);  
  42. }  
  43.   
  44. void Time::changefrom0()  
  45. {  
  46.     int flag;  
  47.     cout << "1:有前导0\n2:无前导0" << endl;  
  48.     cin >> flag;  
  49.     from0 = (flag == 1 ? true:false);  
  50. }  
  51.   
  52. void Time::show_time()  
  53. {  
  54.     if(is_24)  
  55.         if(from0)  
  56.             printf("%02d:%02d:%02d\n",hour,minute,sec);  
  57.         else  
  58.             printf("%d:%d:%d\n",hour,minute,sec);  
  59.     else  
  60.         if(from0)  
  61.             printf("%02d:%02d:%02d\n",hour,minute,sec);  
  62.         else  
  63.             printf("%d:%d:%d\n",hour,minute,sec);  
  64. }  
  65.   
  66. void Time::add_seconds(int s)  
  67. {  
  68.     sec = sec + s;  
  69.     if(sec > 59)  
  70.     {  
  71.         add_minutes(sec/60);  
  72.         sec = sec%60;  
  73.     }  
  74. }  
  75.   
  76. void Time::add_minutes(int m)  
  77. {  
  78.     minute = minute + m;  
  79.     if(minute > 59)  
  80.     {  
  81.         add_hours(minute/60);  
  82.         minute = minute%60;  
  83.     }  
  84. }  
  85.   
  86. void Time::add_hours(int h)  
  87. {  
  88.     hour = hour + h;  
  89.     if(is_24)  
  90.     {  
  91.         if(hour > 23)  
  92.             hour = hour%24;  
  93.     }  
  94.     else  
  95.         if(hour > 11)  
  96.             hour = hour%12;  
  97. }  
  98.   
  99. void Time::set_time()  
  100. {  
  101.     cout << "默认时间格式为24小时制,时间不足两位数字时无前导0" << endl;  
  102.     cout << "请输入时间(格式hh:mm:ss)";  
  103.     char c1,c2;  
  104.     while(1)  
  105.     {  
  106.         cin >> hour >> c1 >> minute >> c2 >> sec;  
  107.         if(c1 != ':' || c2 != ':')  
  108.         {  
  109.             cout << "格式不正确重输" << endl;  
  110.             cin.clear();  
  111.             cin.sync();  
  112.         }  
  113.         else if(!is_time(hour,minute,sec))  
  114.             cout << "时间非法,请重新输入" << endl;  
  115.         else  
  116.             break;  
  117.     }  
  118. }  
  119.   
  120. bool Time::is_time(int h, int m,int s)  
  121. {  
  122.     if(is_24)  
  123.         if(h < 0 || h > 23 || m <0 || m > 60 || s <0 || s > 60)  
  124.             return false;  
  125.         else  
  126.             return true;  
  127.     else  
  128.         if(h < 0 || h > 11 || m <0 || m > 60 || s <0 || s > 60)  
  129.             return false;  
  130.         else  
  131.             return true;  
  132. }  
  133.   
  134.   
  135.   
  136. int main()  
  137. {  
  138.     Time t1;  
  139.     t1.show_time();  
  140.     t1.set_time();  
  141.     t1.show_time();  
  142.     t1.changefrom0();  
  143.     t1.show_time();  
  144.     t1.add_seconds(100);  
  145.     t1.add_minutes(100);  
  146.     t1.add_hours(15);  
  147.     t1.show_time();  
  148.     t1.change24();  
  149.     t1.check();  
  150.     t1.show_time();  
  151.     return 0;  
  152. }  
0 0
原创粉丝点击