/* Dictionary.c Implementation of the Dictionary Class */ #include #include #include #include "Boolean.h" struct dictionary { int count; int max_count; char **English; char **Foreign; }; #include "Dictionary.h" #define VALID_WORD(w) assert(w!=NULL);assert(strlen(w)>0); Dictionary ConsDictionary( int max_words ) { /* Construct a new dictionary. Pre-cond: max_words > 0 Post-cond: returns a newly constructed Dictionary or NULL if insufficient memory */ Dictionary d; assert( max_words > 0 ); d = (Dictionary)malloc( sizeof( struct dictionary ) ); if ( d != NULL ) { d->max_count = max_words; d->count = 0; d->English = (char **)malloc( max_words*sizeof( char * ) ); d->Foreign = (char **)malloc( max_words*sizeof( char * ) ); } else { printf("ConsDictionary: insuff space\n"); } return d; } int DeleteDictionary( Dictionary d ) { /* Delete a dictionary. Pre-cond: d is a valid Dictionary Post-cond: space used by d has been freed returns TRUE if no errors, FALSE on error */ assert( d != NULL ); assert( d->English != NULL ); assert( d->Foreign != NULL ); free( d->English ); free( d->Foreign ); free( d ); } int WordCount( Dictionary d ) { /* Count words in the dictionary Pre-cond: d is a valid Dictionary Post-cond: returns count of words currently in Dictionary */ assert( d != NULL ); return d->count; } int AddTranslation( Dictionary d, char *lang1, char *lang2 ) { /* Add a new translation to the dictionary Neither lang1 nor lang2 may be in the dictionary already Pre-cond: d is a valid Dictionary and lang1 != NULL and strlen( lang1 ) > 0 and lang2 != NULL and strlen( lang2 ) > 0 and FindForeign( d, lang1 ) == NULL and FindEnglish( d, lang2 ) == NULL Post-cond: WordCount( d ) = old WordCount( d ) + 1 and strcmp( FindForeign( d, lang1 ), lang2 ) == 0 and strcmp( FindEnglish( d, lang2 ), lang1 ) == 0 */ assert( d != NULL ); VALID_WORD( lang1 ); VALID_WORD( lang2 ); assert( FindForeign( d, lang1 ) == NULL ); assert( FindEnglish( d, lang2 ) == NULL ); d->English[d->count] = strdup( lang1 ); d->Foreign[d->count] = strdup( lang2 ); d->count++; return TRUE; } int ReplaceTranslation( Dictionary d, char *lang1, char *lang2 ) { /* Replace an existing translation in the dictionary lang1 must be in the dictionary already lang2 must not be in the dictionary already Pre-cond: d is a valid Dictionary and lang1 != NULL and strlen( lang1 ) > 0 and lang2 != NULL and strlen( lang2 ) > 0 and FindForeign( d, lang1 ) != NULL and FindEnglish( d, lang2 ) == NULL Post-cond: WordCount( d ) = old WordCount( d ) + 1 and strcmp( FindForeign( d, lang1 ), lang2 ) == 0 and strcmp( FindEnglish( d, lang2 ), lang1 ) == 0 */ int i; assert( d != NULL ); VALID_WORD( lang1 ); VALID_WORD( lang2 ); assert( FindForeign( d, lang1 ) != NULL ); assert( FindEnglish( d, lang2 ) == NULL ); for( i=0; icount; i++ ) { if ( strcmp( d->English[i], lang1 ) == 0 ) { free( d->English[i] ); d->English[i] = strdup( lang1 ); return TRUE; } } return FALSE; } char *FindForeign( Dictionary d, char *word ) { int i; assert( d != NULL ); VALID_WORD( word ); for( i=0; icount; i++ ) { if ( strcmp( d->English[i], word ) == 0 ) { return d->Foreign[i]; } } return NULL; } char *FindEnglish( Dictionary d, char *word ) { int i; assert( d != NULL ); VALID_WORD( word ); for( i=0; icount; i++ ) { if ( strcmp( d->Foreign[i], word ) == 0 ) { return d->English[i]; } } return NULL; }