第四周实验报告2(时间增减)

//time.h
#include<iostream> 
using namespace std;  


class Time    
{    
public:    
    void set_time( );    
      
    void show_time( );  
      
    inline void add_a_sec();  //增加1秒钟   
      
    inline void add_a_minute(); //增加1分钟    
  
    inline void add_an_hour(); //增加1小时   
      
    void add_seconds(int n); //增加n秒钟   
      
    void add_minutes(int n); //增加n分钟   
      
    void add_hours(int n); //增加n小时    
        
private:     
    bool is_time ( int, int, int );    
  
    int hour;   
      
    int minute;    
  
    int sec;  
      
    int hour1;    
  
    int minute1;  
      
    int sec1;    
  
};
inline void Time::add_a_sec()  //增加1秒钟    
{    
   sec = sec+1;  
  
   if(sec>60||sec==60)  
    {  
        minute = minute+(sec/60);  
  
        sec = sec%60;  
    }  
     
}    
    
inline void Time::add_a_minute() //增加1分钟    
{    
    minute = minute+1;  
  
    if(minute>60||minute == 60)  
    {  
        hour = hour+(minute/60);  
  
        minute = minute%60;  
    }  
  
  
}    
    
inline void Time::add_an_hour() //增加1小时    
{    
    hour = hour+1;  

if (hour>23)  

{  

hour=hour%24;  

   }  
      
}    
//time.cpp
#include<iostream>
using namespace std;
#include"time.h"    
void Time :: set_time()     
{    
    char c1,c2;    
  
    cout << "请输入时间 ( 格式 hh:mm:ss ) ";  
      
    while(1)    
    {    
        cin >> hour >> c1 >> minute >> c2 >> sec;    
        hour1 = hour;  
          
        minute1 = minute;  
          
        sec1 = sec;  
          
        if (c1 != ':' || c2 != ':')   
              
            cout << "格式不正确,请重新输入" << endl;    
  
        else if (!is_time(hour,minute,sec))    
  
            cout << "时间非法,请重新输入" << endl;    
        else     
            break;    
    }    
}    
    
void Time::show_time( )          
{    
    cout << hour << ":" << minute << ":" << sec << endl;    
}    
    
bool Time::is_time ( int h,int m, int s )    
{    
    if (h<0 ||h>24 || m<0 ||m>60 || s<0 ||s>60)    
  
        return false;    
  
    return true;    
}    
    

  
  
void Time::add_seconds(int n)//增加n秒钟    
{    
    sec = sec+n;  
  
  if(sec>60||sec==60)  
    {  
        minute = minute + (sec/60);  
        sec = sec%60;  
    }  
     
}    
    
void Time::add_minutes(int n) //增加n分钟    
{    
  minute = minute + n;  
  
if(minute > 60||minute == 60)  
    {  
        hour = hour + ( minute/60 );  
  
        minute = minute % 60;  
    }  
  
   
}    
    
void Time::add_hours(int n) //增加n小时    
{    
    hour = hour + n;  

if (hour>23)  

{  

hour=hour%24;  

}  
}
//main.cpp
#include<iostream>
using namespace std;
#include"time.h"    
int main( )    
{    
    int x,y,z;  
  
    Time t1;  
  
    Time&t2 = t1;  
  
    t1.set_time( );  
  
    t2.show_time ( );

    t1.add_a_sec() ; //增加1秒钟

    cout<<"增加一秒后的时间为:";        
    t2.show_time( );        
    t1.add_a_minute( ); //增加1分钟       
    cout<<"增加一分钟后的时间为:";        
    t2.show_time( );        
    t1.add_an_hour( ); //增加1小时      
    cout<<"增加一小时后的时间为:";        
    t2.show_time( );        

	
      
    cout << "请输入要增加的时分秒:" << endl;    
    
    cin >> x >> y >> z;    
    
    t1.add_seconds ( z );    
    
    t1.add_minutes ( y );    
    
    t1.add_hours ( x );    
    
    t2.show_time ();  
      system("PAUSE");
      
      
      
    return 0;    
}    

第四周实验报告2(时间增减)_第1张图片



上机感言:

inline函数一定要和类在一个文件中,都放在头文件中。要记住了,昨晚害我到十二点啊。。。

你可能感兴趣的:(c,System,Class)