第四章 重载课堂作业

#include<iostream>    
#include<string>    
using namespace std;    
class Time    
{    
public:    
    void display()
    {
        cout<<hour<<":"<<minute<<":"<<sec<<endl;
    }    
protected:    
    int hour;    
    int minute;    
    int sec;    
};    
class Date    
{    
public:    
    void display1()
    {
        cout<<year<<":"<<month<<":"<<day<<endl;
    }    
protected:    
    int year;    
    int month;    
    int day;    
};    
class DateTime:public Time,public Date    
{     
public:    
    friend istream& operator >>(istream &input,DateTime &t1);    /*运算符的>> << + -的重载都设为友元函数  */  
    friend ostream& operator <<(ostream &output,DateTime &t1);    
    friend DateTime operator +(DateTime &t1,DateTime &t2);    
    friend DateTime operator -(DateTime &t1,DateTime &t2);    
private:    
   string place;    
};    
istream& operator >>(istream &input,DateTime &t1)    
{    
    input>>t1.place>>t1.year>>t1.month>>t1.day>>t1.hour>>t1.minute>>t1.sec;    
    return input;    
}    
ostream& operator<<(ostream &output,DateTime &t1)    
{    
    output<<t1.place<<":"<<t1.year<<"/"<<t1.month<<"/"<<t1.day<<"/"<<t1.hour<<":"<<t1.minute<<":"<<t1.sec;    
    return output;    
}    
DateTime operator +(DateTime &t1,DateTime &t2)    
{    
    DateTime t3;    
    t3.sec=t1.sec+t2.sec;    
    t3.minute=t1.minute+t2.minute;    
    t3.hour=t1.hour+t2.hour;    
    t3.day=t1.day+t2.day;    
    t3.month=t1.month+t2.month;     
    t3.year=t1.year;    
    t3.place=t1.place;     
    if(t3.sec>60)    
    {    
        t3.sec-=60;    
        t3.minute+=1;    
    }    
    if(t3.minute>60)    
    {    
        t3.minute-=60;    
        t3.hour+=1;    
    }    
    if(t3.hour>24)    
    {    
        t3.hour-=24;    
        t3.day+=1;    
    }    
    if(t3.month==1||3||5||7||8||10||12)    
    {    
        if(t3.day>31)    
        {    
            t3.day-=31;    
            t3.month+=1;    
        }    
    }    
    if(t3.month==4||6||8||10||11)    
    {    
        if(t3.day>30)    
        {    
            t3.day-=30;    
            t3.month+=1;    
        }    
    }    
    if(t3.month==2)    
    {    
        if(t3.day>28)    
        {    
            t3.day-=28;    
            t3.month+=1;    
        }    
    }    
    if(t3.month>12)    
    {    
        t3.month-=12;    
        t3.year+=1;    
    }    
    return t3;      
}    
DateTime operator -(DateTime &t1,DateTime &t2)    
{    
    DateTime t3;    
    t3.sec=t1.sec-t2.sec;    
    t3.minute=t1.minute-t2.minute;    
    t3.hour=t1.hour-t2.hour;    
    t3.day=t1.day-t2.day;    
    t3.month=t1.month-t2.month;     
    t3.year=t1.year;    
    t3.place=t1.place;     
    if(t3.sec<0)    
    {    
        t3.sec+=60;    
        t3.minute-=1;    
    }    
    if(t3.minute<0)    
    {    
        t3.minute+=60;    
        t3.hour-=1;    
    }    
    if(t3.hour<0)    
    {    
        t3.hour+=24;    
        t3.day-=1;    
    }    
    if(t3.month==1||3||5||7||8||10||12)    
    {    
        if(t3.day<0)    
        {    
            t3.day+=31;    
            t3.month-=1;    
        }    
    }    
    if(t3.month==4||6||8||10||11)    
    {    
        if(t3.day<0)    
        {    
            t3.day+=30;    
            t3.month-=1;    
        }    
    }    
    if(t3.month==2)    
    {    
        if(t3.day<0)    
        {    
            t3.day+=28;    
            t3.month-=1;    
        }    
    }    
    if(t3.month<0)    
    {    
        t3.month+=12;    
        t3.year-=1;    
    }    
    return t3;      
}    
        
int main()    
{    
    DateTime d1,d2,d3,d4;    
    cout<<"地点:"<<'\t'<<"年:"<<'\t'<<"月"<<'\t'<<"日"<<'\t'<<"时"<<'\t'<<"分"<<'\t'<<"秒"<<endl;     
    cin>>d1;    
    cin>>d2;     
    cout<<"两时间相加为:"<<endl;     
    d3=d1+d2;    
    d4=d1-d2;    
    cout<<d3<<endl;    
    cout<<"两时间相减为:"<<endl;    
    cout<<d4<<endl;    
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值