Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define _CRT_SECURE_NO_WARNINGS
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <math.h>
- unsigned long long hashfunc(char* word, int n)
- {
- unsigned long long hashcode = 0;
- for(int j = 0; j < strlen(word);++j)
- {
- hashcode += pow(word[j], j);
- }
- printf("%lld\n",hashcode%n);
- return hashcode % n;
- }
- void printa(char** names, int n)
- {
- for (int i = 0; i < n; ++i)
- {
- if (names[i] != 0)
- {
- printf("%d - %s\n", i, names[i]);
- }
- }
- }
- int main()
- {
- int n = 0;
- printf("Enter amount of cells\n");
- scanf("%d", &n);
- getchar();
- char** names = calloc(n, sizeof(char*));
- printf("Enter name to get hash code and save in database\nEnter \'STOP\' for stop entering infomation\n");
- int i = 0;
- char buffer[255] = {0};
- while (1)
- {
- gets_s(buffer, 255);
- if (strcmp(buffer, "STOP") == 0)
- {
- break;
- }
- names[hashfunc(buffer, n)] = calloc(strlen(buffer)+1, sizeof(char));
- strcpy(names[hashfunc(buffer, n)], buffer);
- printf("word number - %d\n", i+1);
- ++i;
- }
- printa(names, n);
- getchar();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement