From 0780361c81b7658b685ec57c5d1f7d7f910ce4bf Mon Sep 17 00:00:00 2001 From: Patrick Pelissier Date: Thu, 30 Mar 2017 21:29:43 +0200 Subject: [PATCH] Add new example --- example/Makefile | 2 +- example/ex09.c | 95 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 96 insertions(+), 1 deletion(-) create mode 100644 example/ex09.c diff --git a/example/Makefile b/example/Makefile index 1ea162b1..e2935750 100644 --- a/example/Makefile +++ b/example/Makefile @@ -3,7 +3,7 @@ CFLAGS=-Wall -W -pedantic -Wshadow -Wpointer-arith -Wcast-qual -Wstrict-prototyp CPPFLAGS=-I.. RM=rm -f EXE=./a.exe -EXAMPLES=ex01b.c ex01.c ex02.c ex03.c ex04.c ex05.c ex06.c ex07.c +EXAMPLES=ex01b.c ex01.c ex02.c ex03.c ex04.c ex05.c ex06.c ex07.c ex08.c ex09.c all: @echo "Nothing to be done." diff --git a/example/ex09.c b/example/ex09.c new file mode 100644 index 00000000..98ce72d1 --- /dev/null +++ b/example/ex09.c @@ -0,0 +1,95 @@ +// Based upon http://www.sanfoundry.com/cpp-program-implement-map-stl/ +#include +#include + +#include "m-dict.h" +#include "m-string.h" + +DICT_DEF2(map, char, M_DEFAULT_OPLIST, int, M_DEFAULT_OPLIST) + +int main(void) +{ + dict_map_t mp; + dict_it_map_t it; + int choice, item; + string_t str; + char s; + + dict_map_init(mp); + string_init (str); + + while (true) + { + printf ("\n---------------------\n"); + printf ("Map Implementation in M*LIB\n"); + printf ("\n---------------------\n"); + printf("1.Insert Element into the Map\n"); + printf("2.Delete Element of the Map\n"); + printf("3.Size of the Map\n"); + printf("4.Find Element at a key in Map\n"); + printf("5.Display by Iterator\n"); + printf("6.Exit\n"); + printf("Enter your Choice: "); + + string_fgets (str, stdin, STRING_READ_PURE_LINE); + choice = atoi(string_get_cstr(str)); + + switch(choice) + { + case 1: + printf("Enter value to be inserted: "); + string_fgets (str, stdin, STRING_READ_PURE_LINE); + item = atoi(string_get_cstr(str)); + printf("Enter the key: "); + string_fgets (str, stdin, STRING_READ_PURE_LINE); + s = atoi(string_get_cstr(str)); + dict_map_set_at(mp, s, item); + break; + + case 2: + printf("Enter the mapped string to be deleted: "); + string_fgets (str, stdin, STRING_READ_PURE_LINE); + s = atoi(string_get_cstr(str)); + dict_map_remove(mp, s); + break; + + case 3: + printf("Size of Map: %lu\n", (unsigned long) dict_map_size(mp) ); + break; + + case 4: + printf("Enter the key at which value to be found: "); + string_fgets (str, stdin, STRING_READ_PURE_LINE); + s = atoi(string_get_cstr(str)); + int *ref = dict_map_get(mp, s); + if (ref != NULL) + { + printf("Found %d\n", *ref); + } + else + { + printf("No Element Found\n"); + } + break; + + case 5: + printf("Displaying Map by Iterator:\n"); + for (dict_map_it(it, mp); !dict_map_end_p(it); dict_map_next(it)) + { + const dict_pair_map_t *cref = dict_map_cref(it); + printf ("%d : %d\n", (*cref)->key, (*cref)->value); + } + break; + + case 7: + dict_map_clear(mp); + string_clear (str); + exit(0); + break; + + default: + printf("Wrong Choice\n"); + } + } +} +