第九周项目二Time类中的运算符重载(续)(2)

来源:互联网 发布:打造最好的网络电玩城 编辑:程序博客网 时间:2024/05/16 19:25

问题及代码:

/* * Copyright (c) 2014, 烟台大学计算机与控制工程学院 * All rights reserved. * 文件名称:test.cpp * 作    者:郝俊宇 * 完成日期:2015年 5 月9 日 * 版 本 号:v1.0 * 问题描述:在第八周项目二的基础上(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);    //二目的比较运算符重载    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 &in,CTime &c);    friend ostream &operator<<(ostream &out,const CTime &c);};CTime::CTime(int h,int m,int s)//赋值{    hour=h;    minute=m;    second=s;}bool CTime::operator>(CTime &t)//判断t1>t2{    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)//判断t1<t2{    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)//判断t1>=t2{    if(*this<t)        return false;    return true;}bool CTime::operator<=(CTime &t)//判断t1<=t2{    if(*this>t)        return false;    return true;}bool CTime::operator==(CTime &t)//判断t1=t2{    if((*this>t)||(*this<t))        return false;    return true;}bool CTime::operator!=(CTime &t)//判断t1!=t2{    if((*this>t)||(*this<t))        return true;    return false;}CTime CTime::operator+(CTime &t)//计算t1+t2{    CTime c;    c.hour=hour+t.hour;    c.minute=minute+t.minute;    c.second=second+t.second;    if(c.second>=60)    {        c.minute++;        c.second=c.second-60;    }    if(c.minute>=60)    {        c.hour++;        c.minute=c.minute-60;    }    if(c.hour>=24)    {        c.hour=c.hour-24;    }    return c;}CTime CTime::operator-(CTime &t)//计算t1-t2{    int h,m,s;    s=second-t.second;    m=minute-t.minute;    h=hour-t.hour;    if (s<0)    {        s+=60;        m--;    }    if (m<0)    {        m+=60;        h--;    }    if (h<0) h+=24;    CTime c(h,m,s);    return c;}CTime CTime::operator+(int s)//增加s秒{    CTime c;    c.hour=hour;    c.minute=minute;    c.second=second+s;    if(c.second>=60)    {        c.minute=c.minute+c.second/60;        c.second=c.second%60;    }    if(c.minute>=60)    {        c.hour=c.hour+c.minute/60;        c.minute=c.minute%60;    }    if(c.hour>=24)    {        c.hour=c.hour-24;    }    return c;}CTime CTime::operator-(int s)//减去s秒{    int ss,mm,hh;    ss=s%60;    mm=(s/60)%60;    hh=s/3600;    CTime c(hh,mm,ss);    return *this-c;}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;}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;}istream &operator>>(istream &in,CTime &c){    char ch1,ch2;    while(1)    {        cout<<"请输入时间(hh:mm:ss) ";        cin>>c.hour>>ch1>>c.minute>>ch2>>c.second;        if (ch1==':' && ch2==':')            if (c.hour>-1 && c.hour<24 && c.minute>-1 && c.minute<60 && c.second>-1 && c.second<60) break;        cerr<<"时间格式不正确! 请重新输入\n";    }    return in;}ostream &operator<<(ostream &out,const CTime &c){    out<<c.hour<<":"<<c.minute<<":"<<c.second;    return out;}int main(){    CTime t1,t2;    cin>>t1;    cin>>t2;    cout<<"t1为:"<<t1<<endl;    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;    cout<<"t1++="<<t1++<<endl;    cout<<"t2--="<<t2--<<endl;    cout<<"++t1="<<++t1<<endl;    cout<<"--t2="<<--t2<<endl;    cout<<"t1+t2="<<t1+t2<<endl;    cout<<"t1-t2="<<t1-t2<<endl;    cout<<"t1+2000="<<t1+2000<<endl;    cout<<"t1-5000="<<t1-5000<<endl;    return 0;}


运行结果:

知识点总结:

Time类中<<和>>的应用

学习心得:

对于<<和>>运算符的重载,感觉已经熟练了

0 0