home *** CD-ROM | disk | FTP | other *** search
/ Microsoftware Monthly 19…2 Programming Power Tools / MASO9512.ISO / cpptutor / cpptutor.arj / EXAMPLES / EX09091.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1993-10-25  |  3.1 KB  |  134 lines

  1. // \EXAMPLES\EX09091.CPP
  2.  
  3. // Files used in this example
  4. //-------------------------------------------------------------
  5. // %F,15,EX09091.H%EX09091.H
  6. // EX09091.CPP    this file
  7. // %F,15,EX0909.CPP%EX0909.CPP
  8. //-------------------------------------------------------------
  9.  
  10. class Grade;
  11. class GradeIterator;
  12.  
  13. #include <string.h>
  14. #include <search.h>
  15. #include <iostream.h>
  16. #include "EX09091.H"
  17.  
  18. const LongPair Grade::null = {0, 0};
  19.  
  20. ostream&
  21. operator<<( ostream& os, LongPair range)
  22. {
  23.    // Print the grade range in brackets
  24.    os << "("
  25.       << range.first
  26.       << ", " << range.second
  27.       << ")" << flush;
  28.    return os;
  29. }
  30.  
  31.  
  32. ostream&
  33. operator<<( ostream& os, Grade::GradeAssoc assoc)
  34. {
  35.    // Print the letter grade
  36.    os << (char *)assoc.letter
  37.       << " "
  38.       << assoc.range
  39.       << flush;
  40.    return os;
  41. }
  42.  
  43. char*
  44. Grade::operator[]( int x)
  45. {
  46.    GradeAssoc* pAssoc = (GradeAssoc *)NULL;
  47.    GradeAssoc* pCurrAssoc = &GradeList[0];
  48.    long count = NumElem();
  49.  
  50.    // Iterate through the associative array, stop if
  51.    // a range containing the value is found or if
  52.    // we go past the end of the array
  53.  
  54.    do
  55.    {
  56.       if ((pCurrAssoc->range.first <= x)
  57.            && (pCurrAssoc->range.second >= x))
  58.       {
  59.          pAssoc = pCurrAssoc;
  60.          break;
  61.       }
  62.       else
  63.       {
  64.          pCurrAssoc++;
  65.       }
  66.    } while ((--count) > 0);
  67.  
  68.    // return letter grade if found, NULL otherwise
  69.    return (pAssoc ? pAssoc->letter : (char *)NULL);
  70. }
  71.  
  72. LongPair
  73. Grade::operator[]( char* p)
  74. {
  75.    // Use the "C" library binary search to find the letter grade
  76.    GradeAssoc* pAssoc = (GradeAssoc *)bsearch
  77.                (
  78.                  (const void*)p,
  79.                  (const void*)GradeList,
  80.                  NumElem(),
  81.                  sizeof( GradeAssoc),
  82.                  &CompareLetter
  83.                );
  84.  
  85.    // if found return numeric range, return null otherwise
  86.    if ( pAssoc == (GradeAssoc *)NULL)
  87.       return null;
  88.    else
  89.       return pAssoc->range;
  90. };
  91.  
  92.  
  93. //   int _Optlink              //  IBM CSet II
  94. int                            //  Microsoft and Borland
  95. CompareLetter( const void* keyval, const void* pvDatum)
  96. {
  97.    Grade::GradeAssoc* pDatum = (Grade::GradeAssoc *)pvDatum;
  98.    int x = strcmp(
  99.                     (char *)keyval,
  100.                     (char *)(pDatum->letter)
  101.                  );
  102.    return x;
  103. }
  104.  
  105.  
  106. // Define and initialize to associative array
  107. Grade::GradeAssoc Grade::GradeList[] =
  108.                   {
  109.                      {"A", {80, 100}}, {"B", {70, 79}},
  110.                      {"C", {60, 69}}, {"D", {50, 59}},
  111.                      {"F", {0, 49}}
  112.                   };
  113.  
  114. GradeIterator::GradeIterator( const Grade& grade)
  115. {
  116.    pGrade = &grade; // point to the associative array
  117.    index = 0;  // set index to first element of associative array
  118. }
  119.  
  120.  
  121. Grade::GradeAssoc*
  122. GradeIterator::operator()()
  123. {
  124.    // return the next association if one exists
  125.    // otherwise return NULL
  126.    if (index < pGrade->NumElem())
  127.       return &(pGrade->GradeList[index++]);
  128.    else
  129.       return ((Grade::GradeAssoc *)NULL);
  130. }
  131.  
  132.  
  133.  
  134.