#include <iostream>

蓋州網站制作公司哪家好,找創新互聯!從網頁設計、網站建設、微信開發、APP開發、自適應網站建設等網站項目制作,到程序開發,運營維護。創新互聯自2013年創立以來到現在10年的時間,我們擁有了豐富的建站經驗和運維經驗,來保證我們的工作的順利進行。專注于網站建設就選創新互聯。
#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;
}
文章標題:c++實現電子詞典
URL網址:http://www.yijiale78.com/article46/ghdheg.html
成都網站建設公司_創新互聯,為您提供自適應網站、服務器托管、營銷型網站建設、網站導航、企業建站、網站維護
聲明:本網站發布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創新互聯