c项目实现(1)实现电子词典的翻译 您所在的位置:网站首页 电子词典项目介绍 c项目实现(1)实现电子词典的翻译

c项目实现(1)实现电子词典的翻译

2023-09-19 15:55| 来源: 网络整理| 查看: 265

项目实现功能

通过用户的输入,在字典文件中进行 查找,返回对应的翻译内容。

字典文件的样式,该文件已经上传。

#a                                  --------要查找的单词 Trans:art. 一;字母A        --------注释 #a.m. Trans:n. 上午

项目实现的思路

1、首先读取字典文件

2、读取用户的输入

3、查找输入匹配的解释,返回结果

4、控制程序的结束和内存的释放

项目的测试

需要通过gcc先编译文件,再在cmd中执行exe文件实现

项目代码 #include #include #include #include #define MAX 111111 //设置最大的记录数 struct dict{ //将字典文件中的内容保存到结构体中 char *key; //词条 char *content; //对应的翻译 }; //实现读取字典文件的功能,对于字典的文件名是不变的,所以可以用const保证其不会改变 int open_dict(struct dict **p, const char *dict_filename){ //打开文件 FILE *pfile = fopen(dict_filename, "r"); if(pfile == NULL){ //如果打开文件失败 return 0; } //固定分配MAX大小的内存 *p = (struct dict *)malloc(sizeof (struct dict) * MAX); //将内存初始化为0 memset(*p, 0, sizeof (struct dict) * MAX); //pD指向数组p的首地址 struct dict *pD = *p; //设置字符的暂存区 char buf[1024] = {0}; size_t len = 0; int i = 0; //循环实现读取文件的每一行 while(!feof(pfile)){ //对暂存区的内存清0 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); //将读取到的内存拷贝到key中,这里从1开始读取是因为词条前面有个#,需要去掉 strcpy(pD[i].key, &buf[1]); } //再读取字典中的解释 memset(buf, 0, sizeof (struct dict)); //读取文件的每一行 fgets(buf, sizeof (buf), pfile); //得到读取的字符串的长度 len = strlen(buf); //根据字符串的长度分配内存 if(len > 0){ //首先读取词条 pD[i].content = (char *)malloc(len); memset(pD[i].content, 0, len); //将读取到的内存拷贝到content中,这里从6读取是因为在解释前面有Trans:,需要去掉 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){ //利用顺序查找遍历字典 for(int 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){ //循环释放key和content的内存 for(int i = 0; i < size; i++){ if(p[i].key){ free(p[i].key); } if(p[i].content){ free(p[i].content); } } //释放p内存 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(); //1、首先读取字典文件,根据用户输入的第一个参数读取 int size = open_dict(&p, args[1]); if(size == 0){ //字典文件打开失败,程序退出 return 0; } //读取字典文件结束 end_ms = clock(); //打印一共花了多少时间 printf("open_dict use %ld ms\n", end_ms - start_ms); //2、读取用户的输入 char key[1024]; char content[1024]; while (1) { //对key和content的空间清0 memset(key, 0, sizeof (key)); memset(content, 0, sizeof (content)); //得到用户的输入 scanf("%s", key); //判断用户的输入 if(strncmp(key, "command=exit", 12) == 0){ break; } //开始查找 start_ms = clock(); //3、查找输入匹配的解释,返回结果 if(search_dict(p, size, key, content)){ printf("%s", content); }else{ printf("not find\n"); } end_ms = clock(); printf("search_dict use %ld ms\n", end_ms - start_ms); } //4、内存的释放 start_ms = clock(); free_dict(p, size); end_ms = clock(); printf("free_dict use %ld ms\n", end_ms - start_ms); return 0; }

 



【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有