c言語で辞書を作る
英語の単語検索を簡単に実現した辞書で、VSのプロジェクトディレクトリの下に辞書を置きます(別の場所に置いてもいいですが、開くときにパスを変えればいいです)
この辞書を実現する方法はTDD PSです.
#include
#include
#include
#include
//
#define FILE_NAME "dictionary.txt"
//
#define SUM 8000
//
typedef struct {
char english[25];
char chinese[50];
char my_class[20];
}words;
// ,
words library[SUM] = { 0 };
int number = 0;
int binary_find(const char *English)
{
int low = 0;
int high = SUM - 1;
int mid = 0;
while (low <= high)
{
mid = low + ((high - low) >> 1);
if (strcmp(English, library[mid].english) < 0)
{
high = mid - 1;
}
else if (strcmp(English, library[mid].english) > 0)
{
low = mid + 1;
}
else if (strcmp(English, library[mid].english) == 0)
{
return mid;
}
/*else
return -1; // return -1 , ,
*/
}
return -1;
}
void find_print(char *English)
{
int key = -1;
key = binary_find(English);
if (key == -1)
{
printf("%s not found!
", English);
}
else
{
printf("%s %s %s
", library[key].english,
library[key].chinese,
library[key].my_class);
}
}
void print_menu(void)
{
printf("
*************************************************
");
printf("**** *****
");
printf("***** -> : );
printf("******* 1> *********
");
printf("******** 2> ***********
");
printf("*************************************************
");
}
int main(void)
{
char English[25] = { 0 };
FILE *fp = fopen(FILE_NAME,"r");
int choice = 0;
if (fp == NULL)
{
perror("file");
exit(1);
}
while (!feof(fp)){
fscanf(fp, "%s%s%s",
library[number].english,
library[number].chinese,
library[number].my_class);
number++;
}
while (1)
{
system("cls");
print_menu();
printf("
:>");
scanf("%d", &choice);
fflush(stdin);
if (2 == choice)
{
exit(1);
}
else
{
printf(" :>");
scanf("%s", English);
fflush(stdin);
find_print(English);
system("pause");
//printf(" :>");
//scanf("%s", English);
//fflush(stdin);
//find_print(English);
}
fclose(fp);
}
return 0;
}
この辞書を実現する方法はTDD PSです.