C#应用程序我怎么能实现词典或哈希表呢?应用程序、词典、我怎么能、哈希表呢

2023-09-04 05:52:36 作者:寄给你的风

这是我的问题,我想要写在那里我输入一个日期作为输入,一个基本的控制台应用程序,如果该日期处理不当被输入的应用程序,然后让时间进入一个音符时,即2013年7月8日时间5:00 - 下午7:00输入文本等等等等

This is my problem, i want to write a basic console application where i enter a date as input, if that date hasnt been entered the application then allow a time to enter a note, i.e for 07/08/2013 for time 5:00 - 7:00 pm enter text blah blah

然后应用程序将继续循环,如果我输入相同的日期,我不应该是能够进入 同倍以上,但我应该能够进入7点到8例如。

then application will keep looping, if i enter the same date, i shouldnt be able to enter the same times as above, but i should be able to enter 7:00 to 8 for example.

我在考虑使用字典的:

Dictionary<string, Booking> BookingDict = new Dictionary<string, Booking>();

和添加日期作为ID,但似乎只有一个元素的ID都可以被唯一进入

and adding date as id, but it seems only one element id can be entered uniquely

可有一个人请帮助

推荐答案

创建了两个日期时间(开始和结束日期时间)的一个关键。当你想进入一个新的预订,查询(LINQ的举例),如果有一个已知的结束日期时间那是你的新的开始和结束日期时间之间以及与已知的开始日期时间这样做。如果没有重叠,添加它。

create a key with two dateTime (the start and end dateTime). When you want to enter a new booking, check (with linq for instance) if there's a known end Datetime that is between your new start and end datetime and do the same with the known start datetime. If there is no overlap, add it.

下面是一个例子:

Dictionary<TwoUintsKeyInfo,object> test = new Dictionary<TwoUintsKeyInfo, object>();
        test.Add(new TwoUintsKeyInfo { IdOne = 3, IdTwo = 9 }, new object());
        test.Add(new TwoUintsKeyInfo { IdOne = 10, IdTwo = 15 }, new object());

        uint newStartPoint1 = 16,newEndPoint1=20;
        bool mayUse = (from result in test 
                            let newStartPointIsBetweenStartAndEnd = newStartPoint1.Between(result.Key.IdOne,result.Key.IdTwo)
                            let newEndPointIsBetweenStartAndEnd = newEndPoint1.Between(result.Key.IdOne,result.Key.IdTwo)
                            let completeOverlap = result.Key.IdOne < newStartPoint1 && result.Key.IdTwo > newEndPoint1
                            let oldDateWithingNewRange = result.Key.IdOne.Between(newStartPoint1, newEndPoint1) || result.Key.IdTwo.Between(newStartPoint1, newEndPoint1)
                            let FoundOne = 1
                            where newStartPointIsBetweenStartAndEnd || newEndPointIsBetweenStartAndEnd || completeOverlap || oldDateWithingNewRange
                            select FoundOne).Sum()==0;

使用:

 public static class LinqExtentions
{
    /// <summary>
    /// Note: For the compare parameters; First the low, than the High
    /// </summary>
    /// <returns>Bool</returns>
    public static bool Between<T1, T2, T3>(this T1 actual, T2 lowest, T3 highest)
        where T1 : IComparable
        where T2 : IConvertible
        where T3 : IConvertible
    {
        return actual.CompareTo(lowest.ToType(typeof(T1), null)) >= 0 &&
               actual.CompareTo(highest.ToType(typeof(T1), null)) <= 0;
    }
}

public class TwoUintsKeyInfo
{
    public uint IdOne { get; set; }
    public uint IdTwo { get; set; }

    public class EqualityComparerTwoUintsKeyInfo : IEqualityComparer<TwoUintsKeyInfo>
    {
        public bool Equals(TwoUintsKeyInfo x, TwoUintsKeyInfo y)
        {
            return x.IdOne == y.IdOne &&
                    x.IdTwo == y.IdTwo;
        }

        public int GetHashCode(TwoUintsKeyInfo x)
        {
            return (Math.Pow(x.IdOne, Math.E) + Math.Pow(x.IdTwo, Math.PI)).GetHashCode();
        }
    }
}

我测试过,似乎是工作。 祝你好运!

I tested it, seems to be working. Good luck!