第五周 静态成员应用 23

来源:互联网 发布:工程进度表制作软件 编辑:程序博客网 时间:2024/06/03 22:10
  1. /* 
  2. *Copyright (c) 2015,烟台大学计算机学院 
  3. *All rights reserved. 
  4. *文件名称:text.cpp 
  5. *作者:李德彪
  6. *完成日期:2015年4月2日 
  7. *版本号:v1.0 
  8. * 
  9. *问题描述:设计含有静态数据成员和成员函数的time类, 
  10.             静态数据成员是类中所有的对象共有的数据, 
  11.             在下面的设计中,时钟要采取12小时制,还是要采用24小时制, 
  12.             显示时,不足两位的数字前是否前导0,都是影响全局变量的设置, 
  13.             适合做为类中的静态数据成员。 
  14. *输入描述:无 
  15. *程序输出:输出是否前导0以及是否切换时间制 
  16. */  
  17. #include <iostream>   
  18. #include<cstdlib>   
  19. using namespace std;  
  20. class Time  
  21. {  
  22.   public:  
  23.       Time(int=0,int=0,int=0);  
  24.       void show_time();  
  25.       void add_seconds(int );  
  26.       void add_minutes(int );  
  27.       void add_hours(int );  
  28.       static void change24();  
  29.       static void changefrom0();  
  30.   private:  
  31.     static bool is_24;  
  32.     static bool from0;  
  33.     int hour;  
  34.     int minute;  
  35.     int sec;  
  36. };  
  37.  bool Time::is_24=true;  
  38.  bool Time::from0=false;  
  39. Time::Time(int _hour,int _minute,int _sec)  
  40. {  
  41.     hour=_hour;  
  42.     minute=_minute;  
  43.     sec=_sec;  
  44. }  
  45. void Time::show_time()  
  46. {  
  47.   
  48. int _hour;  
  49.     if(is_24)  
  50.       _hour=hour;  
  51.     else  
  52.      _hour=hour%12;  
  53.     if (_hour<10&&from0) cout<<'0';  
  54.     cout<<_hour<<':';  
  55.     if(minute<10&&from0) cout<<'0';  
  56.     cout<<minute<<':';  
  57.     if(sec<10&&from0) cout<<'0';  
  58.     cout<<sec;  
  59.     if(!is_24)  
  60.   
  61.         if (hour>12)  
  62.           cout<<" pm";  
  63.         else  
  64.           cout<<" am";  
  65.     cout<<endl;  
  66. }  
  67.   
  68.   
  69. void Time::add_seconds(int n)  
  70. {  
  71.     sec=sec+n;  
  72.     if(sec>=60)  
  73.     {  
  74.         sec=sec%60;  
  75.         minute=minute+sec/60;  
  76.         if(minute>=60)  
  77.         {  
  78.            minute=minute%60;  
  79.            hour=hour+minute/60;  
  80.         }  
  81.         if(hour>=24)  
  82.             hour=hour%24;  
  83.     }  
  84. }  
  85. void Time::add_minutes(int n)  
  86. {  
  87.     minute=minute+n;  
  88.       if(minute>=60)  
  89.         {  
  90.            minute=minute%60;  
  91.            hour=hour+minute/60;  
  92.         }  
  93.         if(hour>=24)  
  94.             hour=hour%24;  
  95.   
  96. }  
  97. void Time::add_hours(int n)  
  98. {  
  99.     hour=hour+n;  
  100.     if(hour>=24)  
  101.             hour=hour%24;  
  102. }  
  103. void Time::change24()  
  104. {  
  105.     is_24=!is_24;  
  106. }  
  107. void Time::changefrom0()  
  108. {  
  109.     from0=!from0;  
  110. }  
  111. int main()  
  112. {  
  113.     Time t1(23,14,25),t2(8,45,6);  
  114.     cout<<"24时制, 不前导:"<<endl;  
  115.     cout<<"    t1是:";  
  116.     t1.show_time();  
  117.     cout<<"    t2是:";  
  118.     t2.show_time();  
  119.     t1.add_hours(10);  
  120.     t2.add_hours(10);  
  121.     Time::changefrom0();   
  122.     cout<<"10小时后, 切换是否前导:"<<endl;  
  123.     cout<<"    t1是:";  
  124.     t1.show_time();  
  125.     cout<<"    t2是:";  
  126.     t2.show_time();  
  127.     t1.change24();  
  128.     cout<<"换一种制式:"<<endl;  
  129.     cout<<"    t1是:";  
  130.     t1.show_time();  
  131.     cout<<"    t2是:";  
  132.     t2.show_time();  
  133.     system("pause");  
  134.     return 0;  
  135. }  
0 0
原创粉丝点击