Help with some complex decision making

This is a discussion on Help with some complex decision making within the C Programming forums, part of the General Programming Boards category; Hello, I am writing a function that searches a linked list(database) against a key of the same type as the ...

  1. #1
    Registered User mlupo's Avatar
    Join Date
    Oct 2001
    Posts
    72

    Help with some complex decision making

    Hello,
    I am writing a function that searches a linked list(database) against a key of the same type as the struct members of the linked list.

    Structure looks like this:
    typedef struct
    {
    int month,day,year;
    }DATE;

    typedef struct Key KEY;
    typedef struct Key* KEYS;

    struct Key
    {
    char *sender;
    char *addressee;
    char *regarding;
    DATE date;
    int id;
    char *fname;
    KEYS next;
    };
    ----------------------

    so far so good.

    Function prototype looks like this
    list = the linked list (all the records in our database)
    sKey = a search key.
    both containing all the same members of the defined struture.

    KEY search(KEYS list, KEY sKey )

    First of all I check to see if there is a match for unique data.
    if(sKey.fname) or if(sKey.id) This is fine. There is no need to search any further if we have these. It's when we don't have these things get a little more complex.

    The real problem here is that the search criteria could be wide (1 search term) or very narrow (4 search terms)

    So consider one instance for example. A user wishes to search the linked list by "sender" and "addressee". This should return a list of matching nodes from the data we are searching. So ALL nodes in the list that match where John Doe send Jane Doe an email will be pulled up from our linked list (it's a database)

    The rest of the "unknown" data is initialized to "0" before being passed into the search function.
    i.e looking at each value of this in the debugger...

    sKey.addressee = "Jane Doe"
    sKey.sender ="John Doe"
    sKey.regarding="0"
    sKey.fname = "0"
    sKey.date.month = 11
    sKey.date.day= 17
    sKey.date.yeah = 2001
    sKey.id="0"

    Sure, it's easy to compare any ONE of the members of this structure to all the nodes in the list that's passed in.

    BUT when you have to compare MORE than one things get hairy.
    If I could see an example of this complex if statement it might help me to write the rest.

    Thanks,
    Mike

    ps...Just incase you wish to see the code or have it run locally...
    it's in the zip attached.
    It's got everything needed to build and run.
    I am working inside of search.c
    the value for sKey is built and passed from Menu.c to give me stuff to pass in so I can test my work.
    use command line arg keylist.dat
    Attached Files Attached Files
    NEVER PET YOUR DOG WHILE IT'S ON FIRE!

  2. #2
    Registered User
    Join Date
    Sep 2001
    Posts
    752
    Perhaps you could use a function that takes two keys and returns 1 or zero if the first key meets the criteria for the second key?
    Code:
    int isKeyMatch(const KEY * key1, const KEY * key2)
    {
     return (
      (strcmp (key1.sender, "0") == 0 || strcmp (key1.sender, key2.sender) == 0) &&
      (strcmp (key1.addressee, "0") == 0 || strcmp (key1.addressee, key2.addressee) == 0) &&
      (strcmp (key1.regarding, "0") == 0 || strcmp (key1.regarding, key2.regarding) == 0) &&
      (key1.date.day == 0 || key1.date.day == key2.date.day) &&
      (key1.date.month == 0 || key1.date.month == key2.date.month) &&
      (key1.date.year == 0 || key1.date.year == key2.date.year) &&
      (key1.id == 0 || key1.id == key2.id) &&
      (strcmp (key1.fname, "0") == 0 || strcmp (key1.fname, key2.fname) == 0));
    }
    That's a hell of a return statement, eh? I'm not sure it's all syntactically correct, but the idea is that it goes through each element of the structure, and for each element, it checks to see if the search value is 0, or if the search value is the same as the other value passed. The way I wrote it, key1 is the search value, and key2 is the value you are checking to see whether it matches key1's criteria. If I might make a suggestion though, instead of using "0", maybe you should try using something like NULL, or an empty string... it's just kinda a good C-habit to use.
    Callou collei we'll code the way
    Of prime numbers and pings!

  3. #3
    Registered User mlupo's Avatar
    Join Date
    Oct 2001
    Posts
    72
    Unfortunately,
    this is being integrated into a design. I cannot call anything outside of the standard C library. I can't add any new functions to the flow.

    I am only writing the search function for this database. I am blind to what else is going on around the search() function.

    So I am sorta in a pickle.

    Does the If statement you wrote (mine actually resembles yours) cover ALL the cases? or should I implement several if statements using this design one for each "first" member to check?



    Thanks
    NEVER PET YOUR DOG WHILE IT'S ON FIRE!

  4. #4
    Registered User mlupo's Avatar
    Join Date
    Oct 2001
    Posts
    72
    QuestionC,
    thanks for your input.
    I looked at your example. I implemented the idea in my code. It works!
    Yippee!!

    Thanks to you....I am doing the breakthrough dance!


    Mike
    NEVER PET YOUR DOG WHILE IT'S ON FIRE!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. making sprites
    By DavidP in forum Game Programming
    Replies: 9
    Last Post: 02-20-2010, 07:00 AM
  2. a good decision?
    By Masterx in forum A Brief History of Cprogramming.com
    Replies: 9
    Last Post: 02-14-2009, 12:13 PM
  3. Making great graphics
    By MadCow257 in forum Game Programming
    Replies: 1
    Last Post: 02-20-2006, 11:59 PM
  4. thinking about making a network traffic monitor
    By jimjamjahaa in forum C++ Programming
    Replies: 9
    Last Post: 10-13-2005, 12:38 PM
  5. Problem from texbook
    By Unregistered in forum C++ Programming
    Replies: 5
    Last Post: 07-26-2002, 05:55 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21