用链表实现电子词典

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>


//#define MAX 111111 //最大记录数


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;//打开文件失败,函数返回
int filerow = 0;

while (1)
{
filerow++;
char buf0[1024] = { 0 };
if (fgets(buf0, sizeof(buf0), pfile) == NULL)
{
printf("(%s)",buf0);
break;
}
//else
 


}
printf("%d",filerow);
/*fclose(pfile);
FILE *pfile1 = fopen(dict_filename, "r");
if (pfile1 == NULL)
return 0;//打开文件失败,函数返回*/
fseek(pfile,0,SEEK_SET);

*p = (struct dict *)malloc(sizeof(struct dict) * (filerow/2));//固定分配MAX大小内存
memset(*p, 0, sizeof(struct dict) * (filerow/2));//将分配内存初始化为0
struct dict *pD = *p;//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->key = (char *)malloc(len);//根据字符串长度分配内存
memset(pD->key, 0, len);
strcpy(pD->key, &buf[1]);//将读取到的内容拷贝到key中
}


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 *)calloc(sizeof(struct dict), 1);
pD = pD->next;


i++;//计数器加1
}
fclose(pfile);//关闭字典文件


return i;//返回读取到的字典词条数
}


//根据关键字key,在字典中查找内容
int search_dict(const struct dict *p, const char *key, char *content)
{
struct dict *pD=p;

while (pD)//遍历字典
{
if ((pD->key == NULL) || (pD->content == NULL))
{
continue;
}


if (strncmp(pD->key, key, strlen(key)) == 0)
{
strcpy(content, pD->content);
return 1;//找到符合条件记录,返回1
}
pD = pD->next;
}
return 0;//没有找到符合条件记录,返回0
}


//释放内存
void free_dict(struct dict *p)
{
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;//记录函数执行的结束时间
struct dict *p = NULL;
start_ms = clock();
int dict_size = open_dict(&p, args[1]);
printf("%d",dict_size);
//根据命令行第一个参数做为字典文件名,打开字典文件
if (dict_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)
{


printf("hahah");
break;
}
start_ms = clock();
if (search_dict(p, 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);
end_ms = clock();
printf("free_dict used %ld ms\n", end_ms - start_ms);//打印函数执行时间,单位:毫秒
system("pause");
return 0;
}
  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值