功能比较两个约会 [英] Function to compare two appointment
本文介绍了功能比较两个约会的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
用户将输入两个约会
比较的计算是否冲突
User will input two appointments
What is the calculation to compare is conflict or not
int main(){
struct Appointment aptOne, aptTwo;
make_appointment(&aptOne, "Appointment One",
2018, 1, 23, 11, 30, 12, 45);
make_appointment(&aptTwo, "Appointment Two",
2018, 1, 23, 12, 45, 13, 15);
}
我的尝试:
What I have tried:
引用:
试图拥有一个函数和布尔值
trying to have a function and boolean
struct Appointment {
char description;
int year;
int month;
int day;
int startHour;
int startMinute;
int endHour;
int endMinute;
};
bool has_conflict( struct Appointment* pAptA,
struct Appointment* pAptB)
{
return true;
}
}
int main(){
-----
if (has_conflict( &aptOne, &aptTwo)){
printf("Appointments CLASH!\n");
} else {
printf("Appointments Fine!\n");
}
}
推荐答案
想一想:你会在真实的中做些什么世界?
你会说:约会A从X开始,到Y结束。预约B的开始是否属于那个?预约B的结束是否属于那个?
如果任何一个问题都是真的,它们会重叠。
但是...制造东西整个负载更容易,不要存储这样的约会。
有一个名为 time_t [ ^ ]哪些商店一个日期和时间值,其中包括比较功能,这将使您的生活更轻松 - 特别是如果约会可能跨越午夜。
如果您在约会中存储两个time_t值来表示开始和约会的终点,您可以使用 difftime [ ^ ]非常容易比较两个完整的日期时间对象。
Think about it: what would you do in the "real world"?
You'd say: Appointment A starts at X, and end at Y. Does the start of Appointment B fall inside that? Does the end of Appointment B fall inside that?
If either question is true, they overlap.
But... to make things a whole load easier, don't store your appointments like that.
There is a struct called time_t[^] which stores a date and time value, and which includes comparison functions which would make your life a whole load easier - particularly if an appointment might span midnight.
If you store two time_t values in your Appointment to represent the start and end point of the appointment, you can use difftime[^] to compare the two whole datetime objects very easily.
以愚蠢的方式做到这一点mpare每个元素
do it in a dumb way compare each of the element
bool has_conflict( struct Appointment* pAptA,
struct Appointment* pAptB)
{
if((*pAptA).year == (*pAptB).year &&
(*pAptA).month == (*pAptB).month &&
(*pAptA).day == (*pAptB).day &&
(*pAptA).startHour == (*pAptB).startHour &&
(*pAptA).startMinute == (*pAptB).startMinute &&
(*pAptA).endHour == (*pAptB).endHour &&
(*pAptA).endMinute == (*pAptB).endMinute){
return true;
}else {return false;}
这篇关于功能比较两个约会的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文