如何比较C+&#x2B中的两个DateTime结构有效?
我有以下DateTime结构:
struct DateTime
{
std::uint16_t year;
std::uint8_t month;
std::uint8_t day;
std::uint8_t hour;
std::uint8_t minute;
std::uint8_t second;
std::uint16_t milisecond;
};
我的疑问是关于 Lessthan 和更大的方法方法。我的实施如下,以避免一堆 ifs 和 elses ,但我可能没有涵盖所有可能的情况:
bool GreaterThan(const DateTime& datetime)
{
bool greater{true};
// When found a different value for the most significant value, the evaluation is interrupted
if ((year <= datetime.year) && (month <= datetime.month || year < datetime.year) &&
(day <= datetime.day || month < datetime.month) && (hour <= datetime.hour || day < datetime.day) &&
(minute <= datetime.minute || hour < datetime.hour) &&
(second <= datetime.second || minute < datetime.minute) &&
(milisecond <= datetime.milisecond || second < datetime.second))
{
greater = false;
}
return greater;
}
bool LessThan(const DateTime& datetime)
{
bool less{true};
// When found a different value for the most significant value, the evaluation is interrupted
if ((year >= datetime.year) && (month >= datetime.month || year > datetime.year) &&
(day >= datetime.day || month > datetime.month) && (hour >= datetime.hour || day > datetime.day) &&
(minute >= datetime.minute || hour > datetime.hour) &&
(second >= datetime.second || minute > datetime.minute) &&
(milisecond >= datetime.milisecond || second > datetime.second))
{
less = false;
}
return less;
}
请让我知道哪种情况不是覆盖。
I have the following DateTime structure:
struct DateTime
{
std::uint16_t year;
std::uint8_t month;
std::uint8_t day;
std::uint8_t hour;
std::uint8_t minute;
std::uint8_t second;
std::uint16_t milisecond;
};
My doubt is about the LessThan and GreaterThan methods. I have implemented as follows in order to avoid a bunch of ifs and elses, but I might have not covered all possible situations:
bool GreaterThan(const DateTime& datetime)
{
bool greater{true};
// When found a different value for the most significant value, the evaluation is interrupted
if ((year <= datetime.year) && (month <= datetime.month || year < datetime.year) &&
(day <= datetime.day || month < datetime.month) && (hour <= datetime.hour || day < datetime.day) &&
(minute <= datetime.minute || hour < datetime.hour) &&
(second <= datetime.second || minute < datetime.minute) &&
(milisecond <= datetime.milisecond || second < datetime.second))
{
greater = false;
}
return greater;
}
bool LessThan(const DateTime& datetime)
{
bool less{true};
// When found a different value for the most significant value, the evaluation is interrupted
if ((year >= datetime.year) && (month >= datetime.month || year > datetime.year) &&
(day >= datetime.day || month > datetime.month) && (hour >= datetime.hour || day > datetime.day) &&
(minute >= datetime.minute || hour > datetime.hour) &&
(second >= datetime.second || minute > datetime.minute) &&
(milisecond >= datetime.milisecond || second > datetime.second))
{
less = false;
}
return less;
}
Please, let me know which possible situation is not covered.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
从所有评论中,我认为最好的解决方案是由 -
Richard Critten :
From all the comments, I think the best solution was proposed by –
Richard Critten:
您的实施看起来过于痛苦。有一种更简单,合乎逻辑的方法来实现这一目标。如果您知道贪婪的算法,那是与此类似的思考过程。
您可以类似地实现
Lessthan
。Your implementation looks overly painful. There's a simpler, logical way to acheive this. If you know of the greedy algorithm, its a similar thought process to that.
You can implement
LessThan
similarly.