查字典项目的源代码.doc_第1页
查字典项目的源代码.doc_第2页
查字典项目的源代码.doc_第3页
查字典项目的源代码.doc_第4页
查字典项目的源代码.doc_第5页
已阅读5页,还剩3页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

#include #include #include #define BUF_SIZE 4096typedef struct char *key;int num;char *trans;word_t;void sys_err(const char *msg)perror(msg);exit(1);int word_num(const char *filename)FILE *fp;char bufBUF_SIZE;int total = 0;fp = fopen(filename, r);if(fp = NULL)sys_err(open filen);while(fgets(buf, BUF_SIZE, fp) != NULL)if(buf0 = #)total+;fclose(fp);return total;int nword(const char *filename)FILE *fp;char bufBUF_SIZE;int total = 0;fp = fopen(filename, r+);if(fp = NULL)sys_err(open filen);while(fgets(buf, BUF_SIZE, fp) != NULL)if(buf0 = #)total+;fclose(fp);return total;word_t *init_word(int total, const char *filename)/将文本写入缓存中FILE *fp;char bufBUF_SIZE;int i, j;fp = fopen(filename, r);if(fp = NULL)sys_err(open filen);word_t *dict = (word_t *)malloc(total * sizeof(word_t);while(fgets(buf, BUF_SIZE, fp) != NULL)if(buf0 = #)dict-key = malloc(strlen(buf) - 1);bufstrlen(buf) - 1 = 0;strcpy(dict-key, buf+1);elsedict-num = 0;bufstrlen(buf)-1 = ;for(i = 0; i num+;dict-trans = (char *)malloc(sizeof(char *) * dict-num);int pos = -1;char *p = buf + 6;for(j = 6; bufj != 0; j+)if(bufj = )pos+;bufj = 0;dict-transpos = (char *)malloc(sizeof(char) * (strlen(p) + 1);strcpy(dict-transpos, p);p = buf+ j + 1;dict+;fclose(fp);dict = dict-total;return dict;int partition(word_t *dict,int start, int end)/排序int i, last = start;word_t tmp, *pivot;pivot = dict+start;for(i = start+1; i key, pivot-key) start)mid = partition(dict, start, end);quicksort(dict, start, mid-1);quicksort(dict, mid+1, end);word_t *binarysearch(word_t *dict,char *str, int total)/二分查找int mid, start = 0, end = total - 1;while(start key, str) key, str) 0)end = mid - 1;elsereturn dict+mid;return NULL;void print(const word_t *dict)/打印单词和解释int i;printf(-n);printf(word : n, dict-key);for(i = 0;i num; i+)printf(Trans %d: %sn, i+1,dict-transi);printf(-n);void insert_new_word(const char *filename)/添加新词到自己的词库FILE *fp;char bufBUF_SIZE;fp = fopen(filename, a+);if(fp = NULL)sys_err(open filen);printf(Please enter the word like this: (#hello)n);fgets(buf, BUF_SIZE, stdin);fprintf(fp, %s, buf);printf(Please enter the trans like this: (Trans:vt nihaowei)n);fgets(buf, BUF_SIZE, stdin);fprintf(fp,%s, buf);fclose(fp);void usr_txt(char *str,const char *filename)/用户自己的词库查找int n_word, i;word_t *dict;n_word = nword(filename);dict = init_word(n_word, filename);for(i = 0; i 0)system(clear);count+;if(strcmp(str, #) = 0)break;if(binarysearch(dict, str, end) != NULL)print(binarysearch(dict, str, end);elsechar ch32;printf(Sorry, cant find such word in system-library !nDo you want to go to yourselfs word-library ? ( y or n) );while(1)fgets(ch, 32, stdin);chstrlen(ch)-1 = 0;if(strcmp(ch, y) = 0)printf(Please input the filename of your word-library : )while(1)fgets(filename, 32, stdin);filenamestrlen(filename)-1 = 0;FILE *fp;if(fp = fopen(filename, r) != NULL)break;elseprintf(Cant find such text which you inputed.nPlease input the filename again: );usr_txt(str, filename);break;else if(strcmp(ch, n) = 0)break;elseprintf(Undefind command: %snPlease input your choice again :(y or n), ch);else if(strcmp(choice, 2) = 0)system(clear);printf(*n);printf(* Byebye! *n);printf(*n);return 1;elseprintf(Undefind command: %sn.Please i

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论