資源簡(jiǎn)介
使用c++實(shí)現(xiàn)英文拼寫檢查,對(duì)照自定義字典進(jìn)行檢查。
使用c++實(shí)現(xiàn)英文拼寫檢查,對(duì)照自定義字典進(jìn)行檢查。
使用c++實(shí)現(xiàn)英文拼寫檢查,對(duì)照自定義字典進(jìn)行檢查。

代碼片段和文件信息
#include?
#include?
#include?
#include?
#include?
#include?
#include?“dictionary.h“
using?namespace?std;
void?lower(string&?s);
string?strip_punct(const?string&?s);
void?check_spelling(ifstream&?in?Dictionary&?dict);
int?main()
{
char*?filename?=?“test.txt“;
ifstream?inf(filename);
if?(!inf)
{
cerr?<“Could?not?open?“?< return?EXIT_FAILURE;
}
//?開始導(dǎo)入字典到哈希表中
cout?<“Loading?dictionary?this?may?take?awhile...\n“;
Dictionary?d(“wordlist.txt“);
check_spelling(inf?d);
inf.close();
//?調(diào)試時(shí)起暫停作用
system(“pause“);
return?EXIT_SUCCESS;
}
//?單詞內(nèi)全部字母兩兩交換后,在字典中查找是否正確,正確則輸出,否則
//?繼續(xù)上述操作直到全部交換過為止
void?AllLetter_Swap(int?nPos?const?string&?word?Dictionary&?dict)
{
//?直到單詞最后一個(gè)字母,結(jié)束遞歸
if?(nPos?==?word.length())
return;
string?strWord;
char?chLetter?=?word[nPos];
for?(int?j?=?nPos?+?1;?j? {
//?恢復(fù)原始單詞值
strWord?=?word;
//?互換指定nPos位置與其后字母
strWord[nPos]?=?strWord[j];
strWord[j]?=?chLetter;
//?字典中查找,找到輸出
if?(dict.search(strWord))
cout?<“\t\t“?< }
//?遞歸調(diào)用
AllLetter_Swap(nPos?+?1?word?dict);
}
//?單詞內(nèi)相鄰兩字母交換后,在字典中查找是否正確,正確則輸出,否則
//?繼續(xù)上述操作直到最后兩字母交換過為止
void?AdjacentLetter_Swap(const?string&?word?Dictionary&?dict)
{
string?strWord;
for?(int?nPos?=?0;?nPos? {
//?恢復(fù)原始單詞值
strWord?=?word;
//?兩相鄰字母互換(當(dāng)前字母與相鄰后面一個(gè)字母互換)
char?chLetter?=?word[nPos];
strWord[nPos]?=?strWord[nPos?+?1];
strWord[nPos?+?1]?=?chLetter;
//?字典中查找,找到輸出
if?(dict.search(strWord))
cout?<“\t\t“?< }
}
//?逐次刪除單詞中每個(gè)字母后,在字典中查找是否正確,正確則輸出
void?RemoveLetter(const?string&?word?Dictionary&?dict)
{
vector?vecWord; //?存放刪除單詞字母后,正確單詞的數(shù)組用于避免有重復(fù)的正確單詞輸出
string?strWord;
for?(int?nPos?=?0;?nPos? {
//?恢復(fù)原始單詞值
strWord?=?word;
//?刪除一個(gè)字母
strWord.erase(nPos?1);
//?字典中查找,找到輸出
if?(dict.search(strWord))
{
//?在前一次正確單詞的數(shù)組中查找,如果存在的話,不再輸出和壓入到數(shù)組
vector::iterator?Iter?=?vecWord.begin();
for?(;?Iter?!=?vecWord.end();?++Iter)
{
if?((*Iter)?==?strWord)
break;
}
//?否則不存在,則壓入該正確單詞到數(shù)組并輸出
if?(Iter?==?vecWord.end())
{
vecWord.push_back(strWord);
cout?<“\t\t“?< }
}
}
}
//?逐次替換單詞中每個(gè)字母為其它一個(gè)字母,在字典中查找是否正確,正確則輸出
void?ReplaceLetter(const?string&?word?Dictionary&?dict)
{
string?strWord;
string?strAlpha?=?“abcdefghigklmnopqrstuvwxyz“; //?26個(gè)小寫字母
for?(int?nPos?=?0;?nPos? {
//?單詞中逐次將每位字母用26個(gè)字母代替,判斷是否正確單詞
for?(int?nAlpha?=?0;?nAlpha? {
//?恢復(fù)原始單詞值
strWord?=?word;
//?將單詞strWord中nPos位置開始的1個(gè)字母,用字母串
//?strAlpha中的nAlpha位置開始的1個(gè)字母代替
strWord.replace(nPos?1?strAlpha?nAlpha?1);
//?字典中查找,找到輸出
if?(dict.search(strWord))
cout?<“\t\t“?< }
?屬性????????????大小?????日期????時(shí)間???名稱
-----------?---------??----------?-----??----
?????文件????????6348??2017-09-23?17:01??main.cpp
?????文件?????????107??2009-01-03?13:21??test.txt
?????文件?????1154336??2002-09-19?11:53??wordlist.txt
?????文件????????1092??2017-09-23?17:12??dictionary.h
評(píng)論
共有 條評(píng)論