资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

c++实现电子词典


#include

盖州网站制作公司哪家好,找创新互联!从网页设计、网站建设、微信开发、APP开发、自适应网站建设等网站项目制作,到程序开发,运营维护。创新互联自2013年创立以来到现在10年的时间,我们拥有了丰富的建站经验和运维经验,来保证我们的工作的顺利进行。专注于网站建设就选创新互联。

#include

#include

#include

#define MAX  33227

#include

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;

}


当前文章:c++实现电子词典
本文链接:http://cdkjz.cn/article/ghdheg.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

大客户专线   成都:13518219792   座机:028-86922220