home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / misc / volume1 / 8708 / 7 / form_cost.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-07-13  |  2.9 KB  |  121 lines

  1. #include <stdio.h>
  2. #include <string.h>
  3.  
  4. #define SYSTEMS 100    /* Max number of systems to track */
  5. #define SYS_SIZE 11    /* Number of chars in system name (including null) */
  6. #define BUF_SIZE 80    /* Max chars in line */
  7.  
  8. FILE *fopen(), *costs_file;
  9.  
  10. struct costs {
  11.     char system[SYS_SIZE];
  12.     int first;
  13.     int cost;
  14.     int minimum;
  15.     long minutes;
  16.     long tot_cost;
  17.     int used;
  18. } system_costs[SYSTEMS];
  19.  
  20. struct costs *c_ptr;
  21.  
  22. #define max(a,b) ((a)>(b)?(a):(b))
  23.  
  24. int systems;
  25.  
  26. int lookfor();
  27.  
  28. int main() {
  29.     int mins;
  30.     int i;
  31.     char sys[SYS_SIZE];
  32.     long this_cost;
  33.     int sc;
  34.     char buffer[BUF_SIZE];
  35.     long total;
  36.     long tot_mins;
  37.  
  38.     /*
  39.     Read the costs file, one record per system.
  40.     */
  41.     costs_file = fopen("/usr/lib/uucp/uucost/costs","r");
  42.     c_ptr = system_costs;
  43.  
  44.     while (fgets (buffer, (int)sizeof(buffer), costs_file)) {
  45.     if (buffer[0] != '#') { /* ignore comments */
  46.         sc = sscanf(buffer, "%s%d%d%d", c_ptr->system, &c_ptr->first, 
  47.         &c_ptr->cost, &c_ptr->minimum);
  48.         if (sc == 4) {
  49.         c_ptr->minutes = 0L;
  50.         c_ptr->tot_cost = 0L;
  51.         c_ptr->used = 0;
  52.         c_ptr++;
  53.         } else {
  54.         (void)fprintf (stderr, "Format error in costs file.  ");
  55.         (void)fprintf (stderr, "Input was: %s\n", buffer);
  56.         }
  57.     }
  58.     }
  59.  
  60.     systems = c_ptr - system_costs;  /* number of system_costs entries used */
  61.  
  62.     /*
  63.     For each line of standard input:
  64.  
  65.     1) look up system in systems_costs structure
  66.     2) add the time to the running count for this system
  67.     3) figure the cost of the session and add it in
  68.     */
  69.     while (scanf("%s%s%d", sys, buffer, &mins) != EOF) {
  70.     if ((i = lookfor(sys)) == -1) {
  71.             (void) fprintf (stderr,"System %s not found in costs file.\n", sys);
  72.     } else {
  73.         c_ptr = &system_costs[i];
  74.         c_ptr->used = 1;
  75.         c_ptr->minutes += (long)mins;
  76.         this_cost = (long)c_ptr->first;
  77.         this_cost += (long)c_ptr->cost * (long)(mins-1);
  78.         this_cost = max(this_cost, (long)c_ptr->minimum);
  79.         c_ptr->tot_cost += this_cost;
  80. #ifdef DEBUG
  81.         (void) printf ("%-10s %-30s %3d mins = $%3ld.%.2ld\n", 
  82.         sys, buffer, mins, this_cost/100L, this_cost%100L);
  83. #endif /* DEBUG */
  84.     }
  85.     }
  86.  
  87.     /*
  88.     For each system that had activity:
  89.     1) print the name and minutes and cost
  90.     2) keep total of minutes used
  91.     3) keep total of cents used
  92.     */
  93.     total = 0L;
  94.     tot_mins = 0L;
  95.     (void) printf ("\nSystem      Mins       Cost\n");
  96.     for (c_ptr = system_costs; c_ptr < &system_costs[systems]; c_ptr++) {
  97.     if (c_ptr->used) {
  98.         (void) printf ("%-10s %5ld    $%3ld.%.2ld\n", c_ptr->system, 
  99.         c_ptr->minutes, c_ptr->tot_cost/100L, c_ptr->tot_cost%100L);
  100.         total += c_ptr->tot_cost;
  101.         tot_mins += c_ptr->minutes;
  102.     }
  103.     }
  104.     (void) printf ("\nTOTAL      %5ld    $%3ld.%.2ld\n", 
  105.     tot_mins, total/100L, total%100L);
  106.     return (0);
  107. }
  108.     
  109.  
  110. int lookfor(sys)
  111.     char *sys;
  112. {
  113.     int i;
  114.  
  115.     for (i = 0; i < systems; i++) {
  116.     if (!strcmp (sys, system_costs[i].system))
  117.         return(i);
  118.     }
  119.     return (-1); /* Indicate system not found */
  120. }
  121.