- 电子词典的构成:
#兜圈子
Trans:go around in circles@circle@take a joy-ride@beat about the bush.
搜索字条用#开始占一行,翻译以Trans:开始占一行
- 电子词典搜索实现思路
- 打开电子词典读入内存中
- 输入查询单词输出对应结果
- 释放内存
- 循序实现
#define _CRT_SECURE_NO_WARNINGS
#include
#include
#include
#include
#define MAX 111111
struct dict
{
char *key;
char *content;
};
int open_dict(struct dict **p, const char *dict_filename)
{
FILE *pfile = fopen(dict_filename, "r");
if (pfile == NULL)
return 0;
*p = (struct dict *)malloc(sizeof(struct dict) * MAX);
memset(*p, 0, sizeof(struct dict) * MAX);
struct dict *pD = *p;
char buf[1024] = { 0 };
size_t len = 0;
int i = 0;
while (!feof(pfile))
{
memset(buf, 0, sizeof(buf));
fgets(buf, sizeof(buf), pfile);
len = strlen(buf);
if (len > 0)
{
pD[i].key = (char *)malloc(len);
memset(pD[i].key, 0, len);
strcpy(pD[i].key, &buf[1]);
}
memset(buf, 0, sizeof(buf));
fgets(buf, sizeof(buf), pfile);
len = strlen(buf);
if (len > 0)
{
pD[i].content = (char *)malloc(len);
memset(pD[i].content, 0, len);
strcpy(pD[i].content, &buf[6]);
}
i++;
}
fclose(pfile);
return i;
}
int search_dict(const struct dict *p, int size, const char *key, char *content)
{
int i = 0;
for (i = 0; i < size; i++)
{
if ((p[i].key == NULL) || (p[i].content == NULL))
continue;
if (strncmp(p[i].key, key, strlen(key)) == 0)
{
strcpy(content, p[i].content);
return 1;
}
}
return 0;
}
void free_dict(struct dict *p, int size)
{
int i = 0;
for (i = 0; i < size; i++)
{
if (p[i].key)
free(p[i].key);
if (p[i].content)
free(p[i].content);
}
free(p);
}
int main(int argc, char *args[])
{
if (argc < 2)
{
printf("usage: %s dict-filename\n", args[0]);
return 0;
}
long start_ms = 0;
long end_ms = 0;
struct dict *p = NULL;
start_ms = clock();
int size = open_dict(&p, args[1]);
if (size == 0)
return 0;
end_ms = clock();
printf("open_dict used %ld ms\n", end_ms - start_ms);
char key[1024];
char content[1024];
while (1)
{
memset(key, 0, sizeof(key));
memset(content, 0, sizeof(content));
scanf("%s", key);
if (strncmp(key, "command=exit", 12) == 0)
break;
start_ms = clock();
if (search_dict(p, size, key, content))
{
printf("%s", content);
} else
{
printf("not found\n");
}
end_ms = clock();
printf("search_dict used %ld ms\n", end_ms - start_ms);
}
start_ms = clock();
free_dict(p, size);
end_ms = clock();
printf("free_dict used %ld ms\n", end_ms - start_ms);
return 0;
}
- 链表实现
#define _CRT_SECURE_NO_WARNINGS
#include
#include
#include
#include
struct dict
{
char *key;
char *content;
struct dict *next;
};
int open_dict(struct dict **p, const char *dict_filename)
{
FILE *pfile = fopen(dict_filename, "r");
if (pfile == NULL)
return 0;
char buf[2048] = { 0 };
size_t len = 0;
int i = 0;
*p = (struct dict *)malloc(sizeof(struct dict));
memset(*p, 0, sizeof(struct dict));
struct dict *pD = *p;
while (!feof(pfile))
{
memset(buf, 0, sizeof(buf));
fgets(buf, sizeof(buf), pfile);
len = strlen(buf);
if (len > 0)
{
pD->key = (char *)malloc(len);
memset(pD->key, 0, len);
strcpy(pD->key, &buf[1]);
}
memset(buf, 0, sizeof(buf));
fgets(buf, sizeof(buf), pfile);
len = strlen(buf);
if (len > 0)
{
pD->content = (char *)malloc(len);
memset(pD->content, 0, len);
strcpy(pD->content, &buf[6]);
}
pD->next = (struct dict *)malloc(sizeof(struct dict));
memset(pD->next, 0, sizeof(struct dict));
pD = pD->next;
i++;
}
fclose(pfile);
return i;
}
int search_dict(const struct dict *p, int size, const char *key, char *content)
{
const struct dict *pD = p;
while (pD)
{
if ((pD->key) && (pD->content))
{
if (strncmp(pD->key, key, strlen(key)) == 0)
{
strcpy(content, pD->content);
return 1;
}
}
pD = pD->next;
}
return 0;
}
void free_dict(struct dict *p, int size)
{
struct dict *pD = p;
while (pD)
{
if (pD->key)
free(pD->key);
if (pD->content)
free(pD->content);
struct dict *tmp = pD->next;
free(pD);
pD = tmp;
}
}
int main(int argc, char *args[])
{
if (argc < 2)
{
printf("usage: %s dict-filename\n", args[0]);
return 0;
}
long start_ms = 0;
long end_ms = 0;
start_ms = clock();
struct dict *p = NULL;
int size = open_dict(&p, args[1]);
if (size == 0)
return 0;
end_ms = clock();
printf("open_dict used %ld ms\n", end_ms - start_ms);
char key[502];
char content[1024];
while (1)
{
memset(key, 0, sizeof(key));
memset(content, 0, sizeof(content));
scanf("%s", key);
if (strncmp(key, "command=exit", 12) == 0)
break;
start_ms = clock();
if (search_dict(p, size, key, content))
{
printf("%s", content);
} else
{
printf("not found\n");
}
end_ms = clock();
printf("search_dict used %ld ms\n", end_ms - start_ms);
};
start_ms = clock();
free_dict(p, size);
end_ms = clock();
printf("free_dict used %ld ms\n", end_ms - start_ms);
return 0;
}