Advertisement
Guest User

Untitled

a guest
Feb 23rd, 2020
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.23 KB | None | 0 0
  1. #define _CRT_SECURE_NO_WARNINGS
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. typedef struct
  6. {
  7. char* name;
  8. }hash;
  9.  
  10. typedef
  11. struct Vector
  12. {
  13. hash** ptr;
  14. int size;
  15.  
  16. }Vector;
  17.  
  18. typedef unsigned int uint;
  19.  
  20. void initVec(Vector* vec,int quantity)
  21. {
  22. vec->ptr = (hash**)calloc(quantity, sizeof(hash*));
  23. vec->size = quantity;
  24. }
  25.  
  26. void addGoods(Vector* vec, char* name)
  27. {
  28.  
  29. if (vec->ptr == NULL)
  30. {
  31. perror("Mem error\n");
  32. exit(1);
  33. }
  34.  
  35. unsigned long long countt = 0;
  36. int i = 0;
  37. while (name[i] != '\0')
  38. {
  39. countt += name[i] * i + 1;
  40. ++i;
  41. }
  42.  
  43. vec->ptr[countt % vec->size] = calloc(1, sizeof(hash));
  44. vec->ptr[countt % vec->size]->name = malloc(sizeof(char) * (strlen(name) + 1));
  45. strcpy(vec->ptr[countt% vec->size]->name, name);
  46.  
  47. }
  48.  
  49. void printDb(Vector* vec)
  50. {
  51. for (uint i = 0; i < vec->size; ++i)
  52. {
  53. if (vec->ptr[i] != NULL)
  54. {
  55. printf("name: %s | code - %d\n", vec->ptr[i]->name, i);
  56. printf("\n");
  57. }
  58. }
  59. }
  60.  
  61. int main()
  62. {
  63. Vector hash1;
  64. initVec(&hash1,50);
  65.  
  66. char buffer[255];
  67. while (1)
  68. {
  69. printf("Enter word\n");
  70. gets_s(buffer, 255);
  71. if (strcmp(buffer, "STOP") == 0)
  72. {
  73. break;
  74. }
  75. addGoods(&hash1, buffer);
  76. }
  77. printDb(&hash1);
  78. getchar();
  79. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement