您的位置:首页 > 其它

第九周项目二 Time类中的运算符重载(续)-(2)定义Time类的>>和<<

2015-05-09 21:28 615 查看

项目要求

(2)定义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);
    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);
    //二目的加减运算符的重载
    //返回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 istream& operator>>(istream&,CTime &);
    friend ostream& operator<<(ostream&,const CTime &);
    };

//
istream& operator>>(istream& input,CTime &t)
    {
    char ch1,ch2;
    while(1)
        {
        cout<<"请以hh:mm:ss格式输入时间:"<<endl;
        cin>>t.hour>>ch1>>t.minute>>ch2>>t.second;
            if  (t.hour<24&&t.hour>=0&&ch1==':'&&t.minute<60&&t.minute>=0&&ch2==':'&&t.second<60&&t.second>=0)
                break;
            else
                cout<<"时间格式输入错误,请重新输入!"<<endl;
        }
    return input;
    }
ostream& operator<<(ostream& output,const CTime &t)
    {
    output<<t.hour<<":"<<t.minute<<":"<<t.second;
    return output;
    }
//
CTime CTime::operator++(int)
    {
    CTime t=*this;
    *this+=1;
    return t;//!!
    }
CTime CTime::operator++()
    {
    *this+=1;
    return *this;//!!
    }
CTime CTime::operator--(int)
    {
    CTime t=*this;
    *this-=1;
    return t;
    }
CTime CTime::operator--()
    {
    *this-=1;
    return *this;
    }

//
CTime::CTime(int h,int m,int s)
    {
    hour=h;
    minute=m;
    second=s;
    return;
    }
void CTime::setTime(int h,int m,int s)
    {
    hour=h;
    minute=m;
    second=s;
    return;
    }
void CTime::display()
    {
    cout<<hour<<": "<<minute<<": "<<second<<endl;
    return;
    }

//
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;
    else return true;
    }
bool CTime::operator <= (CTime &t)
    {
    if (*this>t)return false;
    else return true;
    }
bool CTime::operator == (CTime &t)
    {
    if (*this<t||*this>t)return false;
    else return true;
    }
bool CTime::operator != (CTime &t)
    {
    if (*this==t)return false;
    else return true;
    }

//
CTime CTime::operator+(CTime &t)
    {
    int h,m,s;
    h=hour+t.hour;
    m=minute+t.minute;
    s=second+t.second;
    if (s>59)
        {
        m++;
        s-=60;//(莫忘了这一步)
        }
    if (m>59)
        {
        h++;
        m-=60;
        }
    if (h>23)
        {
        h-=24;
        }
    CTime t1(h,m,s);//注意不能写成 hour=h;minute=m;second=s; 应不改变原值
    return t1;
    }
CTime CTime::operator-(CTime &t)
    {
    int h,m,s;
    h=hour-t.hour;
    m=minute-t.minute;
    s=second-t.second;
    if (s<0)
        {
        m--;
        s+=60;//莫忘了这一步
        }
    if (m<0)
        {
        h--;
        m+=60;
        }
    if (h<0)
        {
        h+=24;
        }
    CTime t1(h,m,s);//注意不能写成 hour=h;minute=m;second=s; 应不改变原值
    return t1;

    }
CTime CTime::operator+(int s)
    {
    int ss=s%60, mm=(s/60)%60, hh=s/3600;
    CTime t(hh,mm,ss);
    return *this+t;
    /*之前用的下面这段,有点复杂了,而且减法不好处理
        int ss=second+s,mm=minute,hh=hour;
       if (ss>59)
        {
    mm+=ss/60;
            ss%=60;
        }
        if (mm>59)
        {
            hh+=mm/60;
            mm%=60;
        }
        if (hh>23)
        {
            hh%=24;
        }
        CTime t1(hh,mm,ss);
        return t1;
        */
    }
CTime CTime::operator-(int s)
    {
    int ss=s%60, mm=(s/60)%60, hh=s/3600;
    CTime t(hh,mm,ss);
    return *this-t;
    }

//
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)
    {
    *this=*this+s;
    return *this;
    }
CTime CTime::operator-=(int s)
    {
    *this=*this-s;
    return *this;
    }
int main()
    {
    CTime t1(20,30,41),t2(13,32,20),t3(20,11,36),tt1,tt2,tt3;
    cout<<"t1时刻为:";
    t1.display();
    cout<<"t2时刻为:";
    t2.display();
    cout<<"t3时刻为:";
    t3.display();
    cout<<endl;
    //
    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;
    if (t1<=t2)
        cout<<"t1≤t2"<<endl;
    cout<<endl;
    //
    cout<<"t2与t3时刻比较大小:"<<endl;
    if (t2>t3)
        cout<<"t2>t3"<<endl;
    if (t1<t3)
        cout<<"t2<t3"<<endl;
    if (t2==t3)
        cout<<"t2=t3"<<endl;
    if (t2!=t3)
        cout<<"t2≠t3"<<endl;
    if (t2>=t3)
        cout<<"t2≥t3"<<endl;
    if (t2<=t3)
        cout<<"t2≤t3"<<endl;
    cout<<endl;
    //在测试下面的代码时,请采用单步执行的方法跟踪
    cout<<"下面执行时间计算更改调试:"<<endl;
    cout<<"t1+t2=";
    tt1=t1+t2;
    tt1.display();
    cout<<"t2-t3=";
    tt2=t2-t3;
    tt2.display();
    cout<<"t1+2000s=";
    tt3=t1+2000;
    tt3.display();
    cout<<"t1+2000s-5000s=";
    tt3=tt3-5000;
    tt3.display();
    cout<<"t1+=t2: ";
    t1+=t2;
    t1.display();
    cout<<"t2-=t3: ";
    t2-=t3;
    t2.display();
    cout<<"t3+=100: ";
    t3+=100;
    t3.display();
    cout<<"t3'-=600: ";
    t3-=600;
    t3.display();
    cout<<"t1++ :";
    tt1=t1++;
    tt1.display();
    cout<<"++t1 :";
    tt1=++t1;
    tt1.display();
    cout<<"t1-- :";
    tt1=t1--;
    tt1.display();
    cout<<"--t1 :";
    tt1=--t1;
    tt1.display();

    cout<<endl;
    cin>>tt2;
    cout<<"输入的时刻显示:"<<tt2;

    return 0;
    }


运行结果

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐