Advertisement
Guest User

Untitled

a guest
Mar 29th, 2017
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.87 KB | None | 0 0
  1. #include <iostream>
  2. #include "hashtable.h"
  3. #include <string.h>
  4. #include <sstream>
  5. #include <stdio.h>
  6.  
  7. #define VMAX 100
  8.  
  9. using namespace std;
  10.  
  11. unsigned int customHash(string key) {
  12. unsigned int hkey = 0;
  13. for (unsigned int i = 0; i < key.length(); i++)
  14. hkey = (hkey * 31 + key[i]) % VMAX;
  15. return hkey;
  16. }
  17.  
  18. int main() {
  19. Hashtable< string, string> myHashtable = Hashtable< string, string>( VMAX, customHash);
  20. string inputLine;
  21. //char *command, *key, *value;
  22. //string command, key, value;
  23. char command[10], key[100], value[10000];
  24.  
  25. while(1) {
  26. getline( cin, inputLine); /* put "me" "there" */
  27. command[0] = '\0';
  28. key[0] = '\0';
  29. value[0] = '\0';
  30. sscanf( inputLine.c_str(), "%s %s %[^\t\n]", command, key, value);
  31. for( unsigned int i=1; i < strlen(key); i++) {
  32. key[i-1] = key[i];
  33. }
  34. key[ strlen(key)-2] = '\0';
  35. for( unsigned int i=1; i < strlen(value); i++) {
  36. value[i-1] = value[i];
  37. }
  38. value[ strlen(value)-2] = '\0';
  39.  
  40. //cout << command << endl << key << endl << value << endl;
  41.  
  42. if( strcmp( command, "put") == 0) {
  43. string keyString( key);
  44. string valueString( value);
  45. if( myHashtable.has_key( keyString)) {
  46. myHashtable.put( keyString, valueString);
  47. cout << keyString << " - modificat cu succes!" << endl;
  48. }
  49. else {
  50. myHashtable.put( keyString, valueString);
  51. cout << keyString << " - adăugat cu succes!" << endl;
  52. }
  53. }
  54.  
  55. if( strcmp( command, "get") == 0) {
  56. string keyString( key);
  57. if( myHashtable.has_key( keyString)) {
  58. cout << keyString << " - " << myHashtable.get( keyString) << endl;
  59. }
  60. else {
  61. cout << keyString << " - cuvânt inexistent!" << endl;
  62. }
  63. }
  64.  
  65. if( strcmp( command, "quit") == 0) {
  66. break;
  67. }
  68. }
  69.  
  70.  
  71. return 0;
  72. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement