第九周任務(2)

/* (程序頭部註釋開始)
* 程序的版權和版本聲明部分
* Copyright (c) 2011, 煙臺大學計算機學院學生 
* All rights reserved.
* 文件名稱:                              
* 作    者:       苗影                       
* 完成日期:      2012   年 4      月 16       日
* 版 本 號:          

* 對任務及求解方法的描述部分
* 輸入描述: 
* 問題描述: 
* 程序輸出: 
* 程序頭部的註釋結束
*/
#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);  
   friend ostream& operator << (ostream&,CTime&);//聲明重載運算符“<<”函數 
   friend istream& operator>>(istream&,CTime&);//聲明重載運算符“》”
    //比較運算符(二目)的重載  
    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);//對照+理解  
    CTime operator+(int h);//返回s秒後的時間  
    CTime operator-(int h);//返回s秒前的時間  
    //一目運算符的重載  
    CTime operator++(int);//後置++,下一秒  
    CTime operator++();//前置++,下一秒  
    CTime operator--(int);//後置--,前一秒  
    CTime operator--();//前置--,前一秒  
    //賦值運算符的重載       
    CTime operator+=(CTime &c);  
    CTime operator-=(CTime &c);  
    CTime operator+=(int h);//返回s秒後的時間  
    CTime operator-=(int h);//返回s秒前的時間  
};  
//下面實現所有的運算符重載代碼。  
//爲簡化編程,請注意通過調用已有函數,利用好各函數之間的關係  

ostream& operator << (ostream& output,CTime& c)//定義重載運算符“<<”函數  
{  
	output <<c. hour << ":" <<c. minute<< ":" << c.second<<endl;  
    return output;  
} 

void CTime::setTime(int h,int m,int s)  
{  
    hour=h;  
    minute=m;  
    second=s;  
} 
bool CTime::operator>(CTime &t)  
{  
    if(hour * 3600 + minute * 60 + second > t.hour * 3600 + t.minute * 60 + second)    
    {    
        return true;    
    }    
    
    else    
    {    
        return false;    
    }    
}  
bool CTime::operator<(CTime &t)  
{  
    if(hour * 3600 + minute * 60 + second < t.hour * 3600 + t.minute * 60 + second)    
    {    
        return true;    
    }    
    
    else    
    {    
        return false;    
    }    
}  
bool CTime::operator>=(CTime &t)  
{  
    if(hour * 3600 + minute * 60 + second >= t.hour * 3600 + t.minute * 60 + second)    
    {    
        return true;    
    }    
    
    else    
    {    
        return false;    
    }    
}  
bool CTime::operator<=(CTime &t)  
{  
    if(hour * 3600 + minute * 60 + second <= t.hour * 3600 + t.minute * 60 + second)    
    {    
        return true;    
    }    
    
    else    
    {    
        return false;    
    }    
}  
bool CTime::operator==(CTime &t)  
{  
    if(hour * 3600 + minute * 60 + second == t.hour * 3600 + t.minute * 60 + second)    
    {    
        return true;    
    }    
    
    else    
    {    
        return false;    
    }    
}  
bool CTime::operator!=(CTime &t)  
{  
    if(hour * 3600 + minute * 60 + second != t.hour * 3600 + t.minute * 60 + second)    
    {    
        return true;    
    }    
    
    else    
    {    
        return false;    
    }    
}  
  
CTime CTime::operator+(CTime &c)  
{  
    hour=c.hour+hour;  
    minute=c.minute+minute;  
    second=c.second+second;   
      
        if (second>60)  
        {  
            minute=minute+(second/60);  
            second=second%60;  
        }  
       if(minute>60)  
       {  
           minute=minute%60;  
           hour=hour+minute/60;  
       }  
       if(hour>24)  
          {  
              hour=hour%24;  
          }  
return (*this);  
}  
CTime CTime::operator-(CTime &c)  
{  
    if(c.second<second)  
    {  
        minute=minute-1;  
        second=c.second+60-second;  
    }  
    else  
    {  
        second=c.second-second;  
     }  
    if(c.minute<minute)  
    {  
        hour=hour-1;  
        minute=c.minute+60-minute;  
    }  
    else  
    {  
        minute=c.minute-minute;  
    }  
    hour=c.hour-hour;  
    return (*this);  
}  
CTime CTime::operator+(int h)  
{  
    second=second+hour;  
    if(second>60)  
    {  
        second=second%60;  
       minute=minute+second/60;  
    }  
    if(minute>60)  
    {  
        minute=minute%60;  
       minute=minute+minute/60;  
    }  
  if(hour>24)  
  {  
     hour= hour%24;  
  }  
  return (*this);  
}  
CTime CTime::operator-(int h)  
{  
int T;    
    
    T = hour * 3600 + minute *60 + second - h;    
    hour = T / 3600;    
    T = T % 3600;    
    minute = T / 60;    
    second = T % 60;    
    
    return (*this);    
}  
CTime CTime::operator ++(int )  
{  
return *this;  
*this+1;  
}  
  
CTime CTime::operator ++()  
{  
   *this+1;  
   return *this;  
  
}  
CTime CTime::operator --(int )  
{    
    return*this;  
    *this-1;  
       
}  
CTime CTime::operator --()  
{  
    *this-1;  
    return*this;  
}  
CTime CTime::operator+=(CTime &c)    
{    
    return(*this+c );  
}    
CTime CTime::operator-=(CTime &c)    
{    
    return(*this-c);  
  
}    
CTime CTime::operator+=(int h)    
{    
    return(*this+h);   
}    
CTime CTime::operator-=(int h)    
{    
    return (*this-h);    
}    
    
void main()  
{  
    CTime t1(8,20,25),t2(11,20,50),t;  
    cout<<"t1爲:";  
    cout<<t1;
    cout<<"t2爲:";  
    cout<<t2; 
    cout<<"下面比較兩個時間大小:\n";  
    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;    
      
    t = t + t2;      
      
    cout << "t1 + t2 = ";      
      
    cout<<t;    
    
    t = t1;    
    
    t = t - t2;      
      
    cout << "t1 - t2 = ";      
      
   cout<<t;  
    
    t = t1;    
    
    t = t + 5;    
    
    cout<< "t1 + 5 = ";    
        
    cout<<t;  
    
    t = t2;    
    
    t = t - 5;    
    
    cout<< "t2 - 5 = " ;    
    
    cout<<t;   
    
    t = t1;    
    
    t = t ++;    
    
    cout << endl;    
    
    cout << "t1++ = ";    
    
   cout<<t;  
    
    t = t1;    
    
    t = ++t;    
    
    cout << "++t1 = ";    
    
   cout<<t; 
    
    t = t2;    
    
    t = t --;    
    
    cout << "t1-- = ";    
    cout<<t;  
    
    t = t2;    
    
    t = --t;    
    
    cout << "--t1 = ";    
    
    cout<<t;  
    
    t = t1;    
    
    t +=t2;    
    
    cout << "t1 += t2,t1 = " ;    
    
   cout<<t;  
    
    t = t1;    
    
    t -= t2;    
    
    cout << "t1 -= t2,t1 = " ;    
    
    cout<<t; 
    
    t = t1;    
    
    t += 5;    
    
    cout << "t1 += 8 = " ;    
    
  cout<<t;  
    
    t = t1;    
    
    t -= 6;    
     
    cout << "t1 -= 6 = " ;    
    
    cout<<t;
    
    system("pause");      
	}  


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章