关于时间的计算

输入:T1= 11时:50分:40秒,T2= 12时:20分:20秒

输出:T2-T1= 0时:29分:40秒


#include 
#include 
using namespace std;
class Data
{
private:
    int hour;
    int minute;
    int second;

public:
    Data(int phour =0,int pminute =0,int psecond =0):hour(phour),minute(pminute),second(psecond) {}
    friend Data operator-(const Data&T1,const Data&T2);
    friend ostream&operator<<(ostream&out,const Data&T);
    void showtime()const;
};
void Data::showtime()const
{
    cout<Data operator-(const Data&T1,const Data&T2)
{
    int ih, im, is, sum_sec;
    sum_sec=(T1.hour*3600+T1.minute*60+T1.second) - (T2.hour*3600+T2.minute*60+T2.second);
    ih = sum_sec/3600;
    im = sum_sec%3600/60;
    is = sum_sec%60;
    return Data(ih,im,is);
}

方法二:

Data operator-(const Data&T1,const Data&T2)
{
    int ih, im, is;
    ih = T1.hour-T2.hour;
    im = T1.minute-T2.minute;
    is = T1.second-T2.second;
if(is < 0)
    {
        is = 60 +is;
        im--;
    }
    if(im < 0)
    {
        im = 60 +im;
        ih--;
    }
    return Data(ih,im,is);
}

 

你可能感兴趣的:(PTA)