定义一个时间结构体,成员包括时,分,秒,如何编写一个函数用来比较两个时间的大小?
答案:1 悬赏:80
解决时间 2021-02-15 08:23
- 提问者网友:紫柔同归
- 2021-02-14 23:59
定义一个时间结构体,成员包括时,分,秒,如何编写一个函数用来比较两个时间的大小?
最佳答案
- 二级知识专家网友:白日梦制造商
- 2021-02-15 00:56
我试一下,呵呵。其实很简单的,对时间结构体重载一下<和>号,至于时间大小的具体判断方法是可以把时分秒都换算成秒来比较。代码如下:
struct MyTime
{
int m_isec; //秒
int m_imin; //分
int m_ihour; //时
int GetSecond() //将时间格式转换成秒
{
return m_isec+m_imin*60+m_ihour*3600;
}
friend bool operator < (const MyTime&,const MyTime&);
friend bool operator > (const MyTime&,const MyTime&);
};
bool operator < (const MyTime & argtm1,const MyTime &argtm2)
{
MyTime tm1,tm2;
tm1=argtm1;
tm2=argtm2;
return tm1.GetSecond()-tm2.GetSecond()<0;
}
bool operator > (const MyTime & argtm1,const MyTime & argtm2)
{
MyTime tm1,tm2;
tm1=argtm1;
tm2=argtm2;
return tm1.GetSecond()-tm2.GetSecond()>0;
}
struct MyTime
{
int m_isec; //秒
int m_imin; //分
int m_ihour; //时
int GetSecond() //将时间格式转换成秒
{
return m_isec+m_imin*60+m_ihour*3600;
}
friend bool operator < (const MyTime&,const MyTime&);
friend bool operator > (const MyTime&,const MyTime&);
};
bool operator < (const MyTime & argtm1,const MyTime &argtm2)
{
MyTime tm1,tm2;
tm1=argtm1;
tm2=argtm2;
return tm1.GetSecond()-tm2.GetSecond()<0;
}
bool operator > (const MyTime & argtm1,const MyTime & argtm2)
{
MyTime tm1,tm2;
tm1=argtm1;
tm2=argtm2;
return tm1.GetSecond()-tm2.GetSecond()>0;
}
我要举报
如以上问答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯