📄 main.cpp
字号:
//this is op4
//the author is liqinwei
//2007/12/1
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
#include <cstdlib>
#include <cctype>
#include "dictionary.h"
using namespace std;
static char letter[] = { //define the change letter
'a','b','c','d','e','f','g','h','i','j','k','l','m','n'
,'o','p','q','r','s','t','u','v','w','x','y','z'
};
void lower(string& s);
string strip_punct(const string& s);
void check_spelling(ifstream& in, Dictionary& dict);
string transpose_letter(string s,int i);//Transposing of adjacent letters
string replace_letter(string s,int local,int k);//Replacement of each letter
string insert_letter(string s,int local,int l);//Inserting any letter at any position in a word
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) {
lower(word);
word = strip_punct(word);
if(!dict.search(word))
{
cout <<"line " <<line_number<<": '"<< word <<"' "<<endl;
cout << " suggestions:"<<endl;
//Transposing of adjacent letters
for(int i= 0;i < word.size();i++)
{
if(dict.search(transpose_letter(word,i)))
cout << " "<<transpose_letter(word,i)<<endl;
}
//Removal of each letter
for(int j= 0;j < word.size();j++)
{
string s = word;
if(dict.search(s.erase(j,1)))
cout << " "<<s<<endl;
//if the contiguity letter is same,jump the location
if(dict.search(s)&&word[j]==word[j+1]) j++;
}
//Replacement of each letter
for(int k= 0;k < word.size();k++)
{
for(int i = 0; i <sizeof(letter); i++)
{
if(dict.search(replace_letter(word,i,k)))
cout << " "<<replace_letter(word,i,k)<<endl;
}
}
//Inserting any letter at any position in a word
for(int l= 0;l < word.size();l++)
{
for(int i = 0; i <sizeof(letter); i++)
{
if(dict.search(insert_letter(word,i,l)))
cout << " "<<insert_letter(word,i,l)<<endl;
//if the contiguity letter is same,jump the location
if(word[l]==letter[i]&&dict.search(insert_letter(word,i,l))) l++;
}
}
}
}
}
}
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;
}
}
string transpose_letter(string s,int local){
//Transposing of adjacent letters
int j= local+1;
char c = s[local]; s[local]=s[j]; s[j] = c;
return s;
}
string replace_letter(string s,int local,int k){
//Replacement of each letter
s[k] = letter[local];
return s;
}
string insert_letter(string s,int local,int l){
//Inserting any letter at any position in a word
s.insert(l,1,letter[local]);
return s;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -