Advertisement
Guest User

Untitled

a guest
Feb 27th, 2020
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.24 KB | None | 0 0
  1. #define _CRT_SECURE_NO_WARNINGS
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include <math.h>
  6.  
  7. unsigned long long hashfunc(char* word, int n)
  8. {
  9. unsigned long long hashcode = 0;
  10.  
  11. for(int j = 0; j < strlen(word);++j)
  12. {
  13. hashcode += pow(word[j], j);
  14.  
  15. }
  16. printf("%lld\n",hashcode%n);
  17. return hashcode % n;
  18. }
  19. void printa(char** names, int n)
  20. {
  21. for (int i = 0; i < n; ++i)
  22. {
  23. if (names[i] != 0)
  24. {
  25. printf("%d - %s\n", i, names[i]);
  26. }
  27. }
  28. }
  29. int main()
  30. {
  31. int n = 0;
  32. printf("Enter amount of cells\n");
  33. scanf("%d", &n);
  34. getchar();
  35. char** names = calloc(n, sizeof(char*));
  36.  
  37.  
  38. printf("Enter name to get hash code and save in database\nEnter \'STOP\' for stop entering infomation\n");
  39. int i = 0;
  40. char buffer[255] = {0};
  41. while (1)
  42. {
  43.  
  44. gets_s(buffer, 255);
  45. if (strcmp(buffer, "STOP") == 0)
  46. {
  47.  
  48. break;
  49. }
  50. names[hashfunc(buffer, n)] = calloc(strlen(buffer)+1, sizeof(char));
  51. strcpy(names[hashfunc(buffer, n)], buffer);
  52. printf("word number - %d\n", i+1);
  53. ++i;
  54. }
  55.  
  56. printa(names, n);
  57. getchar();
  58. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement