第七周 任务1

来源:互联网 发布:python 调用c dll 编辑:程序博客网 时间:2024/05/21 22:43

/* (程序头部注释开始)
* 程序的版权和版本声明部分
* Copyright (c) 2011, 烟台大学计算机学院学生 
* All rights reserved.
* 文件名称:                              
* 作    者: 韩云龙                   
* 完成日期: 2012 年 4 月 3日
* 版 本 号: 2012040201          
 
* 对任务及求解方法的描述部分
* 输入描述: 
* 问题描述: 
* 程序输出: 
* 程序头部的注释结束
*/

 

 

  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. int main( )     
  22. {  
  23.     Time t1(23,14,25),t2(8,45,6);   
  24.     cout<<"24时制, 不前导0:"<<endl;  
  25.     cout<<"    t1是:";  
  26.     t1.show_time();  
  27.     cout<<"    t2是:";  
  28.     t2.show_time();  
  29.     t1.add_hours(10);  
  30.     t2.add_hours(10);  
  31.     Time::changefrom0(); //注意此处调用静态成员  
  32.     cout<<"10小时后, 切换是否前导0:"<<endl;  
  33.     cout<<    "t1是:";  
  34.     t1.show_time();  
  35.     cout<<    "t2是:";  
  36.     t2.show_time();  
  37.     t1.change24();  
  38.     cout<<"换一种制式:"<<endl;  
  39.     cout<<"    t1是:";  
  40.     t1.show_time();  
  41.     cout<<"    t2是:";  
  42.     t2.show_time();  
  43.     system("pause");  
  44.     return 0;  
  45. }  
  46.   
  47. bool Time::is_24=true;  
  48. bool Time::from0=false;  
  49.   
  50. Time::Time(int h,int m,int s)  
  51. {  
  52.     hour=h;  
  53.     minute=m;  
  54.     sec=s;  
  55. }  
  56.   
  57. void Time::show_time( )        
  58. {     
  59.     if(Time::is_24==true && Time::from0==false)//24小时制  
  60.     {  
  61.             cout << hour << ":" << minute << ":" << sec <<endl;  
  62.     }  
  63.     if(Time::is_24==true && Time::from0!=false)//24小时制  
  64.     {  
  65.             if(hour<10)  
  66.                 cout<<"0"<<hour<<":";  
  67.             else  
  68.                 cout<<hour<<":";  
  69.             if(minute<10)  
  70.                 cout<<"0"<<minute<<":";  
  71.             else  
  72.                 cout<<minute<<":";  
  73.             if(sec<10)  
  74.                 cout<<"0"<<sec<<endl;  
  75.             else  
  76.                 cout<<sec<<endl;  
  77.           
  78.     }  
  79.     if(Time::is_24!=true && Time::from0!=false)//12小时制  
  80.     {  
  81.         if(hour>12)  
  82.         {  
  83.             hour=hour%12;  
  84.             if(hour<10)  
  85.                 cout<<"0"<<hour<<":";  
  86.             else  
  87.                 cout<<hour<<":";  
  88.             if(minute<10)  
  89.                 cout<<"0"<<minute<<":";  
  90.             else  
  91.                 cout<<minute<<":";  
  92.             if(sec<10)  
  93.                 cout<<"0"<<sec<<"  pm  "<<endl;  
  94.             else  
  95.                 cout<<sec<<"  pm  "<<endl;  
  96.         }  
  97.         else  
  98.         {  
  99.             if(hour<10)  
  100.                 cout<<"0"<<hour<<":";  
  101.             else  
  102.                 cout<<hour<<":";  
  103.             if(minute<10)  
  104.                 cout<<"0"<<minute<<":";  
  105.             else  
  106.                 cout<<minute<<":";  
  107.             if(sec<10)  
  108.                 cout<<"0"<<sec<<"  am  "<<endl;  
  109.             else  
  110.                 cout<<sec<<"  am  "<<endl;  
  111.         }  
  112.     }  
  113.   
  114. }       
  115.   
  116. void Time::add_hours(int h)        
  117. {        
  118.     h = hour + h;        
  119.     hour = h % 24;        
  120. }    
  121.   
  122. void Time::change24()  
  123. {  
  124.     is_24=false;  
  125. }  
  126.   
  127. void Time::changefrom0()  
  128. {  
  129.     from0=true;  
  130. }  
原创粉丝点击