第12周项目2:Time类中的运算符重载

/*
 * Copyright(c)2016,烟台大学计算机与控制工程学院
 * All rights reserved.
 * 文件名称:第12周项目2:Time类中的运算符重载
 * 作者:马康泰
 * 完成日期:2016.5.25
 * 版本号:v1.0
 *
 * 问题描述:实现Time类中的运算符重载。
 * 输入描述:
 * 程序输出:
*/
#include<iostream>
using namespace std;
class CTime
{
private:
    short int hour;    // 时
    short int minute;  // 分
    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 judge_is_time();
    void trans_time_standard1();
    void trans_time_standard2();
    //二目的比较运算符重载
   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--();//前置--,前一秒
};
CTime::CTime(int h,int m,int s):hour(h),minute(m),second(s){}
void CTime::setTime(int h,int m,int s)
{
    hour=h;
    minute=m;
    second=s;
}
void CTime::display()
{
    if(hour<10)cout<<"0";
    cout<<hour<<":";
   if(minute<10)cout<<"0";
    cout<<minute<<":";
    if(second<10)cout<<"0";
    cout<<second<<endl;
}
bool CTime::judge_is_time()
{
    if((hour>=0&&hour<=23)&&(minute>=0&&minute<=59)&&(second>=0&&second<=59))
        return true;
    return false;
}
void CTime::trans_time_standard1()
{
    if(!judge_is_time())
    {
        if(second>=60)
        {
            minute+=second/60;
           second%=60;
          if(minute>=60)
            {
                hour+=minute/60;
                minute%=60;
                if(hour>=24)
                    hour%=24;
           }
        }
    }
}
void CTime::trans_time_standard2()
{
    if(second<0)
   {
        second+=60;
        minute-=1;
        if(minute<0)
       {
           minute+=60;
           hour-=1;
            if(hour<0)
                hour+=24;
        }
    }
}
bool CTime::operator > (CTime &t)
{
    if(this->hour>=t.hour)
    {
        if(this->hour>t.hour)
            return true;
        else
            {
               if(this->minute>=t.minute)
              {


                   if(this->minute>t.minute)
                    return true;
                  else
                  {
                      if(this->second>t.second)
                     return true;
                       else
                        return false;
                   }
             }
               return false;
           }
    }
    return false;
}
bool CTime::operator < (CTime &t)
{
     if(this->hour<=t.hour)
    {
        if(this->hour<t.hour)
            return true;
       else
           {
               if(this->minute<=t.minute)
              {
                   if(this->minute<t.minute)
                     return true;
                   else
                   {
                      if(this->second<t.second)
                        return true;
                       else
                         return false;
                 }
               }
              return false;
            }
    }
    return false;
}
bool CTime::operator <= (CTime &t)
{
    return !(*this > t);
}
bool CTime::operator >= (CTime &t)
{
    return !(*this < t);
}
    //二目的加减运算符的重载
    //返回t规定的时、分、秒后的时间
    //例t1(8,20,25),t2(11,20,50),t1+t2为19:41:15
CTime CTime::operator+(CTime &t)
{
    CTime ty;
    ty.hour=this->hour+t.hour;
    ty.minute=this->minute+t.minute;
    ty.second=this->second+t.second;
    ty.trans_time_standard1();
    return ty;
};
CTime CTime::operator-(CTime &t)//对照+理解
{
    CTime ty;
   ty.hour=this->hour-t.hour;
    ty.minute=this->minute-t.minute;
    ty.second=this->second-t.second;
    ty.trans_time_standard2();
    return ty;
}
CTime CTime::operator+(int s)//返回s秒后的时间
{
    this->second+=s;
   this->trans_time_standard1();
   return *this;
}
CTime CTime::operator-(int s)//返回s秒前的时间
{
    this->second-=s;
    this->trans_time_standard2();
    return *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秒后的时间
{
    *this=*this+s;
    return *this;
}
CTime &CTime::operator-=(int s)//返回s秒前的时间
{
    *this=*this-s;
    return *this;
}
//一目运算符的重载
CTime CTime::operator++(int)//后置++,下一秒
{
    CTime ty=*this;
    *this=*this+1;
    ty.display();
   return ty;
}
CTime &CTime::operator++()//前置++,下一秒
{
    *this=*this+1;
    return *this;
}
CTime CTime::operator--(int)//后置--,前一秒
{
    CTime ty=*this;
    *this=*this-1;
    ty.display();
    return ty;
}
CTime &CTime::operator--()//前置--,前一秒
{
   *this=*this-1;
    return *this;
}
int main()
{
    CTime t1(8,20,25),t2(11,20,50),t3;
   if(t1<t2)
       cout<<"t1<t2"<<endl;
   cout<<"二目的加减运算符的重载"<<endl;
    t3=t1+t2;
   t3.display();
   t3=t2-t1;
    t3.display();
    t3=t1+2;
   t3.display();
    t3=t2-2;
    t3.display();
     t3=t1-2;
   t3=t2+2;
   cout<<"二目赋值运算符的重载"<<endl;
   t1+=t2;
    t1.display();
   t2.display();
    cout<<endl;
   t1-=t2;
   t1.display();
    t2.display();
    cout<<endl;
    t1+=2;
   t1.display();
    t2.display();
   cout<<endl;
   t2-=2;
    t1.display();
    t2.display();
    t1-=2;
    t2+=2;
    t1.display();
    t2.display();
    cout<<"一目运算符的重载"<<endl;
   ++t1;
    t1.display();
    --t1;
t1.display();
    cout<<endl;
    t2++;
    t2--;
    return 0;
}
<img src="https://img-blog.csdn.net/20160525192257800?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" />

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值