Advertisement
Purianite

Untitled

Sep 23rd, 2011
392
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.94 KB | None | 0 0
  1. //main.cpp for final exam #2
  2. #include <iostream>
  3. #include <string>
  4.  
  5. using namespace std;
  6.  
  7. //2-dimensional table representing records in your file.
  8. const int row = 7;
  9. const int col = 3;
  10. string data[row][col] =
  11. {
  12.     //In order: name/key, email, balance
  13.     {"Gary Smith", "GarySmith@neit.edu", "$15.00"},
  14.     {"Laila Kerniech", "LailaKerniech@neit.edu", "$30.00"},
  15.     {"Shawn Kemp", "ShawnKemp@neit.edu", "$23.50"},
  16.     {"Sarah Palin", "SarahPalin@neit.edu", "$110.00"},
  17.     {"Ray Connif", "RayConnif@neit.edu", "$0.00"},
  18.     {"Edmund Cortis", "EdmundCortis@neit.edu", "$50.00"},
  19.     {"John Matis", "John Matis@neit.edu", "$15.00"},
  20. };
  21.  
  22. string dataHashed[1324][3]; //Hashed records will be put here.
  23.  
  24. class Search
  25. {
  26. public:
  27.     int hashRoutine(string keyToSearch)
  28.         {
  29.             int sum = 0;
  30.             int len = keyToSearch.length();
  31.             for (int ndx = 0; ndx < len; ndx++)
  32.             {
  33.                 int decVal = keyToSearch.at(ndx);
  34.                 sum = sum +decVal;
  35.             }
  36.             return sum;
  37.         }
  38.  
  39.     void displayRecordFromHashtable(int index)
  40.     {
  41.         cout << "Record = "
  42.             + dataHashed[index][0]
  43.             +", "
  44.             + dataHashed[index][1]
  45.             +", "
  46.             + dataHashed[index][2]
  47.             +"\n";
  48.     }
  49.  
  50.     int hash(string keyToSearch)
  51.         {
  52.             cout << "Searching for " << keyToSearch << " using hash method.\n";
  53.             int foundAtIndex = hashRoutine(keyToSearch);
  54.             cout << "Key " << keyToSearch << " is found at index " << foundAtIndex;
  55.             cout << " after 1 read.\n";
  56.             return foundAtIndex;
  57.         }
  58. };
  59.  
  60. int main(int argc, char* argv[])
  61. {
  62.     Search *search = new Search;
  63.  
  64.     for (int ndx = 0; ndx < row; ndx++)
  65.     {
  66.         int generatedHashedIndex = search->hashRoutine(data[ndx][0]);
  67.         cout << "generatedHashedIndex = " <<generatedHashedIndex << "\n";
  68.         dataHashed[generatedHashedIndex][0] = data[ndx][0];
  69.         dataHashed[generatedHashedIndex][1] = data[ndx][1];
  70.         dataHashed[generatedHashedIndex][2] = data[ndx][2];
  71.     }
  72.  
  73.     int foundAt = search->hash("Sarah Palin");
  74.     search->displayRecordFromHashtable(foundAt);
  75.     cin.get();
  76.     return 0;
  77. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement