8-2-Time类中的运算符的重载(部分)

来源:互联网 发布:arm linux gcj 编辑:程序博客网 时间:2024/05/18 19:36
01./*  02.* 程序的版权和版本声明部分:  03.* Copyright (c) 2011, 烟台大学计算机学院  04.* All rights reserved.  05.* 文件名称:test.cpp  06.* 作    者:刘芳07.* 完成日期:2014 年04 月 19 日  08.* 版 本 号:v1.0  09.* 对任务及求解方法的描述部分: 10.* 输入描述:无  11.* 问题描述: 12.* 程序输出: 13.* 问题分析:略 14.* 算法设计:略  15.*/    

#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);    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为: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秒前的时间};//下面实现所有的运算符重载代码。//自行编制用于测试的main()函数,有些结果不必依赖display()函数,提倡用单步执行查看结果void CTime::setTime(int h,int m,int s){    hour=h;    minute=m;    second=s;}bool CTime::operator>(CTime &t){    if(hour>t.hour||(hour==t.hour&&minute>t.minute)||            (hour==t.hour&&minute==t.minute&&second>t.second))        return true;    else        return false;}bool CTime::operator < (CTime &t){    if(hour<t.hour||(hour==t.hour&&minute<t.minute)||            (hour==t.hour&&minute==t.minute&&second<t.second))        return true;    else        return false;}bool CTime::operator >= (CTime &t){    if(!operator<(t))        return true;    else        return false;}bool CTime::operator <=(CTime &t){    if(!operator>(t))        return true;    else        return false;}bool CTime::operator == (CTime &t){    if(hour==t.hour&&minute==t.minute&&second==t.second)        return true;    else        return false;}bool CTime::operator != (CTime &t){    if(!operator == (t))        return true;    else        return false;}CTime::CTime(int h,int m,int s ){    hour=h;    minute=m;    second=s;}CTime CTime::operator+(CTime &t){    CTime b;    int q,w,e;    q=second+t.second;    w=minute+t.minute;    e=hour+t.hour;    if(q<=59)        b.second=q;    if(q>59)    {        b.second=q%60;        w=(q/60)+w;    }    if(w<=59)    b.minute=w;    if(w>59)    {        b.minute=w%60;        e=(w/60)+e;    }if(e<=23)    b.hour=e;    if(e>23)    b.hour=e%24;    return b;    //if(b.second>=60&&b.minute<59}CTime CTime::operator-(CTime &t){    CTime t4;    int s=0,m=0,h=0;    s=second-t.second;    {        if(s>=0)            t4.second=s;        else        {            s=(s+60);            t4.second=s;            m--;        }    }    m=(minute-t.minute+m);    {        if(m>=0)            t4.minute=m;        else        {            m=(m+60);            h--;            t4.minute=m;        }    }    h=hour-t.hour+h;    {        if(h>=0)            t4.hour=h;        else        {            h=h+24;            t4.hour=h;        }    }    return t4;}void CTime::display(){    cout<<hour<<":"<<minute<<":"<<second<<endl;}CTime CTime::operator+(int s){    CTime b;    b.second=second+s;    b.minute=minute;    b.hour=hour;    if(b.second>59){        b.second=(b.second%60);        b.minute=(b.second/60)+minute;}    if(b.minute>59)    {        b.minute=(b.minute%60);        b.hour=(b.minute/60)+hour;    }    if(b.hour>23)    {        b.hour=b.hour%24;    }    return b;}int main(){    int a,b,c,d;    CTime t1,t2,t3,t4;    cout<<"请输入t1的时,分,秒:"<<endl;    cin>>a>>b>>c;    t1.setTime(a,b,c);    cout<<"请输入t2的时,分,秒:"<<endl;    cin>>a>>b>>c;    t2.setTime(a,b,c);    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<<"t1+t2=";    t3=t1+t2;    t3.display();    cout<<"t1-t2=";    t3=t1-t2;    t3.display();    cout<<"请输入增加的秒数:"<<endl;    cin>>d;    t4=t1+d;    t4.display();    return 0;}

0 0
原创粉丝点击