時間クラス

2391 ワード

01./*               
02.* Copyright (c)2013,              
03.* All rightsreserved.  
04.*     : Circle.cpp  
05.*    :    
06.*     :2013  3 18   
07.*    : v1.0  
08.*  
09.*     : ;
10.*     :   
11.*     :  

#include <iostream>
using namespace std;
class Time
{
public:
	void set_time( );
	void show_time( );
	inline void add_a_sec(int);
    inline void add_a_minute(int);
    inline void add_a_hour(int);
private:
	bool is_time(int, int, int);
	int hour;
	int minute;
	int sec;
    int add_a_sec();
};
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;
   }
}
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;
}
inline void Time::add_a_sec(int n)
{   
	sec+=n;
	if(sec>59)
	{
		add_a_minute(sec/60);
		sec%=60;
	}
}
inline void Time::add_a_minute(int n)
{
  minute+=n;
  if(minute>59)
  {
	  add_a_hour(minute/60);
	  minute%=60;
  }
}
inline void Time::add_a_hour(int n)
{
   hour+=n;
   if(hour>23)
	   hour%=24;
}
int main( )
{ Time t1;
  Time &t2=t1;
  t1.set_time( );
  cout<<"      :";
  t2.show_time( );
  t1.add_a_sec(24);
  cout<<"        :";
  t2.show_time( );
  t1.add_a_minute(30);
  cout<<"        :";
  t2.show_time( );
  t1.add_a_hour(124);
  cout<<"         :";
  t2.show_time( );
  return 0;
}