作业

#include <iostream>  
  
using namespace std;  
 
class Time  
{  
public:  
    void set_time();  

	void show_time();  

	inline add_a_sec();  

	inline add_a_minute();  

	inline add_a_hour();//内置函数,增加一秒  

	add_seconds(int);  
    add_minutes(int);  
	add_hours(int);//增加N秒  

private:  

	bool is_time( int, int, int);  

	int hour;  

	int minute;  

	int sec;  

};  
  
int main()    
{    
    Time t1;    
      
    Time &t2 = t1;    
      
    t1.set_time();   
      
    t2.show_time();    
      
    t1.add_a_sec();    
      
    cout << "增加1秒钟:" << endl;  
      
    t2.show_time();    
      
    t1.add_a_minute();  
      
    cout << "增加1分钟:" << endl;  
      
    t2.show_time();    
      
    t1.add_a_hour();    
      
    cout << "增加1小时:" << endl;  
     
    t2.show_time();    
      
    t1.add_seconds(45);   
      
    cout << "增加45秒钟:" << endl;  
      
    t2.show_time();    
      
    t1.add_minutes(50);    
      
    cout << "增加50分钟:" << endl;  
      
    t2.show_time();    
      
    t1.add_hours(2);    
      
    cout << "增加2小时:" << endl;  
      
    t2.show_time();    
      
    return 0;  
}  ;  
  
void Time::set_time()  
{  
    char c1,c2;  
    cout << "请输入时间:格式 (hh:mm:ss)";  
    while(1)  
    {  
        cin >>hour>>c1>>minute>>c2>>sec;  
          
        if (c1!=':'||c2!=':')  
        {  
            cout << " 时间格式不正确,请重新输入!" <<endl;  
        }  
          
        else if(!is_time(hour,minute,sec))  
        {  
            cout << "时间非法,重新输入!" <<endl;  
        }  
          
        else   
        {  
            break;  
        }  
          
    }  
}  
  
int Time::add_a_sec()  
{  
    sec = sec+1;  
      
    return sec;  
}  
  
int Time::add_a_minute()  
{  
    minute = minute+1;  
    return minute;  
}  
  
int Time::add_a_hour()  
{  
    hour= hour+1;  
    return hour;  
}  
  
int Time::add_seconds(int n)  
{  
    sec=sec+45;  
    return sec;  
      
}  
  
int Time::add_minutes(int n)  
{  
    minute=minute+50;  
    return minute;  
}  

int Time::add_hours(int)  
{  
    hour=hour+2;  

	return hour;  
}  
  
void Time::show_time()  
{  
    if(sec>59 && minute<59)  
    {  
        minute=minute+sec/60;  
        sec=sec%60;  
        cout <<hour<<":"<<minute<<":"<<sec <<endl;  
    }  
     
else if(minute>59&&hour<23)  
    {  
        hour=hour+minute/60;  
        minute=minute%60;  
        cout <<hour<<":"<<minute<<":"<<sec << endl;  
    }  
      
else if(hour>23)  
    {  
        hour=0;  
        minute=0;  
        sec=0;  
        cout <<"第二天"<<hour<<minute<<sec<<endl;  
    }  
    else  
    {  
  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;  
}  

 

你可能感兴趣的:(c,Class,include,iostream,作业)