当前位置: 首页 > news >正文

电子词典--两次扫描文件发/链表法

二次扫描文件法实现的电子词典

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

struct dict
{
	char *key;
	char *content;
};

int get_dict_size(FILE *pfile)//得到字典文件中词条总数
{
	if (pfile == NULL)
		return 0;

	int i = 0;
	char buf[2048];
	while (!feof(pfile))
	{
		fgets(buf, sizeof(buf), pfile);
		fgets(buf, sizeof(buf), pfile);
		i++;//读取两行后,计数器加1
	}
	return i;
}

//打开字典文件,并读取文件内容
int open_dict(struct dict **p, const char *dict_filename)
{
	FILE *pfile = fopen(dict_filename, "r");
	if (pfile == NULL)
		return 0;//打开文件失败,函数返回

	int size = get_dict_size(pfile);//得到字典文件中词条总数
	if (size == 0)
		return 0;

	*p = (struct dict *)malloc(sizeof(struct dict) * size);//根据字典文件词条总数分配内存
	memset(*p, 0, sizeof(struct dict) * size);//将分配内存初始化为0

	struct dict *pD = *p;//pD指向数组p的首地址

	char buf[2048] = { 0 };
	size_t len = 0;
	int i = 0;
	fseek(pfile, 0L, SEEK_SET);//设置读取位置为字典文件开始
	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]);//将读取到的内容拷贝到key中
		}

		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;//返回读取到的字典词条数
}

//根据关键字key,在字典中查找内容
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;//找到符合条件记录,返回1
		}
	}
	return 0;//没有找到符合条件记录,返回0
}

//释放内存
void free_dict(struct dict *p, int size)
{
	int i = 0;
	for (i = 0; i < size; i++)//循环释放key与content成员内存
	{
		if (p[i].key)
			free(p[i].key);
		if (p[i].content)
			free(p[i].content);
	}
	free(p);//释放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[2048];
	char content[2048];
	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 <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>

struct dict
{
	char *key;
	char *content;
	struct dict *next;//指向链表下一个节点的指针
};

//打开字典文件,并读取文件内容
int open_dict(struct dict **p, const char *dict_filename)//open dict.txt,and read dict
{
	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;//pD指向链表首地址
	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 *)malloc(sizeof(struct dict));//为链表的下一个节点分配内存
		memset(pD->next, 0, sizeof(struct dict));

		pD = pD->next;//将pD指向下一个节点位置
		i++;
	}
	fclose(pfile);//关闭字典文件
	return i;//返回读取到的字典词条数
}

//根据关键字key,在字典中查找内容
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;//找到符合条件记录,返回1
			}
		}
		pD = pD->next;//指向链表下一个节点位置
	}
	return 0;//没有找到符合条件记录,返回0
}

//释放链表内存
void free_dict(struct dict *p, int size)
{
	struct dict *pD = p;
	while (pD)
	{
		if (pD->key)//删除链表节点中key成员内存
			free(pD->key);
		if (pD->content)//删除链表节点中content成员内存
			free(pD->content);

		struct dict *tmp = pD->next;//保存链表下一个节点的地址
		free(pD);//删除链表当前节点
		pD = tmp;//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;//记录函数执行的结束时间

	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[2048];
	char content[2048];
	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;
}


相关文章:

  • PL/SQL-05
  • LeetCode - Path sum
  • lucene.net 3.0.3、结合盘古分词进行搜索的小例子(分页功能)
  • ubuntu php 环境配置
  • 多表连接的三种方式详解 hash join、merge join、 nested loop
  • MySQL语句大全
  • centos6.4x64最小化安装部署rsync
  • springmvc 统一处理exception
  • 2015第19周四jquery版本
  • Android 5中不同效果的Toast
  • 短信猫 TIdTCPServer TIdTCPClient
  • 新人初来报道
  • SQL Server 自定义函数(1)把某一列多行的值拼接成一个字符串
  • jdbctemplate 获取数据表结构的方法注意事项
  • 小黑小波比.sublime text的使用
  • 【附node操作实例】redis简明入门系列—字符串类型
  • 【知识碎片】第三方登录弹窗效果
  • Angularjs之国际化
  • mysql 数据库四种事务隔离级别
  • Quartz实现数据同步 | 从0开始构建SpringCloud微服务(3)
  • React Native移动开发实战-3-实现页面间的数据传递
  • Redux 中间件分析
  • Spark学习笔记之相关记录
  • spring security oauth2 password授权模式
  • UMLCHINA 首席专家潘加宇鼎力推荐
  • 阿里云爬虫风险管理产品商业化,为云端流量保驾护航
  • 程序员最讨厌的9句话,你可有补充?
  • 从输入URL到页面加载发生了什么
  • 计算机常识 - 收藏集 - 掘金
  • 记录:CentOS7.2配置LNMP环境记录
  • 聊聊springcloud的EurekaClientAutoConfiguration
  • 批量截取pdf文件
  • 浅谈Kotlin实战篇之自定义View图片圆角简单应用(一)
  • 通过npm或yarn自动生成vue组件
  • 想写好前端,先练好内功
  • 用Canvas画一棵二叉树
  • 云大使推广中的常见热门问题
  • 主流的CSS水平和垂直居中技术大全
  • linux 淘宝开源监控工具tsar
  • Unity3D - 异步加载游戏场景与异步加载游戏资源进度条 ...
  • 策略 : 一文教你成为人工智能(AI)领域专家
  • ​如何在iOS手机上查看应用日志
  • ​用户画像从0到100的构建思路
  • #我与Java虚拟机的故事#连载06:收获颇多的经典之作
  • #我与Java虚拟机的故事#连载11: JVM学习之路
  • (2015)JS ES6 必知的十个 特性
  • (3)STL算法之搜索
  • (6)添加vue-cookie
  • (HAL库版)freeRTOS移植STMF103
  • (第8天)保姆级 PL/SQL Developer 安装与配置
  • (附源码)springboot课程在线考试系统 毕业设计 655127
  • (简单有案例)前端实现主题切换、动态换肤的两种简单方式
  • (七)微服务分布式云架构spring cloud - common-service 项目构建过程
  • (三分钟了解debug)SLAM研究方向-Debug总结
  • (四)图像的%2线性拉伸