简体   繁体   English

C#应用程序如何为此实现字典或哈希表?

[英]C# application how could i implement dictionary or hashtable for this?

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, ie for 07/08/2013 for time 5:00 - 7:00 pm enter text blah blah 这是我的问题,我想编写一个基本的控制台应用程序,在该应用程序中输入日期作为输入,如果尚未输入该日期,则允许该应用程序有时间输入注释,即2013年8月8日的时间为5:00 -晚上7:00输入文字等等

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. 然后应用程序将继续循环播放,如果我输入相同的日期,则我不应该能够输入与上述相同的时间,但是例如,我应该能够输入7:00至8。

i was thinking of using dictionary : 我正在考虑使用字典:

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

and adding date as id, but it seems only one element id can be entered uniquely 并添加日期作为ID,但似乎只能唯一输入一个元素ID

can some one please help 有人可以帮忙吗

Do you have to use a list or dictionary? 您是否必须使用列表或字典? If you will not be searching in it alot and do not have a lot of elements, you are probably best going with a generic list: List<Booking> 如果您不会在其中大量搜索并且没有很多元素,则最好使用通用列表: List<Booking>

Then you can use LINQ to search through the list to retrieve the bookings that you need. 然后,您可以使用LINQ搜索列表以检索所需的预订。

create a key with two dateTime (the start and end dateTime). 用两个dateTime(开始和结束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. 当您要输入新的预订时,请检查(例如,使用linq)是否在新的开始日期和结束日期时间之间存在已知的结束日期时间,并使用已知的开始日期时间进行相同操作。 If there is no overlap, add it. 如果没有重叠,请添加它。

Here's an example: 这是一个例子:

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;

using: 使用:

 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! 祝好运!

i would recommend a dictionary with the key being the date and the value be an array of 24 booleans represent hours. 我会推荐一本字典,键是日期,值是一个由24个布尔值组成的数组,表示小时。 that is only if the booking is for full hours. 只有当预订是全小时。 if it's by the minute then the array will be too big and then another option might be needed. 如果到了分钟,那么数组将太大,那么可能需要另一个选择。

each cell in the array is true if the hour is booked, for example if <07/08/13, booked[2] = true> means that hour 2-3 is booked on 07/08/13 is booked. 如果预订了小时,则数组中的每个单元格都为true,例如,如果<07/08/13,booked [2] = true>表示预订了2-3小时,则预订了07/08/13。

when you get a new booking you'll need to check each hour between the two. 当您获得新的预订时,您需要检查两者之间的每个小时。 if you got hours 4-10 you'll need to check the value in 4,5,6,7,8,9. 如果您有4-10个小时的工作时间,则需要检查4,5,6,7,8,9中的值。 not the best efficiency i think, but not that bad if no one is booking a full week or so. 我认为这不是最好的效率,但是如果没有人预订整整一周左右的时间,那还不是那么糟糕。

sum it up my answer is using 总结一下我的答案正在使用

Dictionary<string/DateTime, bool[24]> bookingTbl

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM