天天看點

第九周(運算符重載時間類)

/*

*copyright(c) 2015,煙台大學計算機學院

*All rights reserved。

*檔案名稱:第九周(運算符重載時間類)

*作者:王忠

*完成日期:2015.5.13

*版本号:v1.0

*

*問題描述:實作Time類中的運算符重載。

*輸入描述:

*程式輸出:

#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)
    {
        hour=h;
        minute=m;
        second=s;
    }
    //二目的比較運算符重載
    bool operator > (CTime &t);
    bool operator < (CTime &t);
    bool operator >= (CTime &t);
    bool operator <= (CTime &t);
    bool operator == (CTime &t);
    bool operator != (CTime &t);
    //二目的加減運算符的重載
    //傳回t規定的時、分、秒後的時間
    //例t1(8,20,25),t2(11,20,50),t1+t2為19:41:15
    CTime operator+(CTime &t);
    CTime operator-(CTime &t);//對照+了解
    CTime operator+(int s);//傳回s秒後的時間
    CTime operator-(int s);//傳回s秒前的時間
    //二目指派運算符的重載
    CTime operator+=(CTime &c);
    CTime operator-=(CTime &c);
    CTime operator+=(int s);//傳回s秒後的時間
    CTime operator-=(int s);//傳回s秒前的時間
    	//一目運算符的重載
	CTime operator++(int);//後置++,下一秒
	CTime operator++();//前置++,下一秒,前置與後置傳回值不一樣
	CTime operator--( int);//後置--,前一秒
	CTime operator--();//前置--,前一秒
	friend  ostream& operator <<(ostream& output,const CTime& t);
	friend  istream& operator >>(istream& input,CTime& t);
};
bool CTime::operator > (CTime &t)
{
    if(hour>t.hour) return true;
    if(hour<t.hour) return false;
    if(minute>t.minute) return true;
    if(minute<t.minute) return false;
    if(second>t.second) return true;
    return false;
}
bool CTime::operator < (CTime &t)
{
    if(hour<t.hour) return true;
    if(hour>t.hour) return false;
    if(minute<t.minute) return true;
    if(minute>t.minute) return false;
    if(second<t.second) return true;
    return false;
}
bool CTime::operator >= (CTime &t)
{
    if(*this<t)return false;
    return true;
}
bool CTime::operator <= (CTime &t)
{
    if(*this>t)return false;
    return true;
}
bool CTime::operator == (CTime &t)
{
    if(*this>t||*this<t) return false;
    return true;
}
bool CTime::operator != (CTime &t)
{
    if(*this==t) return false;
    return true;
}
CTime CTime::operator+(CTime &t)
{
    hour=hour+t.hour;
    minute=minute+t.minute;
    second=second+t.second;
    if(second>59)
    {
        minute++;
        second=second%60;
    }
    if(minute>59)
    {
        hour++;
        minute=minute%60;
    }
    if(hour>23)
        hour=hour%24;
    CTime t2(hour,minute,second);
    return t2;
}
CTime CTime::operator-(CTime &t)
{
    hour=hour-t.hour;
    minute=minute-t.minute;
    second=second-t.second;
    if(second<0)
    {
        minute--;
        second=second+60;
    }
    if(minute<0)
    {
        hour--;
        minute=minute+60;
    }
    if(hour<0)
        hour=hour+24;
    CTime t2(hour,minute,second);
    return t2;
}
CTime CTime::operator+(int s)//傳回s秒後的時間
{
    second=second+s;
    int ss=s/60;
    if(second>59)
    {
        minute+=ss;
        second=second%60;
    }
    int mm=minute/60;
    if(minute>59)
    {
        hour+=mm;
        minute=minute%60;
    }
    if(hour>23)
        hour=hour%24;
    CTime t2(hour,minute,second);
    return t2;
}
CTime CTime::operator-(int s)//傳回s秒前的時間
{
    second=second-s;
    if(second<0)
    {
        minute--;
        second=second+60;
    }
    if(minute<0)
    {
        hour--;
        minute=minute+60;
    }
    if(hour<0)
        hour=hour+24;
    CTime t2(hour,minute,second);
    return t2;
}
CTime CTime::operator+=(CTime &c)
{
    *this=*this+c;
    return *this;
}
CTime CTime::operator-=(CTime &c)
{
    *this=*this-c;
    return *this;
}
CTime CTime::operator+=(int s)//傳回s秒後的時間
{
    *this=*this+s;
    return *this;
}
CTime CTime::operator-=(int s)//傳回s秒前的時間
{
    *this=*this-s;
    return *this;
}
CTime CTime::operator++(int)
{
    CTime t=*this;
    *this=*this+1;
    return t;
}
CTime CTime::operator++()
{
    *this=*this+1;
    return *this;
}
CTime CTime::operator--(int)
{
    CTime t=*this;
    *this=*this-1;
    return t;
}
CTime CTime::operator--()
{
    *this=*this-1;
    return *this;
}
ostream& operator <<(ostream& output,const CTime& t)
{
    output<<t.hour<<':'<<t.minute<<':'<<t.second<<endl;
    return output;
}
istream& operator >>(istream& input,CTime& t)
{
    int h,m,s;
    char sign1,sign2;
    do
    {
        cout<<"input h:m:s"<<endl;
        input>>h>>sign1>>m>>sign2>>s;
    }while(!(sign1==':'||sign2==':'));
    t.hour=h;
    t.minute=m;
    t.second=s;
    return input;
}
int main()
{
    CTime t1,t2,t;
    cin>>t1>>t2;
    cout<<"t1為:"<<t1<<endl;
    cout<<"t2為:"<<t2<<endl;
    t1++;
    cout<<"t1為:"<<t1<<endl;
    t2--;
    cout<<"t2為:"<<t2<<endl;
    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+t2;
    cout<<t<<endl;
    t=t1-t2;
    t=t1+2000;
    cout<<t<<endl;
    t=t1-5000;
    t1+=t2;
    t1-=t2;
    t1+=2000;
    t1-=5000;
    return 0;
}

           
第九周(運算符重載時間類)

這個是自己寫出來的,自己寫的測試  好棒

繼續閱讀