#include #include #include #include /* Maximum length (not counting trailing NULL) of a word */ #define MAX_LENGTH 20 /* Structured type for a dictionary entry */ typedef struct { char english[MAX_LENGTH+1] ; /* English word */ char latin[MAX_LENGTH+1]; /* Latin word */ int decl; /* declension of Latin word */ } dictentry_t; /* maximum size of dictionary */ #define DICT_SIZE 40 /* global array holding dictionary */ dictentry_t dict[DICT_SIZE] = { { "daughter", "filia", 1 }, { "gate", "porta", 1 }, { "master", "dominus", 2 } }; /* number of entries currently in dictionary */ int num_entries = 3; /* Lookup takes a string representing an English word, and tries to find it in the dictionary, by simple linear search. If it succeeds, it returns the index of the dictionary entry; if it fails, it return -1. */ int Lookup(char *e) { /* BEGIN ANSWER (a) -- do not delete this line */ // ADD YOUR CODE HERE /* END ANSWER (a) -- do not delete this line */ } /* ReadDict() reads the dictionary file from standard input. It should parse each line into the next available dictionary entry. If a word in the file exceeds MAX_LENGTH, or if there are more than DICT_SIZE lines, or if a line does not contain the necessary three fields, ReadDict() should print a message to standard output and return 0; if it parses all lines successfully it should return 1. */ int ReadDict(char *filename) { FILE *dictfile; dictfile = fopen(filename,"r"); if ( dictfile == NULL ) { printf("Error opening dictionary file\n"); return 0; } num_entries = 0; /* clear the built-in dictionary */ while ( !feof(dictfile) ) { /* BEGIN ANSWER (c) -- do not delete this line */ // ADD YOUR CODE HERE /* END ANSWER (c) -- do not delete this line */ } return 1; } int main(int argc, char **argv) { if ( argc > 1 ) { if ( argc != 2 ) { printf("Bad number of arguments!\n"); return EXIT_FAILURE; } if ( ReadDict(argv[1]) == 0 ) { printf("ReadDict() failed\n"); return EXIT_FAILURE; } } printf("Printing out the dictionary.\n"); int i; for (i=0 ; i < num_entries; i++) { dictentry_t *d = &dict[i]; printf("%s %s %d\n",d->english,d->latin,d->decl); } /* take words from user and look them up */ while ( 1 ) { char tmpbuf[1024]; printf("Enter English word to look up: "); if ( scanf(" %s",tmpbuf) != 1 ) { /* end of input */ return EXIT_SUCCESS; } int i = Lookup(tmpbuf); if ( i < 0 ) { printf("No translation found!\n"); /* If the input word ends in "s", assume it's a plural. Remove the "s" and try again. If we find the word this time, then print out the plural form of its Latin translation, as specified in the question. */ /* BEGIN ANSWER (b) -- do not delete this line */ // ADD YOUR CODE HERE /* END ANSWER (b) -- do not delete this line */ } else { printf("Latin translation is: %s\n",dict[i].latin); } } return EXIT_SUCCESS; }