Code:
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
int main()
{
int i,t,j;
FILE * p; //Pointer for file opening
char a[8010] [88]; //Store 7989 English and Chinese, each word and other comments are concentrated in a string
char b[8010][88]; //Only store English words (extracted from a string)
char c[88 ],d; //Store the English word to be searched
int first=1,last=7989,y; //Location for binary search
if((p=fopen("dictionary.txt","r"))= =NULL) //Open a text file that records words and their comments in a read-only mode
{
printf("not find the dictionary!\n");
exit;
}
printf("Please enter the word you want to find (end with 0000): \n"); //input prompt
for(i=1;i<=7989;i++) //Enter words and their comments from the file
fgets(a[i],80,p);
for(i=1;i<=7989;i++) // Extract b string from a string
{
j=0;
while(a[i][j]!=' ')
{
b[i][j]=a[i][j];
j++;
}
b[ i][j]='\0';
}
while(1) //Search multiple times, through the judgment in the loop, break ends the program
{
scanf("%s",c); //Get the word to be searched from the keyboard
if(strcmp(c,"0000")==0) //Judging input, whether to end the program
break;
first=1,last=7989; //Dichotomy starts
y=(first+last)/2;
while(1)
{
if(strcmp(c,b[y])==0) //找到该单词
{
printf("\""); //格式化输出
for(i=0;a[y][i]!='\0';i++)
{
if(a[y][i-1]==' '&&a[y][i]==' ')
continue;
printf("%c",a[y][i]);
if(a[y][i-1]!=' '&&a[y][i]==' '&&a[y][i+1]==' ')
printf("\"的中文意思是:");
}
break;//The search of the word can be ended after the comment is output if(strcmp(c,b[y])>0) //If the word to be searched is larger than the middle word, search the second half
}
first=y+1;
else if(strcmp(c,b[y])<0) //If the word to be searched is smaller than the middle word, search the second half
last=y-1;
y=(first+ last)/2;
if(first>last) //If the search is completed once and no word is found, give a hint and end the search for high words
{
printf("The word was not found in the dictionary!\n");
break;
}
}
}
fclose(p); //close the file
return 0;
}
Note:
File operation: (dictiona.txt in the same root directory, about 8000 words)
Test run: