diff --git a/main.c b/main.c index 7a4af81ce300ddccc7eeb8deb94d5e7d87c4e0a4..354750d38c22c7b42850d723360c65dcd019062c 100644 --- a/main.c +++ b/main.c @@ -5,40 +5,58 @@ #include <stdio.h> #include <stdlib.h> +#include <string.h> #include "hashtable.h" int main() { - hm *hashmap = hm_create(10); - hm_set(hashmap, "Key1", "Value1"); - hm_set(hashmap, "Key2", "Value1"); - hm_set(hashmap, "Key1", "Value2"); - hm_set(hashmap, "Key3", "Value1"); - hm_set(hashmap, "Key4", "FOUR"); - hm_set(hashmap, "Key1", "Value3"); - hm_set(hashmap, "Key5", "I think it's good now"); - hm_set(hashmap, "Key6", "6"); - hm_set(hashmap, "Key7", "7"); - hm_set(hashmap, "Key8", "8"); - hm_set(hashmap, "Key9", "9"); - hm_set(hashmap, "Key27", "MORE ?"); - hm_set(hashmap, "Key10", "Is this the end ?"); + srand(0); + hm *hashmap = hm_create(5); - printf("Value of %s : %s\n", "Key1", hm_get(hashmap, "Key1")); - printf("Value of %s : %s\n", "Key2", hm_get(hashmap, "Key2")); - printf("Value of %s : %s\n", "Key3", hm_get(hashmap, "Key3")); - printf("Value of %s : %s\n", "Key4", hm_get(hashmap, "Key4")); - printf("Value of %s : %s\n", "Key5", hm_get(hashmap, "Key5")); - printf("Value of %s : %s\n", "Key6", hm_get(hashmap, "Key6")); - printf("Value of %s : %s\n", "Key7", hm_get(hashmap, "Key7")); - printf("Value of %s : %s\n", "Key8", hm_get(hashmap, "Key8")); - printf("Value of %s : %s\n", "Key9", hm_get(hashmap, "Key9")); - printf("Value of %s : %s\n", "Key10", hm_get(hashmap, "Key10")); + while (true) { + printf("Add (1), Delete (2), Search (3), Print (4), Exit (5)...\n"); + int selection; + scanf("%d", &selection); + if (selection < 1 || selection > 5) { + printf("Bad input, try again.\n"); + continue; + } + char phone[10]; + char name[10]; - hm_print(hashmap); - hm_rm(hashmap, "Key1"); - hm_print(hashmap); - hm_rm(hashmap, "Key10"); - hm_print(hashmap); + if (selection == 1) { + printf("Phone :\n"); + scanf("%s", phone); + printf("Name :\n"); + scanf("%s", name); + char *p = strdup(phone); + char *n = strdup(name); + hm_set(hashmap, p, n); + hm_print(hashmap); + free(n); + free(p); + } + if (selection == 2) { + printf("Phone to delete :\n"); + scanf("%s", phone); + hm_rm(hashmap, phone); + hm_print(hashmap); + } + if (selection == 3) { + printf("Phone to search :\n"); + scanf("%s", phone); + char *p = strdup(phone); + char *name = hm_get(hashmap, p); + if (name != NULL) { + printf("%s\n", name); + } + } + if (selection == 4) { + hm_print(hashmap); + } + if (selection == 5) { + break; + } + } hm_destroy(&hashmap);