一.这一部曲是紧接第二部没有介绍完的内容,主要是函数接口实体的实现,代码比较多,如果没有看前两部曲的先去看看,再来看这里,不然不好理解,话不说多上代码,

 #define  _CRT_SECURE_NO_WARNINGS
#include "dict.h" /**************函数实现************************/
/*统计文件中单词数量,返回单词数量*/
int statWordCount(char *filaename)
{
FILE *F = fopen(filaename, "r");
if (F == NULL)
{
printf("文件打开失败");
return -;
}
else
{
int num = ;
while (!feof(F))
{
char buf[];
fgets(buf, , F);
num++;
}
fclose(F);
return num / ;//两行对应一个单词 }
}
/*加载文本词库文件*/
void loadTextFile(char *filename)
{
LEN = statWordCount(filename);//计算单词数量
mallocForSArra(LEN);//为单词结构体数组分配内存
FILE *F = fopen(filename, "r");
if (F == NULL)
{
printf("加载文件失败");
return;
}
createInitArray(F);//创建并初始化结构体数组
printf("OK\n"); }
/*加载二进制词库文件*/
void loadBinFile(char *filename)
{
FILE *F = fopen(filename, "rb");
if (F == NULL)
{
printf("打开文件失败");
return;
} createInitBinArr(F); }
/*为长度为len的结构体数组分配内存*/
void mallocForSArra(int len)
{
DICT = (SWORD *)malloc(sizeof(SWORD)*len);
}
/*对结构体dict数组进行排序,使用插入排序*/
void sortStructArr(SWORD * dict)
{
SWORD SW;
int j;
int i;
for (i = ; i < LEN; i++)
{
SW = dict[i];
for (j = i; j> && wordcmp(dict[j-],SW) > ; j--)
{
dict[j] = dict[j - ];
}
dict[j] = SW;
}
printf("ok"); }
/*输入模块,返回输入字符串*/
char *getInput(char *p)
{ scanf("%s",p);
//printf("%s", p);
return p;
}
/*输出模块,输出一个结构体*/
void setOutput(SWORD *word)
{
if (word == NULL)
{
printf("没有找到您输入的单词\n");
return;
}
printf("\n%s", word->key);
int len = word->count_trans;
printf("\n解释:\n");
for (int i = ; i < len; i++)
{
printf("\n\t%s", word->trans[i]);
} }
/*查询模块,使用二分法,据关键词key在词库里查询,找到返回这个结构体位置,否则返回NULL*/
SWORD *searchWord(char *key)
{
//printf("%s\n",key);
//int low = 0;
//int high = LEN;
//while (low < high)
//{
// int mid = (low + high) / 2; // if (strcmp(key,DICT[mid].key) == 0)
// {
// printf("%s", DICT[mid].key);
// return DICT + mid; // }
// else if (strcmp(key, DICT[mid].key) > 0)
// {
// low = mid + 1;
// }
// else
// {
// high = mid - 1;
// printf("%s*********", DICT[mid].key);
//
// }
//}
//int len = strlen(key);
//int dictlen = strlen(DICT[0].key);
//printf("%d\t%d", len, dictlen);
//printf("%s*********", DICT[0].key);
for (int i = ; i < LEN; i++)
{
if (strcmp(key, DICT[i].key) == )
{
return DICT + i;
} } return NULL; }
/*从输入获取要添加的单词*/
void getWord(SWORD *word)
{ printf("请输入要添加的单词(输入#over结束添加):");
scanf("%s", word->key); printf("%s", word->key); printf("请输入解释数量:");
scanf("%d", &(word->count_trans));
for (int i = ; i < word->count_trans; i++)
{
printf("请输入第%d条解释:",i+);
scanf("%s", (word->trans)[i]);
}
printf("hello"); } /*添加单词word到结构体数组,弃用*/
void addWordToArr(SWORD *word)
{
//给词库添加单词
//int len = (*word).count_trans; //puts((*word).key);
//for (int i = 0; i < len; i++)
//{
// puts((*word).trans[i]);
//} //printf("%d\n", LEN);
//printf("%s\n", DICT[LEN - 1].key);
//printf("%d\n", DICT[LEN - 1].count_trans);
//for (size_t i = 0; i < DICT[LEN - 1].count_trans; i++)
//{
// printf("%s\n", DICT[LEN - 1].trans[i]);
//}
LEN = LEN + ;
DICT = (SWORD *)realloc(DICT, LEN);
DICT[ - ] = *word; //puts((*word).key);
//for (int i = 0; i < len; i++)
//{
// puts((*word).trans[i]);
//}
//printf("%d\n", (*word).count_trans); //int keylen = strlen((*word).key)+1;
//分配单词空间并填充
//DICT[LEN - 1].key = (char *)malloc(sizeof(char) * keylen);
//strcpy(DICT[LEN - 1].key, word->key);
//DICT[LEN - 1].key[keylen - 1] = '\0';
////解释长度
//int transnum = word->count_trans;
//DICT[LEN - 1].count_trans = transnum; //DICT[LEN - 1].trans = (char **)malloc(sizeof(char *)*transnum);
////解释空间分配与填充
//for (int i = 0; i < transnum; i++)
//{
// int trlen = strlen(word->trans[i])+1;
// DICT[LEN - 1].trans[i] = (char *)malloc(sizeof(char)* trlen);
// strcpy(DICT[LEN - 1].trans[i], word->trans[i]);
// DICT[LEN - 1].trans[i][trlen - 1] = '\0';
//} }
/*保存一个单词到文件*/
void saveWord(char *filename,SWORD *sw)
{
FILE *F = NULL;
if (!_access(filename, ))//如果文件存在
{
F = fopen(filename, "a");
if (F == NULL)
{
printf("保存词库到文件失败");
return;
} char str[];
sprintf(str, "#%s", sw->key);
fputs(str, F); char trans[] = "";
for (int i = ; i < sw->count_trans; i++)
{
char strs[];
if (i == )
{
sprintf(strs, "Trans:%s", sw->trans[i]);
}
else
{
sprintf(strs, "@%s", sw->trans[i]);
}
strcat(trans, str); }
fputs(trans, F);//将解释写入文件 fclose(F); }
else
{
F = fopen(filename, "w");
if (F == NULL)
{
printf("保存词库到文件失败");
return;
} char str[];
sprintf(str, "#%s", sw->key);
fputs(str, F); char trans[] = "";
for (int i = ; i < sw->count_trans; i++)
{
char strs[];
if (i == )
{
sprintf(strs, "Trans:%s", sw->trans[i]);
}
else
{
sprintf(strs, "@%s", sw->trans[i]);
}
strcat(trans, str); }
fputs(trans, F);//将解释写入文件 fclose(F);
} }
/*生成二进制的词库文件,30位字符块大小*/
void createBinFile(SWORD *word)
{
FILE *F = fopen(dictbitname, "wb");
if (F == NULL)
{
printf("打开文件失败");
return;
}
printf("打开文件成功");
fwrite(&LEN, sizeof(int), , F);//前四个字节存储单词个数
printf("写入长度成功");
for (; word <= DICT+LEN-; word += )
{
fwrite(word->key, sizeof(char),, F);
//printf("写入单词成功");
fwrite(&(word->count_trans), sizeof(int),, F); //printf("写入解释数量%d成功", word->count_trans);
for (int i = ; i < word->count_trans; i++)
{ fwrite(word->trans+i, sizeof(char), , F);
//printf("\n写入解释成功"); }
}
fclose(F); } /*计算一个字符串里某个ch字符的个数*/
int calcNumber(char *str,char ch)
{
int num = ;
for (char *p =str; *p != '\0'; p++)
{
if (*p == ch)
{
num++;
}
}
return num;
}
/*从字符串str中取出字符ch后面的字符串*/
char *rearstr(char *str, char ch,int *pos)
{
char *p = strrchr(str, ch);
*pos = p - str;
p = p + ;
return p;
} /*创建并初始化词库结构体数组*/
void createInitArray(FILE *F)
{
for (int i = ; i < LEN; i++)
{
char str[] = { };
fgets(str, , F);
char *temp = str + ;
int length = strlen(temp);//分配空间长度排除#号
DICT[i].key = (char *)malloc(sizeof(char)*length);//一条关键字分配内存
strcpy(DICT[i].key, temp);
DICT[i].key[length - ] = '\0';//关键词 char strtrans[] = { };
fgets(strtrans, , F); int len = strlen(strtrans) + ;//加1是考虑到'\0'
char *ch = (char *)malloc(sizeof(char)*len);//给每一个指针所指的字符串分配内存
strcpy(ch, strtrans);//将数组里的字符串拷贝到ch中但不带'\0'
ch[len - ] = '\0';//添加字符串结束标记 int number = calcNumber(strtrans, '@') + ;
DICT[i].count_trans = number;//解释的数量 DICT[i].trans = (char **)malloc(sizeof(char *)*number); char *transtemp = strtrans + ;
char *tok = strtok(transtemp, "@");
int j = ;
while (tok != NULL)
{
int translen = strlen(tok)+;
DICT[i].trans[j] = (char *)malloc(sizeof(char)*translen);//给每一条解释分配存储空间
strcpy(DICT[i].trans[j], tok);
DICT[i].trans[j][translen - ] = '\0'; tok = strtok(NULL, "@");
j++;
} }
} /*比较两个SWORD单词的大小,前一个大于后一个返回1,前一个小于后一个返回-1,前一个等于后一个返回0*/
int wordcmp(SWORD wordf, SWORD wordr)
{
if (strcmp(wordf.key, wordr.key) == )
{
return ;
}
else if (strcmp(wordf.key, wordr.key) > )
{
return ;
}
else
{
return -;
}
} /*创建并初始化二进制加载文件对应的的数组,30为写入块大小*/
void createInitBinArr(FILE *F)
{
fread(&LEN, sizeof(int), , F);//先读取四个字节初始化词库长度
mallocForSArra(LEN);//为词库数组分配内存
SWORD *TEMP = DICT;
while (!feof(F))//不在文件末尾
{ TEMP->key = (char *)malloc(sizeof(char)* );//一条关键字分配内存
fread(TEMP->key, sizeof(char), , F); fread(&(TEMP->count_trans), sizeof(int), , F);//读取解释数量
TEMP->trans = (char **)malloc(sizeof(char *)*(TEMP->count_trans)); for (int i = ; i < TEMP->count_trans; i++)
{
TEMP->trans[i] = (char *)malloc(sizeof(char)*);//给每一条解释分配存储空间
fread(TEMP->trans[i], sizeof(char), , F);
} }
}
/*查询模块函数*/
void searchModel()
{ //开始查询
char p[] = { };
printf("请输入要查找的单词:");
char *instr = getInput(p);
while (strcmp(instr, "#exit") != )//不等于#exit就一直查找
{
/* printf("%s", instr);*/ SWORD *wd = searchWord(instr); setOutput(wd);
printf("请输入要查找的单词(输入#exit退出查询):");
instr = getInput(p);
} }
/*添加单词模块函数*/
void addWordModel()
{ SWORD word;
word.key = (char *)malloc(sizeof(char)* );//开辟单词输入缓冲区 word.trans = (char **)malloc(sizeof(char *)* );//开辟解释输入缓冲区 for (int i = ; i < ; i++)
{
word.trans[i] = (char *)malloc(sizeof(char)* );
}
//char key[100] = {0};
//char trans[10][50] = {0};
//word.key = key;
//word.trans = trans;
// getWord(&word); //int len = word.count_trans;
//puts(word.key);
//for (int i = 0; i < len; i++)
//{
// puts(word.trans[i]);
//} do
{ getWord(&word);//获取单词 printf("是否保存词库数组到文件(输入0退出程序,1保存,2不保存):0/1/2?\n"); int yesorno;
scanf("%d", &yesorno);
if (yesorno == )
{
saveWord(dicttxtname, &word);/*保存词库结构数组到文件*/
}
else if (yesorno == )
{
exit();
}
else
{ } } while (strcmp(word.key, "#over") != ); }

二.至此整个电子词典项目介绍完了,在加载二进制文件和读取时有些小bug,运行结果在第二部曲中有描述,本人目前换有一个计划,那就是监听键盘输入以及获取聊天信息,有意的朋友们欢迎一起交流,再次感谢大家。

04-29 21:21