第八号周项目三 时间类

 

问题及代码

*/
ALL rights reserved.                       
*文件名称: 初学对象8                   
作者:李长鸿                    
*完成时间:2015.5.21              
*问题描述: 时间类问题               
*/
#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):hour(h),minute(m),second(s) {}
    void setTime(int h,int m,int s)
    {
        hour=h;
        minute=m;
        second=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为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秒前的时间
};
void CTime::display()
{
    cout<<hour<<":"<<minute<<":"<<second<<endl;
}
bool CTime::operator>(CTime &t)
{
    if(hour!=t.hour)
        return (hour>t.hour);
    if(minute!=t.minute)
        return (minute>t.minute);
    if(second!=t.second)
        return (second>t.second);
    return 0;
}
bool CTime::operator<(CTime&t)
{
    if(hour!=t.hour)
        return !(hour<t.hour);
    if(minute!=t.minute)
        return!(minute<t.minute);
    if(second!=t.second)
        return !(second<t.second);
    return 0;
}
bool CTime::operator>=(CTime &t)
{

    return (*this>t||*this==t);
}
bool CTime::operator<=(CTime &t)
{
    return !(*this>t);
}
bool CTime::operator==(CTime &t)
{
    if(hour==t.hour&&minute==t.minute&&second==t.second)
        return 1;
    return 0;
}
bool CTime::operator!=(CTime &t)
{
    if(*this==t)
        return 0;
    return 1;
}
CTime CTime::operator+(CTime &t)
{
    CTime T;
    T.second=second+t.second;
    T.minute=minute+t.minute+T.second/60;
    T.hour=(hour+t.hour+T.minute/60)%24;
    T.minute=T.minute%60;
    T.second=T.second%60;
    return T;
}
CTime CTime::operator-(CTime &t)
{
    CTime T;
    if(*this>=t)
    {
        if(second>t.second)
            T.second=second-t.second;
        else
        {
            T.second=t.second-second;
            minute--;
        }
        if(minute>t.minute)
            T.minute=minute-t.minute;
        else
        {
            T.minute=t.minute-minute;
            hour--;
        }
        T.hour=hour-t.hour;
        return T;
    }
    else cout<<"数据有误,不能进行计算。";
}
CTime CTime::operator+(int s)
{
    return CTime((((s+second)/60+minute)/60+hour)%24,((second+s)/60+minute)%60,(second+s)%60);
}
CTime CTime::operator-(int s)
{
    CTime T((s/3600)%24,(s/60)%60,s%60);
    return *this-T;
}
CTime CTime::operator+=(CTime &t)
{
    return *this=*this+t;
}
CTime CTime::operator-=(CTime &t)
{
    return *this=*this-t;
}
CTime CTIME;;
CTime CTime::operator+=(int s)
{
    return *this=*this+s;
}
CTime CTime::operator-=(int s)
{
    return *this=*this-s;
}
int main()
{
    CTime t1(18,56,39),t2(13,37,25),t3(15,42,19),t(18,56,39);
    int s=590;
    cout<<"t1=";
    t1.display();
    cout<<"t2=";
    t2.display();
    cout<<"t=";
    t.display();
    cout<<"t1"<<((t1==t)?"等于":"不等于")<<"t"<<endl;
    cout<<"t2"<<((t2!=t)?"不等于":"等于")<<"t"<<endl;
    cout<<"t1"<<((t1>t2)?"大于":"小于")<<"t2"<<endl;
    cout<<"t1"<<((t1>=t2)?"大于等于":"小于等于")<<"t2"<<endl;
    cout<<endl;
    cout<<"t1+t2=";
    (t1+t2).display();
    cout<<"t1-t2=";
    (t1-t2).display();
    cout<<endl;
    cout<<"s="<<s<<"秒"<<endl;
    cout<<"t1+s=";
    (t1+s).display();
    cout<<"t2+s=";
    (t2+s).display();
    cout<<"t1-s=";
    (t1-s).display();
    cout<<"t2-s=";
    (t2-s).display();
    cout<<endl;
    cout<<"t2=";
    t2.display();
    cout<<"t3=";
    t3.display();
    cout<<"t2"<<((t2>t3)?"大于":"小于")<<"t3"<<endl;
    cout<<"t2"<<((t2<=t3)?"小于等于":"大于等于")<<"t3"<<endl;
    cout<<"t2+=t3结果为:"<<"t2=";
    (t2+=t3).display();
    cout<<"t2-=t3结果为:"<<"t2=";
    (t2-=t3).display();
    return 0;
}



总结:写等于函数和不等于函数时,想用那种简单写法,== : return!(*this > t || *this <t );  != :  return(*this > t  | | *this < t )  , 最后结果老是不对.最后多亏机房可爱的小伙伴,给改了回来。其实还是笨,为什么当时没想起来看看老师的呢??

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值