Results 1 to 3 of 3

Thread: Code Challenge (Help)

  1. #1
    Guest

    Talking

    The problem: sorting.

    I have a datatype User
    Code:
    Public Type User
         Number as Long
         ManagerNumber as Long
    End Type
    The Users are in a User array (that has a total items value) but are not in any particular order. If the user has a manager it will appear in User.ManagerNumber, the ManagerNumber corresponds to the Number of another user. If the user does not have a manager User.ManagerNumber will = 0. I want to populate a listbox with the hierarchy first with the bigboss(has users reporting to him but reports to no one) on top and then list all other users at the end.

    Here are the users(they don't necessarily appear in this order in the array):
    User A.Number = 1
    User A.ManagerNumber = 3
    User B.Number = 2
    User B.ManagerNumber = 1
    User C.Number = 3
    User C.ManagerNumber = 0
    User D.Number = 4
    User D.ManagerNumber = 0

    Here is my solution in english/pseudo code...
    1. make a temp list (user number and manager number)of all users that have managers
    2. find the one ManagerNumber that is not in the list under user number (that's the big boss) add to new list
    3. find the person that reports to the big boss in the temp list add to new list
    4. is the peron I just added in temp list under managernumber?
    5. find the person that reports to him and add to new list.
    6. (repeat 4 & 5 until user not in list under managernumber)
    7. go through original list and add all users whose managernumber = 0 AND are not the big boss to the new list

    The Answer
    C <-- the big boss
    A <-- reports to C
    B <-- reports to A
    D <-- does not report to anyone and no one reports to D

    There has got to be a better way (the datatype can't change)
    Can you come up with a better solution? before I start hurting my head with my logic?

  2. #2
    PowerPoster Lethal's Avatar
    Join Date
    Oct 2000
    Location
    Ohio
    Posts
    2,496

    Talking Something on the lines

    Here's something I did way back for a school assignment, it's similare to what your looking for, try implementing this into your project, I used the Itemdata property, which I suspect you should do also.

    Code:
    Option Explicit
    Dim ticketPrice(0 To 3) As Ticket
    
    Private Sub LoadArray()
    ticketPrice(0).price = 40#
    ticketPrice(1).price = 27.5
    ticketPrice(2).price = 15#
    ticketPrice(3).price = 10#
    End Sub
    
    Private Sub cmdCalculate_Click()
    Dim intPrice As Integer
    Dim curTotal As Currency
    Dim totaltickets As Integer
    
    totaltickets = Val(txtTickets.Text)
    If lstSeating.ListIndex = -1 Or txtTickets.Text = "" Then
        Beep
        MsgBox "You must select a seat and enter number of tickets," + vbCrLf + Space(18) + "Please try again!", vbInformation, "Selection"
        Exit Sub
    End If
    intPrice = lstSeating.ItemData(lstSeating.ListIndex)
    curTotal = ticketPrice(intPrice).price * Val(txtTickets.Text)
    lblPrice.Caption = FormatCurrency(curTotal, 2)
    ticketPrice(intPrice).tickets = ticketPrice(intPrice).tickets + totaltickets
    
    Select Case lstSeating.ListIndex
        Case 0
            lblBalcony.Caption = ticketPrice(intPrice).tickets
        Case 1
            lblGeneral.Caption = ticketPrice(intPrice).tickets
        Case 2
            lblMezzanine.Caption = ticketPrice(intPrice).tickets
        Case 3
            lblOrchestra.Caption = ticketPrice(intPrice).tickets
    End Select
    End Sub
    
    Private Sub cmdClear_Click()
    txtTickets.Text = ""
    lblPrice.Caption = ""
    txtTickets.SetFocus
    End Sub
    
    Private Sub cmdExit_Click()
    Dim Form As Form
    For Each Form In Forms
      Unload Me
    Next
    End Sub
    
    Private Sub Form_Activate()
    lstSeating.SetFocus
    SendKeys "{Down}"
    End Sub
    
    Private Sub Form_KeyPress(KeyAscii As Integer)
    If KeyAscii = 13 Then
        cmdCalculate_Click
    End If
    End Sub
    
    Private Sub Form_KeyUp(KeyCode As Integer, Shift As Integer)
    Select Case KeyCode
        Case vbKeyDelete
            cmdClear_Click
        
        Case vbKeyF1
            lstSeating.SetFocus
    End Select
    End Sub
    
    Private Sub Form_Load()
    Call LoadArray
    End Sub
    
    Private Sub txtTickets_GotFocus()
    txtTickets.SetFocus
    txtTickets.SelStart = 0
    txtTickets.SelLength = Len(txtTickets.Text)
    End Sub
    Put this in a module
    Code:
    Type Ticket
        price As Currency
        tickets As Integer
    End Type

  3. #3
    Guest
    Thanks, I'll look it over.

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