资源简介

使用c++实现英文拼写检查,对照自定义字典进行检查。 使用c++实现英文拼写检查,对照自定义字典进行检查。 使用c++实现英文拼写检查,对照自定义字典进行检查。

资源截图

代码片段和文件信息

#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 “ << filename << “\n“;
return EXIT_FAILURE;
}

// 开始导入字典到哈希表中
cout << “Loading dictionary this may take awhile...\n“;

Dictionary d(“wordlist.txt“);

check_spelling(inf d);

inf.close();

// 调试时起暂停作用
system(“pause“);

return EXIT_SUCCESS;
}

// 单词内全部字母两两交换后,在字典中查找是否正确,正确则输出,否则
// 继续上述操作直到全部交换过为止
void AllLetter_Swap(int nPos const string& word Dictionary& dict)
{
// 直到单词最后一个字母,结束递归
if (nPos == word.length())
return;

string strWord;
char chLetter = word[nPos];
for (int j = nPos + 1; j < word.length(); ++j)
{
// 恢复原始单词值
strWord = word;

// 互换指定nPos位置与其后字母
strWord[nPos] = strWord[j];
strWord[j] = chLetter;

// 字典中查找,找到输出
if (dict.search(strWord))
cout << “\t\t“ << strWord << endl;
}

// 递归调用
AllLetter_Swap(nPos + 1 word dict);
}

// 单词内相邻两字母交换后,在字典中查找是否正确,正确则输出,否则
// 继续上述操作直到最后两字母交换过为止
void AdjacentLetter_Swap(const string& word Dictionary& dict)
{
string strWord;
for (int nPos = 0; nPos < word.length() - 1; ++nPos)
{
// 恢复原始单词值
strWord = word;

// 两相邻字母互换(当前字母与相邻后面一个字母互换)
char chLetter = word[nPos];
strWord[nPos] = strWord[nPos + 1];
strWord[nPos + 1] = chLetter;

// 字典中查找,找到输出
if (dict.search(strWord))
cout << “\t\t“ << strWord << endl;
}
}

// 逐次删除单词中每个字母后,在字典中查找是否正确,正确则输出
void RemoveLetter(const string& word Dictionary& dict)
{
vector vecWord; // 存放删除单词字母后,正确单词的数组用于避免有重复的正确单词输出
string strWord;
for (int nPos = 0; nPos < word.length(); ++nPos)
{
// 恢复原始单词值
strWord = word;

// 删除一个字母
strWord.erase(nPos 1);

// 字典中查找,找到输出
if (dict.search(strWord))
{
// 在前一次正确单词的数组中查找,如果存在的话,不再输出和压入到数组
vector::iterator Iter = vecWord.begin();
for (; Iter != vecWord.end(); ++Iter)
{
if ((*Iter) == strWord)
break;
}

// 否则不存在,则压入该正确单词到数组并输出
if (Iter == vecWord.end())
{
vecWord.push_back(strWord);
cout << “\t\t“ << strWord << endl;
}
}
}
}

// 逐次替换单词中每个字母为其它一个字母,在字典中查找是否正确,正确则输出
void ReplaceLetter(const string& word Dictionary& dict)
{
string strWord;
string strAlpha = “abcdefghigklmnopqrstuvwxyz“; // 26个小写字母
for (int nPos = 0; nPos < word.length(); ++nPos)
{
// 单词中逐次将每位字母用26个字母代替,判断是否正确单词
for (int nAlpha = 0; nAlpha < strAlpha.length(); ++nAlpha)
{
// 恢复原始单词值
strWord = word;

// 将单词strWord中nPos位置开始的1个字母,用字母串
// strAlpha中的nAlpha位置开始的1个字母代替
strWord.replace(nPos 1 strAlpha nAlpha 1);

// 字典中查找,找到输出
if (dict.search(strWord))
cout << “\t\t“ << strWord << endl;
}

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----
     文件        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

评论

共有 条评论