第八週實驗報告(二)實現Time類中的運算符重載,並對運算符的重載進行測試

/* (程序頭部註釋開始)

* 程序的版權和版本聲明部分

* Copyright(c) 2011, 煙臺大學計算機學院學生 

* All rightsreserved.

* 文件名稱:實現Time類中的運算符重載,並對運算符的重載進行測試 

*    者:                            

* 完成日期:     2012    04    16

* 號:      T1.0   

* 對任務及求解方法的描述部分

* 輸入描述: 

* 問題描述: 

* 程序輸出:

 * 程序頭部的註釋結束

*/

#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);  
    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);//對照+理解  
    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秒前的時間  
};  
CTime::CTime(int h,int m,int s)  
{  
    hour=h;  
    minute=m;  
    second=s;  
}  
void CTime::setTime(int h,int m,int s)  
{  
    hour=h;  
    minute=m;  
    second=s;  
}  

void CTime::display()  
{  
    cout<<hour<<":"<<minute<<":"<<second<<endl;    
}  
//下面實現所有的運算符重載代碼。  
//爲簡化編程,請注意通過調用已有函數,利用好各函數之間的關係  
//比較運算符(二目)的重載  
bool CTime::operator > (CTime &t)  
{  
    if(hour>t.hour)  
        return true;  
    else if(hour<t.hour)  
        return false;  
    else if(minute>t.minute )  
        return true;  
    else if(minute<t.minute )  
        return false;  
    else if(second>t.second )  
        return true;  
    else if(second<t.second )  
        return false;  
    return false;  
}  
bool CTime::operator < (CTime &t)  
{  
    if(hour<t.hour)  
        return true;  
    else if(hour>t.hour)  
        return false;  
    else if(minute<t.minute )  
        return true;  
    else if(minute>t.minute )  
        return false;  
    else if(second<t.second )  
        return true;  
    else if(second>t.second )  
        return false;  
    return false;  
}  
bool CTime::operator >= (CTime &t)  
{  
    CTime t1;  
    t1.hour =hour;  
    t1.minute =minute ;  
    t1.second =second ;  
    if (t1<t)  
        return false;  
    return true;  
}  
bool CTime::operator <= (CTime &t)  
{  
    CTime t1;  
    t1.hour =hour;  
    t1.minute =minute ;  
    t1.second =second ;  
    if (t1>t)  
        return false;  
    return true;  
}  
bool CTime::operator == (CTime &t)  
{  
    CTime t1;  
    t1.hour =hour;  
    t1.minute =minute ;  
    t1.second =second ;  
    if (t1>t)  
        return false;  
    if (t1<t)  
        return false;  
    return true;  
}  

bool CTime::operator != (CTime &t)  
{  
    CTime t1;  
    t1.hour =hour;  
    t1.minute =minute ;  
    t1.second =second ;  
    if(t1==t)  
        return false;  
    return true;  
}  
//二目運算符的重載  
CTime CTime::operator+(CTime &c)//返回c所規定的時、分、秒後的時間,例t1(8,20,25),t2(11,20,50),t1+t2爲:41:15  
{  
    CTime t;  
    t.hour =hour+c.hour ;  
    t.minute =minute+c.minute ;  
    t.second =second +c.second ;  
    if (t.second >59)          
    {    
        t.minute=(t.minute +t.second/60);    //增加sec/60分鐘   
        if (t.minute>59)        
        {    
            t.hour=(t.hour+t.minute/60);    
            if (t.hour>23)    
                t.hour=(t.hour %24);      
            t.minute=(t.minute %60);    
        }  
        t.second=(t.second %60);                //秒數應該是sec%=60    
    }    
    if (t.minute>59)        
    {    
        t.hour=(t.hour +t.minute/60);    
        if (t.hour>23)    
            t.hour=(t.hour %24);   
        t.minute=(t.minute %60);    
    }  
    if (t.hour>23)    
        t.hour=(t.hour %24);           
	return t;  
}  
CTime CTime::operator-(CTime &c)//對照+理解  
{  
    CTime c2; 
	
    int time;  
	
    time = (hour * 3600 + minute * 60 + second) - (c.hour * 3600 + c.minute * 60 + c.second);  
	
    c2.hour = time / 3600;  
	
    time = time % 3600;  
	
    c2.minute = time / 60;  
	
    time = time % 60;  
	
    c2.second = time;  
	
    return c2;  
}  
CTime CTime::operator+(int s)//返回s秒後的時間  
{  
    CTime t;  
	
    t.hour =hour;  
    t.minute =minute ;  
    t.second =second ;  
    t.second =(t.second +s);  
    if (t.second >59)          
    {    
        t.minute+=t.second/60;    //增加sec/60分鐘   
        if (t.minute>59)        
        {    
            t.hour+=t.minute/60;    
            if (t.hour>23)    
                t.hour%=24;      
            t.minute%=60;    
        }  
        t.second%=60;                //秒數應該是sec%=60    
    }    
    else if (t.minute>59)        
    {    
        t.hour+=t.minute/60;    
        if (t.hour>23)    
            t.hour%=24;   
        t.minute%=60;    
    }  
    else  if (t.hour>23)    
        t.hour%=24;       
	return t;  
}  
CTime CTime::operator-(int s)//返回s秒前的時間  
{  
	int time = hour * 3600 + minute * 60 + second;  
	
    time = time - s;  
	
    hour = time / 3600;  
	
    time = time % 3600;  
	
    minute = time / 60;  
	
    time = time % 60;  
	
    second = time;  
	
    return (*this); 
	
}  
//一目運算符的重載  
CTime CTime::operator++(int)//後置++,下一秒  
{  
    CTime temp(*this);  
    second++;  
    if(second>=60)  
    {   
        second-=60;  
        ++minute;  
    }  
    return temp;  
}  
CTime CTime::operator++()//前置++,下一秒  
{  
    if(++second>=60)  
    {  
        second-=60;  
        ++minute;  
    }  
	
    return *this;  
}  
CTime CTime::operator--(int)//後置--,前一秒  
{  
    CTime temp(*this);  
    second--;  
    if(second<0)  
    {   
        second=59;  
        --minute;  
    }  
    return temp;  
}  
CTime CTime::operator--()//前置--,前一秒  
{  
    if(--second<0)  
    {  
        second=59;  
        --minute;  
    }  
    return *this;  
}  
//賦值運算符的重載       
CTime CTime::operator+=(CTime &c)  
{  
    CTime t1,t;  
    t1.hour =hour;  
    t1.minute =minute ;  
    t1.second =second ;  
    t=t1+c;  
    hour=t.hour ;  
    minute=t.minute ;  
    second=t.second ;  
    return t;  
}  
CTime CTime::operator-=(CTime &c)  
{  
    CTime t1,t;  
    t1.hour =hour;  
    t1.minute =minute ;  
    t1.second =second ;  
    t=t1-c;  
    hour=t.hour ;  
    minute=t.minute ;  
    second=t.second ;  
    return t;  
	
}  
CTime CTime::operator+=(int s)//返回s秒後的時間  
{  
    CTime t1,t;  
    t1.hour =hour;  
    t1.minute =minute ;  
    t1.second =second ;  
    t=t1+s;  
    hour=t.hour ;  
    minute=t.minute ;  
    second=t.second ;  
    return t;  
}  
CTime CTime::operator-=(int s)//返回s秒前的時間  
{  
    CTime t1,t;  
    t1.hour =hour;  
    t1.minute =minute ;  
    t1.second =second ;  
    t=t1-s;  
    hour=t.hour ;  
    minute=t.minute ;  
    second=t.second ;  
    return t;  
}  
void main()  
{  
    CTime t1(8,20,25),t2(11,20,50),t;  
    int n;  
    cout<<"t1爲:";  
    t1.display();  
    cout<<"t2爲:";  
    t2.display();  
    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;  
    cout<<"t1+t2的時間爲:";  
    t=t1+t2;  
    t.display();  
    cout<<"t2-t1的時間爲:";  
    t=t2-t1;  
    t.display();  
    cout<<endl;  
    cout<<"請輸入您想增加的秒數:";  
    cin>>n;  
    t=t1+n;  
    cout<<"增加"<<n<<"秒後的時間爲:";  
    t.display ();  
    t=t1-n;  
    cout<<"增加"<<n<<"秒前的時間爲:";  
    t.display ();  
    cout<<endl;  
    cout<<"對t1後置++,結果爲:";  
    t=t1++;  
    t.display ();  
    cout<<"運算結束後,t1的值爲:";  
    t1.display ();  
    cout<<endl;  
    cout<<"對t1前置++,結果爲:";  
    t=++t1;  
    t.display ();  
    cout<<"運算結束後,t1的值爲:";  
    t1.display ();  
    cout<<endl;  
    cout<<"對t1後置--,結果爲:";  
    t=t1--;  
    t.display ();  
    cout<<"運算結束後,t1的值爲:";  
    t1.display ();  
    cout<<endl;  
    cout<<"對t1前置--,結果爲:";  
    t=--t1;  
    t.display ();  
    cout<<"運算結束後,t1的值爲:";  
    t1.display ();  
    cout<<endl;  
    cout<<"運算t1+=t2的結果爲:";  
    t1+=t2;  
    t1.display ();  
    cout<<endl;  
    cout<<"此時t1的值爲:";  
    t1.display ();  
    cout<<"此時t2的值爲:";  
    t2.display ();  
    cout<<endl;  
    cout<<"運算t1-=t2的結果爲:";  
    t1-=t2;  
    t1.display ();  
    cout<<"此時t1的值爲:";  
    t1.display ();  
    cout<<"此時t2的值爲:";  
    t2.display ();  
    cout<<endl;  
    cout<<"請輸入您想增加的秒數:";  
    cin>>n;  
    t1+=n;  
    cout<<"t1增加"<<n<<"秒後的時間爲:";  
    t1.display ();  
    cout<<"t2增加"<<n<<"秒前的時間爲:";  
    t2-=n;  
    t2.display ();  
    cout<<endl;  
    system("PAUSE");  
}    


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