Thread: Implementation ideas needed

  1. #1
    Registered User Sharke's Avatar
    Join Date
    Jun 2008
    Location
    NYC
    Posts
    303

    Implementation ideas needed

    I wrote a simple little program for my business a while ago in Python and I would really like to rewrite it in C (or maybe even C++) as an exercise. However I'm having trouble visualizing how to do the same thing in C.

    Its function is very simple. I use multiple Outlook calendars to keep work schedules for my employees. They have recurring appointments with regular clients every week as well as various other appointments. The appointments are stored in Outlook as the name of the client and the fee. At the end of every week I need to tally up all of the appointments and their fees and display a summary to help me when I do the books. It's a pain to do by hand because clients sometimes have appointments at various different fees and may be seen by more than one employee during the week.

    I export the calendars into text files containing tab separated values with two fields - client/fee. My current program reads the files line by line, splits the line by the tab and feeds the data into a Python dictionary whose keys represent the clients and the value of each key is another nested dictionary whose keys are the different fees (in string form) and the value of those keys is a numeric tally. It's then a simple matter to calculate totals and print a detailed summary like the following for each client:

    Code:
    P Taylor
    5 @ $15
    2 @ $17.50
    2 @ $20
    Total: $150
    What would be the best way to achieve the same in C? Would it be a linked list of structs (one for each client) each containing a link list of structs (for each fee)? I can't seem to wrap my head around it and it just seems like it should be so simple. I don't want to have a set number of fees either, it has to be open ended because sometimes negotiations are made and different fees charged.
    Last edited by Sharke; 05-01-2009 at 01:19 AM.

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    I like the struct idea, but I'd use pair of double array's inside them. One for rate and one for time at the corresponding index of rate.

    Something like:
    Code:
    #define MaxRates 50
    
    typedef struct client  {
       char Name[50];
       double rate[MaxRates];
       double time[50];
    };  
    
    
    client clients[200];
    The idea being parallel arrays, where the rate at rate[0], would correspond with that clients time in time[0], same for rate[1] and time[1], etc.

    If the program was just in-house, I'd be quite tempted to make the array global in scope. It's just easier. If you look to commercialize it, then a local clients array with the needed dynamic memory allocation, is the way to go.

    Sure, it sets a max limit to the number of rates for any client, but I'm sure you can pick a number that is way larger than what you'd need to change, within the next decade.

    Unless you're quite comfortable with linked lists, I wouldn't put two of them inside another linked list.

    I'm not certain if a dictionary in Python is the same as a hash table in C, or not.
    Last edited by Adak; 05-01-2009 at 03:18 AM.

  3. #3
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Sure, but this question is more about relational structures/entities than anything else.

    Personally I would probably do:
    Code:
    typedef struct appointment_
    {
       int appTime;            /* time of appointment, just an example */
       int fee;                /* perhaps the fee in cents (no rounding errors) */
       appointment_s * next;
    } appointment_s;
    
    typedef struct client_
    {
       char firstName[32];
       char lastName[32];
       appointment_s * app;    /* first appointment */
    } client_s;
    
    /* ... */
    
    /* returns the client total fee for a given time frame */
    int tally_client(client_s * cl, int from, int to)
    {
       appointment_s * app = cl->app;
       int cents = 0;
    
       /* TODO: You could keep a sorted linked list of appointment "times" */
       while(app != NULL)
       {
          if(app->appTime <= to && app->appTime >= from)
             cents += fee;
    
          app = app->next;
       }
       
       return cents;
    }
    Far more flexible, and pretty much database theory applies.

  4. #4
    Registered User Sharke's Avatar
    Join Date
    Jun 2008
    Location
    NYC
    Posts
    303
    Thanks guys, I've got some good ideas from that. Is it that big a deal to have a linked list of linked lists though? I have some pretty solid linked list generics which just require a few small application specific functions. I was thinking that the top level would be a linked list of client structs each of which contain a name and a pointer to a small linked list of fee structs, each of which contain a rate and a tally. So when I read a line from the text file it would just be a matter of:

    Code:
    if (client does not exist)
      {
          Add new name to client list
          Create fee struct and initialize count to 1
      }
    else
      {
          if (fee exists)
              Add 1 to tally
          else
              Create new fee struct and initialize count to 1, add to fee list
      }
    Then when it comes to displaying the summary I just traverse the client list and traverse each clients fee list. I have to preserve all the separate fee information because the purpose of the summary is to help me create the invoices in Quickbooks, so I can't just make do with a grand total for each client - it has to be broken up into rates.

    I like the idea of two corresponding arrays for the fees though, that would make things a lot simpler. I'm torn between going for the easier option or making it deliberately harder for myself as an exercise (after all, the Python program works fine so it's not as if its performance needs improving.)

    Python does implement its dictionaries as hash tables as far as I know.

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    If there's no fixed maximum number of either clients or fees, then a list of lists would be the way to go, IMO.


    Quzah.
    Hope is the first step on the road to disappointment.

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Sure, but this question is more about relational structures/entities than anything else.
    Meaning:

    1) You'll never know - only the heart of a true believer can understand it.

    2) We should be expecting a visit from an "entity", any day now.

    3) We will be baffled with more bull........ as soon as I find a bigger shovel.

    4) I'm from the government, and I'm here to help you.

    5) It's time for the bat signal, Mr. Mayor!

    6) I read this in a book somewhere, and it really sounds intellectual.


Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Project ideas.
    By Wraithan in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 04-25-2009, 03:29 PM
  2. Ideas, how do you get them? xD
    By Akkernight in forum A Brief History of Cprogramming.com
    Replies: 10
    Last Post: 01-22-2009, 03:53 AM
  3. Project ideas
    By steve1_rm in forum C++ Programming
    Replies: 6
    Last Post: 04-14-2008, 05:34 AM
  4. c++ OOP project ideas
    By newbie17 in forum C++ Programming
    Replies: 4
    Last Post: 02-25-2002, 12:40 PM
  5. Program Ideas Needed
    By pkananen in forum C++ Programming
    Replies: 9
    Last Post: 02-24-2002, 10:08 PM