WvStreams
|
00001 /* 00002 * Worldvisions Weaver Software: 00003 * Copyright (C) 1997-2002 Net Integration Technologies, Inc. 00004 * 00005 * WvHashTable sample program. 00006 * Suppose you are learning a new language that is alphabetical and you want 00007 * to register all the words you added to your vocabulary in that 00008 * language. So you will need a dictionary where you can look up on words 00009 * that you have learned. 00010 * Assuming that you will not forget any word you had previously learned, 00011 * this dictionary shall not contain repetitive words. 00012 */ 00013 00014 #include "wvhashtable.h" 00015 #include "wvstring.h" 00016 #include "wvlinklist.h" 00017 #include <stdio.h> 00018 00019 // Declare a HashTable class that handles WvString data types 00020 DeclareWvTable(WvString); 00021 00022 /*this subfunction ascending is used for sorting*/ 00023 int ascending(const WvString *a, const WvString *b) 00024 { 00025 return strncasecmp(*a, *b, strlen(a->cstr())); 00026 } 00027 00028 00029 int main() 00030 { 00031 // This is a dictionary that can contain at most 10 words 00032 WvStringTable t(100); 00033 00034 // Here's a list of new words in your vocabulary 00035 WvString s1("aussi"), s2("Bonjour"), s3("comment"); 00036 WvString s4("demain"), s5("non"), s6("oui"); 00037 WvString s7("matin"), s8("bonsoir"), s9("bien"); 00038 WvString s10("depanneur"); 00039 00040 // Add the list of new words to the dictionary 00041 // false = do not autofree the WvString 00042 t.add(&s1, false); t.add(&s2, false), t.add(&s3, false); 00043 t.add(&s4, false); t.add(&s5, false), t.add(&s6, false); 00044 t.add(&s7, false); t.add(&s8, false), t.add(&s9, false), t.add(&s10, false); 00045 00046 // Using an iterator, we can print out the entire content of the hashtable 00047 printf("What words do we have in the dictionary?\n"); 00048 WvStringTable::Iter i(t); 00049 for( i.rewind(); i.next(); ) 00050 { 00051 printf("%s\n", i->cstr() ); 00052 } 00053 00054 printf("There are %d words stored in the dictionary so far.\n", t.count()); 00055 00056 // To look up words in the dictionary, put the WvString data inside the [] 00057 // and do the following to print it out 00058 00059 WvString sample1("Bonjour"); 00060 printf("Is 'Bonjour' in the dictionary? %s\n", t[sample1]?"Yes":"No"); 00061 WvString sample2("Salut"); 00062 printf("Is 'Salut' in the dictionary? %s\n", t[sample2]?"Yes":"No"); 00063 00064 // To remove a word from the dictionary 00065 // For example, if you want to remove the word "aussi" in your dictionary 00066 t.remove(&s1); 00067 00068 // Then print out the list of words in the dictionary again 00069 printf("Modified List:\n"); 00070 for( i.rewind(); i.next(); ) 00071 { 00072 printf("%s\n", i->cstr() ); 00073 } 00074 00075 WvStringTable::Sorter s(t,ascending); 00076 printf("Sorted modified List:\n"); 00077 for( s.rewind(); s.next(); ) 00078 { 00079 printf("%s\n", s->cstr() ); 00080 } 00081 00082 00083 // You can empty the entire dictionary by doing this: 00084 t.zap(); 00085 00086 // Then print out the list of words in the dictionary again 00087 printf("Empty List:\n"); 00088 for( i.rewind(); i.next(); ) 00089 { 00090 printf("%s\n", i->cstr() ); 00091 } 00092 00093 } 00094