码迷,mamicode.com
首页 > 其他好文 > 详细

Time 类中的运算符重载(续)

时间:2015-05-13 10:20:04      阅读:90      评论:0      收藏:0      [点我收藏+]

标签:   博客   iostream   算法   对象   

  输入代码:

/*
*Copyright (c)2015,烟台大学计算机与控制工程学院
*All rights reserved.
*文件名称:sum123.cpp
*作    者:林海云
*完成日期:2015年5月13日
*版 本 号:v2.0
*
*问题描述:在Time类中的运算符重载基础上
(1)定义对时间对象的自增和自减一目运算符
(2)定义Time类中的<<和>>运算符重载,实现时间的输入输出,改造原程序中对运算结果显示方式,使程序读起来更自然。
*程序输入:规定的两个时间点
*程序输出:分别完成两个时间的运算符操作和重载,并输出
*/
#include <iostream>
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);
    void setTime(int h,int m,int s);
    //输入输出运算的重载
    friend istream &operator>>(istream &in,CTime &t);
    friend ostream &operator<<(ostream &out,CTime t);
    //比较运算符(二目)的重载
    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 &c);//返回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 operator+=(CTime &c);
    CTime operator-=(CTime &c);
    CTime operator+=(int s);//返回s秒后的时间
    CTime operator-=(int s);//返回s秒前的时间
};
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;
}
istream &operator>>(istream &in,CTime &t)
{
    char ch1,ch2;
    while(1)
    {
        cout<<"请输入时间(hh:mm:ss)";
        cin>>t.hour>>ch1>>t.minute>>ch2>>t.second;
        if((ch1==':')&&(ch2==':'))
            break;
        else
            cout<<"输入格式错误"<<endl;
        if(t.hour>-1&&t.hour<24&&t.minute>-1&&t.minute<60&&t.second>-1&&t.second<60)
            break;
        else
            cout<<"输入时间形式错误,请重新输入"<<endl;
    }
    return  cin;
}
//重新输出运算符
ostream &operator<<(ostream &out,CTime t)
{
    out<<t.hour<<':'<<t.minute<<':'<<t.second<<endl;
    return out;
}
bool CTime::operator > (CTime &t)
{
    if(hour>t.hour)
        return true ;
    if(hour<t.hour)
        return false ;
    if(minute>t.minute)
        return true;
    if(minute<t.minute)
        return false ;
    if(second>t.second)
        return true;
    else
        return false;
}
bool CTime::operator < (CTime &t)
{
    if(hour<t.hour)
        return true ;
    if(hour>t.hour)
        return false ;
    if(minute<t.minute)
        return true;
    if(minute>t.minute)
        return false ;
    if(second<t.second)
        return true;
    else
        return false;
}
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)
        return false;
    else
        return true;
}
CTime CTime:: operator+(CTime &t)
{
    int h,m,s;
    h=hour+t.hour;
    m=minute+t.minute;
    s=second+t.second;
    if(s>60)
    {
        s-=60;
        m++;
    }
    if(m>60)
    {
        m-=60;
        h++;
    }
    if(h>23)
    {
        h-=24;
    }
    CTime t0(h,m,s);
    return t0;
}
CTime CTime::operator-(CTime &t)
{
    int h,m,s;
    h=hour-t.hour;
    m=minute-t.minute;
    s=second-t.second;
    if(s<0)
    {
        m--;
        s+=60;
    }
    if(m<0)
    {
        h--;
        m+=60;
    }
    if(h<0)
    {
        h+=24;
    }
    CTime t0(h,m,s);
    return t0;
}
CTime CTime:: operator+(int s)
{
    int hh,mm,ss;
    ss=s%60;
    mm=(s/60)%60;
    hh=s/3600;
    CTime t0(hh,mm,ss);
    return *this+t0;
}
CTime CTime:: operator-(int s)
{
    int hh,mm,ss;
    ss=s%60;
    mm=(s/60)%60;
    hh=s/3600;
    CTime t0(hh,mm,ss);
    return *this-t0;
}
//一目运算符的重载
CTime CTime:: operator++(int)//后置++
{
    CTime t=*this;
    *this=*this+1;
    return t;
}
CTime CTime:: operator--(int)//后置--
{
    CTime t=*this;
    *this=*this-1;
    return t;
}
CTime CTime:: operator++()//前置++
{
    *this=*this+1;
    return *this;
}
CTime CTime:: operator--()//前置--
{
    *this=*this-1;
    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)
{
    *this=*this+s;
    return *this;
}
CTime CTime:: operator-=(int s)
{
    *this=*this-s;
    return *this;
}
int main()
{
    CTime t1,t2,t;
    cout<<"t1为:";
    cin>>t1;
    cout<<"t2为:";
    cin>>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;
    cout<<endl;
    cout<<"t1= "<<t1<<endl;
    cout<<"t2= "<<t2<<endl;

    cout<<"t=t1++"<<endl;
    t=t1++;
    cout<<"t= "<<t<<"    t1= "<<t1<<endl;
    cout<<"t=++t1"<<endl;
    t=++t1;
    cout<<"t= "<<t<<"    t1= "<<t1<<endl;
    cout<<"t1+t2= "<<t1+t2<<endl;
    cout<<"t1-t2= "<<t1-t2<<endl;
    cout<<"t1+2000= "<<t1+2000<<endl;
    cout<<"t1-5000= "<<t1-5000<<endl;
    return 0;
}


运行结果:

技术分享

Time 类中的运算符重载(续)

标签:   博客   iostream   算法   对象   

原文地址:http://blog.csdn.net/linhaiyun_ytdx/article/details/45688863

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!