Add new example
This commit is contained in:
1 parent
e6b1f7d7b5
commit
0780361c81
2 files changed
+96
-1
No files matched your search
+1
-1
@@ -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."
|
||||
|
||||
@@ -0,0 +1,95 @@
|
||||
// Based upon http://www.sanfoundry.com/cpp-program-implement-map-stl/
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#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");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user