Monday, May 24, 2010

In C program how do i do this?

Use the following structure to set up a dictionary:





struct dictionary


{


char word[10];


char definition[80];


int noun_or_verb; /* 1 for noun 2 for verb */


};





Your dictionary will initially have at least 5 words in it, with room for a maximum of 10 words. You decide on the words and their definitions.





Have the user input a word. Test to see if the word is in the dictionary. If it is in the dictionary, display it to the screen in the following format:





the word - N (for noun) or V (for verb) - definition


For example:


sweat - V - to do C programming homework


If the word is not in the dictionary, ask the user if he/she wishes to add the word to the dictionary. If the user does want to add it to the dictionary, prompt for definition and whether the word is a verb or noun. If the user does not want to put the word in the dictionary do not prompt them for information, simply continue the program.

In C program how do i do this?
Well, first off, you'll need some way to store a bunch of those dictionary entry objects. For simplicity (and because it doesn't sound like this assignment is about hash tables), I'd suggest using a linked list. e.g.





struct DictionaryEntry {....};





struct DictionaryNode {


struct DictionaryEntry entry;


struct DictionaryNode *next;


};





struct Dictionary {


struct DictionaryNode *first;


}





(Note that my terminology is slightly different than yours - my Dictionary struct means the whole thing, whereas DictionaryEntry is a specific item).





Then write some functions to find words in the dictionary and to add new ones. If you were using C++ this would be slightly simpler, but it's good to know how to use malloc...





void addDictionaryEntry( struct Dictionary *dict, struct DictionaryEntry *entry ) {


struct DictionaryNode *node = malloc(sizeof(DictionaryNode));


memcpy( node-%26gt;entry.word, entry-%26gt;word, 10 );


memcpy( node-%26gt;entry.definition, entry-%26gt;word, 80 );


node-%26gt;entry.noun_or_verb = entry-%26gt;noun_or_verb;


node-%26gt;next = dict-%26gt;first;


dict-%26gt;first = node;


}





struct DictionaryEntry *getDictionaryEntryByWord( struct Dictionary *dict, char *word ) {


DictionaryNode *node = dict-%26gt;first;


while( node ) {


if( strcmp(node-%26gt;entry.word, word) == 0 ) {


return %26amp;node-%26gt;entry;


}


node = node-%26gt;next;


}


return null;


}





void initDictionary( struct Dictionary *dict ) {


dict-%26gt;first = 0;


}





// might want a dictionary destructor, too...





That should take care of the storage, which, this being C, is probably the trickiest part. Then you just have to have a decent main() and do the user I/O.





int main( int argc, char **argv ) {


struct Dictionary dict;


struct DictionaryEntry inputEntry;


struct DictionaryEntry *foundEntry;





initDictionary( %26amp;dict );


while( true ) {


printf("Enter a word%26gt; ");


scanf("%9s", inputEntry.word); // at most 9 chars, as we need one more for the string-terminating NUL


foundEntry = getDictionaryEntryByWord( %26amp;dict, inputEntry.word );


if( foundEntry ) {


// tell user about the word


} else {


// ask him if he wants to enter a new word, put the def


// in inputEntry, and addDictionaryEntry it.


}


}


}





I'll leave a few parts for you to fill in, as you probably already know how to make the UI code, but it is rather tedious to type in this text entry area. My code probably has a few syntax errors that'll need fixing, anyway ;)

tropical flowers

No comments:

Post a Comment