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>
- typedef struct
- {
- char* name;
- }hash;
- typedef
- struct Vector
- {
- hash** ptr;
- int size;
- }Vector;
- typedef unsigned int uint;
- void initVec(Vector* vec,int quantity)
- {
- vec->ptr = (hash**)calloc(quantity, sizeof(hash*));
- vec->size = quantity;
- }
- void addGoods(Vector* vec, char* name)
- {
- if (vec->ptr == NULL)
- {
- perror("Mem error\n");
- exit(1);
- }
- unsigned long long countt = 0;
- int i = 0;
- while (name[i] != '\0')
- {
- countt += name[i] * i + 1;
- ++i;
- }
- vec->ptr[countt % vec->size] = calloc(1, sizeof(hash));
- vec->ptr[countt % vec->size]->name = malloc(sizeof(char) * (strlen(name) + 1));
- strcpy(vec->ptr[countt% vec->size]->name, name);
- }
- void printDb(Vector* vec)
- {
- for (uint i = 0; i < vec->size; ++i)
- {
- if (vec->ptr[i] != NULL)
- {
- printf("name: %s | code - %d\n", vec->ptr[i]->name, i);
- printf("\n");
- }
- }
- }
- int main()
- {
- Vector hash1;
- initVec(&hash1,50);
- char buffer[255];
- while (1)
- {
- printf("Enter word\n");
- gets_s(buffer, 255);
- if (strcmp(buffer, "STOP") == 0)
- {
- break;
- }
- addGoods(&hash1, buffer);
- }
- printDb(&hash1);
- getchar();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement