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

来源:互联网 发布:淘宝嘉年华日期 编辑:程序博客网 时间:2024/05/14 20:52
/* *Copyright (c) 2014, 烟台大学计算机学院 *All rights reserved. *文件名称:week9-2-1.cpp *作者:高赞 *完成日期:2015年 5 月 4 日 *版本号:v1.1 * * 问题描述:在Time类中的运算符重载基础上,定义对时间对象的自增和自减一目运算符 */#include <iostream>#include "CTime.h"using namespace std;int main(){    CTime t1(8,20,25),t2(11,20,50),t;    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;    t=t1+t2;    cout<<"t1+t2=";    t.display();    t=t1-t2;    cout<<"t1-t2=";    t.display();    t=t1+2000;    cout<<"t1+2000s=";    t.display();    t=t1-5000;    cout<<"t1-5000s=";    t.display();    t1+=t2;    cout<<"t1+=t2,t1=";    t1.display();    t1-=t2;    cout<<"t1-=t2,t1=";    t1.display();    t1+=2000;    cout<<"t1+=2000s,t1=";    t1.display();    t1-=7000;    cout<<"t1-=7000s,t1=";    t1.display();    ++t1;    cout<<"++t1 =";    t1.display();    --t1;    cout<<"--t1 =";    t1.display();     t1++;    cout<<"t1++ =";    t1.display();      t1--;    cout<<"t1-- =";    t1.display();    return 0;}


CTime.h

#ifndef CTIME_H_INCLUDED#define CTIME_H_INCLUDEDclass CTime{private:    int hour;    // 时    int minute;  // 分    int sec;  // 秒public:    CTime(int h=0,int m=0,int s=0):hour(h),minute(m),sec(s){}    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--();//前置--,前一秒};#endif // CTIME_H_INCLUDED


CTime.cpp

#include <iostream>#include "CTime.h"using namespace std;void CTime::setTime(int h,int m,int s){    hour=h;    minute=m;    sec=s;}void CTime::display(){    cout<<hour<<":"<<minute<<":"<<sec<<endl;}bool CTime::operator > (CTime &t){    bool b;    if(hour>t.hour)        b=true;    else if(hour<t.hour)        b=false;    else    {        if(minute>t.minute)            b=true;        else if(minute<t.minute)            b=false;        else        {            if(sec>t.sec)                b=true;            else if(sec<=t.sec)                b=false;        }    }    return b;}bool CTime::operator < (CTime &t){    bool b;    if(hour<t.hour)        b=true;    else if(hour>t.hour)        b=false;    else    {        if(minute<t.minute)            b=true;        else if(minute>t.minute)            b=false;        else        {            if(sec<t.sec)                b=true;            else if(sec>=t.sec)                b=false;        }    }    return b;}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||*this<t)        return true;    else return false;}CTime CTime::operator+(CTime &t){    int h=hour+t.hour,m=minute+t.minute,s=sec+t.sec;    if(s>=60)    {        s-=60;        ++m;    }    if(m>=60)    {        m-=60;        ++h;    }    if(h>=24)        h-=24;    return CTime(h,m,s);}CTime CTime::operator-(CTime &t){    int h=hour-t.hour,m=minute-t.minute,s=sec-t.sec;    if(s<0)    {        s+=60;        --m;    }    if(m<0)    {        m+=60;        --h;    }    if(h<0)        h+=24;    return CTime(h,m,s);}CTime CTime::operator+(int S){    int h,m,s;    s=S%60;    S=S/60;    m=S%60;    h=S/60;    CTime t2(h,m,s);    return (*this+t2);}CTime CTime::operator-(int S){    int h,m,s;    s=S%60;    S=S/60;    m=S%60;    h=S/60;    CTime t2(h,m,s);    return (*this-t2);}CTime CTime::operator+=(CTime &t){    hour+=t.hour,minute+=t.minute,sec+=t.sec;    if(sec>=60)    {        sec-=60;        ++minute;    }    if(minute>=60)    {        minute-=60;        ++hour;    }    if(hour>=24)        hour-=24;    return *this;}CTime CTime::operator-=(CTime &t){    hour-=t.hour,minute-=t.minute,sec-=t.sec;    if(sec<0)    {        sec+=60;        --minute;    }    if(minute<0)    {        minute+=60;        --hour;    }    if(hour<0)        hour+=24;    return *this;}CTime CTime::operator+=(int S){    int h,m,s;    s=S%60;    S=S/60;    m=S%60;     h=S/60;    CTime t2(h,m,s);    return (*this+=t2);}CTime CTime::operator-=(int S){    int h,m,s;    s=S%60;    S=S/60;    m=S%60;    h=S/60;    CTime t2(h,m,s);    return (*this-=t2);}//一目运算符的重载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;}


0 0