Advertisement
rinab333

CSCI 340 assignment8.cc

Jul 16th, 2017
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.82 KB | None | 0 0
  1.  
  2. #include <iostream>
  3. #include <vector>
  4. #include <cstdlib>
  5. #include <iomanip>
  6. #include <algorithm>
  7. #include<string>
  8. #include <fstream>
  9. #include <cstdio>
  10. //Terina Burr
  11. //Section One
  12. //Due 10/30/15
  13. //Assignment 6
  14.  
  15.  
  16. #include "assignment8.h"
  17. using namespace std;
  18.  
  19. // key is in form of letter-letter-digit
  20. // compute sum <-- ascii(pos1)+ascii(pos2)+ascii(pos3)
  21. // compute sum%htable_size
  22. Entry* get_entry (const string& line) ;
  23. string get_key (const string& line) ;
  24.  
  25. /***************************************************************
  26. Function: HT
  27.  
  28. Use:    It is a contructor it initializes the hash table and sets table_size to s  
  29. Arguments:  1. s: hash table size default size is 11          
  30.  
  31. Returns:   nothing
  32. ***************************************************************/
  33.  
  34. HT::HT(int s= 11 )
  35. {
  36.     hTable = new vector <Entry>(s);
  37.     table_size = s;
  38.  
  39. }
  40.  
  41. /***************************************************************
  42. Function: ~HT
  43.  
  44. Use:    it is a deconstructor that deletes the hash table
  45. Arguments: none        
  46.  
  47. Returns:   nothing
  48. ***************************************************************/
  49.  
  50.  
  51. HT::~HT()
  52. {
  53.  
  54.     delete hTable;
  55.  
  56. }
  57.  
  58. /***************************************************************
  59. Function: search
  60. Use:    it is a function that searches the hash table for certain key
  61. Arguments:  1. key: item searching for          
  62.  
  63. Returns:   - 1 if couldn’t find the key if the key is found then the position is returned
  64. ***************************************************************/
  65.  
  66. int HT::search(const string& key)
  67. {
  68.     int index = hashing(key);
  69.     int x = 0;
  70.  while ((*hTable)[(index + x) % table_size].key != key)//loop through the table till found
  71.         {
  72.                 x++;
  73.                 if (x >= table_size)//if its not in there
  74.                 {
  75.                         return -1;
  76.                 }
  77.         }
  78.     //return position
  79.         return (index + x) % table_size;
  80. }
  81.  
  82. /***************************************************************
  83. Function: insert
  84. Use:    used to insert an entry into hash table  
  85. Arguments:  1. e: entry that is inserted          
  86.  
  87. Returns:   true or false if the item was inserted
  88. ***************************************************************/
  89.  
  90. bool HT :: insert ( const Entry& e )
  91. {
  92.     if (search(e.key) != -1)//if search was correct then
  93.         {
  94.                 cerr << "Table already contains key " << e.key << endl;
  95.                 return false;
  96.         }
  97.         if (item_count >= table_size) //if table is full
  98.         {
  99.                 cerr << "Table is full" << endl;
  100.                 return false;
  101.         }
  102.         int index = hashing(e.key);
  103.         int x = 0;
  104.         while ((*hTable)[(index+x)%table_size].key != "---") //while the entry is not empty
  105.         {
  106.                 x++;
  107.         }
  108.         (*hTable)[(index + x)%table_size] = e;
  109.         item_count++;
  110.         return true;
  111. }
  112.  
  113. /***************************************************************
  114. Function: remove
  115. Use:    it is a function that removes an item with the key s
  116. Arguments:  1. s: key being removed        
  117.  
  118. Returns:   false if not found true if removed and found
  119. ***************************************************************/
  120.  
  121. bool HT::remove(const string& s)
  122. {
  123.     int index = hashing(s);
  124.         int x = 0;
  125.         while ((*hTable)[(index + x) % table_size].key != s) //loop thru hash table while key isnt in there
  126.         {
  127.                 x++;
  128.                 if (x >= table_size)
  129.                 {
  130.                         return false;
  131.                 }
  132.         }
  133.         (*hTable)[(index + x) % table_size].key = "---";
  134.         item_count--;
  135.         return true;
  136. }
  137. /***************************************************************
  138. Function: print
  139. Use:    prints hash table entries
  140. Arguments:  none        
  141.  
  142. Returns:   none
  143. ***************************************************************/
  144.  
  145. void HT :: print ( )
  146. {
  147.         cout << endl;
  148.         cout << "----Hash Table-----" << endl;
  149.         Entry e;
  150.         for (int i = 0; i < table_size; i++) //go through the hash tbale
  151.         {
  152.                 if ((*hTable)[i].key != "---") //if the key isnt null
  153.                 {
  154.                         e = (*hTable)[i];
  155.                         cout << setw(2) << i;
  156.                         cout << ": " << e.key << " " << e.description << endl;
  157.                 }
  158.         }
  159.         cout << "-------------------" << endl;
  160.         cout << endl;
  161. }
  162.  
  163. int HT::hashing(const string& key)
  164. {
  165.    return ((int)key[0] + (int)key[1] + (int)key[2])%table_size;
  166. }
  167.  
  168. /***************************************************************
  169. Function: get entry
  170. Use:    gets entry from line of string
  171. Arguments:  1. line: string being converted to entry          
  172.  
  173. Returns:   pointer to an entry
  174. ***************************************************************/
  175.  
  176. Entry* get_entry (const string& line)
  177. {
  178.     Entry *h = new Entry();
  179.         h->key = get_key(line);
  180.    
  181.     h->description = line.substr(6, line.length() - 1);
  182.        
  183.     return h;
  184. }
  185.  
  186. /***************************************************************
  187. Function: get_key
  188. Use:    it gets line of input and parses it to return the key
  189. Arguments:  1. line: string being converted to key
  190.  
  191. Returns:   string of key
  192. ***************************************************************/
  193.  
  194. string get_key (const string& line)
  195. {
  196.     if (line.length() < 5)
  197.         return "";
  198.  
  199.     return line.substr(2, 3);
  200. }
  201.  
  202. int main(int argc, char** argv ) {
  203.     if ( argc < 2 ) {
  204.         cerr << "argument: file-name\n";
  205.         return 1;
  206.     }
  207.  
  208.     HT ht;
  209.     ifstream infile(argv[1], ios::in);
  210.     string line;
  211.     infile >> line;    
  212.     while ( !infile.eof() ) {
  213.         if ( line[0] == 'A' ) {
  214.             Entry* e = get_entry( line );
  215.             ht.insert( *e );
  216.             delete e;
  217.         }
  218.         else {
  219.             string key = get_key(line);
  220.             if ( line[0] == 'D' ) {
  221.                 cout << "Removing " << key << "...\n";
  222.                 bool removed = ht.remove( key );
  223.                 if ( removed )
  224.                     cout << key << " is removed successfully...\n\n";
  225.                 else
  226.                     cout << key << " does not exist, no key is removed...\n\n";
  227.             }
  228.             else if ( line[0] == 'S' ) {
  229.                 int found = ht.search( key );
  230.                 if ( found < 0 )
  231.                     cout << key << " does not exit in the hash table ..." << endl << endl;
  232.                 else
  233.                    cout << key << " is found at table position " << found << endl << endl;
  234.             }
  235.             else if ( line[0] == 'P' ) {
  236.                 cout << "\nDisplaying the table: " << endl;
  237.                 ht.print();
  238.             }
  239.             else
  240.                 cerr << "wrong input: " << line << endl;
  241.         }
  242.         infile >> line;
  243.  
  244.     }
  245.  
  246.     infile.close();
  247.     return 0;
  248. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement