C# 有人能为我简化这个算法吗?
基本上,我只想检查一个时间段是否与另一个时间段重叠。 空结束日期表示无限期结束。有人能帮我缩短一下吗?因为有时读起来很难。干杯C# 有人能为我简化这个算法吗?,c#,.net,algorithm,C#,.net,Algorithm,基本上,我只想检查一个时间段是否与另一个时间段重叠。 空结束日期表示无限期结束。有人能帮我缩短一下吗?因为有时读起来很难。干杯 public class TimePeriod { public DateTime StartDate { get; set; } public DateTime? EndDate { get; set; } public bool Overlaps(TimePeriod other) {
public class TimePeriod
{
public DateTime StartDate { get; set; }
public DateTime? EndDate { get; set; }
public bool Overlaps(TimePeriod other)
{
// Means it overlaps
if (other.StartDate == this.StartDate
|| other.EndDate == this.StartDate
|| other.StartDate == this.EndDate
|| other.EndDate == this.EndDate)
return true;
if(this.StartDate > other.StartDate)
{
// Negative
if (this.EndDate.HasValue)
{
if (this.EndDate.Value < other.StartDate)
return true;
if (other.EndDate.HasValue && this.EndDate.Value < other.EndDate.Value)
return true;
}
// Negative
if (other.EndDate.HasValue)
{
if (other.EndDate.Value > this.StartDate)
return true;
if (this.EndDate.HasValue && other.EndDate.Value > this.EndDate.Value)
return true;
}
else
return true;
}
else if(this.StartDate < other.StartDate)
{
// Negative
if (this.EndDate.HasValue)
{
if (this.EndDate.Value > other.StartDate)
return true;
if (other.EndDate.HasValue && this.EndDate.Value > other.EndDate.Value)
return true;
}
else
return true;
// Negative
if (other.EndDate.HasValue)
{
if (other.EndDate.Value < this.StartDate)
return true;
if (this.EndDate.HasValue && other.EndDate.Value < this.EndDate.Value)
return true;
}
}
return false;
}
}
公共类时间段
{
公共日期时间起始日期{get;set;}
公共日期时间?结束日期{get;set;}
公共布尔重叠(时间段其他)
{
//意味着它重叠了
如果(other.StartDate==this.StartDate
||other.EndDate==this.StartDate
||other.StartDate==this.EndDate
||other.EndDate==此.EndDate)
返回true;
如果(this.StartDate>other.StartDate)
{
//否定的
if(this.EndDate.HasValue)
{
if(this.EndDate.Valuethis.StartDate)
返回true;
if(this.EndDate.HasValue&&other.EndDate.Value>this.EndDate.Value)
返回true;
}
其他的
返回true;
}
else if(this.StartDateother.StartDate)
返回true;
if(other.EndDate.HasValue&&this.EndDate.Value>other.EndDate.Value)
返回true;
}
其他的
返回true;
//否定的
if(other.EndDate.HasValue)
{
if(other.EndDate.Value
查看以下内容:
通常,如果所有变量都是可为空的日期时间,那么
return (StartA.HasValue? StartA.Value:DateTime.Minimum) <=
(EndB.HasValue? EndB.Value:DateTime.Maximum) &&
(EndA.HasValue? EndA.Value:DateTime.Maximum) >=
(StartB.HasValue? StartB.Value:DateTime.Minimum);
失败案例2:其他启动后的顶端-失败
|-----------|
|------|
在所有其他情况下,开始在另一端之前,结束在另一端之后
案例A
|----------|
|-----|
案例B
| ---------|
|-------------------|
案例C
|-----------|
|------|
案例D
|-----------|
|-------------|
公共布尔重叠(时间段其他)
{
返回(other.StartDate>=StartDate&&
(EndDate==null | | other.StartDate=other.StartDate&&
(other.EndDate==null | | StartDate这个怎么样:
public bool Overlaps(TimePeriod other)
{
bool isOtherEarlier = this.StartDate > other.StartDate;
TimePeriod earlier = isOtherEarlier ? other : this;
TimePeriod later = isOtherEarlier ? this : other;
return !earlier.EndDate.HasValue || earlier.EndDate > later.StartDate;
}
在处理纯布尔逻辑的任何时候,都可以将算法提取为一条语句。但不要因为可以,就认为应该这样做。除非性能至关重要,否则一定要选择可读代码而不是紧凑代码。(不一定是紧凑性==性能)
这很容易理解,因为它完全由单个和表达式组成,很明显,它们都决定了不重叠:
public bool Overlaps(TimePeriod other)
{
if (other.EndDate.HasValue && other.EndDate < StartDate)
return false;
if (EndDate.HasValue && EndDate < other.StartDate)
return false;
if (!EndDate.HasValue && other.EndDate < StartDate)
return false;
if (!other.EndDate.HasValue && EndDate < other.StartDate)
return false;
return true;
}
公共布尔重叠(时间段其他)
{
if(other.EndDate.HasValue&&other.EndDate
并不是说其他答案不好(我喜欢Adam的;他的格式显然是为了帮助可读性)。我这么说是因为很明显你是个初学者,我认为这是一个没有得到足够重视的教训(我很内疚)。有人(我想Martin Fowler)曾经说过这样的话:任何傻瓜都能写出计算机能理解的代码,但一个好的程序员却能写出人类能理解的代码。“不要认为它处理空结束日期不像Adams解决方案那样有一行,但我认为可读性更高一些。但这可能只是口味的问题。这也是错误的……首先比较必须检查这个。开始日期与其他。结束日期我认为唯一的“错误”这是因为phil选择假设接触边界的日期不被认为是重叠的。但这实际上只是一个选择的问题,而不是一个对错的问题。只需将>改为>=以另一种方式。(OP改为另一种方式)好问题,但我可以建议一个更好的名称吗?怎么样判断两个日期时间对是否重叠的简明方法?"这是一个典型的“精神暴力”问题的案例,通过尝试/如果套用所有可能的星座。人们通常会立即尝试解决问题,而不是真正思考问题。然后解决方案会变得更容易,而且更可能是一个经过深思熟虑的清晰解决方案。哇!!!代码的野兽。但你只需要通过检查来简化ur“Previous_date.endDate”日期为空(在这种情况下,后面的日期会重叠),然后将“Previous_date.endDate”与“later_date.Start”进行比较会议日期overlap@Adam,检查算法…第一次比较必须是startdate和enddate,而不是两个startdate。startdate=5 jan,enddate=10 jan;other.start=1 jan,other.enddate=1 Feb将返回false@Charles:不,不会。StartDate>other.StartDate&&StartDate(testDate>=rangeStart&&(rangeEnd==null | | testDate
public bool Overlaps(TimePeriod other)
{
bool isOtherEarlier = this.StartDate > other.StartDate;
TimePeriod earlier = isOtherEarlier ? other : this;
TimePeriod later = isOtherEarlier ? this : other;
return !earlier.EndDate.HasValue || earlier.EndDate > later.StartDate;
}
public bool Overlaps(TimePeriod other)
{
if (other.EndDate.HasValue && other.EndDate < StartDate)
return false;
if (EndDate.HasValue && EndDate < other.StartDate)
return false;
if (!EndDate.HasValue && other.EndDate < StartDate)
return false;
if (!other.EndDate.HasValue && EndDate < other.StartDate)
return false;
return true;
}