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

/*
 *Copyright (c) 2014, 烟台大学计算机学院
 *All rights reserved.
 *文件名称:week9-2-2.cpp
 *作者:高赞
 *完成日期:2015年 5 月  日
 *版本号:v1.2
 *
 * 问题描述:定义Time类中的<<和>>运算符重载,实现时间的输入输出
 */
#include <iostream>
#include "CTime.h"
using namespace std;
int main()
{
    CTime t1(8,20,25),t2(11,20,50),t;
    cout<<"输入t1;"<<endl;
    cin>>t1;
    cout<<"t1为:"<<t1;
    cout<<"t2为:"<<t2;
    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=";
    cout<<t;
    t=t1-t2;
    cout<<"t1-t2="<<t;
    t=t1+2000;
    cout<<"t1+2000s="<<t;
    t=t1-5000;
    cout<<"t1-5000s="<<t;
    t1+=t2;
    cout<<"t1+=t2,t1="<<t1;
    t1-=t2;
    cout<<"t1-=t2,t1="<<t1;
    t1+=2000;
    cout<<"t1+=2000s,t1="<<t1;
    t1-=7000;
    cout<<"t1-=7000s,t1="<<t1;
    ++t1;
    cout<<"++t1 ="<<t1;
    --t1;
    cout<<"--t1 ="<<t1;
     t1++;
    cout<<"t1++ ="<<t1;
      t1--;
    cout<<"t1-- ="<<t1;
    return 0;
}


CTime.h

#ifndef CTIME_H_INCLUDED
#define CTIME_H_INCLUDED

#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){}
    friend istream &operator>>(istream &,CTime &);
    friend ostream &operator<<(ostream &,CTime &);
    //二目的比较运算符重载
    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;
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;
}
istream &operator>>(istream &input,CTime &t)
{
    cout<<"输入时分秒:";
    input>>t.hour>>t.minute>>t.sec;
    return input;
}
ostream &operator<<(ostream &output,CTime &t)
{
    output<<t.hour<<":"<<t.minute<<":"<<t.sec<<endl;
    return output;
}



突然想到,既然是程序功能的增添和扩充,版本号是不是该改一改了?大笑

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值