Back: Teacher's course teaching link
"Item 2-e-dictionary"
make a simple electronic dictionary. In document Dictionary.txt, a dictionary is kept in English and Chinese, with a vocabulary of nearly 8,000 words, separated by ' t ' between the English and the interpretation. Programming, read the contents of the file to two arrays e[] and c[], respectively, representing English and Chinese, the user input English words, display Chinese meaning. After running the program, the user is supported to search the dictionary continuously until the input "0000" is finished, as shown in the figure:
Hint: the words in the file are sorted, so in the search, the two-point search method is used to improve the efficiency.
[Reference solution]
#include <stdio.h> #include <string.h> #include <stdlib.h> char e[8000][20],c[8000][20]; English and Chinese arrays, to be read into the file by the int wordsnum=0;
The actual lexical number in the thesaurus int Binseareh (int low, int high, char *k); int main () {char key[20];
Query keywords//read the data in the file into the object array file *fp; fp = fopen ("Dictionary.txt", "R");
Open the file as input (fp==null)//test if {printf ("Open error!\n") is successfully opened;
Exit (1);
while (!feof (FP))//read succeeded, read {fscanf (FP,%s%s, E[wordsnum), C[wordsnum] repeatedly from the file;
++wordsnum;
Fclose (FP);
Enter the search keyword and use the binary lookup method to query do {printf ("Please enter the word to check (0000 end):");
scanf ("%s", key);
if (strcmp (key, "0000") ==0) break; else {int low=0,high=wordsnum-1;
Place the initial value int Index=binseareh (low, High, key) on the current lookup interval and lower bound; if (index = = 1) printf ("No such word is found.")
\ n ');
else printf ("The Chinese meaning of%s is:%s\n\n", Key, C[index]); } while (1); printf ("Welcome to use again.")
\ n ');
return 0;
///Two-point lookup, the result is the subscript int Binseareh in the array (int low, int high, char *k) {int mid;
while (Low<=high) {mid= (low + high)/2;
if (strcmp (e[mid],k) ==0) {return mid;//find successfully returned} if (strcmp (e[mid],k) >0) High=mid-1; Continue in E[low. Mid-1] Find else low=mid+1; Continue to find} return-1 in E[mid+1..high]; When Low>high indicates that the lookup interval is empty, the lookup fails}