제8 주 미 션 2

#include <iostream>
using namespace std;
class CTime
{
private:
	unsigned short int hour;    //  
	unsigned short int minute;  //  
	unsigned short int second;  //  
public:
	CTime(int h=0,int m=0,int s=0):hour(h),minute(m),second(s){}
	void setTime(int h,int m,int s);
	void display();
	//     (  )   
	bool operator > (CTime &t);
	bool operator < (CTime &t);
	bool operator >= (CTime &t);
	bool operator <= (CTime &t);
	bool operator == (CTime &t);
	bool operator != (CTime &t);
	//        
	CTime operator+(CTime &c);//  c     、 、     , t1(8,20,25),t2(11,20,50),t1+t2 :41:15
	CTime operator-(CTime &c);//  +  6
	CTime operator+(int s);//  s     
	CTime operator-(int s);//  s     
	//        
	CTime operator++(int);//  ++,   
	CTime operator++();//  ++,   
	CTime operator--(int);//  --,   
	CTime operator--();//  --,   
	//             
	CTime operator+=(CTime &c);
	CTime operator-=(CTime &c);
	CTime operator+=(int s);//  s     
	CTime operator-=(int s);//  s     
};
//              。
//     ,           ,           
void CTime::display()
{
	cout << hour <<":" << minute << ":" << second << endl;
}
void CTime::setTime(int h,int m,int s)
{
	hour = h;
	minute = m;
	second = s;
}

CTime CTime::operator+(CTime &c)
{
	CTime time;

	time.hour = hour + c.hour;
	time.minute = minute + c.minute;
	time.second = second + c.second;

	while(time.second >= 60)
	{
		time.second = time.second - 60;
		time.minute ++;
	}
	while(time.minute >= 60)
	{
		time.minute = time.minute - 60;
		time.hour ++;
	}
	return time;
}
CTime CTime::operator-(CTime &c)
{
	CTime time;
	int s1,s2;

	s1 = second + minute * 60 + hour * 3600;
	s2 = c.second + c.minute * 60 + c.hour * 3600;

	if(s1 > s2)                   //////////////////////////////////////////////////////////////////
		time.second = s1 - s2;
	else 
		time.second = s2 - s1;

	while(time.second >= 60)
	{
		time.second = time.second - 60;
		time.minute ++;
	}
	while(time.minute >= 60)
	{
		time.minute = time.minute - 60;
		time.hour ++;
	}


	return time;
}
CTime CTime::operator+(int s)
{
    second = second + s;

	while(second >= 60)
	{
		second = second - 60;
		minute ++;
	}
	while(minute >= 60)
	{
		minute = minute - 60;
	    hour ++;
	}
    return *this;
}
CTime CTime::operator-(int s)
{
	while(second < s)
   {
		minute --;
		while(minute < 0)
		{
		    hour--;
			minute = minute + 60;
		}
		second = second + 60;
	}
	second = second - s;
	return *this;
}
CTime CTime::operator++(int)
{
	CTime temp(*this);
	second++;
	if(second >= 60)
	{
		second = second - 60;
		++ minute;
	}
	return temp;
}
CTime CTime::operator++()
{
	if(++ second >= 60)
	{
		second = second - 60;
		++ minute;
	}
	return *this;
}

CTime CTime::operator--(int)
{
	CTime temp(*this);
	if(second-- < 0)
	{
		minute--;
			if(--minute < 0)
		{
			hour--;
			minute = minute + 60;
		}
		second = second + 60;
	}
	second --;
	return temp;
}
CTime CTime::operator--()
{
	if(-- second < 0)
	{
		
		-- minute;
		if(--minute < 0)
		{
			hour--;
			minute = minute + 60;
		}
		second = second + 60;
	}
	return *this;
}

CTime CTime::operator+=(CTime &c)
{
	hour = hour + c.hour;
	minute = minute + c.minute;
	second = second + c.second;

    while(second >= 60)
	{
		second = second - 60;
		minute ++;
	}
    while(minute >= 60)
	{
		minute = minute - 60;
		hour ++;
	}
	return *this;
}

CTime CTime::operator-=(CTime &c)
{
	CTime time;

	second = second + minute * 60 + hour * 3600;
	c.second = c.second + c.minute * 60 + c.hour * 3600;

	if(second > c.second ) 
	{
		second = second - c.second ;
		while(second >= 60)
	   {
	    	second = second - 60;
		    minute ++;
	   }
		while(minute >= 60)
	   {
	    	minute = minute - 60;
	    	hour ++;
	   }
		time = *this;
	}
	else 
	{
		c.second = c.second - second;
		while(c.second >= 60)
	   {
	    	c.second = c.second - 60;
		    c.minute ++;
	   }
		while(c.minute >= 60)
	   {
	    	c.minute = c.minute - 60;
	    	c.hour ++;
	   }
	   time = c;
	}	
	return time;
}
CTime CTime::operator+=(int s)
{
	second = second + s;

	while(second >= 60)
	{
		second = second - 60;
		minute ++;
	}
	while(minute >= 60)
	{
		minute = minute - 60;
		hour ++;
	}
	return *this;
}
CTime CTime::operator-=(int s)
{
	int min;

	min = minute;

	while(second < s)
   {
		minute = minute --;
		while(min < 0)
		{
			hour--;
			minute = minute + 60;
		}
		second = second + 60;
	}
    second = second - s;

	return *this;
}







bool CTime::operator > (CTime &t)
{
	if(hour > t.hour) return true;
	else if (minute > t.minute) return true;
	else if (minute > t.second) return true;
	else 
		return false;
}
bool CTime::operator < (CTime &t)
{
	if(hour < t.hour) return true;
	else if (minute < t.minute) return true;
	else if (minute < t.second) return true;
	else 
		return false;
}
bool CTime::operator == (CTime &t)
{
	if(hour == t.hour && minute == t.minute && second == t.second)
		return true;
	return false;
}
bool CTime::operator >= (CTime &t)
{
	if(operator < (t) != 1)
		return true;
	return false;
}
bool CTime::operator <= (CTime &t)
{
	if(operator > (t) != 1)
		return true;
	return false;
}
bool CTime::operator != (CTime &t)
{
	if(hour != t.hour || minute != t.minute || second != t.second)
		return true;
	return false;
}



void main()    
{    
    CTime t1(8,20,25), t2(11,20,50),t;    
    
    cout << "t1 :";    
    
    t1.display();    
    
    cout << "t2 :";    
    
    t2.display();    
    
    cout << "        :" << endl;    
    
    if (t1 > t2) cout << "t1 > t2" << endl;    
    
    if (t1 < t2) cout << "t1 < t2" <<endl;    
    
    if (t1 == t2) cout <<"t1 = t2" <<endl;     
    
    if (t1 != t2) cout << "t1 ≠ t2" << endl;    
    
    if (t1 >= t2) cout << "t1 ≥ t2" << endl;    
    
    if (t1 <= t2) cout << "t1 ≤ t2" << endl;    
    
    cout<<endl;   
   
    t = t1 + t2;    
     
    cout << "t1 + t2 = ";    
    
    t.display();  
 
  
    t = t1 - t2;    
    
    cout << "t1 - t2 = ";    
    
    t.display();  
  
    t = t1;  
  
    t = t + 35;  
  
    cout<< "t1 + 35 = ";  
      
    t .display();  
	
  
    t = t2;  
  
    t = t - 8;  
  
    cout<< "t2 - 8 = " ;  
  
    t.display();  
  
    t = t1;  
  
    t = t ++;  
  
    cout << endl;  
  
    cout << "t1++ = ";  
  
    t.display();  
  
    t = t1;  
  
    t = ++t;  
  
    cout << "++t1 = ";  
  
    t.display();  
  
    t = t2;  
  
    t = t --;  
  
    cout << "t2-- = ";  
  
    t.display();  
  
  
  
    t2 = --t;  
  
    cout << "--t2 = ";  
  
    t.display();  
  
 
  
    t1 +=t2;  

	cout << endl;
  
    cout << "t1 += t2,t1 = " ;  
  
    t1.display();  
	t2.display ();////////////
  
    t2 -= t1;  
  
    cout << "t2 -= t1,t2 = " ;  
  
    t2.display();  
  
    t1 += 5;  
  
    cout << "t1 += 5 = " ;  
  
    t1.display();  
  
    t1 -= 6;  
   
    cout << "t1 -= 6 = " ;  
  
    t1.display();  
  
    system("pause");

}  

좋은 웹페이지 즐겨찾기