首页 > 代码库 > c++实现电子词典

c++实现电子词典


#include <iostream>

#include <string.h>

#include <ctime>

#include <fstream>

#define MAX  33227

#include <string>

using namespace std;

class dict_word

{

public:

char *word;//英文单词指针

//char *chinese;//中文意思指针

};


//创建一个打开字典的函数

int open_dict(dict_word **word, const char * dict_filename)//读取字典文件中的内容放到堆内存中

{

//使用文件IO流打开字典文件,打开文件用于读

ifstream infile(dict_filename, ios::in | ios::binary);

FILE *fp = fopen(dict_filename, "r");

if (fp == NULL)

{

cerr << "open file error" << endl;

return 0;

}


/*if (!infile)

{

cerr << "file open error" << endl;

return -1;

}*/

//给字典类的指针分配内存出并清空(根据单词个数来分配内存)MAX是文件中的单词个数

*word = (dict_word*)malloc(sizeof(dict_word)*MAX);//固定分配MAX的内存*word是一个存放结构体指针的数组的首地址

memset(*word, 0, sizeof(dict_word)*MAX);//清空结构体指针数组的内存

dict_word *pd = *word;//pd 指向结构体指针数组的首地址

 //定义一个存放单词内容的BUF

char buf[1024] = { 0 };

int len = 0;//计算读取到字符串的长度

int i = 0;//单词计数器

//while (getline(infile, buf))//一行一行读,将读到的内容放到buf中

while(fgets(buf,sizeof(buf),fp))

{

len = strlen(buf);//读取到字符串的长度

if (len >0)

{

pd[i].word = (char  *)malloc(len+1);//根据字符串长度分配空间

//清空分配好的空间

memset(pd[i].word, 0, len+1);

strncpy(pd[i].word, buf,len+1);//将读取到的内容拷贝到中

}

len = 0;

i++;

}

infile.close();//关闭文件IO

fclose(fp);

return i;

}

int serach_word(char *cin_word, const dict_word*word1)//在堆内存中查找cin_word的单词

{

string st1 = cin_word;

string st2;

int i = 0,n = strlen(cin_word);

while (i < MAX && word1->word !=NULL)

{

st2 = word1->word;


//if (!strcmp(cin_word, word1->word, strlen(cin_word)))//查找到对应的单词

if(!st1.compare(0,n,st2,0,n))

{

strcpy(cin_word, word1->word);//将找到的单词及汉语意思复制到cin_word

return 1;

}

i++;

word1++;

}

strcpy(cin_word, "没有找到你要的单词");

return 0;

}

void free_dict(dict_word *dictword)

{

int i = 0;

for (i = 0; i < MAX; i++)

{

free(dictword[i].word);

dictword[i].word = NULL;

}

free(dictword);

dictword = NULL;

}




int main(int argc, char **argv)

{

/*if (argc < 2)

{

cout << "请在可执行文件后加上字典所在的路径" << endl;

cout << "usage: a.out dictionaryFilePath" << endl;

return -1;

}*/

long start_time = clock();//记录函数开始执行的时间

dict_word *word = NULL;//定义类指针

int word_count = open_dict(&word, "D:\\hanyingdictionary.txt");

if (word_count <= 0)

{

cerr << "文件中单词数不正确" << endl;

return -1;

}

long end_time = clock();

cout << "打开文件并将文件中的内容读取到堆内存中所消耗的时长是:" << end_time - start_time << "ms" << endl;

char cin_word[1024] = { 0 };

start_time = clock();//记录查找函数执行的时间

while (1)

{

memset(cin_word, 0, sizeof(cin_word));

cout << "请输入你要查找的词语-->" ;

cout << "输入command=exit退出程序" << endl;

cin >> cin_word;

if (!cin_word)

continue;

string st = cin_word;

if (!st.compare(0,12,"command=exit", 0, 12))

{

break;//输入退出命令,循环退出

}

start_time = clock();//记录查找函数执行的时间

if (serach_word(cin_word, word))//在堆内存中查找所要的单词

{

end_time = clock();

cout << cin_word ;//将找到的单词输出 

cout << "查找该单词所消耗的时长是:" << end_time - start_time << "ms" << endl;

}

else

{

end_time = clock();

cout << "查找单词所消耗的时长是:" << end_time - start_time << "ms" << endl;

cout << cin_word << endl;//没有找到单词,输出没有找到你所要查找的单词

}

}

start_time = clock();

//释放堆内存空间

free_dict(word);

end_time = clock();

cout << "释放堆内存空间消耗的时长是:" << end_time - start_time << "ms" << endl;

return  0;

}


本文出自 “12057626” 博客,转载请与作者联系!

c++实现电子词典