c++實現電子詞典

來源:互聯網
上載者:User

標籤: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++實現電子詞典

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.