📄 main.cpp
字号:
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
#include <cstdlib>
#include <cctype>
#include<set>
#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(int argc, char* argv[]) {
// Output usage message if improper command line args were given.
if (argc != 3) {
cerr << "Usage: " << argv[0] << " wordlist_filename input_file\n";
return EXIT_FAILURE;
}
ifstream inf(argv[2]);
if (! inf) {
cerr << "Could not open " << argv[2] << "\n";
return EXIT_FAILURE;
}
// Read dictionary, but let user know what we are working on.
cout << "Loading dictionary, this may take awhile...\n";
Dictionary d(argv[1]);
check_spelling(inf, d);
inf.close();
return EXIT_SUCCESS;
}
void check_spelling(ifstream& in, Dictionary& dict) {
int line_number = 0;
while (in) {
line_number++;
string line;
getline(in, line);
stringstream ss (stringstream::in | stringstream::out);
ss << line;
string word;
while (ss >> word) {
// TODO: Complete the spell check of each word
lower(word);
word=strip_punct(word);
if(!dict.search(word))
{
cout<<"Line "<<line_number<<" '"<<word<<"'"<<endl;
string suggest;
set<string>suggestions;
string alphabet="abcdefghijklmnopqrstuvwxyz";
int w=0;
int count=0;
string letter="";
for(w=0;w<word.length()-1;w++)//临近交换
{
suggest=word;
suggest[w]=word[w+1];
suggest[w+1]=word[w];
if(dict.search(suggest))
suggestions.insert(suggest);
}
for(w=0;w<word.length();w++ )//删除
{
suggest=word;
if(w==0)
{
suggest=suggest.substr(1,word.length()-1);
}
else if(w==word.length()-1)
suggest=suggest.substr(0,word.length()-1);
else
suggest=suggest.substr(0,w)+suggest.substr(w+1,word.length()-w-1);
if(dict.search(suggest))
suggestions.insert(suggest);
}
for(w=0;w<word.length();w++)//替换字母
{
for(int count=0;count<alphabet.length();count++)
{
suggest=word;
suggest[w]=alphabet[count];
if(dict.search(suggest))
{
suggestions.insert(suggest);
}
}
}
for(w=0;w<word.length();w++)//插入字母
{
for(count=0;count<alphabet.length();count++)
{
suggest=word;
letter=alphabet[count];
suggest.insert(w,letter);
if(dict.search(suggest))
{
suggestions.insert(suggest);
}
}
}
for(count=0;count<alphabet.length();count++)//在尾部添加字母
{
suggest=word;
letter=alphabet[count];
suggest.append(letter);
if(dict.search(suggest))
{
suggestions.insert(suggest);
}
}
if(!suggestions.empty())//输出建议列表
{
cout<<" suggestions:"<<endl;
for(set<string>::iterator it=suggestions.begin();it!=suggestions.end();it++)
cout<<" "<<*it<<endl;
}
else
cout<<" There are no similar words."<<endl;
}
}
}
}
void lower(string& s) {
// Ensures that a word is lowercase
for (int i = 0; i < s.length(); i++) {
s[i] = tolower(s[i]);
}
}
string strip_punct(const string& s) {
// Remove any single trailing
// punctuation character from a word.
if (ispunct(s[s.length() - 1]) ) {
return s.substr (0, s.length() - 1);
}
else {
return s;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -