home *** CD-ROM | disk | FTP | other *** search
/ Il CD di internet / CD.iso / SOURCE / CONTRIB / MBASE / MBASE51.TAR / mbase51 / examples / example2.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-09-04  |  2.1 KB  |  119 lines

  1.  
  2. #include <mbase.h>
  3. #include "example2.h"
  4.  
  5. /*
  6.  * PROTOTYPES -----------------------------------------------------------------
  7.  *
  8.  */
  9.  
  10.    void main XARGS( (void) );
  11.    long avg  XARGS( (char *) );
  12.  
  13.  
  14. /*
  15.  * CODE -----------------------------------------------------------------------
  16.  *
  17.  */
  18.  
  19. void
  20. main ()
  21. {
  22.    long  num;
  23.  
  24.    num = avg ("example2.rel");
  25.  
  26.    printf ("Average was %ld\n", num);
  27.  
  28.    exit (0);
  29. }
  30.  
  31.  
  32. /*
  33.  * Average all values within named relation: ----------------------------------
  34.  *
  35.  */
  36.  
  37. long
  38. avg  (filename)
  39. char *filename;
  40. {
  41.    relation *rel;    /* Relation structure */
  42.    example2  rec;    /* See EXAMPLE2.H and EXAMPLE2.S for this */
  43.    mb_action act;
  44.    long      ttl = 0L, num;
  45.  
  46.  
  47. /*
  48.  * The first thing is to open the named relation...
  49.  *
  50.  */
  51.  
  52.    if ((rel = mb_inc (filename, "")) == RNULL)
  53.       {
  54.       fprintf (stderr, "mb_inc() failed: %s\n", mb_error);
  55.       mb_exit (1);
  56.       }
  57.  
  58.  
  59. /*
  60.  * Then we figure out if there are any records to average.  Do this
  61.  * before we do any calculations, so we don't end up dividing by
  62.  * zero.
  63.  *
  64.  */
  65.  
  66.    if ((num = mb_num (rel)) == 0)
  67.       {
  68.       return 0L;
  69.       }
  70.  
  71.  
  72. /*
  73.  * Now the tricky part.  First, walk through the normal heap--those
  74.  * records that have been indexed--and sum the 'val' field.
  75.  *
  76.  */
  77.  
  78.    for (act = FIRST; ; act = NEXT)
  79.       {
  80.       if (mb_sel (rel, idxnum(rel,"ix_item"), &rec, act, NULL) != MB_OKAY)
  81.          {
  82.          break;
  83.          }
  84.       ttl += rec.val;
  85.       }
  86.  
  87.  
  88. /*
  89.  * And, if there are any records in the queue (secondary heap--those
  90.  * records that haven't yet been indexed) and sum the 'val' field.
  91.  * Why the second loop?  Because records that haven't been indexed
  92.  * won't be reached through the last one.
  93.  *
  94.  */
  95.  
  96.    if (mb_num_q (rel))
  97.       {
  98.       for (act = FIRST_Q; ; act = NEXT_Q)
  99.          {
  100.          if (mb_sel (rel, idxnum(rel,"ix_val"), &rec, act, NULL) != MB_OKAY)
  101.             {
  102.             break;
  103.             }
  104.          ttl += rec.val;
  105.          }
  106.       }
  107.  
  108. /*
  109.  * Now close any relations that are open, and return the average
  110.  * of the values we've found.
  111.  *
  112.  */
  113.  
  114.    mb_die ();       /* close all opened relations */
  115.  
  116.    return (ttl / num);
  117. }
  118.  
  119.