快译通英汉词典设计源码

项目需求分析

一、单词查询

给定文本文件“dict.txt”,该文件用于存储词库。词库为“英-汉”词典,每个单词和其解释的格式固定,如下所示:

#单词

Trans:解释1@解释2@…解释n

每个新单词由“#”开头,解释之间使用“@”隔开。一个词可能有多个解释,解释均存储在一行里,行首固定以“Trans:”开头。下面是一个典型的例子:

#abyssinianTrans:a. 阿比西尼亚的@n. 阿比西尼亚人;依索比亚人

该词有两个解释,一个是“a. 阿比西尼亚的”;另一个是“n. 阿比西尼亚人;依索比亚人”。

要求编写程序将词库文件读取到内存中,接受用户输入的单词,在字典中查找单词,并且将解释输出到屏幕上。用户可以反复输入,直到用户输入“#exit”字典程序退出。

程序执行格式如下所示:

./app –text

-text表示使用文本词库进行单词查找。

二、建立索引,并且使用索引进行单词查询

要求建立二进制索引,索引格式如下图所示。将文本文件“dict.txt”文件转换为下图所示索引文件“dict.dat”,使用索引文件实现单词查找。

程序执行格式如下:

./app –index   -index表示使用文本词库dict.txt建立二进制索引词库dict.dat

./app –bin     -bin表示使用二进制索引词库进行单词查找。

源码

dictory.h 头文件
#ifndef DICTORY_H_
#define DICTORY_H_typedef struct w_trans
{int trans_len;/*save translation length*/char word_trans[100];/*save translation content*/struct w_trans *next;/*point to next trans*/}trans,*trans_list;typedef struct w_word
{char word_cont[60];/*save word content*/int word_len;/*save word length*/int position;/*save offset*/int trans_num;/*save the number of trans*/struct w_word *next;/*point to next word*/trans_list trans_head;/*point to trans*/
}word,*word_list;typedef struct w_index
{int word_len;char word_cont[60];int position;struct w_index *next;
}i_index,*index_list;void create_hash(FILE *fp,word_list hash[26]);/*create hash table*/
int create_b_file(word_list hash[26],FILE *fp);/*create binary hash File*/
int str_tok(char str[500],word_list p);/*create translation list*/
trans_list search_word(word_list hash[26],char word[60]);
void print_trans(trans_list p);
void create_index_hash(FILE *fp,int size_index,index_list hash_index[26]);
int search_word_in_index(index_list hash_index[26],char *key_word);
void print_trans_index(FILE *fp,int position);
word_list init_one_word(FILE *fp);#endif

create_hash.c 创建内存哈希表

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "dictory.h"void create_hash(FILE *fp,word_list hash[26])
{word_list new=NULL;word_list p=NULL;int n;/*save the first letter of word*/while(1){new=init_one_word(fp);if(feof(fp))break;n=(*new->word_cont-'A')%32;if(hash[n]==NULL)        hash[n]=p=new;else{p->next=new;p=new;}}
}word_list init_one_word(FILE *fp)
{   word_list new=NULL;char str[500];new=(word_list)malloc(sizeof(word));if(new==NULL)perror("malloc");else{fscanf(fp,"#%[^\n]%*c",new->word_cont);new->word_len=strlen(new->word_cont);new->position=0;fseek(fp,6,SEEK_CUR);/*jump Trans:*/fscanf(fp,"%[^\n]%*c",str);new->trans_num=str_tok(str,new);new->next=NULL;}return new;
}int str_tok(char str[500],word_list p)
{char *token=NULL;trans_list q=NULL,new=NULL;trans_list head=NULL;int trans_num=0;/*the number of trans*/token=strtok(str,"@");q=(trans_list)malloc(sizeof(trans));if(q==NULL)perror("malloc");else{strcpy(q->word_trans,token);q->trans_len=strlen(q->word_trans);trans_num++;}head=q;while((token=strtok(NULL,"@"))!=NULL){new=(trans_list)malloc(sizeof(trans));if(new==NULL)perror("malloc");else{strcpy(new->word_trans,token);new->trans_len=strlen(new->word_trans);trans_num++;}q->next=new;q=new;}p->trans_head=head;return trans_num;
}

create_b_file.c 创建二进制索引文件

#include <stdio.h>
#include "dictory.h"
#include <stdlib.h>int create_b_file(word_list hash[26],FILE *fp)
{int i=0;long pos=0,pos1=0;/*return index head size*/long sum=0;int number=0;/*word sum*/word_list p=NULL;trans_list q=NULL;fwrite(&number,4,1,fp);/*write word sum to file*/for(i=0;i<26;i++){p=hash[i];while(p!=NULL){fwrite(&p->word_len,4,1,fp);fwrite(p->word_cont,p->word_len,1,fp);fwrite(&p->position,4,1,fp);number++;p=p->next;}}pos1=ftell(fp);fseek(fp,0,SEEK_SET);fwrite(&number,4,1,fp);fseek(fp,pos1,SEEK_SET);for(i=0;i<26;i++){p=hash[i];while(p!=NULL){fwrite(&p->word_len,4,1,fp);fwrite(p->word_cont,p->word_len,1,fp);pos=ftell(fp);sum=sum+8+p->word_len;fseek(fp,sum,SEEK_SET);fwrite(&pos,4,1,fp);fseek(fp,pos,SEEK_SET);fwrite(&p->trans_num,4,1,fp);q=p->trans_head;while(q!=NULL){fwrite(&q->trans_len,4,1,fp);fwrite(q->word_trans,q->trans_len,1,fp);q=q->next;}p=p->next;}}return number;
}

create_index_hash.c 创建内存索引头哈希表

#include <stdio.h>
#include <stdlib.h>
#include "dictory.h"void create_index_hash(FILE *fp,int size_index,index_list hash_index[26])
{index_list new=NULL;int len;int i=0;int num;int j;fread(&num,4,1,fp);/*create hash list*/while(i<num){new=(index_list)malloc(sizeof(i_index));if(new == NULL){printf("malloc failed\n");break;}else{new->word_len=0;fread(&len,4,1,fp);fread(new->word_cont,len,1,fp);fread(&new->position,4,1,fp);new->next=NULL;}/*get the num of word in hash*/j=(new->word_cont[0]-'A')%32;/*inserte to hash list*/new->next=hash_index[j];hash_index[j]=new;i++;}
}

print_trans.c 打印不根据索引找到的解释

#include <stdio.h>
#include "dictory.h"void print_trans(trans_list p)
{while(p!=NULL){printf("%s\n",p->word_trans);p=p->next;}
}

print_index_trans.c 打印根据索引找到的解释

#include <stdio.h>
#include "dictory.h"
#include <string.h>void print_trans_index(FILE *fp,int position)
{int trans_num;int i=0;int trans_len=0;char trans[100];fseek(fp,position,SEEK_SET);fread(&trans_num,4,1,fp);while(i<trans_num){fread(&trans_len,4,1,fp);fread(trans,trans_len,1,fp);trans[trans_len]='\0';printf("%s\n",trans);memset(trans,0,100);i++;}return;
}

search_word.c 文本查找单词

#include <stdio.h>
#include "dictory.h"trans_list search_word(word_list hash[26],char word[60])
{int n;int m;word_list p=NULL;n=(word[0]-'A')%32;p=hash[n];while(p!=NULL){m=strcmp(p->word_cont,word);if(m==0){return p->trans_head;}elsep=p->next;}printf("no the word\n");return NULL;}

search_index_word.c 索引查找单词

#include <stdio.h>
#include <string.h>
#include "dictory.h"int search_word_in_index(index_list hash_index[26],char key_word[60])
{index_list p=NULL;int i;p=hash_index[(key_word[0]-'A')%32];while(p!=NULL){if(strcmp(key_word,p->word_cont)==0)return p->position;elsep=p->next;}  printf("no the word\n");  return 0;
}

main.c

#include <stdio.h>
#include "dictory.h"word_list hash[26];
index_list hash_index[26];int main(int argc,char *argv[])
{if(argc!=2){printf("format error:\n");printf("the format:./a.out -bin or -text\n");return 1;}FILE *fp,*fp2;int i;int pos;char word[60];trans_list s_trans=NULL;int size_index;for (i=0;i<26;++i)hash[i]=NULL;for (i=0;i<26;++i)hash_index[i]=NULL;fp=fopen("dict.txt","r");create_hash(fp,hash);if(!(strcmp(argv[1],"-text"))){while(1){printf("please input the word you want search:\n");scanf("%[^\n]%*c",word);if(!strcmp(word,"#exit"))break;s_trans=search_word(hash,word);print_trans(s_trans);}fclose(fp);}if(!(strcmp(argv[1],"-index"))){fp2=fopen("dict.dat","wb");create_b_file(hash,fp2);fclose(fp2);}if(!(strcmp(argv[1],"-bin"))){fp2=fopen("dict.dat","wb+");size_index=create_b_file(hash,fp2);rewind(fp2);create_index_hash(fp2,size_index,hash_index);while(1){printf("please input the word you want search:\n");scanf("%[^\n]%*c",word);if(!strcmp(word,"#exit"))break;pos=search_word_in_index(hash_index,word);if(pos!=0)print_trans_index(fp2,pos);}}return 0;
}

        本程序可能并不是多么完美,旨在练习自己的C语言基础。

简单快译通英汉词典设计源码相关推荐

  1. c语言英语词典设计案例,c语言课程设计-电子英汉词典(含源码).doc

    . PAGE . C语言课程设计 软件学院 指导老师: 学号: 姓名: 一.实验题目及要求 题目:电子英汉词典 设计要求 : 1.用图形用户界面实现 2.能够编辑词典库中的信息 3.能够实现英译汉,汉 ...

  2. c语言程序设计英汉词典设计,英汉电子词典设计报告_设计_C语言_C语言程序设计.pdf...

    课程设计 课程名称 : 语言程序课程设计 C 题目名称 :电子英汉词典 学生学院 :电气信息学院 专业班级 :自动化1101 学 号 :201101020104 学生姓名 :胡拚 联系方式 指导教师 ...

  3. 独家开发-快译通掌上词典-自建词典生成软件

    独家开发-快译通掌上词典-自建词典生成软件 快译通掌上词典--是一种性价比较高的电子词典工具!至少我觉得我选的V80N是不错的!但是一直以来只能使用快译通提供的内置词典和有限的几种附加词典,这让我觉得 ...

  4. c语言程序设计英汉词典设计,c语言(二)课程设计--电子英汉词典设计.doc

    课 程 设 计 报 告 课程名称 <C语言程序设计> 课题名称 电子英汉词典设计 专 业 班 级 学 号 201713030216 姓 名 指导教师 2018年 7 月 6 日 湖南工程学 ...

  5. 电子英汉词典c语言程序设计报告,英汉电子词典设计报告_设计_C语言_C语言程序设计.doc...

    英汉电子词典设计报告_设计_C语言_C语言程序设计 课程设计 课程名称 :C语言程序课程设计 题目名称 :电子英汉词典 学生学院 :电气信息学院 专业班级 :自动化1101 学 号 :20110102 ...

  6. 电子英汉词典c语言设计报告,C语言课程设计——电子英汉词典汇编.doc

    PAGE 课 程 设 计 报 告 课程名称 C语言课程设计 课题名称 电子英汉词典 专 业 纺织服装学院 班 级 纺工1203 学 号 姓 名 指导教师 田 媛 2014年 01 月06 日 湖南工程 ...

  7. 英汉词典c语言实验报告,大学课程英汉电子词典设计报告设计C语言C语言程序设计.doc...

    课程设计 课程名称 :C语言程序课程设计 题目名称 :电子英汉词典 学生学院 :电气信息学院 专业班级 :自动化1101 学 号 :201101020104 学生姓名 :胡拚 联系方式 指导教师 :陈 ...

  8. 课程设计c语言拼写字母,C语言课程设计电子英汉词典.doc

    C语言课程设计电子英汉词典 课 程 设 计 报 告 课程名称 C语言课程设计 课题名称 电子英汉词典 专 业 纺织服装学院 班 级 纺工1203 学 号 姓 名 指导教师 田 媛 2014年 01 月 ...

  9. 电子英汉词典C语言课程设计

    电子英汉词典 1.问题描述 实现简单电子英汉词典的功能,具体管理操作包括单词的添加.显示.查找.删除.修改和保存等.采用结构体数组,每个数据的结构应当包括:单词的英文拼写,单词的中文释义. 2.功能要 ...

最新文章

  1. 一文读懂如何通过设计模式学习创建对象?
  2. 在应用程序中使用虚拟内存——Windows核心编程学习手札之十五
  3. android4.0 开机启动activity 4.0,如何正确理解和使用Activity的4种启动模式
  4. Scala入门到精通——第七节:类和对象(二)
  5. 导出EXCEL遇到问题
  6. 游泳后精疲力尽_精疲力尽的编程后如何重回正轨
  7. PDF限制复制怎么办?怎么解决这个问题?
  8. ApacheCN 翻译活动进度公告 2019.6.15
  9. html的坐标怎么表示,经纬度怎样表示
  10. 烂到不想考研!大学糟糕宿舍大盘点!
  11. ABO区块链在医疗行业中的重要作用
  12. Adaptive AUTOSAR和Classic AUTOSAR
  13. Android中淡入淡出动画
  14. Java关键字详解-配视频讲解链接(附带一些面试题)
  15. Java对象大小内幕浅析
  16. 蓝牙协议学习整理(一)蓝牙的概述
  17. 利用FME PythonCaller调用7z解压压缩包
  18. 准时制生产方式(Just In Time简称JIT)
  19. Matlab 多元线性回归
  20. 网格员计算机应用基础知识,计算机应用基础(第3版)周南岳(win7office10)期末复习及答案.doc...

热门文章

  1. ios Rn0.44 Xcode9 ScrollView下拉距离过短或下拉刷新后 不能自动回弹、复位置顶
  2. MNIST手写数字识别准确度提升最全、最实用的方法
  3. 玩转黑莓8900,不信你不会。超级实用
  4. C语言作业教师评语咋写,学生写给老师的评语4篇
  5. 豆瓣高分书!Python编程从入门到进阶都有!
  6. 计算机教师专业提升目标,信息技术促进教师专业发展的研究
  7. 微信小程序app.js中获取用户信息以及为golbalData赋值的坑
  8. 项目管理系列:项目验收
  9. 网易指责九城未退《魔兽》上亿元点卡
  10. 2021年流动式起重机司机报名考试及流动式起重机司机找解析