home *** CD-ROM | disk | FTP | other *** search
/ C Programming Starter Kit 2.0 / SamsPublishing-CProgrammingStarterKit-v2.0-Win31.iso / tybc4 / io4.cpp < prev    next >
C/C++ Source or Header  |  1993-04-10  |  4KB  |  161 lines

  1. /*
  2.    C++ program that demonstrates random-access binary file I/O
  3. */
  4.  
  5. #include <iostream.h>
  6. #include <fstream.h>
  7. #include <stdlib.h>                
  8. #include <string.h>
  9.  
  10. const unsigned MIN_SIZE = 5;
  11. const unsigned STR_SIZE = 31;
  12. const double BAD_VALUE = -1.0e+30;
  13. const unsigned NOT_FOUND = 0xffff;
  14. enum boolean { false, true };                 
  15.  
  16. class VmArray
  17. {
  18.    protected:
  19.      fstream f;
  20.      unsigned size;
  21.      double badIndex;
  22.  
  23.    public:
  24.      VmArray(unsigned Size, const char* filename);
  25.      ~VmArray()
  26.        { f.close(); }
  27.      unsigned getSize() const
  28.        { return size; }
  29.      boolean writeElem(const char* str, unsigned index);
  30.      boolean readElem(char* str, unsigned index);
  31.      void Combsort();
  32.      unsigned binSearch(const char* search);
  33. };
  34.  
  35. VmArray::VmArray(unsigned Size, const char* filename)
  36. {                    
  37.   char s[STR_SIZE+1];
  38.   size = (Size < MIN_SIZE) ? MIN_SIZE : Size;
  39.   badIndex = BAD_VALUE;
  40.   f.open(filename, ios::in | ios::out | ios::binary);
  41.   if (f.good()) {
  42.     // fill the file stream with empty strings
  43.     strcpy(s, "");;
  44.     f.seekg(0);
  45.     for (unsigned i = 0; i < size; i++)
  46.       f.write((unsigned char*)s, sizeof(s));
  47.   }
  48. }
  49.  
  50. boolean VmArray::writeElem(const char* str, unsigned index)
  51. {
  52.    if (index < size) {
  53.      f.seekg(index * (STR_SIZE+1));
  54.      f.write((unsigned char*)str, STR_SIZE+1);
  55.      return (f.good()) ? true : false;
  56.    }
  57.    else
  58.      return false;
  59. }
  60.  
  61. boolean VmArray::readElem(char* str, unsigned index)
  62. {
  63.    if (index < size) {
  64.      f.seekg(index * (STR_SIZE+1));
  65.      f.read((unsigned char*)str, STR_SIZE+1);
  66.      return (f.good()) ? true : false;
  67.    }
  68.    else
  69.      return false;
  70. }
  71.  
  72. void VmArray::Combsort()
  73. {
  74.    unsigned i, j, gap = size;
  75.    boolean inOrder;
  76.    char strI[STR_SIZE+1], strJ[STR_SIZE+1];
  77.  
  78.    do {
  79.      gap = (gap * 8) / 11;
  80.      if (gap < 1)
  81.        gap = 1;
  82.      inOrder = true;
  83.      for (i = 0, j = gap; i < (size - gap); i++, j++) {
  84.        readElem(strI, i);
  85.        readElem(strJ, j);
  86.        if (strcmp(strI, strJ) > 0) {
  87.          inOrder = false;
  88.          writeElem(strI, j);
  89.          writeElem(strJ, i);
  90.        }
  91.      }
  92.    } while (!(inOrder && gap == 1));
  93. }
  94.                                             
  95. unsigned VmArray::binSearch(const char* search)
  96. {                             
  97.   unsigned low = 0;
  98.   unsigned high = size - 1;
  99.   unsigned median;
  100.   char str[STR_SIZE+1];                      
  101.   int result;
  102.    
  103.   do {   
  104.     median = (low + high) / 2;
  105.     readElem(str, median);
  106.     result = strcmp(search, str);
  107.     if (result > 0)
  108.       low = median + 1;
  109.     else
  110.       high = median - 1;
  111.   } while (result != 0 && low <= high);
  112.   return (result == 0) ? median : NOT_FOUND;
  113. }                                            
  114.                                             
  115. main()
  116. {                                                
  117.   const unsigned NUM_ELEMS = 10;
  118.   char* data[] = { "Michigan", "California", "Virginia", "Main",
  119.                    "New York", "Florida", "Nevada", "Alaska",
  120.                    "Ohio", "Maryland" };
  121.   VmArray arr(NUM_ELEMS, "arr.dat");
  122.   char str[STR_SIZE+1];
  123.   char c;
  124.   unsigned index;
  125.  
  126.   // assign values to array arr
  127.   for (unsigned i = 0; i < arr.getSize(); i++) {
  128.     strcpy(str, data[i]);
  129.     arr.writeElem(str, i);
  130.   }
  131.   // display unordered array
  132.   cout << "Unsorted arrays is:\n";
  133.   for (i = 0; i < arr.getSize(); i++) {
  134.     arr.readElem(str, i);
  135.     cout << str << "\n";
  136.   }
  137.   // pause
  138.   cout << "\nPress any key and then Return to sort the array...";
  139.   cin >> c;
  140.   // sort the array
  141.   arr.Combsort();
  142.   // display sorted array
  143.   cout << "Sorted arrays is:\n";
  144.   for (i = 0; i < arr.getSize(); i++) {
  145.     arr.readElem(str, i);
  146.     cout << str << "\n";
  147.   }                                 
  148.   // pause
  149.   cout << "\nPress any key and then Return to search the array...";
  150.   cin >> c;                                      
  151.   // search for array elements using the pointer data
  152.   for (i = 0; i < NUM_ELEMS; i++) {
  153.     index = arr.binSearch(data[i]);
  154.     if (index != NOT_FOUND) 
  155.       cout << "Found " << data[i] 
  156.            << " at index " << index << "\n";
  157.     else 
  158.       cout << "No match for " << data[i] << "\n";
  159.   }                                                    
  160.   return 0;
  161. }