您的位置:首页 > 其它

第九周上机实践项目2-Time类中的运算符重载(续)

2015-05-05 22:24 417 查看
Time类中的运算符重载基础上

(1)定义对时间对象的自增和自减一目运算符

[cpp] view
plaincopyprint?





//一目运算符的重载

CTime operator++(int);//后置++,下一秒

CTime operator++();//前置++,下一秒,前置与后置返回值不一样

CTime operator--( int);//后置--,前一秒

CTime operator--();//前置--,前一秒

(2)定义Time类中的<<和>>运算符重载,实现时间的输入输出,改造原程序中对运算结果显示方式,使程序读起来更自然。

#include <iostream>
#include <iomanip>
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&output,const CTime &c);
    //二目的比较运算符重载
    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--();//前置--,前一秒
};
void CTime::setTime(int h,int m,int s)
{
    hour=h;
    minute=m;
    second=s;
}
ostream& operator << (ostream&output,const CTime &c)
{
    output<<setw(2)<<setfill('0')<<c.hour<<":";
    output<<setw(2)<<setfill('0')<<c.minute<<":";
    output<<setw(2)<<setfill('0')<<c.second;
    return output;
}
bool CTime::operator > (CTime &t)
{
    int t1,t2;
    t1=hour*3600+minute*60+second;
    t2=t.hour*3600+t.minute*60+t.second;
    if(t1>t2)
        return true;
    else
        return false;
}
bool CTime::operator < (CTime &t)
{
    int t1,t2;
    t1=hour*3600+minute*60+second;
    t2=t.hour*3600+t.minute*60+t.second;
    if(t1<t2)
        return true;
    else
        return false;
}
bool CTime::operator <=     *this=*this-t0;
    return *this;(CTime &t)
{
    return !(*this>t);
}
bool CTime::operator >= (CTime &t)
{
    return !(*this<t);
}
bool CTime::operator == (CTime &t)
{
    if(hour==t.hour&&minute==t.minute&&second==t.second)
        return true;
    else
        return false;
}
bool CTime::operator != (CTime &t)
{
    return !(*this==t);
}
CTime CTime::operator + (CTime &t)
{
    CTime t1;
    t1.second=second+t.second;
    t1.minute=minute+t.minute;
    t1.hour=hour+t.hour;
    if(t1.second>59)
    {
        t1.second%=60;
        t1.minute++;
    }
    if(t1.minute>59)
    {
        t1.minute%=60;
        t1.hour++;
    }
    if(t1.hour>23)
    {
        hour%=24;
    }
    return t1;
}
CTime CTime::operator - (CTime &t)
{
    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--;
    }
    while (h<0) h+=24;
    CTime t0(h,m,s);
    return t0;
}
CTime CTime::operator+(int s)
{
    CTime t1(0,0,s);
    return *this+t1;
}
CTime CTime::operator-(int s)
{
    CTime t1(0,0,s);
    return *this-t1;
}
//可以直接使用已经重载了的加减运算实现
//这种赋值, 例如 t1+=20,直接改变当前对象的值,所以在运算完成后,将*this作为返回值
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秒后的时间
{
    int ss=s%60;
    int mm=(s/60)%60;
    int hh=s/3600;
    CTime t0(hh,mm,ss);
    *this=*this+t0;
    return *this;
}
CTime CTime::operator-=(int s)//返回s秒前的时间
{
    int ss=s%60;
    int mm=(s/60)%60;
    int hh=s/3600;
    CTime t0(hh,mm,ss),t;
    *this=*this-t0;
    return *this;
}
//下面定义用于测试的main()函数
//一目运算符的重载
CTime CTime::operator++(int)//后置++,下一秒
{
    CTime c=*this;
    *this+=1;
    return c;
}
CTime CTime::operator++()//前置++,下一秒,前置与后置返回值不一样
{
    *this+=1;
    return *this;
}
CTime CTime::operator--( int)//后置--,前一秒
{
    CTime c=*this;
    *this-=1;
    return c;
}
CTime CTime::operator--()//前置--,前一秒
{
    *this-=1;
    return *this;
}
int main()
{
    CTime t1,t2,t3;
    int s=20;
    t1.setTime(8,20,25);
    t2.setTime(11,20,50);
    cout<<"t1的时间为:"<<t1<<endl;
    cout<<"t2的时间为:"<<t2<<endl;
    cout<<"t1是否比t2大?"<<endl;
    cout<<((t1>t2)?"是":"否")<<endl;
    cout<<"t1是否比t2小?"<<endl;
    cout<<((t1<t2)?"是":"否")<<endl;
    cout<<"t1是否大于或等于t2?"<<endl;
    cout<<((t1>=t2)?"是":"否")<<endl;
    cout<<"t1是否小于或等于t2?"<<endl;
    cout<<((t1<=t2)?"是":"否")<<endl;
    cout<<"t1是否等于t2?"<<endl;
    cout<<((t1==t2)?"是":"否")<<endl;
    cout<<"t1是否不等于t2?"<<endl;
    cout<<((t1!=t2)?"是":"否")<<endl;
    cout<<t1<<"+"<<t2<<"="<<t1+t2<<endl;
    cout<<t1<<"-"<<t2<<"="<<t1-t2<<endl;
    cout<<t1<<"+"<<s<<"s"<<"=";
    t3=t1+s;
    cout<<t3<<endl;
    cout<<t1<<"-"<<s<<"s"<<"=";
    t3=t1-s;
    cout<<t3<<endl;
    cout<<t1<<"+="<<t2<<"=";
    t1+=t2;
    cout<<t1<<endl;
    cout<<t1<<"-="<<t2<<"=";
    t1-=t2;
    cout<<t1<<endl;
    cout<<t1<<"+="<<s<<"s"<<" = ";
    t1+=s;
    cout<<t1<<endl;
    cout<<t1<<"-="<<s<<"s"<<" = ";
    t1-=s;
    cout<<t1<<endl;
    cout<<t1<<"++ = ";
    cout<<(t1++)<<endl;
    cout<<"++"<<t1<<" = ";
    cout<<(++t1)<<endl;
    cout<<t2<<"-- = ";
    cout<<(t2--)<<endl;
    cout<<"--"<<t2<<" = ";
    cout<<(--t2)<<endl;
}
图片:



心得:

要吐血了,老是错
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: