第八周项目2 --Time类运算符重载

本文介绍了一个C++中自定义的时间类CTime的实现细节,包括构造函数、各种运算符重载(如加、减、比较等),以及如何进行时间的显示。通过实例演示了如何使用该类进行时间的计算和比较。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

/*
*Copyright (c) 2014, 烟台大学计算机学院
*All rights reserved.
*文件名称:test.cpp
*作者:于凯
*完成日期:2015年 5月 9 日
*版本号:v1.0
*/
#include <iostream>
using namespace std;
class 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);
    CTime operator+(CTime &t);
    CTime operator-(CTime &t);
    CTime operator+(int s);
    CTime operator-(int s);
    CTime operator+=(CTime &c);
    CTime operator-=(CTime &c);
    CTime operator+=(int s);
   CTime operator-=(int s);
};
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);
}
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();
    return 0;
}


运行结果:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值