Results 1 to 10 of 10

Thread: Algorithm problem!

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    Aug 2003
    Location
    Edinburgh, UK
    Posts
    2,773

    Algorithm problem!

    Hi!

    I guess im REALLLLY tired at the minute and will think this through tomorrow but its something that isnt going to make me go to bed easily!

    OK:

    I have a start time and end time
    a user also enters a start time and end time

    I want to see that if the user's entered start time and time conflicts with the set start time and end time.

    how do I find this out?

    I will have 2 DateTime objects for each date time set. One object will be the start time and the other the end time.

    so i wanna check if the user entered their start/end time does NOT conflict with another start/end time.

    how?

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Algorithm problem!

    Define "conflict". Here's an example but it might not do what you want:
    VB Code:
    1. If (startTime2 >= startTime1 AndAlso startTime2 < endTime1) OrElse _
    2.    (endTime2 > startTime1 AndAlso endTime2 <= endTime1) OrElse _
    3.    (startTime2 <= startTime1 AndAlso endTime2 >= endTime1) Then
    4.     MessageBox.Show("Intervals overlap.")
    5. End If
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: Algorithm problem!

    Or even
    Code:
    if ((endTime1 > startTime1) && (endTime2 > startTime2) &&
        ((startTime2 >= endTime1) || (endTime2 <= startTime1)))
    {
        // intervals do not overlap
    }
    else
    {
        // overlap
    }
    Last edited by penagate; Nov 2nd, 2005 at 11:40 PM.

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Algorithm problem!

    Oops. C#, yeah... Nice penagate.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    PowerPoster
    Join Date
    Aug 2003
    Location
    Edinburgh, UK
    Posts
    2,773

    Re: Algorithm problem!

    hehe thanks, i think that's pretty much what I had in mind

    what i mean by conflict is basically the times collashing together

  6. #6

    Thread Starter
    PowerPoster
    Join Date
    Aug 2003
    Location
    Edinburgh, UK
    Posts
    2,773

    Re: Algorithm problem!

    lol im sorry guys

    just forgotten this completely.

    right, im trying to save these times etc... in an XML file. BUT i want them ordered, in otherwords, I want each record to be stored sequentially on the date time

    does that make sense?

    so when a user enters a date/time, that is saved to the xml file
    but when it is saving, it checks the xml nodes to see where it can fit in this new node to make sure that the times/dates do not collash

    understand? :-s

    how can I make this work?

    open xml file
    for each node
    make date time object
    if xmlNode DateTime object < the user set date/time

    .. ??
    end for each

  7. #7

    Thread Starter
    PowerPoster
    Join Date
    Aug 2003
    Location
    Edinburgh, UK
    Posts
    2,773

    Re: Algorithm problem!

    im afraid that solution did not work!

  8. #8
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: Algorithm problem!

    In that case you are going to have to explain, clearly, what exactly you want, and why the solutions given don't work for you. What do you mean by "collashing" ?

  9. #9

    Thread Starter
    PowerPoster
    Join Date
    Aug 2003
    Location
    Edinburgh, UK
    Posts
    2,773

    Re: Algorithm problem!

    as said before - to make sure that they do not overlap

    so for example:

    starttime1: 3.00 endtime1: 4.00
    starttime2: 4.01 endtime2: 5.12
    starttime3: 3.23 endtime3: 3:59

    in that example, starttime3 will collash - because starttime1 and endtime1 fit in that range.

    Basically no time should interfere with any other time, making sure that each time is more or less than the previous time but certainly making sure that they do not collide with each other.

    take for example your daily routine

    you may have several meetings but you have to make sure that you do not have 2 meetings at 1 time but to make sure one proceeds the next....

  10. #10
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: Algorithm problem!

    Put all your start times into one array and end times into another array.

    Then you can check against all of them when adding a new one.
    Code:
    if (newEnd > newStart)
    {
        for (int i = 0; i < startTimes.GetUpperBound(0); i++)
        {
            if (((newStart >= startTimes[i]) && (newStart <= endTimes[i])) ||
                ((newEnd >= startTimes[i]) && (newEnd <= endTimes[i])))
            {
                MessageBox.Show("Intervals overlap.");
            }
            else
            {
                // Add the node
            }
        }
    }
    else
    {
        MessageBox.Show("Idiot, you can't finish before you start.");
    }

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width