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

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