新聞中心
#include
蓋州網(wǎng)站制作公司哪家好,找創(chuàng)新互聯(lián)!從網(wǎng)頁設(shè)計(jì)、網(wǎng)站建設(shè)、微信開發(fā)、APP開發(fā)、自適應(yīng)網(wǎng)站建設(shè)等網(wǎng)站項(xiàng)目制作,到程序開發(fā),運(yùn)營維護(hù)。創(chuàng)新互聯(lián)自2013年創(chuàng)立以來到現(xiàn)在10年的時間,我們擁有了豐富的建站經(jīng)驗(yàn)和運(yùn)維經(jīng)驗(yàn),來保證我們的工作的順利進(jìn)行。專注于網(wǎng)站建設(shè)就選創(chuàng)新互聯(lián)。
#include
#include
#include
#define MAX 33227
#include
using namespace std;
class dict_word
{
public:
char *word;//英文單詞指針
//char *chinese;//中文意思指針
};
//創(chuàng)建一個打開字典的函數(shù)
int open_dict(dict_word **word, const char * dict_filename)//讀取字典文件中的內(nèi)容放到堆內(nèi)存中
{
//使用文件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;
}*/
//給字典類的指針分配內(nèi)存出并清空(根據(jù)單詞個數(shù)來分配內(nèi)存)MAX是文件中的單詞個數(shù)
*word = (dict_word*)malloc(sizeof(dict_word)*MAX);//固定分配MAX的內(nèi)存*word是一個存放結(jié)構(gòu)體指針的數(shù)組的首地址
memset(*word, 0, sizeof(dict_word)*MAX);//清空結(jié)構(gòu)體指針數(shù)組的內(nèi)存
dict_word *pd = *word;//pd 指向結(jié)構(gòu)體指針數(shù)組的首地址
//定義一個存放單詞內(nèi)容的BUF
char buf[1024] = { 0 };
int len = 0;//計(jì)算讀取到字符串的長度
int i = 0;//單詞計(jì)數(shù)器
//while (getline(infile, buf))//一行一行讀,將讀到的內(nèi)容放到buf中
while(fgets(buf,sizeof(buf),fp))
{
len = strlen(buf);//讀取到字符串的長度
if (len >0)
{
pd[i].word = (char *)malloc(len+1);//根據(jù)字符串長度分配空間
//清空分配好的空間
memset(pd[i].word, 0, len+1);
strncpy(pd[i].word, buf,len+1);//將讀取到的內(nèi)容拷貝到中
}
len = 0;
i++;
}
infile.close();//關(guān)閉文件IO
fclose(fp);
return i;
}
int serach_word(char *cin_word, const dict_word*word1)//在堆內(nèi)存中查找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)))//查找到對應(yīng)的單詞
if(!st1.compare(0,n,st2,0,n))
{
strcpy(cin_word, word1->word);//將找到的單詞及漢語意思復(fù)制到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 << "請?jiān)诳蓤?zhí)行文件后加上字典所在的路徑" << endl;
cout << "usage: a.out dictionaryFilePath" << endl;
return -1;
}*/
long start_time = clock();//記錄函數(shù)開始執(zhí)行的時間
dict_word *word = NULL;//定義類指針
int word_count = open_dict(&word, "D:\\hanyingdictionary.txt");
if (word_count <= 0)
{
cerr << "文件中單詞數(shù)不正確" << endl;
return -1;
}
long end_time = clock();
cout << "打開文件并將文件中的內(nèi)容讀取到堆內(nèi)存中所消耗的時長是:" << end_time - start_time << "ms" << endl;
char cin_word[1024] = { 0 };
start_time = clock();//記錄查找函數(shù)執(zhí)行的時間
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;//輸入退出命令,循環(huán)退出
}
start_time = clock();//記錄查找函數(shù)執(zhí)行的時間
if (serach_word(cin_word, word))//在堆內(nèi)存中查找所要的單詞
{
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();
//釋放堆內(nèi)存空間
free_dict(word);
end_time = clock();
cout << "釋放堆內(nèi)存空間消耗的時長是:" << end_time - start_time << "ms" << endl;
return 0;
}
網(wǎng)站欄目:c++實(shí)現(xiàn)電子詞典
標(biāo)題URL:http://fisionsoft.com.cn/article/ghdheg.html